package com.ruoyi.invest.service.impl;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.IdWorker;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.invest.domain.AccountsDealRecord;
import com.ruoyi.invest.domain.AccountsTransferRecord;
import com.ruoyi.invest.domain.dto.AccountsTransferRecordDto;
import com.ruoyi.invest.domain.vo.*;
import com.ruoyi.invest.mapper.*;
import com.ruoyi.invest.service.IAccountsTransferRecordService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 账户转账记录Service业务层处理
 *
 * @author tianyongbao
 * @date 2024-03-30
 */
@Service
public class AccountsTransferRecordServiceImpl implements IAccountsTransferRecordService
{
    @Resource
    private AccountsTransferRecordMapper accountsTransferRecordMapper;

    @Resource
    private PosMachineMapper posMachineMapper;

    @Resource
    private BankCardLendMapper bankCardLendMapper;

    @Resource
    private AccountsMapper accountsMapper;

    @Resource
    private AccountsDealRecordMapper accountsDealRecordMapper;

    @Resource
    private FutureStocksMapper futureStocksMapper;
    /**
     * 查询账户转账记录
     *
     * @param id 账户转账记录主键
     * @return 账户转账记录
     */
    @Override
    public AccountsTransferRecordVo selectAccountsTransferRecordById(Long id)
    {
        AccountsTransferRecordVo vo=accountsTransferRecordMapper.selectAccountsTransferRecordById(id);
                if(vo.getDealType().equals("2")){
                    vo.setInAccountId(vo.getOutAccountId());
                }
        return vo;
    }

    /**
     * 查询账户转账记录列表
     *
     * @param accountsTransferRecordDto 账户转账记录
     * @return 账户转账记录
     */
    @Override
    public List<AccountsTransferRecordVo> selectAccountsTransferRecordList(AccountsTransferRecordDto accountsTransferRecordDto)
    {
        return accountsTransferRecordMapper.selectAccountsTransferRecordList(accountsTransferRecordDto);
    }

    /**
     * 新增账户转账记录
     *
     * @param accountsTransferRecord 账户转账记录
     * @return 结果
     */
    @Override
    public int insertAccountsTransferRecord(AccountsTransferRecord accountsTransferRecord)
    {
        accountsTransferRecord.setCreateBy(SecurityUtils.getUsername());
        accountsTransferRecord.setId(IdWorker.getId());
        accountsTransferRecord.setUpdateBy(SecurityUtils.getUsername());
        accountsTransferRecord.setUpdateTime(DateUtils.getNowDate());
        //pos机刷卡，进行计算
        if(accountsTransferRecord.getType().equals("1")){
            dealPosTransfer(accountsTransferRecord);
        }
        //信用卡还款，进行计算
        if(accountsTransferRecord.getType().equals("2")){
            dealCreditTransfer(accountsTransferRecord);
        }
        //投资转账，进行计算
        if(accountsTransferRecord.getType().equals("3")){
            dealInvestTransfer(accountsTransferRecord);
        }
        //储蓄卡转账，进行计算
        if(accountsTransferRecord.getType().equals("4")){
            dealDebitTransfer(accountsTransferRecord);
        }
        //人情借贷，进行计算
        if(accountsTransferRecord.getType().equals("5")){
            dealLendTransfer(accountsTransferRecord);
        }
        return accountsTransferRecordMapper.insertAccountsTransferRecord(accountsTransferRecord);
    }

    /**
     * 修改账户转账记录
     *
     * @param accountsTransferRecord 账户转账记录
     * @return 结果
     */
    @Override
    public int updateAccountsTransferRecord(AccountsTransferRecord accountsTransferRecord)
    {
        accountsTransferRecord.setUpdateBy(SecurityUtils.getUsername());
        accountsTransferRecord.setUpdateTime(DateUtils.getNowDate());
        return accountsTransferRecordMapper.updateAccountsTransferRecord(accountsTransferRecord);
    }

    /**
     * 批量删除账户转账记录
     *
     * @param ids 需要删除的账户转账记录主键
     * @return 结果
     */
    @Override
    public int deleteAccountsTransferRecordByIds(Long[] ids)
    {
        return accountsTransferRecordMapper.removeAccountsTransferRecordByIds(ids);
    }

    /**
     * 删除账户转账记录信息
     *
     * @param id 账户转账记录主键
     * @return 结果
     */
    @Override
    public int deleteAccountsTransferRecordById(Long id)
    {
        return accountsTransferRecordMapper.removeAccountsTransferRecordById(id);
    }

    /**
     * 处理pos刷卡相关数据
     *
     * @param accountsTransferRecord 账户转账记录主键
     * @return 结果
     */
    public void dealPosTransfer(AccountsTransferRecord accountsTransferRecord){
        //POS机器
        PosMachineVo pos=posMachineMapper.selectPosMachineById(accountsTransferRecord.getPosId());
        //储蓄卡
        accountsTransferRecord.setInAccountId(pos.getDebitCard());
        //信用卡
        BankCardLendVo  creditVo=bankCardLendMapper.selectBankCardLendById(accountsTransferRecord.getOutAccountId());
        Double commission=pos.getRate()*accountsTransferRecord.getAmount()+pos.getRatePlus();
        //手续费
        accountsTransferRecord.setCommission(commission);
        //实际入账金额
        Double actualAmount=accountsTransferRecord.getAmount()-commission;
        accountsTransferRecord.setActualAmount(actualAmount);

        //POS机关联储蓄卡
        BankCardLendVo  debitCardVo=bankCardLendMapper.selectBankCardLendById(pos.getDebitCard());

        //信用卡账户余额计算
        AccountsVo creditAccount=accountsMapper.selectAccountsById(creditVo.getId());
        creditAccount.setBalance(creditAccount.getBalance()-accountsTransferRecord.getAmount());
        creditAccount.setAvailableLimit(creditAccount.getBalance()+creditAccount.getCreditLimit());
        creditAccount.setUpdateTime(accountsTransferRecord.getCreateTime());
        accountsMapper.updateAccounts(creditAccount);
        //信用卡消费记录明细
        AccountsDealRecord creditDeal=new AccountsDealRecord();
        creditDeal.setTransferRecordId(accountsTransferRecord.getId());
        creditDeal.setAccountId(creditAccount.getAccountId());
        creditDeal.setId(IdWorker.getId());
        //支出金额
        creditDeal.setAmount(accountsTransferRecord.getAmount());
        //实时余额
        creditDeal.setCurrentBalance(creditAccount.getBalance());
        //1储蓄卡，2 信用卡，3 网贷，4 人情，5 投资账户
        creditDeal.setType("2");
        //1 收入，2 支出，3 转账，4 借贷
        creditDeal.setDealType("2");
        //POS机刷卡
        creditDeal.setDealCategory("5");
        //POS机名称+商户名称
        creditDeal.setName(pos.getName()+"-"+pos.getMerchantName());
        creditDeal.setRemark("交易商户："+pos.getName()+"-"+pos.getMerchantName());
        creditDeal.setCreateBy(SecurityUtils.getUsername());
        creditDeal.setCreateTime(accountsTransferRecord.getCreateTime());
        //重新计算余额
        //        accountsDealRecordMapper.insertAccountsDealRecord(dealRecord);
        insertAccountsDealRecord(creditDeal);
        //储蓄卡账户余额计算
        AccountsVo debitAccount=accountsMapper.selectAccountsById(debitCardVo.getId());
        debitAccount.setBalance(debitAccount.getBalance()+accountsTransferRecord.getActualAmount());
        debitAccount.setAvailableLimit(debitAccount.getBalance());
        debitAccount.setUpdateTime(accountsTransferRecord.getCreateTime());
        accountsMapper.updateAccounts(debitAccount);
        //储蓄卡消费记录明细,收入
        creditDeal.setAccountId(debitAccount.getAccountId());
        creditDeal.setId(IdWorker.getId());
        //支出金额
        creditDeal.setAmount(accountsTransferRecord.getAmount());
        //实时余额
        creditDeal.setCurrentBalance(debitAccount.getBalance()+accountsTransferRecord.getCommission());
        //1储蓄卡，2 信用卡，3 网贷，4 人情，5 投资账户
        creditDeal.setType("1");
        //1 收入，2 支出，3 转账，4 借贷
        creditDeal.setDealType("1");
        //POS机刷卡
        creditDeal.setDealCategory("5");
        //POS机名称+信用卡+商户名称
        creditDeal.setName(pos.getName()+"-"+creditAccount.getName()+"（"+ StringUtils.getLastNumberChars(3,creditAccount.getCode())+"）"+"-"+pos.getMerchantName());
        creditDeal.setRemark("刷卡金额");
        creditDeal.setCreateBy(SecurityUtils.getUsername());
        creditDeal.setCreateTime(accountsTransferRecord.getCreateTime());
        //重新计算余额
        //        accountsDealRecordMapper.insertAccountsDealRecord(dealRecord);
        insertAccountsDealRecord(creditDeal);

        //储蓄卡消费记录明细,支出
        creditDeal.setAccountId(debitAccount.getAccountId());
        creditDeal.setId(IdWorker.getId());
        //支出金额
        creditDeal.setAmount(accountsTransferRecord.getCommission());
        //实时余额
        creditDeal.setCurrentBalance(debitAccount.getBalance());
        //1储蓄卡，2 信用卡，3 网贷，4 人情，5 投资账户
        creditDeal.setType("1");
        //1 收入，2 支出，3 转账，4 借贷
        creditDeal.setDealType("2");
        //POS机刷卡
        creditDeal.setDealCategory("5");
        //POS机名称+信用卡+商户名称
        creditDeal.setName(pos.getName()+"-"+creditAccount.getName()+"（"+ StringUtils.getLastNumberChars(3,creditAccount.getCode())+"）"+"-"+pos.getMerchantName());
        creditDeal.setRemark("刷卡手续费");
        creditDeal.setCreateBy(SecurityUtils.getUsername());
        Date currentDate = accountsTransferRecord.getCreateTime();
        long millis = currentDate.getTime(); // 获取当前时间的毫秒数
        Date newDate = new Date(millis + 1000); // 加上1000毫秒即1秒
        creditDeal.setCreateTime(newDate);
        //重新计算余额
        //        accountsDealRecordMapper.insertAccountsDealRecord(dealRecord);
        insertAccountsDealRecord(creditDeal);
        accountsTransferRecord.setName(pos.getName()+"-"+creditAccount.getName()+"（"+ StringUtils.getLastNumberChars(3,creditAccount.getCode())+"）"+"-"+pos.getMerchantName());
    }

    /**
     * 处理信用卡还款相关数据
     *
     * @param accountsTransferRecord 账户转账记录主键
     * @return 结果
     */
    public void dealCreditTransfer(AccountsTransferRecord accountsTransferRecord){

        //信用卡
        BankCardLendVo  creditVo=bankCardLendMapper.selectBankCardLendById(accountsTransferRecord.getInAccountId());
        //实际入账金额
        accountsTransferRecord.setActualAmount(accountsTransferRecord.getAmount());
        //储蓄卡
        BankCardLendVo  debitCardVo=bankCardLendMapper.selectBankCardLendById(accountsTransferRecord.getOutAccountId());

        //信用卡账户余额计算
        AccountsVo creditAccount=accountsMapper.selectAccountsById(creditVo.getId());
        creditAccount.setBalance(creditAccount.getBalance()+accountsTransferRecord.getAmount());
        creditAccount.setAvailableLimit(creditAccount.getBalance()+creditAccount.getCreditLimit());
        creditAccount.setUpdateTime(accountsTransferRecord.getCreateTime());
        accountsMapper.updateAccounts(creditAccount);
        //信用卡消费记录明细
        AccountsDealRecord creditDeal=new AccountsDealRecord();
        creditDeal.setTransferRecordId(accountsTransferRecord.getId());
        creditDeal.setAccountId(creditAccount.getAccountId());
        creditDeal.setId(IdWorker.getId());
        //支出金额
        creditDeal.setAmount(accountsTransferRecord.getAmount());
        //实时余额
        creditDeal.setCurrentBalance(creditAccount.getBalance());
        //1储蓄卡，2 信用卡，3 网贷，4 人情，5 投资账户
        creditDeal.setType("2");
        //1 收入，2 支出，3 转账，4 借贷
        creditDeal.setDealType("1");
        //信用卡还款6,0账单还款12
        creditDeal.setDealCategory(accountsTransferRecord.getDealType());
        //储蓄卡还款至信用卡
        creditDeal.setName(debitCardVo.getName()+"（"+StringUtils.getLastNumberChars(3,debitCardVo.getCode())+"）还款至"+creditVo.getName()+"（"+StringUtils.getLastNumberChars(3,creditVo.getCode())+"）");
        creditDeal.setRemark("信用卡还款");
        creditDeal.setCreateBy(SecurityUtils.getUsername());
        Date currentDate = accountsTransferRecord.getCreateTime();
        long millis = currentDate.getTime(); // 获取当前时间的毫秒数
        Date newDate = new Date(millis+1000); // 加上1000毫秒即1秒
        creditDeal.setCreateTime(newDate);
        //重新计算余额
        //        accountsDealRecordMapper.insertAccountsDealRecord(dealRecord);
        insertAccountsDealRecord(creditDeal);
        //储蓄卡账户余额计算
        AccountsVo debitAccount=accountsMapper.selectAccountsById(debitCardVo.getId());
        debitAccount.setBalance(debitAccount.getBalance()-accountsTransferRecord.getActualAmount());
        debitAccount.setAvailableLimit(debitAccount.getBalance());
        debitAccount.setUpdateTime(accountsTransferRecord.getCreateTime());
        accountsMapper.updateAccounts(debitAccount);

        //储蓄卡消费记录明细,支出
        creditDeal.setAccountId(debitAccount.getAccountId());
        creditDeal.setId(IdWorker.getId());
        //支出金额
        creditDeal.setAmount(accountsTransferRecord.getAmount());
        //实时余额
        creditDeal.setCurrentBalance(debitAccount.getBalance());
        //1储蓄卡，2 信用卡，3 网贷，4 人情，5 投资账户
        creditDeal.setType("1");
        //1 收入，2 支出，3 转账，4 借贷
        creditDeal.setDealType("2");
        //信用卡还款
        creditDeal.setDealCategory("6");
        //储蓄卡还款至信用卡
        creditDeal.setName(debitCardVo.getName()+"（"+StringUtils.getLastNumberChars(3,debitCardVo.getCode())+"）还款至"+creditVo.getName()+"（"+StringUtils.getLastNumberChars(3,creditVo.getCode())+"）");
        creditDeal.setRemark("信用卡还款");
        creditDeal.setCreateBy(SecurityUtils.getUsername());
        creditDeal.setCreateTime(accountsTransferRecord.getCreateTime());
        //重新计算余额
        //        accountsDealRecordMapper.insertAccountsDealRecord(dealRecord);
        insertAccountsDealRecord(creditDeal);
        accountsTransferRecord.setName(debitCardVo.getName()+"（"+StringUtils.getLastNumberChars(3,debitCardVo.getCode())+"）还款至"+creditVo.getName()+"（"+StringUtils.getLastNumberChars(3,creditVo.getCode())+"）");
    }

    /**
     * 处理投资记账相关数据
     *
     * @param accountsTransferRecord 账户转账记录主键
     * @return 结果
     */
    public void dealInvestTransfer(AccountsTransferRecord accountsTransferRecord){

        //获取投资账户信息
        FutureStocksVo futureStocks=futureStocksMapper.selectFutureStocksById(accountsTransferRecord.getInAccountId());

        //储蓄卡
        BankCardLendVo  debitCardVo=bankCardLendMapper.selectBankCardLendById(futureStocks.getDebitCard());

        //实际入账金额
        accountsTransferRecord.setActualAmount(accountsTransferRecord.getAmount());
        //投资账户余额计算
        AccountsVo futureStocksAccount=accountsMapper.selectAccountsById(futureStocks.getId());

        //1表示转入，2表示转出
        if(accountsTransferRecord.getDealType().equals("1")){
            accountsTransferRecord.setInAccountId(futureStocks.getId());
            accountsTransferRecord.setOutAccountId(futureStocks.getDebitCard());
            accountsTransferRecord.setName(debitCardVo.getName()+"（"+StringUtils.getLastNumberChars(3,debitCardVo.getCode())+"）转账至"+futureStocks.getName()+"（"+StringUtils.getLastNumberChars(3,futureStocks.getCode())+"）");
            futureStocksAccount.setBalance(futureStocksAccount.getBalance()+accountsTransferRecord.getAmount());
        }else  if(accountsTransferRecord.getDealType().equals("2")){
            accountsTransferRecord.setInAccountId(futureStocks.getDebitCard());
            accountsTransferRecord.setOutAccountId(futureStocks.getId());
            accountsTransferRecord.setName(futureStocks.getName()+"（"+StringUtils.getLastNumberChars(3,futureStocks.getCode())+"）转账至"+debitCardVo.getName()+"（"+StringUtils.getLastNumberChars(3,debitCardVo.getCode())+"）");
            futureStocksAccount.setBalance(futureStocksAccount.getBalance()-accountsTransferRecord.getAmount());
        }
        futureStocksAccount.setAvailableLimit(futureStocksAccount.getBalance());
        futureStocksAccount.setUpdateTime(accountsTransferRecord.getCreateTime());
        accountsMapper.updateAccounts(futureStocksAccount);
        //投资账户消费记录明细
        AccountsDealRecord dealRecord=new AccountsDealRecord();
        dealRecord.setTransferRecordId(accountsTransferRecord.getId());
        dealRecord.setAccountId(futureStocksAccount.getAccountId());
        dealRecord.setId(IdWorker.getId());
        //支出金额
        dealRecord.setAmount(accountsTransferRecord.getAmount());
        //实时余额
        dealRecord.setCurrentBalance(futureStocksAccount.getBalance());
        //1储蓄卡，2 信用卡，3 网贷，4 人情，5 投资账户
        dealRecord.setType("5");
        //1 收入，2 支出，3 转账，4 借贷
        //1表示转入，2表示转出
        if(accountsTransferRecord.getDealType().equals("1")){
            dealRecord.setDealType("1");
            //储蓄卡转账至投资账户
            dealRecord.setName(debitCardVo.getName()+"（"+StringUtils.getLastNumberChars(3,debitCardVo.getCode())+"）转账至"+futureStocks.getName()+"（"+StringUtils.getLastNumberChars(3,futureStocks.getCode())+"）");
        }else  if(accountsTransferRecord.getDealType().equals("2")){
            dealRecord.setDealType("2");
            //投资账户账至储蓄卡转
            dealRecord.setName(futureStocks.getName()+"（"+StringUtils.getLastNumberChars(3,futureStocks.getCode())+"）转账至"+debitCardVo.getName()+"（"+StringUtils.getLastNumberChars(3,debitCardVo.getCode())+"）");
        }
        //投资账户转账
        dealRecord.setDealCategory("7");
        dealRecord.setRemark("投资账户转账");
        dealRecord.setCreateBy(SecurityUtils.getUsername());
        dealRecord.setCreateTime(accountsTransferRecord.getCreateTime());
        //重新计算余额
        //        accountsDealRecordMapper.insertAccountsDealRecord(dealRecord);
        insertAccountsDealRecord(dealRecord);
        //储蓄卡账户余额计算
        AccountsVo debitAccount=accountsMapper.selectAccountsById(debitCardVo.getId());
        //1表示转入，2表示转出
        if(accountsTransferRecord.getDealType().equals("1")){
            debitAccount.setBalance(debitAccount.getBalance()-accountsTransferRecord.getActualAmount());
        }else  if(accountsTransferRecord.getDealType().equals("2")){
            debitAccount.setBalance(debitAccount.getBalance()+accountsTransferRecord.getActualAmount());
        }
        debitAccount.setAvailableLimit(debitAccount.getBalance());
        debitAccount.setUpdateTime(accountsTransferRecord.getCreateTime());
        accountsMapper.updateAccounts(debitAccount);

        //储蓄卡消费记录明细,支出
        dealRecord.setAccountId(debitAccount.getAccountId());
        dealRecord.setId(IdWorker.getId());
        //支出金额
        dealRecord.setAmount(accountsTransferRecord.getAmount());
        //实时余额
        dealRecord.setCurrentBalance(debitAccount.getBalance());
        //1储蓄卡，2 信用卡，3 网贷，4 人情，5 投资账户
        dealRecord.setType("5");
        //1 收入，2 支出，3 转账，4 借贷
        //1表示转入，2表示转出
        if(accountsTransferRecord.getDealType().equals("1")){
            dealRecord.setDealType("2");
            //储蓄卡转账至投资账户
            dealRecord.setName(debitCardVo.getName()+"（"+StringUtils.getLastNumberChars(3,debitCardVo.getCode())+"）转账至"+futureStocks.getName()+"（"+StringUtils.getLastNumberChars(3,futureStocks.getCode())+"）");
        }else  if(accountsTransferRecord.getDealType().equals("2")){
            dealRecord.setDealType("1");
            //投资账户账至储蓄卡转
            dealRecord.setName(futureStocks.getName()+"（"+StringUtils.getLastNumberChars(3,futureStocks.getCode())+"）转账至"+debitCardVo.getName()+"（"+StringUtils.getLastNumberChars(3,debitCardVo.getCode())+"）");
        }
        //投资账户转账
        dealRecord.setDealCategory("7");
        dealRecord.setRemark("投资账户转账");
        dealRecord.setCreateBy(SecurityUtils.getUsername());
        dealRecord.setCreateTime(accountsTransferRecord.getCreateTime());
        //重新计算余额
        //        accountsDealRecordMapper.insertAccountsDealRecord(dealRecord);
        insertAccountsDealRecord(dealRecord);
    }

    /**
     * 处理储蓄卡转账相关数据
     *
     * @param accountsTransferRecord 账户转账记录主键
     * @return 结果
     */
    public void dealDebitTransfer(AccountsTransferRecord accountsTransferRecord){

        //实际入账金额
        accountsTransferRecord.setActualAmount(accountsTransferRecord.getAmount());
        //储蓄卡转出
        BankCardLendVo  outDebitCardVo=bankCardLendMapper.selectBankCardLendById(accountsTransferRecord.getOutAccountId());

        //储蓄卡转入
        BankCardLendVo  inDebitCardVo=bankCardLendMapper.selectBankCardLendById(accountsTransferRecord.getInAccountId());

        //储蓄卡转出余额计算
        AccountsVo outAccount=accountsMapper.selectAccountsById(outDebitCardVo.getId());
        outAccount.setBalance(outAccount.getBalance()-accountsTransferRecord.getAmount());
        outAccount.setAvailableLimit(outAccount.getBalance());
        outAccount.setUpdateTime(accountsTransferRecord.getCreateTime());
        accountsMapper.updateAccounts(outAccount);
        //储蓄卡转出记录明细
        AccountsDealRecord creditDeal=new AccountsDealRecord();
        creditDeal.setTransferRecordId(accountsTransferRecord.getId());
        creditDeal.setAccountId(outAccount.getAccountId());
        creditDeal.setId(IdWorker.getId());
        //支出金额
        creditDeal.setAmount(accountsTransferRecord.getAmount());
        //实时余额
        creditDeal.setCurrentBalance(outAccount.getBalance());
        //1储蓄卡，2 信用卡，3 网贷，4 人情，5 投资账户
        creditDeal.setType("1");
        //1 收入，2 支出，3 转账，4 借贷
        creditDeal.setDealType("2");
        //储蓄卡转账
        creditDeal.setDealCategory("8");
        //储蓄卡转账
        creditDeal.setName(outDebitCardVo.getName()+"（"+StringUtils.getLastNumberChars(3,outDebitCardVo.getCode())+"）转账至"+inDebitCardVo.getName()+"（"+StringUtils.getLastNumberChars(3,inDebitCardVo.getCode())+"）");
        creditDeal.setRemark("储蓄卡转账");
        creditDeal.setCreateBy(SecurityUtils.getUsername());
        creditDeal.setCreateTime(accountsTransferRecord.getCreateTime());
        //重新计算余额
        //        accountsDealRecordMapper.insertAccountsDealRecord(dealRecord);
        insertAccountsDealRecord(creditDeal);

        //储蓄卡转入账户余额计算
        AccountsVo inAccount=accountsMapper.selectAccountsById(inDebitCardVo.getId());
        inAccount.setBalance(inAccount.getBalance()+accountsTransferRecord.getActualAmount());
        inAccount.setAvailableLimit(inAccount.getBalance());
        inAccount.setUpdateTime(accountsTransferRecord.getCreateTime());
        accountsMapper.updateAccounts(inAccount);

        //储蓄卡转入记录明细,收入
        creditDeal.setAccountId(inAccount.getAccountId());
        creditDeal.setId(IdWorker.getId());
        //支出金额
        creditDeal.setAmount(accountsTransferRecord.getAmount());
        //实时余额
        creditDeal.setCurrentBalance(inAccount.getBalance());
        //1储蓄卡，2 信用卡，3 网贷，4 人情，5 投资账户
        creditDeal.setType("1");
        //1 收入，2 支出，3 转账，4 借贷
        creditDeal.setDealType("1");
        //储蓄卡转账
        creditDeal.setDealCategory("8");
        //储蓄卡转账
        creditDeal.setName(outDebitCardVo.getName()+"（"+StringUtils.getLastNumberChars(3,outDebitCardVo.getCode())+"）转账至"+inDebitCardVo.getName()+"（"+StringUtils.getLastNumberChars(3,inDebitCardVo.getCode())+"）");
        creditDeal.setRemark("储蓄卡转账");
        creditDeal.setCreateBy(SecurityUtils.getUsername());
        creditDeal.setCreateTime(accountsTransferRecord.getCreateTime());
        //重新计算余额
        //        accountsDealRecordMapper.insertAccountsDealRecord(dealRecord);
        insertAccountsDealRecord(creditDeal);
        accountsTransferRecord.setName(outDebitCardVo.getName()+"（"+StringUtils.getLastNumberChars(3,outDebitCardVo.getCode())+"）转账至"+inDebitCardVo.getName()+"（"+StringUtils.getLastNumberChars(3,inDebitCardVo.getCode())+"）");
    }

    /**
     * 处理人情借贷相关数据
     *
     * @param accountsTransferRecord 账户转账记录主键
     * @return 结果
     */
    public void dealLendTransfer(AccountsTransferRecord accountsTransferRecord){
        //获取借贷账户信息
        BankCardLendVo  lendVo=bankCardLendMapper.selectBankCardLendById(accountsTransferRecord.getOutAccountId());
        //储蓄卡
        BankCardLendVo  debitCardVo=bankCardLendMapper.selectBankCardLendById(accountsTransferRecord.getInAccountId());

        //实际入账金额
        accountsTransferRecord.setActualAmount(accountsTransferRecord.getAmount()+accountsTransferRecord.getCommission());
        //借贷余额计算
        AccountsVo lendAccount=accountsMapper.selectAccountsById(lendVo.getId());

        //1表示借款，2表示还款
        if(accountsTransferRecord.getDealType().equals("1")){
            accountsTransferRecord.setInAccountId(debitCardVo.getId());
            accountsTransferRecord.setOutAccountId(lendVo.getId());
            accountsTransferRecord.setName(lendVo.getName()+"（"+StringUtils.getLastNumberChars(3,lendVo.getCode())+"）借款至"+debitCardVo.getName()+"（"+StringUtils.getLastNumberChars(3,debitCardVo.getCode())+"）");
            lendAccount.setBalance(lendAccount.getBalance()-accountsTransferRecord.getAmount());
        }else  if(accountsTransferRecord.getDealType().equals("2")){
            accountsTransferRecord.setInAccountId(lendVo.getId());
            accountsTransferRecord.setOutAccountId(debitCardVo.getId());
            accountsTransferRecord.setName(debitCardVo.getName()+"（"+StringUtils.getLastNumberChars(3,debitCardVo.getCode())+"）还款至"+lendVo.getName()+"（"+StringUtils.getLastNumberChars(3,lendVo.getCode())+"）");
            lendAccount.setBalance(lendAccount.getBalance()+accountsTransferRecord.getAmount());
        }
        lendAccount.setAvailableLimit(lendAccount.getBalance());
        lendAccount.setUpdateTime(accountsTransferRecord.getCreateTime());
        accountsMapper.updateAccounts(lendAccount);
        //借贷账户记录明细
        AccountsDealRecord dealRecord=new AccountsDealRecord();
        dealRecord.setTransferRecordId(accountsTransferRecord.getId());
        dealRecord.setAccountId(lendAccount.getAccountId());
        dealRecord.setId(IdWorker.getId());
        //支出金额
        dealRecord.setAmount(accountsTransferRecord.getAmount());
        //实时余额
        dealRecord.setCurrentBalance(lendAccount.getBalance());
        //1储蓄卡，2 信用卡，3 借贷，5 投资账户
        dealRecord.setType("3");
        //1 收入，2 支出，3 转账，4 借贷
        //1表示借款，2表示还款
        if(accountsTransferRecord.getDealType().equals("1")){
            dealRecord.setDealType("2");
            //借贷至储蓄卡
            dealRecord.setName(lendVo.getName()+"（"+StringUtils.getLastNumberChars(3,lendVo.getCode())+"）借款至"+debitCardVo.getName()+"（"+StringUtils.getLastNumberChars(3,debitCardVo.getCode())+"）");
        }else  if(accountsTransferRecord.getDealType().equals("2")){
            dealRecord.setDealType("1");
            //投资账户账至储蓄卡转
            dealRecord.setName(debitCardVo.getName()+"（"+StringUtils.getLastNumberChars(3,debitCardVo.getCode())+"）还款至"+lendVo.getName()+"（"+StringUtils.getLastNumberChars(3,lendVo.getCode())+"）");
        }
        //借贷
        dealRecord.setDealCategory("9");
        dealRecord.setRemark("借贷记账");
        dealRecord.setCreateBy(SecurityUtils.getUsername());
        dealRecord.setCreateTime(accountsTransferRecord.getCreateTime());
        //重新计算余额
        //        accountsDealRecordMapper.insertAccountsDealRecord(dealRecord);
        insertAccountsDealRecord(dealRecord);

        //储蓄卡账户余额计算
        AccountsVo debitAccount=accountsMapper.selectAccountsById(debitCardVo.getId());
        //1表示借款，2表示还款
        if(accountsTransferRecord.getDealType().equals("1")){
            debitAccount.setBalance(debitAccount.getBalance()+accountsTransferRecord.getAmount());
        }else  if(accountsTransferRecord.getDealType().equals("2")){
            debitAccount.setBalance(debitAccount.getBalance()-accountsTransferRecord.getActualAmount());
        }
        debitAccount.setAvailableLimit(debitAccount.getBalance());
        debitAccount.setUpdateTime(accountsTransferRecord.getCreateTime());
        accountsMapper.updateAccounts(debitAccount);

        //储蓄卡消费记录明细,支出
        dealRecord.setAccountId(debitAccount.getAccountId());
        dealRecord.setId(IdWorker.getId());
        //支出金额
        dealRecord.setAmount(accountsTransferRecord.getActualAmount());
        //实时余额
        dealRecord.setCurrentBalance(debitAccount.getBalance());
        //1储蓄卡，2 信用卡，3 网贷，4 人情，5 投资账户
        dealRecord.setType("3");
        //1 收入，2 支出，3 转账，4 借贷
        //1表示借款，2表示还款
        if(accountsTransferRecord.getDealType().equals("1")){
            dealRecord.setDealType("1");
            dealRecord.setRemark("借贷记账借款。");
            //投资账户账至储蓄卡转
            dealRecord.setName(lendVo.getName()+"（"+StringUtils.getLastNumberChars(3,lendVo.getCode())+"）借款至"+debitCardVo.getName()+"（"+StringUtils.getLastNumberChars(3,debitCardVo.getCode())+"）");
        }else  if(accountsTransferRecord.getDealType().equals("2")){
            dealRecord.setDealType("2");
            dealRecord.setRemark("借贷记账还款，本息合计。");
            //储蓄卡还款至投资账户
            dealRecord.setName(debitCardVo.getName()+"（"+StringUtils.getLastNumberChars(3,debitCardVo.getCode())+"）还款至"+lendVo.getName()+"（"+StringUtils.getLastNumberChars(3,lendVo.getCode())+"）");
        }
        //投资账户转账
        dealRecord.setDealCategory("9");

        dealRecord.setCreateBy(SecurityUtils.getUsername());
        dealRecord.setCreateTime(accountsTransferRecord.getCreateTime());
        //重新计算余额
   //        accountsDealRecordMapper.insertAccountsDealRecord(dealRecord);
        insertAccountsDealRecord(dealRecord);
    }

    public int insertAccountsDealRecord(AccountsDealRecord accountsDealRecord)
    {
        accountsDealRecord.setCreateBy(SecurityUtils.getUsername());
        accountsDealRecord.setId(IdWorker.getId());

        //获取最新一条记录的数据，计算余额
        Map<String, Object> lastMap = new HashMap<>();
        lastMap.put("startDateTime", accountsDealRecord.getCreateTime());
        lastMap.put("accountId", accountsDealRecord.getAccountId());
        AccountsDealRecord lastOneAccountsDealRecord=accountsDealRecordMapper.getLastOneAccountsDealRecordByMap(lastMap);
        if(lastOneAccountsDealRecord==null){
            lastOneAccountsDealRecord=new AccountsDealRecord();
            lastOneAccountsDealRecord.setCurrentBalance(0d);
        }
        //收入
        if(accountsDealRecord.getDealType().equals("1")){
            Map<String, Object> map = new HashMap<>();
            map.put("startDateTime", accountsDealRecord.getCreateTime());
            map.put("accountId", accountsDealRecord.getAccountId());
            map.put("currentBalance", accountsDealRecord.getAmount());
            accountsDealRecordMapper.batchPlusCurrentBalance(map);
        }
        //支出
        if(accountsDealRecord.getDealType().equals("2")){
            Map<String, Object> map = new HashMap<>();
            map.put("startDateTime", accountsDealRecord.getCreateTime());
            map.put("accountId", accountsDealRecord.getAccountId());
            map.put("currentBalance", accountsDealRecord.getAmount());
            accountsDealRecordMapper.batchMinusCurrentBalance(map);
        }

        return accountsDealRecordMapper.insertAccountsDealRecord(accountsDealRecord);
    }
}
