package org.dromara.settlement.dubbo;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import org.apache.dubbo.config.annotation.DubboService;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.settlement.api.RemoteAccountService;
import org.dromara.settlement.api.RemoteConfigService;
import org.dromara.settlement.api.domain.*;
import org.dromara.settlement.mapper.AccountLogMapper;
import org.dromara.settlement.mapper.AccountMapper;
import org.dromara.settlement.mapper.AccountSynMapper;
import org.dromara.settlement.mapper.TransactionLogMapper;
import org.dromara.settlement.service.impl.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
@DubboService
public class RemoteAccountServiceImpl implements RemoteAccountService
{
	private static final Logger log = LoggerFactory.getLogger(RemoteAccountServiceImpl.class);

	@Autowired
	private ZhaoShangServiceImpl zhaoShangServiceImpl;//招商银行业务

	@Autowired
	private AccountServiceTransactionImpl transactionImpl;

	@Autowired
	private JitaoTongServiceImpl jitaoTongServiceImpl;//交通银行业务

	@Autowired
	private ZhiFuBaoServiceImpl zhiFuBaoServiceImpl;//支付宝业务

	@Autowired
	private MinShengServiceImpl minShengServiceImpl;//民生银行业务

	@Autowired
	private AccountMapper accountDao;//银行账户dao

	@Autowired
	private AccountLogMapper accountLogDao;//银行账户同步明细dao

	@Autowired
	private AccountSynMapper accountSynDao;//银行账户同步dao

	@Autowired
	private TransactionLogMapper transactionLogDao;//交易记录dao

	@Autowired
	private RemoteConfigService wConfigService;// 配置

	@Override
	public Result<Account> addAccount(long unitId, int userType, Long userId, String userName, String bankType,Long code) {
		return addAccount(unitId, userType, userId, userName, bankType, null, null,code);
	}

	@Override
	public Result<Account> addAccount(long unitId, int userType, Long userId, String userName, String bankType, String bindAcctNo, String bindAcctName,Long code) {
		// TODO 自动生成的方法存根
		Result<Account> result = new Result<Account>();
		try {
			Account account = null;
			if (bankType.equals("招商银行")) {
				account = zhaoShangServiceImpl.createAccount(unitId, code.toString(), userName, userType + "");
			} else if (bankType.equals("交通银行")) {
				account = jitaoTongServiceImpl.createAccount(unitId, userId.toString(), userName, userType + "");
			} else if (bankType.equals("支付宝")) {
				account = zhiFuBaoServiceImpl.createAccount(unitId, userId.toString(), userName, userType + "");
			} else if (bankType.equals("民生银行")) {
				account = minShengServiceImpl.createAccount(unitId, userId.toString(), userName, userType + "", bindAcctNo, bindAcctName);
			}
			if (account == null) {
				result.tip("创建虚拟银行账号：调用银行接口出错");
				return result;
			}
			account.setAddTime(new Date());
			account.setEditTime(new Date());
			account.setStatus((short) 1);
			account.setType((short) 1);
			account.setUnitId(unitId);
			if (StringUtils.isBlank(account.getName())) {
				account.setName(getMainAccountName(unitId, bankType).getAccountName());
			}
			Integer row = accountDao.insertSelective(account);
			if (row == 0) {
				Account acc = accountDao.selectByCardAndType(account.getBankCard(), account.getBankType());
				account.setId(acc.getId());
			}
			result.setRtnData(account);
		} catch (Exception e) {
			String msgStr = "创建虚拟银行账号出错（"+userType+","+userId+"）：";
			log.error(msgStr,e);
			if(e instanceof ServiceException)
				result.tip(e.getMessage());
			else
				result.addError(msgStr+","+e.getMessage());
		}
		return result;
	}

	@Override
	public Result<Account> addAccount(Account account) {
		// TODO 自动生成的方法存根
		Result<Account> result = new Result<Account>();
		try {
			Account exist = accountDao.selectByCardAndType(account.getBankCard(), account.getBankType());
			if(exist != null) {
				//if (exist.getType() == 3 || exist.getType() == 2) {//真实企业账户或真实个人账户
				Account upd = new Account();
				if (StringUtils.isNotBlank(account.getName())) {
					upd.setId(exist.getId());
					upd.setName(account.getName());
					exist.setName(account.getName());
				}
				if (StringUtils.isNotBlank(account.getBankAddress())) {
					upd.setId(exist.getId());
					upd.setBankAddress(account.getBankAddress());
					exist.setBankAddress(account.getBankAddress());
				}
				if (StringUtils.isNotBlank(account.getBankArgNo())) {
					upd.setId(exist.getId());
					upd.setBankArgNo(account.getBankArgNo());
					exist.setBankArgNo(account.getBankArgNo());
				}

				if (StringUtils.isNotBlank(account.getBankNo())) {
					upd.setId(exist.getId());
					upd.setBankNo(account.getBankNo());
					exist.setBankNo(account.getBankNo());
				}
				if (upd.getId() != null) {
					accountDao.updateByPrimaryKeySelective(upd);
				}
				//}
				exist.setBankTypeName(exist.getBankType());
				result.setRtnData(exist);
				return result;
			}else {
				account.setAddTime(new Date());
				account.setEditTime(new Date());
				account.setStatus((short)1);
				account.setBalance(0l);
				Integer row = accountDao.insertSelective(account);
				if (row == 0) {
					account = accountDao.selectByCardAndType(account.getBankCard(), account.getBankType());
				}
				account.setBankTypeName(account.getBankType());
				result.setRtnData(account);
			}
		} catch (Exception e) {
			String msgStr = "创建虚拟银行账号出错（" + JSONObject.toJSONString(account) + "）：";
			log.error(msgStr, e);
			result.addError(msgStr + "," + e.getMessage());
		}
		return result;
	}

	public MainAccount getMainAccountName(long unitId, String bankType) {
		// TODO 自动生成的方法存根
		MainAccount main = new MainAccount();
		if (bankType.equals("招商银行")) {
			String accnbrname = "zhaoshang_accnbrname";
			String khh = "zhaoshang_khh";
			Map<String, String> config = wConfigService.getItems(unitId, accnbrname, khh);
			if (config.containsKey(accnbrname) && StringUtils.isNotEmpty(config.get(accnbrname))) {
				main.setAccountName(config.get(accnbrname));
			}
			if (config.containsKey(khh) && StringUtils.isNotEmpty(config.get(khh))) {
				main.setKhh(config.get(khh));
			}
		} else if (bankType.equals("交通银行")) {
			String accnbrname = "jiaotong_super_acc_name";
			String khh = "jiaotong_khh";
			Map<String, String> config = wConfigService.getItems(unitId, accnbrname, khh);
			if (config.containsKey(accnbrname) && StringUtils.isNotEmpty(config.get(accnbrname))) {
				main.setAccountName(config.get(accnbrname));
			}
			if (config.containsKey(khh) && StringUtils.isNotEmpty(config.get(khh))) {
				main.setKhh(config.get(khh));
			}
		} else if (bankType.equals("支付宝")) {
			String accnbrname = "zhifubao_acc_name";
			Map<String, String> config = wConfigService.getItems(unitId, accnbrname);
			if (config.containsKey(accnbrname) && StringUtils.isNotEmpty(config.get(accnbrname))) {
				main.setAccountName(config.get(accnbrname));
			}
		} else if (bankType.equals("民生银行")) {
			String accnbrname = "minsheng_bank_acctName";
			Map<String, String> config = wConfigService.getItems(unitId, accnbrname);
			if (config.containsKey(accnbrname) && StringUtils.isNotEmpty(config.get(accnbrname))) {
				main.setAccountName(config.get(accnbrname));
			}
		}
		return main;
	}

	@Override
	public Result<MainAccount> getMainAccount(long unitId, String bankType) {
		Result<MainAccount> result = new Result<MainAccount>();
		try {
			result.setRtnData(getMainAccountName(unitId, bankType));
		} catch (Exception e) {
			String msgStr = "获取虚拟银行账号收款账户出错（" + unitId + "," + bankType + "）：";
			log.error(msgStr, e);
			result.addError(msgStr + "," + e.getMessage());
		}
		return result;
	}

	@Override
	public Result<String> getAccountName(long unitId, String bankType, String bankCard) {
		// TODO 自动生成的方法存根
		Result<String> result = new Result<String>();
		try {
			result.setRtnData(getMainAccountName(unitId, bankType).getAccountName());
		} catch (Exception e) {
			String msgStr = "获取虚拟银行账号收款账户名出错（" + unitId + "," + bankType + "," + bankCard + "）：";
			log.error(msgStr, e);
			result.addError(msgStr + "," + e.getMessage());
		}
		return result;
	}

	@Override
	public Result<AccountSyn> synAccount(int id, Short trxdir, long synPosition, List<String> exclude) {
		// TODO 自动生成的方法存根
		Result<AccountSyn> result = new Result<AccountSyn>();
		AccountSyn syn = null;
		boolean locked = false;
		try {
			syn = accountSynDao.selectByPrimaryKey(id);
			Account acc = accountDao.selectByPrimaryKey(id);
			if (syn == null) {
				syn = new AccountSyn();
				syn.setId(acc.getId());
				syn.setUnitId(acc.getUnitId());
				syn.setType(acc.getType());
				syn.setBankCard(acc.getBankCard());
				syn.setBankType(acc.getBankType());
				syn.setType(acc.getType());
				syn.setSynLock((byte)0);
				accountSynDao.insertSelective(syn);
			}
			if (accountSynDao.lock(acc.getId()) == 0) {
				result.tip("正在同步");
				syn = null;
				return result;
			}
			locked = true;
			syn.setBalance(acc.getBalance());
			syn.setAddAmount(0l);
			syn.setReductionAmount(0l);
			syn.setTrxnbrs("");
			boolean isSyn = false;
			if (syn.getBankType().equals("招商银行")) {
				syn = zhaoShangServiceImpl.synAccount(syn);
				isSyn = true;
			}else if (syn.getBankType().equals("交通银行")) {
				syn = jitaoTongServiceImpl.synAccount(syn);
				isSyn = true;
			} else if (syn.getBankType().equals("民生银行")) {
				syn = minShengServiceImpl.synAccount(syn);
				isSyn = true;
			}
			if (isSyn) {
				transactionImpl.saveAccountLog(syn);
			}
			syn.setLogs(accountLogDao.selectByAccId(id, synPosition, trxdir));
			Iterator<AccountLog> iter = syn.getLogs().iterator();
			while (iter.hasNext()) {
				AccountLog log = (AccountLog) iter.next();
				if(exclude != null && exclude.contains(log.getTrxtxt())) {
					iter.remove();
				}else {
					if (log.getTrxdir().intValue() == 1) {//收入
						syn.setAddAmount(syn.getAddAmount()+log.getTrxamt());
					}else if (log.getTrxdir().intValue() == 2) {//支出,Trxamt是复数
						syn.setReductionAmount(syn.getReductionAmount()-log.getTrxamt());
					}
				}
			}
			result.setRtnData(syn);
		} catch (Exception e) {
			String msgStr = "同步银行账户出错（"+id+"）：";
			log.error(msgStr,e);
			result.addError(msgStr+","+e.getMessage());
		}finally {
			if(locked) {
				try {
					AccountSyn unlock = new AccountSyn();
					unlock.setId(syn.getId());
					unlock.setSynLock((byte)0);
					unlock.setSynPosition(syn.getSynPosition());
					unlock.setSynTime(syn.getSynTime());
					accountSynDao.updateByPrimaryKeySelective(unlock);
				} catch (Exception e) {
					// TODO 自动生成的 catch 块
					log.error("同步银行账户时解锁出错：（"+JSONObject.toJSONString(syn)+"）",e);
				}
			}
		}
		return result;
	}

	@Override
	public Result<TransactionLog> applyTransfer(int sourceAccId, int targetAccId, Long amount, String order, String remark) {
		// TODO 自动生成的方法存根
		Result<TransactionLog> result = new Result<TransactionLog>();
		TransactionLog syn = null;
		/*boolean sourceAccLocked = false;
		boolean targetAccLocked = false;*/
		try {
			Account source = accountDao.selectByPrimaryKey(sourceAccId);
			if(source == null) {
				result.tip("付款账户("+sourceAccId+")不存在");
				return result;
			}
			Account target = accountDao.selectByPrimaryKey(targetAccId);
			if(target == null) {
				result.tip("收款账户("+targetAccId+")不存在");
				return result;
			}
			/*if (source.getBalance() < amount) {
				result.tip("付款账户("+sourceAccId+")余额不足");
				return result;
			}*/
			syn = new TransactionLog();
			syn.setUnitId(source.getUnitId());
			syn.setOrderNo(order);
			syn.setAddTime(new Date());
			syn.setAmount(amount);
			syn.setRemark(remark);
			syn.setResult((short)-1);
			syn.setSourceAccId(sourceAccId);
			syn.setSourceCard(source.getBankCard());
			syn.setTargetAccId(targetAccId);
			syn.setTargetCard(target.getBankCard());
			syn.setTargetAccName(target.getName());
			syn.setBankType(source.getBankType());
			transactionLogDao.insertSelective(syn);
			result.setRtnData(syn);
		} catch (Exception e) {
			String msgStr = "申请银行账户转账出错（"+sourceAccId+","+targetAccId+","+amount+","+order+","+remark+"）：";
			log.error(msgStr,e);
			if(e instanceof ServiceException)
				result.tip(e.getMessage());
			else
				result.addError(msgStr+","+e.getMessage());
		}
		return result;
	}

	@Override
	public Result<TransactionLog> transfer(long tranId) {
		// TODO 自动生成的方法存根
		Result<TransactionLog> result = new Result<TransactionLog>();
		TransactionLog syn = null;
		/*boolean sourceAccLocked = false;
		boolean targetAccLocked = false;*/
		Account target = null;
		try {
			syn = transactionLogDao.selectByPrimaryKey(tranId);
			if (syn == null) {
				result.tip("交易记录不存在");
				return result;
			}
			Account source = accountDao.selectByPrimaryKey(syn.getSourceAccId());
			if(source == null) {
				result.tip("付款账户("+syn.getSourceAccId()+")不存在");
				return result;
			}
			syn.setBankType(source.getBankType());
			if (transactionLogDao.lock(tranId) == 0) {
				result.tip("交易记录已处理");
				result.setRtnData(syn);
				return result;
			}
			target = accountDao.selectByPrimaryKey(syn.getTargetAccId());
			if(target == null) {
				result.tip("收款账户("+syn.getTargetAccId()+")不存在");
				return result;
			}
			/*if (source.getBalance() < syn.getAmount()) {
				result.tip("付款账户("+syn.getSourceAccId()+")余额不足");
				return result;
			}*/
			/*if (accountDao.lock(sourceAccId) == 0) {
				result.tip("付款账户("+sourceAccId+")交易中");
				return result;
			}
			sourceAccLocked = true;
			if (accountDao.lock(targetAccId) == 0) {
				result.tip("收款账户("+targetAccId+")交易中");
				return result;
			}
			targetAccLocked = true;*/
			if(syn.getAmount() > 0) {
				if (source.getBankType().equals("招商银行")) {
					if (target.getType() == 1) {//虚拟账户
						syn = zhaoShangServiceImpl.innerTransfer(syn);
					}else if (target.getType() == 2) {//真实个人账户
						syn = zhaoShangServiceImpl.crossPersionTransfer(syn);
					}else if (target.getType() == 3) {//真实企业账户
						syn.setBankType(target.getBankType());
						syn.setTargetAccName(target.getName());
						syn.setTargetAccBankAddr(target.getBankAddress());
						syn = zhaoShangServiceImpl.crossEnterpriseTransfer(syn);
					}
				}else if (source.getBankType().equals("交通银行")) {
					syn.setBankType(target.getBankType());
					syn.setSourceAccName(source.getName());
					if (target.getType() == 1) {//虚拟账户
						syn = jitaoTongServiceImpl.innerTransfer(syn);
					}else if (target.getType() == 2) {//真实个人账户
						syn = jitaoTongServiceImpl.crossPersionTransfer(syn,source.getBankArgNo());
					}else if (target.getType() == 3) {//真实企业账户
						syn = jitaoTongServiceImpl.transfer(syn);
					}
				}else if (source.getBankType().equals("支付宝")) {
					syn.setSourceCard(source.getBankArgNo());
					if (target.getType() == 1) {//虚拟账户
						syn.setTargetCard(target.getBankArgNo());
						syn = zhiFuBaoServiceImpl.innerTransfer(syn);
					} else if (target.getType() == 2) {//真实个人账户
						syn = zhiFuBaoServiceImpl.transfer(syn);
					} else if (target.getType() == 3) {//真实企业账户
						syn = zhiFuBaoServiceImpl.transfer(syn);
					}
				} else if (source.getBankType().equals("民生银行")) {
					syn.setBankType(target.getBankType());
					syn.setSourceAccName(source.getName());
					if (target.getType() == 1) {//虚拟账户
						syn = minShengServiceImpl.innerTransfer(syn);
					} else if (target.getType() == 2) {//真实个人账户
						syn = minShengServiceImpl.crossPersionTransfer(syn);
					} else if (target.getType() == 3) {//真实企业账户
						syn = minShengServiceImpl.transfer(syn, target.getBankNo());
					}
				}
			}else {
				syn.setResult((short)2);
			}
			syn.setBankType(source.getBankType());
			result.setRtnData(syn);
		} catch (Exception e) {
			String msgStr = "银行账户转账出错（"+tranId+"）：";
			log.error(msgStr,e);
			if (target == null || target.getType() != 1) {//非虚拟账户，则出现异常就失败
				syn.setResult((short)3);//失败
				syn.setReason(e.getMessage());
			}
			if(e instanceof ServiceException)
				result.tip(e.getMessage());
			else
				result.addError(msgStr+","+e.getMessage());
		}finally {
			try {
				/*Account unlock = new Account();
				unlock.setStatus((short)1);
				if(sourceAccLocked) {
					unlock.setId(sourceAccId);
					accountDao.updateByPrimaryKeySelective(unlock);
				}
				if(targetAccLocked) {
					unlock.setId(targetAccId);
					accountDao.updateByPrimaryKeySelective(unlock);
				}*/
				if(syn != null) {
					if (syn.getResult() == -1) {
						transactionLogDao.unlock(syn.getId());
					}else {
						TransactionLog upd = new TransactionLog();
						upd.setId(syn.getId());
						upd.setSourceBankNo(syn.getSourceBankNo());
						upd.setTargetBankNo(syn.getTargetBankNo());
						upd.setTranTime(new Date());
						upd.setResult(syn.getResult());
						upd.setReason(syn.getReason());
						transactionLogDao.updateByPrimaryKeySelective(upd);
					}
				}
			} catch (Exception e) {
				// TODO 自动生成的 catch 块
				log.error("保存银行账户交易结果出错：（"+JSONObject.toJSONString(syn)+"）",e);
			}
		}
		return result;
	}

	@Override
	public Result<TransactionLog> synTransfer(long tranId) {
		// TODO 自动生成的方法存根
		Result<TransactionLog> result = new Result<TransactionLog>();
		TransactionLog syn = null;
		try {
			syn = transactionLogDao.selectByPrimaryKey(tranId);
			if (syn == null) {
				result.tip("交易记录不存在");
				return result;
			}
			if (syn.getResult().intValue() > 1) {
				result.setRtnData(syn);
				return result;
			}
			Account source = accountDao.selectByPrimaryKey(syn.getSourceAccId());
			Account target = accountDao.selectByPrimaryKey(syn.getTargetAccId());
			syn.setBankType(source.getBankType());
			if (source.getBankType().equals("招商银行")) {
				if (target.getType() == 2) {//真实个人账户
					syn = zhaoShangServiceImpl.synPersionTransfer(syn);
				}else if (target.getType() == 3) {//真实企业账户
					syn = zhaoShangServiceImpl.synEnterpriseTransfer(syn);
				}
			}else if (source.getBankType().equals("交通银行")) {
				if (target.getType() == 1) {//云账户
					syn = jitaoTongServiceImpl.synInnerTransfer(syn);
				}else if (target.getType() == 2) {//真实个人账户
					syn = jitaoTongServiceImpl.synPersionTransfer(syn);
				}else if (target.getType() == 3) {//真实企业账户
					syn = jitaoTongServiceImpl.synTransfer(syn);
				}
			}else if (source.getBankType().equals("支付宝")) {
				if (target.getType() == 1) {//记账本
					syn = zhiFuBaoServiceImpl.synInnerTransfer(syn);
				} else if (target.getType() == 2) {//真实个人账户
					syn = zhiFuBaoServiceImpl.synTransfer(syn);
				} else if (target.getType() == 3) {//真实企业账户
					syn = zhiFuBaoServiceImpl.synTransfer(syn);
				}
			} else if (source.getBankType().equals("民生银行")) {
				if (target.getType() == 1) {//云账户
					syn = minShengServiceImpl.synInnerTransfer(syn);
				} else if (target.getType() == 2) {//真实个人账户
					syn = minShengServiceImpl.synTransfer(syn);
				} else if (target.getType() == 3) {//真实企业账户
					syn = minShengServiceImpl.synTransfer(syn);
				}
			}
			result.setRtnData(syn);
		} catch (Exception e) {
			String msgStr = "同步银行账户交易结果出错（"+tranId+"）：";
			log.error(msgStr,e);
			if(e instanceof ServiceException)
				result.tip(e.getMessage());
			else
				result.addError(msgStr+","+e.getMessage());
		}finally {
			try {
				if(syn != null) {
					TransactionLog upd = new TransactionLog();
					upd.setId(syn.getId());
					upd.setSourceBankNo(syn.getSourceBankNo());
					upd.setTargetBankNo(syn.getTargetBankNo());
					upd.setTranTime(syn.getTranTime());
					upd.setResult(syn.getResult());
					upd.setReason(syn.getReason());
					transactionLogDao.updateByPrimaryKeySelective(upd);
				}
			} catch (Exception e) {
				// TODO 自动生成的 catch 块
				log.error("保存银行账户交易结果出错：（"+JSONObject.toJSONString(syn)+"）",e);
			}
		}
		return result;
	}

	@Override
	public Result<TransactionLog> getTransfer(long tranId) {
		// TODO 自动生成的方法存根
		Result<TransactionLog> result = new Result<TransactionLog>();
		try {
			result.setRtnData(transactionLogDao.selectByPrimaryKey(tranId));
			if (result.getRtnData() != null) {
				Account source = accountDao.selectByPrimaryKey(result.getRtnData().getSourceAccId());
				if(source != null)
					result.getRtnData().setBankType(source.getBankType());
			}
		} catch (Exception e) {
			String msgStr = "查询交易记录出错（"+tranId+"）：";
			log.error(msgStr,e);
			result.addError(msgStr+","+e.getMessage());
		}
		return result;
	}

	@Override
	public Result<String> getTransferCallBack(long tranId) {
		// TODO 自动生成的方法存根
		Result<String> result = new Result<String>();
		try {
			TransactionLog syn = transactionLogDao.selectByPrimaryKey(tranId);
			if (StringUtils.isNotBlank(syn.getPayReceipt())) {
				result.setRtnData(syn.getPayReceipt());
				return result;
			}
			Account source = accountDao.selectByPrimaryKey(syn.getSourceAccId());
			Account target = accountDao.selectByPrimaryKey(syn.getTargetAccId());
			if (source.getBankType().equals("招商银行")) {
				if (target.getType() == 2) {//真实个人账户
					result.setRtnData(zhaoShangServiceImpl.getTransferCallback(syn));
				}else if (target.getType() == 3) {//真实企业账户
					result.setRtnData(zhaoShangServiceImpl.getTransferCallback(syn));
				}
			}else if (source.getBankType().equals("交通银行")) {
				result.setRtnData(jitaoTongServiceImpl.getTransferCallback(syn));
			}else if (source.getBankType().equals("支付宝")) {
				result.setRtnData(zhiFuBaoServiceImpl.getTransferCallback(syn));
			} else if (source.getBankType().equals("民生银行")) {
				result.setRtnData(minShengServiceImpl.getTransferCallback(syn));
			}
		} catch (Exception e) {
			String msgStr = "获取交易回置单出错（"+tranId+"）：";
			log.error(msgStr,e);
			result.addError(msgStr+","+e.getMessage());
		}
		return result;
	}

	@Override
	public Result<String> getTransferBankNo(long tranId) {
		// TODO 自动生成的方法存根
		Result<String> result = new Result<String>();
		try {
			TransactionLog syn = transactionLogDao.selectByPrimaryKey(tranId);
			Account source = accountDao.selectByPrimaryKey(syn.getSourceAccId());
			Account target = accountDao.selectByPrimaryKey(syn.getTargetAccId());
			if (source.getBankType().equals("招商银行")) {
				if (target.getType() == 2) {//真实个人账户
					result.setRtnData(zhaoShangServiceImpl.getTransferBankNo(syn,""));
				}else if (target.getType() == 3) {//真实企业账户
					result.setRtnData(zhaoShangServiceImpl.getTransferBankNo(syn,""));
				}
//				else if(target.getType() == 1){//虚拟户
//					result.setRtnData(zhaoShangServiceImpl.getTransferBankNo(syn,""));
//				}
			}else if (source.getBankType().equals("交通银行")) {
				result.setRtnData(jitaoTongServiceImpl.getTransferBankNo(syn));
			}else if (source.getBankType().equals("支付宝")) {
				result.setRtnData(zhiFuBaoServiceImpl.getTransferBankNo(syn));
			} else if (source.getBankType().equals("民生银行")) {
				result.setRtnData(minShengServiceImpl.getTransferBankNo(syn));
			}
			if (StringUtils.isNotBlank(result.getRtnData())) {
				TransactionLog upd = new TransactionLog();
				upd.setId(tranId);
				upd.setTargetBankNo(result.getRtnData());
				transactionLogDao.updateByPrimaryKeySelective(upd);
			}
		} catch (Exception e) {
			String msgStr = "获取交易流水号出错（"+tranId+"）：";
			log.error(msgStr,e);
			result.addError(msgStr+","+e.getMessage());
		}
		return result;
	}

	@Override
	public Result<SignZhifubao> getSignInfo(long unitId, String unitName, String bankType) {
		// TODO 自动生成的方法存根
		Result<SignZhifubao> result = new Result<SignZhifubao>();
		try {
			if (bankType.equals("支付宝")) {
				result.setRtnData(zhiFuBaoServiceImpl.getAgreementQrcode(unitId, unitName));
			}
		} catch (Exception e) {
			String msgStr = "获取签约信息出错（"+unitId+","+unitName+","+bankType+"）：";
			log.error(msgStr,e);
			if(e instanceof ServiceException)
				result.tip(e.getMessage());
			else
				result.addError(msgStr+","+e.getMessage());
		}
		return result;
	}
}
