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.dto.AccountsDealRecordDto;
import com.ruoyi.invest.domain.vo.AccountsDealRecordVo;
import com.ruoyi.invest.domain.vo.AccountsVo;
import com.ruoyi.invest.mapper.AccountsDealRecordMapper;
import com.ruoyi.invest.mapper.AccountsMapper;
import com.ruoyi.invest.service.IAccountsDealRecordService;
import org.springframework.stereotype.Service;

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

/**
 * 账户交易记录Service业务层处理
 *
 * @author tianyongbao
 * @date 2024-03-30
 */
@Service
public class AccountsDealRecordServiceImpl implements IAccountsDealRecordService
{
    @Resource
    private AccountsDealRecordMapper accountsDealRecordMapper;

    @Resource
    private AccountsMapper accountsMapper;

    /**
     * 查询账户交易记录
     *
     * @param id 账户交易记录主键
     * @return 账户交易记录
     */
    @Override
    public AccountsDealRecordVo selectAccountsDealRecordById(Long id)
    {
        return accountsDealRecordMapper.selectAccountsDealRecordById(id);
    }

    /**
     * 查询账户交易记录列表
     *
     * @param accountsDealRecordDto 账户交易记录
     * @return 账户交易记录
     */
    @Override
    public List<AccountsDealRecordVo> selectAccountsDealRecordList(AccountsDealRecordDto accountsDealRecordDto)
    {
        return accountsDealRecordMapper.selectAccountsDealRecordList(accountsDealRecordDto);
    }

    /**
     * 新增账户交易记录
     *
     * @param accountsDealRecord 账户交易记录
     * @return 结果
     */
    @Override
    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")){

            //收入账户余额计算
            AccountsVo inAccount=accountsMapper.selectAccountsById(accountsDealRecord.getAccountId());
            inAccount.setBalance(inAccount.getBalance()+accountsDealRecord.getAmount());
            inAccount.setAvailableLimit(inAccount.getAvailableLimit()+accountsDealRecord.getAmount());
            accountsDealRecord.setCurrentBalance(lastOneAccountsDealRecord.getCurrentBalance()+accountsDealRecord.getAmount());
            accountsMapper.updateAccounts(inAccount);
            //收入
            accountsDealRecord.setName(inAccount.getName()+"（"+ StringUtils.getLastNumberChars(3,inAccount.getCode())+"）收入"+accountsDealRecord.getAmount());

            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")){
            //支出余额计算
            AccountsVo outAccount=accountsMapper.selectAccountsById(accountsDealRecord.getAccountId());
            outAccount.setBalance(outAccount.getBalance()-accountsDealRecord.getAmount());
            outAccount.setAvailableLimit(outAccount.getAvailableLimit()-accountsDealRecord.getAmount());
            accountsDealRecord.setCurrentBalance(lastOneAccountsDealRecord.getCurrentBalance()-accountsDealRecord.getAmount());
            accountsMapper.updateAccounts(outAccount);
            //支出
            accountsDealRecord.setName(outAccount.getName()+"（"+ StringUtils.getLastNumberChars(3,outAccount.getCode())+"）支出"+accountsDealRecord.getAmount());

            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);
    }

    /**
     * 修改账户交易记录
     *
     * @param accountsDealRecord 账户交易记录
     * @return 结果
     */
    @Override
    public int updateAccountsDealRecord(AccountsDealRecord accountsDealRecord)
    {
        accountsDealRecord.setUpdateBy(SecurityUtils.getUsername());
        accountsDealRecord.setUpdateTime(DateUtils.getNowDate());
        return accountsDealRecordMapper.updateAccountsDealRecord(accountsDealRecord);
    }

    /**
     * 批量删除账户交易记录
     *
     * @param ids 需要删除的账户交易记录主键
     * @return 结果
     */
    @Override
    public int deleteAccountsDealRecordByIds(Long[] ids)
    {
        for (Long id : ids) {
            updateDeleteDealRecord(id);
        }
        return accountsDealRecordMapper.removeAccountsDealRecordByIds(ids);
    }

    /**
     * 删除账户交易记录信息
     *
     * @param id 账户交易记录主键
     * @return 结果
     */
    @Override
    public int deleteAccountsDealRecordById(Long id)
    {
        updateDeleteDealRecord(id);
        return accountsDealRecordMapper.removeAccountsDealRecordById(id);
    }

    public  void updateDeleteDealRecord(Long id){
        //获取交易记录，删除的话，收入需要减回去，支出需要加回来。
        AccountsDealRecordVo accountsDealRecord=accountsDealRecordMapper.selectAccountsDealRecordById(id);
        //获取最新一条记录的数据，计算余额
        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")){
            //收入账户余额计算
            AccountsVo inAccount=accountsMapper.selectAccountsById(accountsDealRecord.getAccountId());
            inAccount.setBalance(inAccount.getBalance()-accountsDealRecord.getAmount());
            inAccount.setAvailableLimit(inAccount.getAvailableLimit()-accountsDealRecord.getAmount());
            accountsDealRecord.setCurrentBalance(lastOneAccountsDealRecord.getCurrentBalance()-accountsDealRecord.getAmount());
            accountsMapper.updateAccounts(inAccount);

            Map<String, Object> map = new HashMap<>();
            map.put("startDateTime", accountsDealRecord.getCreateTime());
            map.put("accountId", accountsDealRecord.getAccountId());
            map.put("currentBalance", accountsDealRecord.getAmount());

            accountsDealRecordMapper.batchMinusCurrentBalance(map);
        }

        //支出
        if(accountsDealRecord.getDealType().equals("2")){
            //支出余额计算
            AccountsVo outAccount=accountsMapper.selectAccountsById(accountsDealRecord.getAccountId());
            outAccount.setBalance(outAccount.getBalance()+accountsDealRecord.getAmount());
            outAccount.setAvailableLimit(outAccount.getAvailableLimit()+accountsDealRecord.getAmount());
            accountsDealRecord.setCurrentBalance(lastOneAccountsDealRecord.getCurrentBalance()+accountsDealRecord.getAmount());
            accountsMapper.updateAccounts(outAccount);
            Map<String, Object> map = new HashMap<>();
            map.put("startDateTime", accountsDealRecord.getCreateTime());
            map.put("accountId", accountsDealRecord.getAccountId());
            map.put("currentBalance", accountsDealRecord.getAmount());

            accountsDealRecordMapper.batchPlusCurrentBalance(map);
        }
    }

}
