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

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jy.business.capital.dao.CapitalAccountDao;
import com.jy.business.capital.model.entity.CapitalAccount;
import com.jy.business.capital.service.CapitalTransactionsService;
import com.jy.business.contract.dao.ContractFundReceivedDao;
import com.jy.business.contract.model.dto.ContractFundReceivedDto;
import com.jy.business.contract.model.dto.ContractFundReceivedSelectDto;
import com.jy.business.contract.model.entity.ContractFundReceived;
import com.jy.business.contract.model.vo.ContractFundReceivedVo;
import com.jy.business.contract.service.ContractFundReceivedService;
import com.jy.framework.model.base.BaseIdPo;
import com.jy.framework.model.constants.CommonConstant;
import com.jy.framework.utils.AssertUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 合同到账 服务实现类
 * </p>
 *
 * @author
 * @since 2025-06-11
 */
@Service
public class ContractFundReceivedServiceImpl implements ContractFundReceivedService {

    @Resource
    private ContractFundReceivedDao contractFundReceivedDao;

    @Resource
    private CapitalTransactionsService capitalTransactionsService;

    @Resource
    private CapitalAccountDao capitalAccountDao;


    @Override
    public List<ContractFundReceivedVo> getList(ContractFundReceivedSelectDto dto) {
        return contractFundReceivedDao.getList(dto);
    }

    @Override
    public Page<ContractFundReceivedVo> getPage(ContractFundReceivedSelectDto dto) {
        return contractFundReceivedDao.getPage(dto);
    }

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

        if (CommonConstant.NO.equals(vo.getHasReceived()) && vo.getSellBankAccount() != null) {
            CapitalAccount capitalAccount = capitalAccountDao.getOne(q -> q.eq(CapitalAccount::getAccount, vo.getSellBankAccount()));
            if (capitalAccount != null) {
                vo.setCapitalAccountId(capitalAccount.getId());
            }
        }

        return vo;
    }

    @Override
    public void add(ContractFundReceivedDto dto) {
        contractFundReceivedDao.save(dto);
    }

    @Override
    public void edit(ContractFundReceivedDto dto) {
        contractFundReceivedDao.updateById(dto);
    }

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

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void confirmArrivalAccount(ContractFundReceivedDto dto) {
        boolean update = contractFundReceivedDao.update(q -> q
                .eq(BaseIdPo::getId, dto.getId())
                .eq(ContractFundReceived::getHasReceived, CommonConstant.NO)
                .set(ContractFundReceived::getHasReceived, CommonConstant.YES)
                .set(ContractFundReceived::getContractId, dto.getContractId())
                .set(ContractFundReceived::getRemark, dto.getRemark())
        );
        AssertUtil.eqTrue(update, "资金已到账");

        //RabbitMqUtil.sendConfirmFundReceived(dto.getId());
    }

}
