package com.fjhx.account.service.repayment.impl;

import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.account.entity.account.enums.AccountRunningWaterEnum;
import com.fjhx.account.entity.account.po.AccountRunningWater;
import com.fjhx.account.entity.loan.po.LoanInfo;
import com.fjhx.account.entity.repayment.dto.RepaymentRecordsDto;
import com.fjhx.account.entity.repayment.dto.RepaymentRecordsSelectDto;
import com.fjhx.account.entity.repayment.po.RepaymentRecords;
import com.fjhx.account.entity.repayment.vo.RepaymentRecordsVo;
import com.fjhx.account.mapper.repayment.RepaymentRecordsMapper;
import com.fjhx.account.service.account.AccountRunningWaterService;
import com.fjhx.account.service.loan.LoanInfoService;
import com.fjhx.account.service.repayment.RepaymentRecordsService;
import com.fjhx.common.utils.Assert;
import com.ruoyi.common.utils.wrapper.IWrapper;
import com.ruoyi.system.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 还款记录 服务实现类
 * </p>
 *
 * @author
 * @since 2023-09-15
 */
@Service
public class RepaymentRecordsServiceImpl extends ServiceImpl<RepaymentRecordsMapper, RepaymentRecords> implements RepaymentRecordsService {

    @Autowired
    private AccountRunningWaterService accountRunningWaterService;
    @Autowired
    private LoanInfoService loanInfoService;

    @Override
    public List<RepaymentRecordsVo> getList(RepaymentRecordsSelectDto dto) {
        IWrapper<RepaymentRecords> wrapper = getWrapper();
        //借款id过滤
        Assert.notEmpty(dto.getLoanId(), "借款id不能为空");
        wrapper.eq(RepaymentRecords::getLoanId, dto.getLoanId());
        //排序 付款时间>创建时间>id
        wrapper.orderByDesc("rr", RepaymentRecords::getRepaymentTime);
        wrapper.orderByDesc("rr", RepaymentRecords::getCreateTime);
        wrapper.orderByDesc("rr", RepaymentRecords::getId);
        List<RepaymentRecordsVo> list = this.baseMapper.getList(wrapper);
        //赋值登记人名称
        UserUtil.assignmentNickName(list, RepaymentRecordsVo::getCreateUser, RepaymentRecordsVo::setCreateUserName);
        return list;
    }

    @Override
    @DSTransactional
    public void add(RepaymentRecordsDto repaymentRecordsDto) {
        Assert.notEmpty(repaymentRecordsDto.getLoanId(), "借款id不能为空");
        this.save(repaymentRecordsDto);
        //创建收入流水
        AccountRunningWater accountRunningWater = new AccountRunningWater();
        accountRunningWater.setStatus("10");
        accountRunningWater.setType(AccountRunningWaterEnum.REPAYMENT.getKey());
        accountRunningWater.setRemarks(repaymentRecordsDto.getRemarks());
        accountRunningWater.setTransactionTime(repaymentRecordsDto.getRepaymentTime());
        accountRunningWater.setAccountManagementId(repaymentRecordsDto.getRepaymentAccountId());
        accountRunningWater.setAmount(repaymentRecordsDto.getAmount());
        Assert.notEmpty(repaymentRecordsDto.getCurrency(), "还款币种不能为空");
        accountRunningWater.setCurrency(repaymentRecordsDto.getCurrency());
        accountRunningWater.setBusinessId(repaymentRecordsDto.getId());
        //操作余额
        accountRunningWaterService.changeRemainder(accountRunningWater);
        accountRunningWaterService.save(accountRunningWater);
        //修改借款状态
        LoanInfo loanInfo = loanInfoService.getById(repaymentRecordsDto.getLoanId());
        List<RepaymentRecords> repaymentRecordsList = this.list(q -> q.eq(RepaymentRecords::getLoanId, repaymentRecordsDto.getLoanId()));
        BigDecimal repaymentAmount = repaymentRecordsList.stream().map(RepaymentRecords::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
        if (repaymentAmount.compareTo(loanInfo.getAmount()) < 0) {
            loanInfo.setRepaymentStatus(1);//部分还款
        } else {
            loanInfo.setRepaymentStatus(2);//已还款
        }
        loanInfoService.updateById(loanInfo);
    }

}
