package com.bonus.sgzb.material.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.bonus.sgzb.base.api.domain.*;

import com.bonus.sgzb.common.core.exception.ServiceException;
import com.bonus.sgzb.common.core.utils.DateTimeHelper;
import com.bonus.sgzb.common.core.utils.DateUtils;
import com.bonus.sgzb.common.core.web.domain.AjaxResult;
import com.bonus.sgzb.common.security.utils.SecurityUtils;
import com.bonus.sgzb.material.domain.AgreementInfo;
import com.bonus.sgzb.material.domain.TmTask;

import com.bonus.sgzb.material.mapper.CalMonthlyMapper;
import com.bonus.sgzb.material.mapper.SltAgreementInfoMapper;
import com.bonus.sgzb.material.service.SltAgreementInfoService;
import com.bonus.sgzb.material.vo.GlobalContants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author c liu
 * @date 2024/2/22
 */
@Service
public class SltAgreementInfoServiceImpl implements SltAgreementInfoService {
    @Resource
    private SltAgreementInfoMapper sltAgreementInfoMapper;

    @Resource
    private CalMonthlyMapper calMonthlyMapper;

    @Override
    public List<AgreementInfo> getSltAgreementInfo(AgreementInfo bean) {
        return sltAgreementInfoMapper.getSltAgreementInfo(bean);
    }

    @Override
    public SltInfoVo getSltInfo(List<AgreementInfo> list) {
        SltInfoVo sltInfoVo = new SltInfoVo();
        //租赁费用列表
        List<SltAgreementInfo> leaseListOne = getLeaseListOne(list);

        List<SltAgreementInfo> leaseListThree = getLeaseListThree(list);
        //维修费用列表
        List<SltAgreementInfo> repairList = getRepairList(list);
        //报废费用列表
        List<SltAgreementInfo> scrapList = getScrapList(list);
        //预报废费用列表
        List<SltAgreementInfo> preScrapList = getPreScrapList(list);
        //丢失费用列表
        List<SltAgreementInfo> loseList = getLoseList(list);
        //额外增加费用
        List<ApplyRelation> addCostList = getAddLoseList(list);
        sltInfoVo.setLeaseListOne(leaseListOne);
        sltInfoVo.setLeaseListThree(leaseListThree);
        sltInfoVo.setRepairList(repairList);
        sltInfoVo.setScrapList(scrapList);
        sltInfoVo.setPreScrapList(preScrapList);
        sltInfoVo.setLoseList(loseList);
        sltInfoVo.setAddCostList(addCostList);
        List<SltAgreementRelation> relations = getRelations(leaseListOne, leaseListThree, repairList, scrapList, preScrapList, loseList, addCostList, list);
        sltInfoVo.setRelations(relations);
        return sltInfoVo;
    }

    private List<ApplyRelation> getAddLoseList(List<AgreementInfo> list) {
        List<ApplyRelation> applyRelations = new ArrayList<>();
        for (AgreementInfo bean : list) {
            List<ApplyRelation> addLoseList = sltAgreementInfoMapper.getAddLoseList(bean);
            applyRelations.addAll(addLoseList);
        }
        return applyRelations;
    }

    @Override
    public List<SltAgreementInfo> getPreScrapList(List<AgreementInfo> list) {
        List<SltAgreementInfo> scrapList = new ArrayList<>();
        String taskType = "57";
        for (AgreementInfo bean : list) {
            List<TmTask> taskList = sltAgreementInfoMapper.getTaskList(bean, taskType);
            if (taskList != null && taskList.size() > 0) {
                List<SltAgreementInfo> scrapDetailsList = sltAgreementInfoMapper.getPreScrapDetailsList(taskList);
                scrapList.addAll(scrapDetailsList);
            }
        }

        if (scrapList != null && scrapList.size() > 0) {
            for (SltAgreementInfo bean : scrapList) {
                if (bean.getBuyPrice() == null) {
                    bean.setBuyPrice(new BigDecimal(0));
                }
                if (bean.getNum() == null) {
                    bean.setNum(0);
                }
                BigDecimal buyPrice = bean.getBuyPrice();
                BigDecimal num = new BigDecimal(bean.getNum());
                BigDecimal costs = buyPrice.multiply(num);
                bean.setCosts(costs);
            }
        }

        return scrapList;
    }

    @Override
    public SltInfoVo getSltExamInfo(SltAgreementApply apply) {
        SltInfoVo sltInfoVo = new SltInfoVo();
        List<AgreementInfo> list = new ArrayList<>();
        String cost = sltAgreementInfoMapper.getCost(apply);
        List<SltAgreementRelation> relations = sltAgreementInfoMapper.getRelations(apply);
        sltInfoVo.setRelations(relations);
        for (SltAgreementRelation relation : relations) {
            AgreementInfo info = new AgreementInfo();
            info.setAgreementId(Long.valueOf(relation.getAgreementId()));
            list.add(info);
        }
        List<SltAgreementInfo> leaseListOne = getLeaseListOne(list);
        List<SltAgreementInfo> leaseListThree = getLeaseListThree(list);
        List<SltAgreementInfo> repairList = getRepairList(list);
        List<SltAgreementInfo> scrapList = getScrapList(list);
        List<SltAgreementInfo> loseList = getLoseList(list);
        sltInfoVo.setLeaseListOne(leaseListOne);
        sltInfoVo.setLeaseListOne(leaseListThree);
        sltInfoVo.setRepairList(repairList);
        sltInfoVo.setScrapList(scrapList);
        sltInfoVo.setLoseList(loseList);
        sltInfoVo.setCost(cost);
        return sltInfoVo;
    }

    @Override
    @Transactional
    public int settlementReview(SltAgreementApply apply) {
        int i = sltAgreementInfoMapper.updateRelation(apply);
        return i;
    }

    @Override
    public List<SltAgreementInfo> getLeaseList(List<AgreementInfo> list) {
        List<SltAgreementInfo> leaseList = new ArrayList<>();
        for (AgreementInfo bean : list) {
            List<SltAgreementInfo> oneOfList = sltAgreementInfoMapper.getLeaseList(bean);
            leaseList.addAll(oneOfList);
        }
        for (SltAgreementInfo bean : leaseList) {
            if (bean.getLeasePrice() == null) {
                bean.setLeasePrice(new BigDecimal(0));
            }
            if (bean.getNum() == null) {
                bean.setNum(0);
            }
            if (StringUtils.isEmpty(bean.getLeaseDays())) {
                bean.setLeaseDays("0");
            }
            if (Objects.isNull(bean.getTrimDay())) {
                bean.setTrimDay(0);
            }
            BigDecimal leasePrice = bean.getLeasePrice();
            BigDecimal num = new BigDecimal(bean.getNum());
            BigDecimal leaseDays = new BigDecimal(bean.getLeaseDays());
            BigDecimal costs = leasePrice.multiply(num).multiply(leaseDays);
            bean.setCosts(costs);
           /* BigDecimal costs = BigDecimal.ZERO;
            //当单价，数量，租赁天数都不为空时计算总价
            if(bean.getLeasePrice() != null && bean.getNum() != null && bean.getLeaseDays() != null){
                BigDecimal leasePrice = new BigDecimal(bean.getLeasePrice());
                BigDecimal num = new BigDecimal(bean.getNum());
                BigDecimal leaseDays = new BigDecimal(bean.getLeaseDays());
                costs = leasePrice.multiply(num).multiply(leaseDays);
            }
            bean.setCosts(String.valueOf(costs));*/
        }
        return leaseList;
    }

    @Override
    public List<SltAgreementInfo> getLeaseListThree(List<AgreementInfo> list) {
        List<SltAgreementInfo> leaseList = new ArrayList<>();
        for (AgreementInfo bean : list) {
            List<SltAgreementInfo> oneOfList = sltAgreementInfoMapper.getLeaseListThree(bean);
            leaseList.addAll(oneOfList);
        }
        for (SltAgreementInfo bean : leaseList) {
            if (bean.getLeasePrice() == null) {
                bean.setLeasePrice(new BigDecimal(0));
            }
            if (bean.getNum() == null) {
                bean.setNum(0);
            }
            if (StringUtils.isEmpty(bean.getLeaseDays())) {
                bean.setLeaseDays("0");
            }
            if (Objects.isNull(bean.getTrimDay())) {
                bean.setTrimDay(0);
            }
            BigDecimal leasePrice = bean.getLeasePrice();
            BigDecimal num = new BigDecimal(bean.getNum());
            BigDecimal leaseDays = new BigDecimal(bean.getLeaseDays());
            // 实际结算天数（领料天数 + 调整天数(可以为负整数)）
            BigDecimal realDays = leaseDays.add(new BigDecimal(bean.getTrimDay()));
            // 实际结算金额
            BigDecimal realCosts = leasePrice.multiply(num).multiply(realDays);
            // 应结算金额
            BigDecimal costs = leasePrice.multiply(num).multiply(leaseDays);
            bean.setCosts(costs);
            //实际结算天数
            bean.setRealDays(realDays);
            // 实际结算金额
            bean.setRealCosts(realCosts);
        }
        return leaseList;
    }

    private List<SltAgreementRelation> getRelations(List<SltAgreementInfo> leaseListOne, List<SltAgreementInfo> leaseListThree, List<SltAgreementInfo> repairList, List<SltAgreementInfo> scrapList, List<SltAgreementInfo> preScrapList, List<SltAgreementInfo> loseList, List<ApplyRelation> addCostList, List<AgreementInfo> list) {
        List<SltAgreementRelation> relations = new ArrayList<>();
        for (AgreementInfo info : list) {
            SltAgreementRelation relation = new SltAgreementRelation();
            BigDecimal loseCost = BigDecimal.ZERO;
            BigDecimal leaseCostOne = BigDecimal.ZERO;
            BigDecimal leaseCostThree = BigDecimal.ZERO;
            BigDecimal scrapCost = BigDecimal.ZERO;
            BigDecimal preScrapCost = BigDecimal.ZERO;
            BigDecimal repairCost = BigDecimal.ZERO;
            BigDecimal addCost = BigDecimal.ZERO;
            for (SltAgreementInfo lease : leaseListOne) {
                if (lease.getAgreementId().equals(info.getAgreementId().toString())) {
                    relation.setAgreementId(lease.getAgreementId());
                    relation.setProjectName(lease.getProjectName());
                    relation.setUnitName(lease.getUnitName());
                    relation.setCompanyId(lease.getCompanyId());
                    BigDecimal cost = lease.getRealCosts();
                    leaseCostOne = leaseCostOne.add(cost);
                }
            }
            for (SltAgreementInfo lease : leaseListThree) {
                if (lease.getAgreementId().equals(info.getAgreementId().toString())) {
                    relation.setAgreementId(lease.getAgreementId());
                    relation.setProjectName(lease.getProjectName());
                    relation.setUnitName(lease.getUnitName());
                    relation.setCompanyId(lease.getCompanyId());
                    BigDecimal cost = lease.getRealCosts();
                    leaseCostThree = leaseCostThree.add(cost);
                }
            }
            for (SltAgreementInfo repair : repairList) {
                if (repair.getAgreementId().equals(info.getAgreementId().toString())) {
                    // 暂时先计算配件总价，后期需要加上委外维修费用
                    BigDecimal allCost = repair.getPartAllCosts();
                    repairCost = repairCost.add(allCost);
                }
            }
            for (SltAgreementInfo scrap : scrapList) {
                if (scrap.getAgreementId().equals(info.getAgreementId().toString())) {
                    BigDecimal cost = scrap.getCosts();
                    scrapCost = scrapCost.add(cost);
                }
            }
            for (SltAgreementInfo scrap : preScrapList) {
                if (scrap.getAgreementId().equals(info.getAgreementId().toString())) {
                    BigDecimal cost = scrap.getCosts();
                    preScrapCost = preScrapCost.add(cost);
                }
            }
            for (SltAgreementInfo lose : loseList) {
                if (lose.getAgreementId().equals(info.getAgreementId().toString())) {
                    //TODO 上面已经set过值，这里为什么还要set值
                    relation.setAgreementId(lose.getAgreementId());
                    relation.setProjectName(lose.getProjectName());
                    relation.setUnitName(lose.getUnitName());
                    relation.setCompanyId(lose.getCompanyId());
                    BigDecimal cost = lose.getCosts();
                    loseCost = loseCost.add(cost);
                }
            }

            for (ApplyRelation applyRelation : addCostList) {
                if (applyRelation.getAgreementId().equals(info.getAgreementId().toString())) {
                    BigDecimal cost = applyRelation.getAddCost();
                    addCost = addCost.add(cost);
                }
            }
            // 判断是否已经结算(01承担方)
            boolean b = leaseListOne.stream().allMatch(t -> "1".equals(t.getIsSlt()));
            if (b) {
                relation.setIsSltOne("1");
            } else {
                relation.setIsSltOne("0");
            }
            // 判断是否已经结算(03承担方)
            boolean f = leaseListThree.stream().allMatch(t -> "1".equals(t.getIsSlt()));
            if (f) {
                relation.setIsSltThree("1");
            } else {
                relation.setIsSltThree("0");
            }
            relation.setLeaseCostOne(leaseCostOne);
            relation.setLeaseCostThree(leaseCostThree);
            relation.setRepairCost(repairCost);
            relation.setScrapCost(scrapCost);
            relation.setPreScrapCost(preScrapCost);
            relation.setLoseCost(loseCost);
            relation.setAddCost(addCost);
            relations.add(relation);
        }
        return relations;
    }

    @Override
    @Transactional
    public AjaxResult submitFee(SltAgreementApply apply) {
        SltAgreementRelation relation = apply.getRelation();
        BigDecimal addPrice = new BigDecimal(0);
        if (StringUtils.isNotBlank(apply.getRepairIds())) {
            for (String id : apply.getRepairIds().split(",")) {
                sltAgreementInfoMapper.updateRecodeIsSlt(id);
            }
        }
        sltAgreementInfoMapper.updateInfoStatus(apply.getAgreementId());
        sltAgreementInfoMapper.updateBmAgreementInfo(apply.getAgreementId());
        relation.setAgreementId(apply.getAgreementId());
        List<ApplyRelation> applyRelation = relation.getApplyRelation();
        if (applyRelation != null && applyRelation.size() > 0) {
            for (ApplyRelation applyRelation1 : applyRelation) {
                addPrice = addPrice.add(applyRelation1.getAddCost());
                applyRelation1.setAgreementId(apply.getAgreementId());
                sltAgreementInfoMapper.insertApplyRelation(applyRelation1);
            }
        }
        relation.setAddCost(addPrice);
        int i = sltAgreementInfoMapper.insRelation(relation);
        if (i > 0) {
            return AjaxResult.success("结算成功");
        } else {
            return AjaxResult.error("结算失败");
        }
    }

    @Override
    public AjaxResult submitFeeOne(SltAgreementInfo apply) {
        int i = sltAgreementInfoMapper.updateInfoOneStatus(apply);
        if (i > 0) {
            return AjaxResult.success("结算成功");
        } else {
            return AjaxResult.error("结算失败");
        }
    }

    @Override
    public List<SltAgreementApply> getSltExam(AgreementInfo bean) {
        return sltAgreementInfoMapper.getSltExam(bean);
    }


    @Override
    public List<SltAgreementInfo> getLoseList(List<AgreementInfo> list) {
        List<SltAgreementInfo> loseList = new ArrayList<>();
        for (AgreementInfo bean : list) {
            List<SltAgreementInfo> oneOfList = sltAgreementInfoMapper.getLoseList(bean);
            loseList.addAll(oneOfList);
        }
        for (SltAgreementInfo bean : loseList) {
            if (bean.getBuyPrice() == null) {
                bean.setBuyPrice(new BigDecimal(0));
            }
            if (bean.getNum() == null) {
                bean.setNum(0);
            }
            BigDecimal buyPrice = bean.getBuyPrice();
            BigDecimal num = new BigDecimal(bean.getNum());
            BigDecimal costs = buyPrice.multiply(num);
            //计算租赁费用
            /*BigDecimal costs = BigDecimal.ZERO;
            if (bean.getBuyPrice() != null && bean.getNum() != null){
                BigDecimal buyPrice = new BigDecimal(bean.getBuyPrice());
                BigDecimal num = new BigDecimal(bean.getNum());
                costs = buyPrice.multiply(num);
            }*/
            bean.setCosts(costs);
        }
        return loseList;
    }

    @Override
    public List<SltAgreementInfo> getScrapList(List<AgreementInfo> list) {
        List<SltAgreementInfo> scrapList = new ArrayList<>();
        String taskType = "57";
        //String taskType = TaskTypeEnum.SCRAP_TASK.getCode();
        for (AgreementInfo bean : list) {
            List<TmTask> taskList = sltAgreementInfoMapper.getTaskList(bean, taskType);
            if (taskList != null && taskList.size() > 0) {
                List<SltAgreementInfo> scrapDetailsList = sltAgreementInfoMapper.getScrapDetailsList(taskList);
                scrapList.addAll(scrapDetailsList);
            }
        }

        if (scrapList != null && scrapList.size() > 0) {
            for (SltAgreementInfo bean : scrapList) {
                if (bean.getBuyPrice() == null) {
                    bean.setBuyPrice(new BigDecimal(0));
                }
                if (bean.getNum() == null) {
                    bean.setNum(0);
                }
                BigDecimal buyPrice = bean.getBuyPrice();
                BigDecimal num = new BigDecimal(bean.getNum());
                BigDecimal costs = buyPrice.multiply(num);
                bean.setCosts(costs);
            }
        }

        return scrapList;
    }

    @Override
    @Transactional
    public int updateTrimDay(List<SltAgreementInfo> sltAgreementInfo) {
        int i = 0;
        for (SltAgreementInfo agreementInfo : sltAgreementInfo) {
            i = sltAgreementInfoMapper.updateTrimDay(agreementInfo);
            if (i == 0) {
                return i;
            }
        }
        return i;
    }

    @Override
    public List getSltInfoMonth(List<AgreementInfo> list) {
        List<SltAgreementInfo> leaseListMonth = new ArrayList<>();
        Integer num = 0;
        //租赁费用列表
        for (AgreementInfo agreementInfo : list) {
            // 如果选择了最新月份则需要查数据
            if (StringUtils.isNotBlank(agreementInfo.getStartTime()) && StringUtils.isNotBlank(agreementInfo.getEndTime()) && DateTimeHelper.getNowMonth().equals(DateTimeHelper.getNowMonth(DateTimeHelper.parse(agreementInfo.getEndTime(), "yyyy-MM")))) {
                List<SltAgreementInfo> listMonth = getLeaseListMonth(agreementInfo, num);
                leaseListMonth.addAll(listMonth);
            } else {
                // 如果不传日期或传以往日期 则查定时任务记录的数据
                List<SltAgreementInfo> listMonth = getLeaseJobListMonth(agreementInfo);
                leaseListMonth.addAll(listMonth);
            }
        }
        return leaseListMonth;
    }

    private List<SltAgreementInfo> getLeaseJobListMonth(AgreementInfo agreementInfo) {
   /*     if (StringUtils.isBlank(agreementInfo.getEndTime())) {
            String lastMonthYearMonth = DateTimeHelper.getLastMonthYearMonth();
            agreementInfo.setEndTime(lastMonthYearMonth);
        }*/
        List<SltAgreementInfo> monthCosts = calMonthlyMapper.getMonthCosts(agreementInfo);
        int num = 0;
        for (SltAgreementInfo monthCost : monthCosts) {
            List<SltAgreementInfo> monthDetails = new ArrayList<>();
            monthCost.setCodeNum(num++);
            for (String id : monthCost.getIds().split(",")) {
                monthCost.setId(Long.parseLong(id));
                monthDetails.addAll(calMonthlyMapper.getMonthDetails(monthCost));
            }
            monthCost.setNode(monthDetails);
        }
        return monthCosts;
    }

    @Override
    public int updateOutSourceCosts(SltAgreementInfo sltAgreementInfo) {
        return sltAgreementInfoMapper.updateOutSourceCosts(sltAgreementInfo);
    }

    private List<SltAgreementInfo> getLeaseListMonth(AgreementInfo bean, Integer num) {
        List<SltAgreementInfo> leaseList = new ArrayList<>();
//        for (AgreementInfo bean : list) {
//        if (StringUtils.isNotBlank(bean.getStartTime()) && StringUtils.isNotBlank(bean.getEndTime())) {
        List<SltAgreementInfo> monthList = sltAgreementInfoMapper.getLeaseListMonthNotNull(bean);
        for (SltAgreementInfo sltAgreementInfo : monthList) {
            AgreementInfo agreementInfo = new AgreementInfo();
            agreementInfo.setAgreementId(bean.getAgreementId());
            agreementInfo.setCostBearingParty(sltAgreementInfo.getCostBearingParty());
            agreementInfo.setStartTime(sltAgreementInfo.getBeginTime());
            agreementInfo.setIds(sltAgreementInfo.getIds());
            sltAgreementInfo.setMonth(DateTimeHelper.getNowMonth(DateTimeHelper.parse(bean.getEndTime(), "yyyy-MM")));
            sltAgreementInfo.setCodeNum(num++);
            agreementInfo.setEndTime(sltAgreementInfo.getOffTime());
            List<SltAgreementInfo> leaseListOneMonth = getLeaseListOneMonth(agreementInfo, sltAgreementInfo);
            sltAgreementInfo.setNode(leaseListOneMonth);
        }
        leaseList.addAll(monthList);
       /* } else {
            List<SltAgreementInfo> oneOfList = sltAgreementInfoMapper.getLeaseListMonth(bean);
            for (SltAgreementInfo sltAgreementInfo : oneOfList) {
                AgreementInfo agreementInfo = new AgreementInfo();
                agreementInfo.setAgreementId(bean.getAgreementId());
                agreementInfo.setIds(sltAgreementInfo.getIds());
                agreementInfo.setCostBearingParty(sltAgreementInfo.getCostBearingParty());
                agreementInfo.setStartTime(sltAgreementInfo.getBeginTime());
                sltAgreementInfo.setCodeNum(num++);
                agreementInfo.setEndTime(sltAgreementInfo.getOffTime());
                List<SltAgreementInfo> leaseListOneMonth = getLeaseListOneMonth(agreementInfo, sltAgreementInfo);
                sltAgreementInfo.setNode(leaseListOneMonth);
            }
            leaseList.addAll(oneOfList);
        }*/
//        }
        return leaseList;
    }

    private List<SltAgreementInfo> getLeaseListOneMonth(AgreementInfo list, SltAgreementInfo sltAgreementInfo) {
        ArrayList<String> idList = new ArrayList<>();
        String[] ids = list.getIds().split(",");
        for (String id : ids) {
            idList.add(id);
        }
        List<SltAgreementInfo> leaseList = sltAgreementInfoMapper.getLeaseListOneMonth(list, idList);
        BigDecimal leaseCostOne = BigDecimal.ZERO;
        for (SltAgreementInfo bean : leaseList) {
            if (bean.getLeasePrice() == null) {
                bean.setLeasePrice(new BigDecimal(0));
            }
            if (bean.getNum() == null) {
                bean.setNum(0);
            }
            if (StringUtils.isEmpty(bean.getLeaseDays())) {
                bean.setLeaseDays("0");
            }
            if (Objects.isNull(bean.getTrimDay())) {
                bean.setTrimDay(0);
            }
            BigDecimal leasePrice = bean.getLeasePrice();
            BigDecimal num = new BigDecimal(bean.getNum());
            BigDecimal leaseDays = new BigDecimal(bean.getLeaseDays());
            // 实际结算天数（领料天数 + 调整天数(可以为负整数)）
            BigDecimal realDays = leaseDays.add(new BigDecimal(bean.getTrimDay()));
            // 实际结算金额
            BigDecimal realCosts = leasePrice.multiply(num).multiply(realDays);
            // 应结算金额
            BigDecimal costs = leasePrice.multiply(num).multiply(leaseDays);
            bean.setCosts(costs);
            bean.setRealDays(realDays);
            bean.setRealCosts(realCosts);
            leaseCostOne = leaseCostOne.add(costs);
            bean.setMonth(sltAgreementInfo.getMonth());
        }
        sltAgreementInfo.setCosts(leaseCostOne);
        return leaseList;
    }

    @Override
    public List<SltAgreementInfo> getRepairList(List<AgreementInfo> list) {
        List<SltAgreementInfo> repairList = new ArrayList<>();
        String taskType = "41";
        //String taskType = TaskTypeEnum.MAINTENANCE_TASK.getCode();
        for (AgreementInfo bean : list) {
            List<TmTask> taskList = sltAgreementInfoMapper.getTaskList(bean, taskType);
            if (taskList != null && taskList.size() > 0) {
                List<SltAgreementInfo> repairDetailsList = sltAgreementInfoMapper.getRepairDetailsList(taskList);
                repairList.addAll(repairDetailsList);
            }
        }
        return repairList;
    }

    @Override
    public List<SltAgreementInfo> getLeaseListOne(List<AgreementInfo> list) {
        List<SltAgreementInfo> leaseList = new ArrayList<>();
        for (AgreementInfo bean : list) {
            List<SltAgreementInfo> oneOfList = sltAgreementInfoMapper.getLeaseListOne(bean);
            leaseList.addAll(oneOfList);
        }
        for (SltAgreementInfo bean : leaseList) {
            if (bean.getLeasePrice() == null) {
                bean.setLeasePrice(new BigDecimal(0));
            }
            if (bean.getNum() == null) {
                bean.setNum(0);
            }
            if (StringUtils.isEmpty(bean.getLeaseDays())) {
                bean.setLeaseDays("0");
            }
            if (Objects.isNull(bean.getTrimDay())) {
                bean.setTrimDay(0);
            }
            BigDecimal leasePrice = bean.getLeasePrice();
            BigDecimal num = new BigDecimal(bean.getNum());
            BigDecimal leaseDays = new BigDecimal(bean.getLeaseDays());
            // 实际结算天数（领料天数 + 调整天数(可以为负整数)）
            BigDecimal realDays = leaseDays.add(new BigDecimal(bean.getTrimDay()));
            // 实际结算金额
            BigDecimal realCosts = leasePrice.multiply(num).multiply(realDays);
            // 应结算金额
            BigDecimal costs = leasePrice.multiply(num).multiply(leaseDays);
            bean.setCosts(costs);
            //实际结算天数
            bean.setRealDays(realDays);
            // 实际结算金额
            bean.setRealCosts(realCosts);
        }
        return leaseList;
    }

    /**
     * 结算单号编码生成规则
     */
    private String sltCodeRule(String code) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        Date nowDate = DateUtils.getNowDate();
        String format = dateFormat.format(nowDate);
        //String format = DateUtils.dateTime();
        //为什么要加1？是为了防止结果为0？
        int taskNum = sltAgreementInfoMapper.selectNumByDate(nowDate) + 1;
        String codeNum = "";
        if (taskNum > GlobalContants.NUM1 && taskNum < GlobalContants.NUM2) {
            codeNum = code + format + "-00" + taskNum;
        } else if (taskNum > GlobalContants.NUM3 && taskNum < GlobalContants.NUM4) {
            codeNum = code + format + "-0" + taskNum;
        } else {
            codeNum = code + format + "-000" + taskNum;
        }
        return codeNum;
    }
}
