package com.jy.business.payment.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jy.business.capital.model.dto.CapitalTransactionsDto;
import com.jy.business.capital.service.CapitalTransactionsService;
import com.jy.business.payment.dao.PaymentRemitDao;
import com.jy.business.payment.dao.PaymentRequestsDao;
import com.jy.business.payment.dao.PaymentRequestsDetailDao;
import com.jy.business.payment.model.dto.PaymentRequestsDto;
import com.jy.business.payment.model.dto.PaymentRequestsSelectDto;
import com.jy.business.payment.model.entity.PaymentRemit;
import com.jy.business.payment.model.entity.PaymentRequests;
import com.jy.business.payment.model.entity.PaymentRequestsDetail;
import com.jy.business.payment.model.vo.PaymentRequestsVo;
import com.jy.business.payment.service.PaymentRequestsService;
import com.jy.flow.model.dto.RetrieveDto;
import com.jy.flow.model.enums.FlowStatusEnum;
import com.jy.flow.service.ExecuteService;
import com.jy.flow.utils.FlowUtil;
import com.jy.framework.model.constants.CommonConstant;
import com.jy.framework.model.constants.FlowConstant;
import com.jy.framework.utils.AssertUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * <p>
 * 请款 服务实现类
 * </p>
 *
 * @author
 * @since 2024-10-18
 */
@Service
public class PaymentRequestsServiceImpl implements PaymentRequestsService {

    @Resource
    private PaymentRequestsDao paymentRequestsDao;

    @Resource
    private PaymentRequestsDetailDao paymentRequestsDetailDao;

    @Resource
    private PaymentRemitDao paymentRemitDao;

    @Resource
    private CapitalTransactionsService capitalTransactionsService;

    @Resource
    private ExecuteService executeService;

    @Override
    public Page<PaymentRequestsVo> getPage(PaymentRequestsSelectDto dto) {
        return paymentRequestsDao.getPage(dto);
    }

    @Override
    public PaymentRequestsVo getDetail(Long id) {
        PaymentRequestsVo vo = paymentRequestsDao.getDetail(id);
        AssertUtil.notNull(vo, "未知数据");

        List<PaymentRequestsDetail> list = paymentRequestsDetailDao.getListByPaymentRequestsId(id);
        vo.setPaymentRequestsDetailList(list);
        return vo;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(PaymentRequestsDto dto) {
        dto.setApprovalStatus(FlowStatusEnum.APPROVAL.getKey());
        dto.setPaymentStatus(CommonConstant.NO);
        dto.setRemitAmount(BigDecimal.ZERO);
        paymentRequestsDao.save(dto);

        List<PaymentRequestsDetail> paymentRequestsDetailList = dto.getPaymentRequestsDetailList();
        paymentRequestsDetailList.forEach(item -> item.setPaymentRequestsId(dto.getId()));
        paymentRequestsDetailDao.saveBatch(paymentRequestsDetailList);

        // 启动流程
        FlowUtil.start(FlowConstant.PAYMENT_REQUESTS, dto.getId());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(PaymentRequestsDto dto) {
        dto.setApprovalStatus(FlowStatusEnum.APPROVAL.getKey());
        paymentRequestsDao.updateById(dto);
        List<PaymentRequestsDetail> paymentRequestsDetailList = dto.getPaymentRequestsDetailList();
        paymentRequestsDetailList.forEach(item -> item.setPaymentRequestsId(dto.getId()));
        paymentRequestsDetailDao.updateLinked(paymentRequestsDetailList, PaymentRequestsDetail::getPaymentRequestsId, dto.getId());

        FlowUtil.next(dto.getId());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<Long> idList) {
        paymentRequestsDao.removeBatchByIds(idList);
    }

    @Override
    public Page<PaymentRequestsVo> getRemitPage(PaymentRequestsSelectDto dto) {
        return paymentRequestsDao.getRemitPage(dto);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public synchronized void remit(PaymentRemit dto) {
        PaymentRequests paymentRequests = paymentRequestsDao.getById(dto.getPaymentRequestsId());
        AssertUtil.notNull(paymentRequests, "未知请款id");
        AssertUtil.eqTrue(dto.getAmount().compareTo(BigDecimal.ZERO) > 0, "请款金额必须大于0");
        AssertUtil.notEqual(paymentRequests.getPaymentStatus(), 2, "打款已完成");

        BigDecimal totalAmount = paymentRequests.getRemitAmount().add(dto.getAmount());
        int amountCompareTo = paymentRequests.getTotalAmount().compareTo(totalAmount);
        AssertUtil.eqTrue(amountCompareTo >= 0, "打款金额超过请款金额");

        // 更新打款金额和状态
        paymentRequests.setRemitAmount(totalAmount);
        paymentRequests.setPaymentStatus(amountCompareTo == 0 ? 2 : 1);
        paymentRequestsDao.updateById(paymentRequests);

        // 保存打款记录
        paymentRemitDao.save(dto);

        // 添加资金流水
        CapitalTransactionsDto capitalTransactionsDto = new CapitalTransactionsDto();
        capitalTransactionsDto.setCapitalAccountId(dto.getCapitalAccountId());
        capitalTransactionsDto.setTradingTime(dto.getRemitTime());
        capitalTransactionsDto.setType(0);
        capitalTransactionsDto.setAmount(dto.getAmount());
        capitalTransactionsDto.setTargetType(90);
        capitalTransactionsDto.setTargetAccountName(paymentRequests.getAccountName());
        capitalTransactionsDto.setTargetDepositBank(paymentRequests.getDepositBank());
        capitalTransactionsDto.setTargetAccount(paymentRequests.getAccount());
        capitalTransactionsDto.setRemark(dto.getRemark());
        capitalTransactionsService.add(capitalTransactionsDto);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void retrieve(RetrieveDto dto) {
        paymentRequestsDao.updateFlowStatus(dto.getBusinessId(), FlowStatusEnum.RETRIEVE.getKeyStr());
        executeService.retrieveByBusinessId(dto.getBusinessId());
    }

}
