package com.fjhx.sale.service;

import cn.hutool.core.util.ObjectUtil;
import com.fjhx.account.entity.account.po.AccountRunningWater;
import com.fjhx.account.service.AccountService;
import com.fjhx.sale.entity.claim.po.Claim;
import com.fjhx.sale.entity.claim.po.ClaimContract;
import com.fjhx.sale.service.claim.ClaimContractService;
import com.fjhx.sale.service.claim.ClaimService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AccountServiceImpl implements AccountService {
    @Autowired
    private ClaimService claimService;
    private ClaimContractService claimContractService;

    /**
     * 根据流水信息 重新更新到账信息的人民币金额
     */
    @Override
    public void updateClaimInfo(AccountRunningWater accountRunningWater) {
        List<Long> claimIds = claimService.listObject(Claim::getId, q -> q.eq(Claim::getBusinessId, accountRunningWater.getId()));
        if (ObjectUtil.isEmpty(claimIds)) {
            return;
        }
        List<ClaimContract> claimContracts = claimContractService.list(q -> q.in(ClaimContract::getClaimId, claimIds));
        if (ObjectUtil.isEmpty(claimContracts)) {
            return;
        }

        for (ClaimContract claimContract : claimContracts) {
            claimContract.setRate(accountRunningWater.getRate());
            claimContract.setMoneyCny(claimContract.getMoney().multiply(accountRunningWater.getRate()));
        }

        claimContractService.updateBatchById(claimContracts);
    }
}
