package com.jwipc.contract.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jwipc.contract.entity.ContractPayStage;
import com.jwipc.contract.mapper.ContractPayStageMapper;
import com.jwipc.contract.model.po.ContractPayStagePO;
import com.jwipc.contract.model.query.ContractPayStageQuery;
import com.jwipc.contract.service.IContractPayStageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zx
 * @since 2024-03-08
 */
@Service
public class ContractPayStageServiceImpl extends ServiceImpl<ContractPayStageMapper, ContractPayStage> implements IContractPayStageService {
    @Override
   public List<ContractPayStage> getContractPayStageList(Long contractId){
        List<ContractPayStage> contractPayStages = baseMapper.getContractPayStageList(contractId);
        contractPayStages.forEach(contractPayStage -> {
            if(contractPayStage.getPaymentAlreadyAmount() == null){
                contractPayStage.setPaymentAlreadyAmount(BigDecimal.ZERO);
            }
            if (ObjectUtil.isNotNull(contractPayStage.getPaymentAmount())){
                contractPayStage.setPaymentPlusAmount(contractPayStage.getPaymentAmount().subtract(contractPayStage.getPaymentAlreadyAmount()));
            }
        });
      return  contractPayStages;
    }

    @Override
    public List<ContractPayStagePO> findByPage(Page<ContractPayStage> dbPage, ContractPayStageQuery query) {
        if (dbPage==null){
            return baseMapper.findPageByQuery(query);
        }
        return baseMapper.findPageByQuery(dbPage,query);
    }

    @Override
    public List<ContractPayStagePO> findContractPayStageList() {
        return baseMapper.findContractPayStageList();
    }
}

