package cn.palmte.work.service;

import cn.palmte.work.bean.*;
import cn.palmte.work.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.jfunc.common.utils.CollectionUtil;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 项目预算service
 * @author xiongshiyan at 2021/10/29 , contact me with email yanshixiong@126.com or phone 15208384257
 */
@Service
public class ProjectBudgetService {
    @Autowired
    private ProjectBudgetIncomeRepository projectBudgetIncomeRepository;
    @Autowired
    private ProjectBudgetCostRepository projectBudgetCostRepository;
    @Autowired
    private ProjectBudgetCostManageRepository projectBudgetCostManageRepository;
    @Autowired
    private ProjectBudgetIncomeDetailRepository projectBudgetIncomeDetailRepository;
    @Autowired
    private ProjectBudgetIncomeDetailTempRepository projectBudgetIncomeDetailTempRepository;
    @Autowired
    private ProjectBudgetCostDetailRepository projectBudgetCostDetailRepository;
    @Autowired
    private ProjectBudgetCostDetailTempRepository projectBudgetCostDetailTempRepository;
    @Autowired
    private ProjectBudgetCostProjectManageDetailRepository projectBudgetCostProjectManageDetailRepository;
    @Autowired
    private ProjectBudgetCostProjectManageDetailTempRepository projectBudgetCostProjectManageDetailTempRepository;
    @Autowired
    private ProjectBudgetPlanDetailRepository projectBudgetPlanDetailRepository;
    @Autowired
    private ProjectBudgetPlanDetailTempRepository projectBudgetPlanDetailTempRepository;
    @Autowired
    private ProjectInstanceService projectInstanceService;
    @Autowired
    private ActTaskDefService actTaskDefService;
    @Autowired
    private ProjectService projectService;
    @Autowired
    private ProjectRepository projectRepository;
    @Autowired
    private SysConfigService sysConfigService;

    @Value("#{'${fourcal.fixedprojectmanagedetails}'.split('\\|')}")
    private String[] fixedProjectManageDetails;

    public void clearBudget(Project project){
        List<ProjectBudgetIncome> incomes = projectBudgetIncomeRepository.findAllByProjectIdEquals(project.getId());
        if(CollectionUtil.isNotEmpty(incomes)){
            projectBudgetIncomeRepository.deleteInBatch(incomes);
        }

        List<ProjectBudgetCost> costs = projectBudgetCostRepository.findAllByProjectIdEquals(project.getId());
        if(CollectionUtil.isNotEmpty(costs)){
            projectBudgetCostRepository.deleteInBatch(costs);
        }

        List<ProjectBudgetCostManage> costManages = projectBudgetCostManageRepository.findAllByProjectIdEquals(project.getId());
        if(CollectionUtil.isNotEmpty(costManages)){
            projectBudgetCostManageRepository.deleteInBatch(costManages);
        }

    }

    /**
     * 【除开其他其他、公司管理费用，其他的都是从资金明细中获取
     * 此处保存起，但是不用，可以起对照作用】
     */
    public void saveBudget(Project project, BudgetBean budgetBean){
        //收入记录
        income(project, budgetBean);
        //成本记录
        cost(project, budgetBean);
        //管理记录
        costManage(project, budgetBean);
    }

    private void cost(Project project, BudgetBean budgetBean) {
        ProjectBudgetCost projectBudgetCostDevice = new ProjectBudgetCost();
        projectBudgetCostDevice.setProjectId(project.getId());
        projectBudgetCostDevice.setFee(ProjectBudgetCost.FEE_PURCHASE);
        projectBudgetCostDevice.setType(ProjectBudgetCost.TYPE_DEVICE);
        projectBudgetCostDevice.setCostTaxInclude(budgetBean.getCostPurchaseDeviceTaxInclude());
        projectBudgetCostDevice.setCostTaxExclude(budgetBean.getCostPurchaseDeviceTaxExclude());
        projectBudgetCostRepository.saveAndFlush(projectBudgetCostDevice);


        ProjectBudgetCost projectBudgetCostBuild = new ProjectBudgetCost();
        projectBudgetCostBuild.setProjectId(project.getId());
        projectBudgetCostBuild.setFee(ProjectBudgetCost.FEE_PURCHASE);
        projectBudgetCostBuild.setType(ProjectBudgetCost.TYPE_BUILDING);
        projectBudgetCostBuild.setCostTaxInclude(budgetBean.getCostPurchaseBuildTaxInclude());
        projectBudgetCostBuild.setCostTaxExclude(budgetBean.getCostPurchaseBuildTaxExclude());
        projectBudgetCostRepository.saveAndFlush(projectBudgetCostBuild);

        ProjectBudgetCost projectBudgetCostService = new ProjectBudgetCost();
        projectBudgetCostService.setProjectId(project.getId());
        projectBudgetCostService.setFee(ProjectBudgetCost.FEE_PURCHASE);
        projectBudgetCostService.setType(ProjectBudgetCost.TYPE_SERVICE);
        projectBudgetCostService.setCostTaxInclude(budgetBean.getCostPurchaseServiceTaxInclude());
        projectBudgetCostService.setCostTaxExclude(budgetBean.getCostPurchaseServiceTaxExclude());
        projectBudgetCostRepository.saveAndFlush(projectBudgetCostService);

        ProjectBudgetCost projectBudgetCostOther = new ProjectBudgetCost();
        projectBudgetCostOther.setProjectId(project.getId());
        projectBudgetCostOther.setFee(ProjectBudgetCost.FEE_PURCHASE);
        projectBudgetCostOther.setType(ProjectBudgetCost.TYPE_OTHER);
        projectBudgetCostOther.setCostTaxInclude(budgetBean.getCostPurchaseOtherTaxInclude());
        projectBudgetCostOther.setCostTaxExclude(budgetBean.getCostPurchaseOtherTaxExclude());
        projectBudgetCostRepository.saveAndFlush(projectBudgetCostOther);

        ProjectBudgetCost projectBudgetCostProject = new ProjectBudgetCost();
        projectBudgetCostProject.setProjectId(project.getId());
        projectBudgetCostProject.setFee(ProjectBudgetCost.FEE_PROJECT_MANAGE);
        projectBudgetCostProject.setType(ProjectBudgetCost.TYPE_PROJECT_MANAGE);
        /*projectBudgetCostProject.setCostTaxInclude(budgetBean.getCostProjectManageTaxInclude());*/
        projectBudgetCostProject.setCostTaxExclude(budgetBean.getCostProjectManageTaxExclude());
        projectBudgetCostRepository.saveAndFlush(projectBudgetCostProject);

        ProjectBudgetCost projectBudgetCostOtherOther = new ProjectBudgetCost();
        projectBudgetCostOtherOther.setProjectId(project.getId());
        projectBudgetCostOtherOther.setFee(ProjectBudgetCost.FEE_OTHER);
        projectBudgetCostOtherOther.setType(ProjectBudgetCost.TYPE_OTHER_OTHER);
        projectBudgetCostOtherOther.setCostTaxInclude(budgetBean.getCostOtherOtherTaxInclude());
        projectBudgetCostOtherOther.setCostTaxExclude(budgetBean.getCostOtherOtherTaxExclude());
        projectBudgetCostRepository.saveAndFlush(projectBudgetCostOtherOther);

    }
    private void costManage(Project project, BudgetBean budgetBean) {
        ProjectBudgetCostManage projectBudgetCostZijin = new ProjectBudgetCostManage();
        projectBudgetCostZijin.setProjectId(project.getId());
        projectBudgetCostZijin.setType(ProjectBudgetCostManage.TYPE_EXPROPRIATION);
        projectBudgetCostZijin.setCostTaxExclude(budgetBean.getCostExpropriationTaxExclude());
        projectBudgetCostManageRepository.saveAndFlush(projectBudgetCostZijin);

        ProjectBudgetCostManage projectBudgetCostManage = new ProjectBudgetCostManage();
        projectBudgetCostManage.setProjectId(project.getId());
        projectBudgetCostManage.setType(ProjectBudgetCostManage.TYPE_COMPANY_MANAGE);
        projectBudgetCostManage.setCostTaxExclude(budgetBean.getCostCompanyManageTaxExclude());
        projectBudgetCostManageRepository.saveAndFlush(projectBudgetCostManage);
    }
    private void income(Project project, BudgetBean budgetBean) {
        ProjectBudgetIncome projectBudgetIncomeDevice = new ProjectBudgetIncome();
        projectBudgetIncomeDevice.setProjectId(project.getId());
        projectBudgetIncomeDevice.setType(ProjectBudgetIncome.TYPE_DEVICE);
        projectBudgetIncomeDevice.setIncomeTaxInclude(budgetBean.getIncomeDeviceTaxInclude());
        projectBudgetIncomeDevice.setIncomeTaxExclude(budgetBean.getIncomeDeviceTaxExclude());
        projectBudgetIncomeRepository.saveAndFlush(projectBudgetIncomeDevice);

        ProjectBudgetIncome projectBudgetIncomeEngineer = new ProjectBudgetIncome();
        projectBudgetIncomeEngineer.setProjectId(project.getId());
        projectBudgetIncomeEngineer.setType(ProjectBudgetIncome.TYPE_ENGINEER);
        projectBudgetIncomeEngineer.setIncomeTaxInclude(budgetBean.getIncomeEngineerTaxInclude());
        projectBudgetIncomeEngineer.setIncomeTaxExclude(budgetBean.getIncomeEngineerTaxExclude());
        projectBudgetIncomeRepository.saveAndFlush(projectBudgetIncomeEngineer);

        ProjectBudgetIncome projectBudgetIncomeService = new ProjectBudgetIncome();
        projectBudgetIncomeService.setProjectId(project.getId());
        projectBudgetIncomeService.setType(ProjectBudgetIncome.TYPE_SERVICE);
        projectBudgetIncomeService.setIncomeTaxInclude(budgetBean.getIncomeServiceTaxInclude());
        projectBudgetIncomeService.setIncomeTaxExclude(budgetBean.getIncomeServiceTaxExclude());
        projectBudgetIncomeRepository.saveAndFlush(projectBudgetIncomeService);
    }


    /**
     * 其实最好是通过明细表生成，不然可能有数据不一致的情况
     */
    public BudgetBean getBudget(Project project) {
        BudgetBean budgetBean = new BudgetBean();

        List<ProjectBudgetIncomeDetail> incomeDetails = projectBudgetIncomeDetailRepository.findAllByProjectIdEquals(project.getId());
        if(CollectionUtil.isNotEmpty(incomeDetails)){
            //设备类收入，含税和不含税
            List<ProjectBudgetIncomeDetail> collectDevice = incomeDetails.stream().filter(d -> d.getType() == ProjectBudgetIncomeDetail.TYPE_DEVICE).collect(Collectors.toList());
            budgetBean.setIncomeDeviceTaxInclude(getIncomeTotalTaxInclude(collectDevice));
            budgetBean.setIncomeDeviceTaxExclude(getIncomeTotalTaxExclude(collectDevice));
            //工程类收入，含税和不含税
            List<ProjectBudgetIncomeDetail> collectEngineer = incomeDetails.stream().filter(d -> d.getType() == ProjectBudgetIncomeDetail.TYPE_ENGINEER).collect(Collectors.toList());
            budgetBean.setIncomeEngineerTaxInclude(getIncomeTotalTaxInclude(collectEngineer));
            budgetBean.setIncomeEngineerTaxExclude(getIncomeTotalTaxExclude(collectEngineer));
            //服务类收入，含税和不含税
            List<ProjectBudgetIncomeDetail> collectService = incomeDetails.stream().filter(d -> d.getType() == ProjectBudgetIncomeDetail.TYPE_SERVICE).collect(Collectors.toList());
            budgetBean.setIncomeServiceTaxInclude(getIncomeTotalTaxInclude(collectService));
            budgetBean.setIncomeServiceTaxExclude(getIncomeTotalTaxExclude(collectService));
        }

        List<ProjectBudgetCostDetail> projectBudgetCostDetails = projectBudgetCostDetailRepository.findAllByProjectIdEquals(project.getId());
        if(CollectionUtil.isNotEmpty(projectBudgetCostDetails)){
            //采购成本-设备，含税和不含税
            List<ProjectBudgetCostDetail> collectDevice = projectBudgetCostDetails.stream().filter(d -> d.getType() == ProjectBudgetCostDetail.TYPE_DEVICE).collect(Collectors.toList());
            budgetBean.setCostPurchaseDeviceTaxInclude(getCostTotalTaxInclude(collectDevice));
            budgetBean.setCostPurchaseDeviceTaxExclude(getCostTotalTaxExclude(collectDevice));
            //采购成本-施工，含税和不含税
            List<ProjectBudgetCostDetail> collectBuild = projectBudgetCostDetails.stream().filter(d -> d.getType() == ProjectBudgetCostDetail.TYPE_BUILD).collect(Collectors.toList());
            budgetBean.setCostPurchaseBuildTaxInclude(getCostTotalTaxInclude(collectBuild));
            budgetBean.setCostPurchaseBuildTaxExclude(getCostTotalTaxExclude(collectBuild));
            //采购成本-服务，含税和不含税
            List<ProjectBudgetCostDetail> collectService = projectBudgetCostDetails.stream().filter(d -> d.getType() == ProjectBudgetCostDetail.TYPE_SERVICE).collect(Collectors.toList());
            budgetBean.setCostPurchaseServiceTaxInclude(getCostTotalTaxInclude(collectService));
            budgetBean.setCostPurchaseServiceTaxExclude(getCostTotalTaxExclude(collectService));
            //采购成本-其他，含税和不含税
            List<ProjectBudgetCostDetail> collectOther = projectBudgetCostDetails.stream().filter(d -> d.getType() == ProjectBudgetCostDetail.TYPE_OHTER).collect(Collectors.toList());
            budgetBean.setCostPurchaseOtherTaxInclude(getCostTotalTaxInclude(collectOther));
            budgetBean.setCostPurchaseOtherTaxExclude(getCostTotalTaxExclude(collectOther));

            budgetBean.setCostPurchaseTotalTaxInclude(getCostTotalTaxInclude(collectDevice).add(getCostTotalTaxInclude(collectBuild)).add(getCostTotalTaxInclude(collectService)).add(getCostTotalTaxInclude(collectOther)));
            budgetBean.setCostPurchaseTotalTaxExclude(getCostTotalTaxExclude(collectDevice).add(getCostTotalTaxExclude(collectBuild)).add(getCostTotalTaxExclude(collectService)).add(getCostTotalTaxExclude(collectOther)));
        }

        //项目管理成本
        List<ProjectBudgetCostProjectManageDetail> projectManageDetails = projectBudgetCostProjectManageDetailRepository.findAllByProjectIdEquals(project.getId());
        if(CollectionUtil.isNotEmpty(projectManageDetails)){
            budgetBean.setCostProjectManageTaxExclude(getCostProjectManageTotalTaxExclude(projectManageDetails));
        }

        //其他其他成本
        List<ProjectBudgetCost> costs = projectBudgetCostRepository.findAllByProjectIdEquals(project.getId());
        if(CollectionUtil.isNotEmpty(costs)){
            ProjectBudgetCost projectBudgetCostOtherOther = costs.stream().filter(d -> d.getType() == ProjectBudgetCost.TYPE_OTHER_OTHER).collect(Collectors.toList()).get(0);
            budgetBean.setCostOtherOtherTaxInclude(projectBudgetCostOtherOther.getCostTaxInclude());
            budgetBean.setCostOtherOtherTaxExclude(projectBudgetCostOtherOther.getCostTaxExclude());
        }


        //资金占用成本
        List<ProjectBudgetPlanDetail> budgetPlanDetails = projectBudgetPlanDetailRepository.findAllByProjectIdEquals(project.getId());
        if(CollectionUtil.isNotEmpty(budgetPlanDetails)){
            budgetBean.setCostExpropriationTaxExclude(getTotalCapitalInterest(budgetPlanDetails));
        }

        //公司管理成本
        List<ProjectBudgetCostManage> manages = projectBudgetCostManageRepository.findAllByProjectIdEquals(project.getId());
        if(CollectionUtil.isNotEmpty(manages)){
            ProjectBudgetCostManage costManageCompany = manages.stream().filter(d -> d.getType() == ProjectBudgetCostManage.TYPE_COMPANY_MANAGE).collect(Collectors.toList()).get(0);
            budgetBean.setCostCompanyManageTaxExclude(costManageCompany.getCostTaxExclude());
        }

        return budgetBean;
    }

    private BigDecimal getIncomeTotalTaxInclude(List<ProjectBudgetIncomeDetail> list){
        BigDecimal total = new BigDecimal(0);
        if(CollectionUtil.isEmpty(list)){
            return total;
        }

        for (ProjectBudgetIncomeDetail detail : list) {
            total = total.add(detail.getTotalTaxInclude());
        }

        return total;
    }
    private BigDecimal getIncomeTotalTaxExclude(List<ProjectBudgetIncomeDetail> list){
        BigDecimal total = new BigDecimal(0);
        if(CollectionUtil.isEmpty(list)){
            return total;
        }

        for (ProjectBudgetIncomeDetail detail : list) {
            total = total.add(detail.getTotalTaxExclude());
        }

        return total;
    }
    private BigDecimal getCostTotalTaxInclude(List<ProjectBudgetCostDetail> list){
        BigDecimal total = new BigDecimal(0);
        if(CollectionUtil.isEmpty(list)){
            return total;
        }

        for (ProjectBudgetCostDetail detail : list) {
            total = total.add(detail.getTotalTaxInclude());
        }

        return total;
    }
    private BigDecimal getCostTotalTaxExclude(List<ProjectBudgetCostDetail> list){
        BigDecimal total = new BigDecimal(0);
        if(CollectionUtil.isEmpty(list)){
            return total;
        }

        for (ProjectBudgetCostDetail detail : list) {
            total = total.add(detail.getTotalTaxExclude());
        }

        return total;
    }
    private BigDecimal getCostProjectManageTotalTaxExclude(List<ProjectBudgetCostProjectManageDetail> list){
        BigDecimal total = new BigDecimal(0);
        if(CollectionUtil.isEmpty(list)){
            return total;
        }

        for (ProjectBudgetCostProjectManageDetail detail : list) {
            total = total.add(detail.getTotal());
        }

        return total;
    }
    private BigDecimal getTotalCapitalInterest(List<ProjectBudgetPlanDetail> list){
        BigDecimal total = new BigDecimal(0);
        if(CollectionUtil.isEmpty(list)){
            return total;
        }

        for (ProjectBudgetPlanDetail detail : list) {
            total = total.add(detail.getCapitalInterest());
        }

        return total;
    }

    /**
     * 清空项目的收入明细
     */
    public void clearBudgetIncomeDetail(Project project){
        List<ProjectBudgetIncomeDetail> incomeDetails = projectBudgetIncomeDetailRepository.findAllByProjectIdEquals(project.getId());
        if(CollectionUtil.isNotEmpty(incomeDetails)){
            projectBudgetIncomeDetailRepository.deleteInBatch(incomeDetails);
        }
    }
    public void clearBudgetIncomeDetailTemp(Project project){
        List<ProjectBudgetIncomeDetailTemp> incomeDetails = projectBudgetIncomeDetailTempRepository.findAllByProjectIdEquals(project.getId());
        if(CollectionUtil.isNotEmpty(incomeDetails)){
            projectBudgetIncomeDetailTempRepository.deleteInBatch(incomeDetails);
        }
    }

    /**
     * 保存项目的收入明细
     */
    @Transactional(rollbackFor = RuntimeException.class)
    public void saveBudgetIncomeDetail(Project project, List<ProjectBudgetIncomeDetail> detailList){
        clearBudgetIncomeDetail(project);
        if(CollectionUtil.isNotEmpty(detailList)){
            for (ProjectBudgetIncomeDetail projectBudgetIncomeDetail : detailList) {
                projectBudgetIncomeDetail.setProjectId(project.getId());
            }
            projectBudgetIncomeDetailRepository.save(detailList);
        }
    }
    @Transactional(rollbackFor = RuntimeException.class)
    public void saveBudgetIncomeDetailTemp(Project project, List<ProjectBudgetIncomeDetailTemp> detailList){
        clearBudgetIncomeDetailTemp(project);
        if(CollectionUtil.isNotEmpty(detailList)){
            for (ProjectBudgetIncomeDetailTemp projectBudgetIncomeDetail : detailList) {
                projectBudgetIncomeDetail.setProjectId(project.getId());
            }
            projectBudgetIncomeDetailTempRepository.save(detailList);
        }

    }

    /**
     * 获取项目的收入明细
     */
    public List<ProjectBudgetIncomeDetail> getBudgetIncomeDetail(Project project){
        return projectBudgetIncomeDetailRepository.findAllByProjectIdEquals(project.getId());
    }
    /**
     * 获取项目的收入总数量
     */
    public Integer getBudgetIncomeAmount(Project project){
        return projectBudgetIncomeDetailRepository.findAmountByProject(project.getId());
    }
    /**
     * 清空项目的成本明细
     */
    public void clearBudgetCostDetail(Project project){
        List<ProjectBudgetCostDetail> costDetails = projectBudgetCostDetailRepository.findAllByProjectIdEquals(project.getId());
        if(CollectionUtil.isNotEmpty(costDetails)){
            projectBudgetCostDetailRepository.deleteInBatch(costDetails);
        }
    }
    public void clearBudgetCostDetailTemp(Project project){
        List<ProjectBudgetCostDetailTemp> costDetails = projectBudgetCostDetailTempRepository.findAllByProjectIdEquals(project.getId());
        if(CollectionUtil.isNotEmpty(costDetails)){
            projectBudgetCostDetailTempRepository.deleteInBatch(costDetails);
        }
    }

    /**
     * 保存项目的成本明细
     */
    @Transactional(rollbackFor = RuntimeException.class)
    public void saveBudgetCostDetail(Project project, List<ProjectBudgetCostDetail> detailList){
        clearBudgetCostDetail(project);

        if(CollectionUtil.isNotEmpty(detailList)){
            for (ProjectBudgetCostDetail projectBudgetCostDetail : detailList) {
                projectBudgetCostDetail.setProjectId(project.getId());
            }
            projectBudgetCostDetailRepository.save(detailList);
        }
    }
    @Transactional(rollbackFor = RuntimeException.class)
    public void saveBudgetCostDetailTemp(Project project, List<ProjectBudgetCostDetailTemp> detailList){
        clearBudgetCostDetailTemp(project);

        if(CollectionUtil.isNotEmpty(detailList)){
            for (ProjectBudgetCostDetailTemp projectBudgetCostDetail : detailList) {
                projectBudgetCostDetail.setProjectId(project.getId());
            }
            projectBudgetCostDetailTempRepository.save(detailList);
        }
    }

    /**
     * 获取项目的成本明细
     */
    public List<ProjectBudgetCostDetail> getBudgetCostDetail(Project project){
        return projectBudgetCostDetailRepository.findAllByProjectIdEquals(project.getId());
    }
    /**
     * 获取项目的成本总数量
     */
    public Integer getBudgetCostAmount(Project project){
        return projectBudgetCostDetailRepository.findAmountByProject(project.getId());
    }
    /**
     * 清空项目的项目管理成本明细
     */
    public void clearBudgetCostProjectManageDetail(Project project){
        List<ProjectBudgetCostProjectManageDetail> costDetails = projectBudgetCostProjectManageDetailRepository.findAllByProjectIdEquals(project.getId());
        if(CollectionUtil.isNotEmpty(costDetails)){
            projectBudgetCostProjectManageDetailRepository.deleteInBatch(costDetails);
        }
    }
    public void clearBudgetCostProjectManageDetailTemp(Project project){
        List<ProjectBudgetCostProjectManageDetailTemp> costDetails = projectBudgetCostProjectManageDetailTempRepository.findAllByProjectIdEquals(project.getId());
        if(CollectionUtil.isNotEmpty(costDetails)){
            projectBudgetCostProjectManageDetailTempRepository.deleteInBatch(costDetails);
        }
    }

    /**
     * 保存项目的项目管理成本明细
     */
    @Transactional(rollbackFor = RuntimeException.class)
    public void saveBudgetCostProjectManageDetail(Project project, List<ProjectBudgetCostProjectManageDetail> detailList){
        clearBudgetCostProjectManageDetail(project);
        if(CollectionUtil.isNotEmpty(detailList)){
            for (ProjectBudgetCostProjectManageDetail projectBudgetCostProjectManageDetail : detailList) {
                projectBudgetCostProjectManageDetail.setProjectId(project.getId());
            }
            projectBudgetCostProjectManageDetailRepository.save(detailList);
        }
    }
    @Transactional(rollbackFor = RuntimeException.class)
    public void saveBudgetCostProjectManageDetailTemp(Project project, List<ProjectBudgetCostProjectManageDetailTemp> detailList){
        clearBudgetCostProjectManageDetailTemp(project);
        if(CollectionUtil.isNotEmpty(detailList)){
            for (ProjectBudgetCostProjectManageDetailTemp projectBudgetCostProjectManageDetail : detailList) {
                projectBudgetCostProjectManageDetail.setProjectId(project.getId());
            }
            projectBudgetCostProjectManageDetailTempRepository.save(detailList);
        }
    }

    /**
     * 获取项目管理明细
     */
    public List<ProjectBudgetCostProjectManageDetail> getBudgetCostProjectManageDetail(Project project){
        List<ProjectBudgetCostProjectManageDetail> projectManageDetails = projectBudgetCostProjectManageDetailRepository.findAllByProjectIdEquals(project.getId());
        return projectManageDetails;
//        if(CollectionUtil.isNotEmpty(projectManageDetails)){
//            return projectManageDetails;
//        }else {
//            //默认必填的明细
//            return getFixedNotDeletable();
//        }
    }
    /**
     * 获取项目的项目管理总数量
     */
    public Integer getBudgetCostProjectManageAmount(Project project){
        return projectBudgetCostProjectManageDetailRepository.findAmountByProject(project.getId());
    }
    private List<ProjectBudgetCostProjectManageDetail> getFixedNotDeletable() {
        List<ProjectBudgetCostProjectManageDetail> projectManageDetails = new ArrayList<>(6);
        for (String fixedProjectManageDetail : fixedProjectManageDetails) {
            String[] split = fixedProjectManageDetail.split(",");
            ProjectBudgetCostProjectManageDetail detail = new ProjectBudgetCostProjectManageDetail();
            detail.setType(Integer.parseInt(split[0]));
            detail.setName(split[1]);
            detail.setPrice(new BigDecimal(0));
            detail.setAmount(0);
            detail.setDeletable(0);
            projectManageDetails.add(detail);
        }

        return projectManageDetails;
    }
    /**
     * 清空项目的资金计划明细
     */
    public void clearBudgetPlanDetail(Project project){
        List<ProjectBudgetPlanDetail> costDetails = projectBudgetPlanDetailRepository.findAllByProjectIdEquals(project.getId());
        if(CollectionUtil.isNotEmpty(costDetails)){
            projectBudgetPlanDetailRepository.deleteInBatch(costDetails);
        }
    }
    public void clearBudgetPlanDetailTemp(Project project){
        List<ProjectBudgetPlanDetailTemp> costDetails = projectBudgetPlanDetailTempRepository.findAllByProjectIdEquals(project.getId());
        if(CollectionUtil.isNotEmpty(costDetails)){
            projectBudgetPlanDetailTempRepository.deleteInBatch(costDetails);
        }
    }

    /**
     * 保存项目的资金计划明细
     */
    @Transactional(rollbackFor = RuntimeException.class)
    public void saveBudgetPlanDetail(Project project, List<ProjectBudgetPlanDetail> detailList){
        clearBudgetPlanDetail(project);
        if(CollectionUtil.isNotEmpty(detailList)){
            for (ProjectBudgetPlanDetail projectBudgetPlanDetail : detailList) {
                projectBudgetPlanDetail.setProjectId(project.getId());
                projectBudgetPlanDetail.setUnderwrittenTaxRate(project.getUnderwrittenTaxRate());
            }
            projectBudgetPlanDetailRepository.save(detailList);
        }
    }
    @Transactional(rollbackFor = RuntimeException.class)
    public void saveBudgetPlanDetailTemp(Project project, List<ProjectBudgetPlanDetailTemp> detailList){
        clearBudgetPlanDetailTemp(project);
        if(CollectionUtil.isNotEmpty(detailList)){
            for (ProjectBudgetPlanDetailTemp projectBudgetPlanDetail : detailList) {
                projectBudgetPlanDetail.setProjectId(project.getId());
                projectBudgetPlanDetail.setUnderwrittenTaxRate(project.getUnderwrittenTaxRate());
            }
            projectBudgetPlanDetailTempRepository.save(detailList);
        }
    }

    /**
     * 获取资金计划数据
     */
    public List<ProjectBudgetPlanDetail> getProjectBudgetPlanDetails(Project project){
        return projectBudgetPlanDetailRepository.findAllByProjectIdEquals(project.getId());
    }

    /**
     * 根据每个月的计算资金计划的总
     */
    public ProjectBudgetPlanDetail getProjectBudgetPlanDetailTotal(Project project, List<ProjectBudgetPlanDetail> projectBudgetPlanDetails) {
        BigDecimal deviceCost = new BigDecimal(0);
        BigDecimal engineerCost = new BigDecimal(0);
        BigDecimal projectManageCost = new BigDecimal(0);
        BigDecimal earnestMoneyCost = new BigDecimal(0);
        BigDecimal totalCost = new BigDecimal(0);
        BigDecimal saleIncome = new BigDecimal(0);
        BigDecimal earnestMoneyIncome = new BigDecimal(0);
        BigDecimal totalIncome = new BigDecimal(0);
        BigDecimal fundBalance = new BigDecimal(0);
        BigDecimal capitalInterest = new BigDecimal(0);
        BigDecimal underwrittenPlan = new BigDecimal(0);
        BigDecimal repaymentPlan = new BigDecimal(0);
        /*BigDecimal underwrittenTaxRate = project.getUnderwrittenTaxRate();*/

        if(CollectionUtil.isNotEmpty(projectBudgetPlanDetails)){
            for (ProjectBudgetPlanDetail budgetPlan : projectBudgetPlanDetails) {
                deviceCost = deviceCost.add(budgetPlan.getDeviceCost());
                engineerCost = engineerCost.add(budgetPlan.getEngineerCost());
                projectManageCost = projectManageCost.add(budgetPlan.getProjectManageCost());
                earnestMoneyCost = earnestMoneyCost.add(budgetPlan.getEarnestMoneyCost());
                totalCost = totalCost.add(budgetPlan.getTotalCost());
                saleIncome = saleIncome.add(budgetPlan.getSaleIncome());
                earnestMoneyIncome = earnestMoneyIncome.add(budgetPlan.getEarnestMoneyIncome());
                totalIncome = totalIncome.add(budgetPlan.getTotalIncome());
                /*fundBalance = fundBalance.add(budgetPlan.getFundBalance());*/
                capitalInterest = capitalInterest.add(budgetPlan.getCapitalInterest());
                underwrittenPlan = underwrittenPlan.add(budgetPlan.getUnderwrittenPlan());
                repaymentPlan = repaymentPlan.add(budgetPlan.getRepaymentPlan());
            }
        }

        //总余额等于总收入-总支出
        fundBalance = totalIncome.subtract(totalCost);


        ProjectBudgetPlanDetail projectBudgetPlanDetail = new ProjectBudgetPlanDetail();
        projectBudgetPlanDetail.setMonth("合计");
        projectBudgetPlanDetail.setDeviceCost(deviceCost);
        projectBudgetPlanDetail.setEngineerCost(engineerCost);
        projectBudgetPlanDetail.setProjectManageCost(projectManageCost);
        projectBudgetPlanDetail.setEarnestMoneyCost(earnestMoneyCost);
        projectBudgetPlanDetail.setTotalCost(totalCost);
        projectBudgetPlanDetail.setSaleIncome(saleIncome);
        projectBudgetPlanDetail.setEarnestMoneyIncome(earnestMoneyIncome);
        projectBudgetPlanDetail.setTotalIncome(totalIncome);
        projectBudgetPlanDetail.setFundBalance(fundBalance);
        /*projectBudgetPlanDetail.setUnderwrittenTaxRate(underwrittenTaxRate);*/
        projectBudgetPlanDetail.setCapitalInterest(capitalInterest);
        projectBudgetPlanDetail.setUnderwrittenPlan(underwrittenPlan);
        projectBudgetPlanDetail.setRepaymentPlan(repaymentPlan);
        return projectBudgetPlanDetail;
    }

    /**
     * 计算资金计划的表头展示数据
     */
    public ProjectBudgetPlanDetail getProjectBudgetPlanDetailTotalTitle(Project project,
                                                                        List<ProjectBudgetIncomeDetail> budgetIncomeDetail,
                                                                        List<ProjectBudgetCostDetail> budgetCostDetail,
                                                                        List<ProjectBudgetCostProjectManageDetail> budgetCostProjectManageDetail,
                                                                        List<ProjectBudgetPlanDetail> projectBudgetPlanDetails) {

        BigDecimal deviceCost = calDeviceCost(budgetCostDetail);
        BigDecimal engineerCost = calEngineerCost(budgetCostDetail);
        BigDecimal projectManageCost = calProjectManageCost(budgetCostProjectManageDetail);

        BigDecimal saleIncome = calSaleIncome(budgetIncomeDetail);

        BigDecimal earnestMoneyIncome = new BigDecimal(0);
        BigDecimal earnestMoneyCost = new BigDecimal(0);
        BigDecimal capitalInterest = new BigDecimal(0);
        BigDecimal underwrittenPlan = new BigDecimal(0);
        BigDecimal repaymentPlan = new BigDecimal(0);

        if(CollectionUtil.isNotEmpty(projectBudgetPlanDetails)){
            for (ProjectBudgetPlanDetail budgetPlan : projectBudgetPlanDetails) {
                earnestMoneyCost = earnestMoneyCost.add(budgetPlan.getEarnestMoneyCost());
                earnestMoneyIncome = earnestMoneyIncome.add(budgetPlan.getEarnestMoneyIncome());
                capitalInterest = capitalInterest.add(budgetPlan.getCapitalInterest());
                underwrittenPlan = underwrittenPlan.add(budgetPlan.getUnderwrittenPlan());
                repaymentPlan = repaymentPlan.add(budgetPlan.getRepaymentPlan());
            }
        }
        BigDecimal totalCost = deviceCost.add(engineerCost).add(projectManageCost).add(earnestMoneyCost);
        BigDecimal totalIncome = saleIncome.add(earnestMoneyIncome);

        //总余额等于总收入-总支出
        BigDecimal fundBalance = totalIncome.subtract(totalCost);


        ProjectBudgetPlanDetail projectBudgetPlanDetail = new ProjectBudgetPlanDetail();
        projectBudgetPlanDetail.setMonth("合计");
        projectBudgetPlanDetail.setDeviceCost(deviceCost);
        projectBudgetPlanDetail.setEngineerCost(engineerCost);
        projectBudgetPlanDetail.setProjectManageCost(projectManageCost);
        projectBudgetPlanDetail.setEarnestMoneyCost(earnestMoneyCost);
        projectBudgetPlanDetail.setTotalCost(totalCost);
        projectBudgetPlanDetail.setSaleIncome(saleIncome);
        projectBudgetPlanDetail.setEarnestMoneyIncome(earnestMoneyIncome);
        projectBudgetPlanDetail.setTotalIncome(totalIncome);
        projectBudgetPlanDetail.setFundBalance(fundBalance);

        projectBudgetPlanDetail.setCapitalInterest(capitalInterest);
        projectBudgetPlanDetail.setUnderwrittenPlan(underwrittenPlan);
        projectBudgetPlanDetail.setRepaymentPlan(repaymentPlan);
        return projectBudgetPlanDetail;
    }

    private BigDecimal calSaleIncome(List<ProjectBudgetIncomeDetail> budgetIncomeDetail) {
        BigDecimal saleIncome = new BigDecimal(0);
        if(CollectionUtil.isNotEmpty(budgetIncomeDetail)){
            for (ProjectBudgetIncomeDetail projectBudgetIncomeDetail : budgetIncomeDetail) {
                saleIncome = saleIncome.add(projectBudgetIncomeDetail.getTotalTaxInclude());
            }
        }
        return saleIncome;
    }

    private BigDecimal calProjectManageCost(List<ProjectBudgetCostProjectManageDetail> budgetCostProjectManageDetail) {
        BigDecimal projectManageCost = new BigDecimal(0);
        if(CollectionUtil.isNotEmpty(budgetCostProjectManageDetail)){
            for (ProjectBudgetCostProjectManageDetail projectBudgetCostProjectManageDetail : budgetCostProjectManageDetail) {
                projectManageCost = projectManageCost.add(projectBudgetCostProjectManageDetail.getTotal());
            }
        }
        return projectManageCost;
    }

    private BigDecimal calEngineerCost(List<ProjectBudgetCostDetail> budgetCostDetail) {
        BigDecimal engineerCost = new BigDecimal(0);
        if(CollectionUtil.isNotEmpty(budgetCostDetail)){
            List<ProjectBudgetCostDetail> costDetailList = budgetCostDetail.stream().filter(b -> b.getType() != ProjectBudgetCostDetail.TYPE_DEVICE).collect(Collectors.toList());
            for (ProjectBudgetCostDetail projectBudgetCostDetail : costDetailList) {
                engineerCost = engineerCost.add(projectBudgetCostDetail.getTotalTaxInclude());
            }
        }
        return engineerCost;
    }
    private BigDecimal calDeviceCost(List<ProjectBudgetCostDetail> budgetCostDetail) {
        BigDecimal deviceCost = new BigDecimal(0);
        if(CollectionUtil.isNotEmpty(budgetCostDetail)){
            List<ProjectBudgetCostDetail> costDetailList = budgetCostDetail.stream().filter(b -> b.getType() == ProjectBudgetCostDetail.TYPE_DEVICE).collect(Collectors.toList());
            for (ProjectBudgetCostDetail projectBudgetCostDetail : costDetailList) {
                deviceCost = deviceCost.add(projectBudgetCostDetail.getTotalTaxInclude());
            }
        }
        return deviceCost;
    }

    /**
     * 根据资金计划获取资金使用统计值
     * 月份去峰值月份
     * 峰值累计
     * 资金利息累计
     */
    public ProjectUnderwrittenPlanStatisticBean getProjectUnderwrittenPlanStatisticBean(List<ProjectBudgetPlanDetail> projectBudgetPlanDetails){
        ProjectUnderwrittenPlanStatisticBean bean = new ProjectUnderwrittenPlanStatisticBean();
        if(CollectionUtil.isEmpty(projectBudgetPlanDetails)){
            return bean;
        }

        BigDecimal amount = new BigDecimal(0);
        BigDecimal capitalInterest = new BigDecimal(0);
        BigDecimal max = new BigDecimal(0);
        String maxMonth = "";
        for (ProjectBudgetPlanDetail projectBudgetPlanDetail : projectBudgetPlanDetails) {
            capitalInterest = capitalInterest.add(projectBudgetPlanDetail.getCapitalInterest());
            BigDecimal underwrittenPlan = projectBudgetPlanDetail.getUnderwrittenPlan();
            amount = amount.add(underwrittenPlan);
            if(underwrittenPlan.compareTo(max)>0){
                max = underwrittenPlan;
                maxMonth = projectBudgetPlanDetail.getMonth();
            }
        }

        bean.setMaxMonth(maxMonth);
        bean.setAmount(amount);
        bean.setCapitalInterest(capitalInterest);

        return bean;
    }

    /**
     * 计算现金流量表
     */
    public CashFlowBean getCashFlowBean(Project project, List<ProjectBudgetPlanDetail> projectBudgetPlanDetails){
        CashFlowBean cashFlowBean = new CashFlowBean();
        //获取统计值
        ProjectBudgetPlanDetail detailTotal = getProjectBudgetPlanDetailTotal(project, projectBudgetPlanDetails);

        //a
        cashFlowBean.setSaleIncomeCash(detailTotal.getSaleIncome());
        //c
        cashFlowBean.setEarnestMoneyIncome(detailTotal.getEarnestMoneyIncome());
        //d
        cashFlowBean.setPurchaseCost(detailTotal.getDeviceCost()
                .add(detailTotal.getEngineerCost()));
        //f
        cashFlowBean.setEarnestMoneyCost(detailTotal.getProjectManageCost()
                .add(detailTotal.getEarnestMoneyCost())
                .add(detailTotal.getCapitalInterest()));
        //k
        cashFlowBean.setFinancingCapitalInflow(detailTotal.getUnderwrittenPlan());
        //l
        cashFlowBean.setFinancingCapitalOutflow(detailTotal.getRepaymentPlan());
        return cashFlowBean;
    }


    /**
     * 编辑预算保存项目并审核
     */
    //@Transactional(rollbackFor = RuntimeException.class)
    public void budgetEditSaveAndApprove(Project project, Project projectInDb, BudgetBean budgetBean, Admin admin) throws Exception {
        Integer approveStatusBudget = projectInDb.getApproveStatusBudget();

        budgetEditSave(project, projectInDb, budgetBean, admin, ApproveStatusEnum.APPROVAL_PENDING);

        budgetApprove(approveStatusBudget, projectInDb, admin);
    }

    public void budgetApprove(Integer approveStatusBudget, Project projectInDb, Admin admin) throws Exception {
        projectInstanceService.startBudgetProcessInstance(projectInDb, admin);
    }

    /**
     * 编辑预算保存项目
     */
    @Transactional(rollbackFor = RuntimeException.class)
    public Project budgetEditSave(Project project, Project projectInDb, BudgetBean budgetBean, Admin admin, ApproveStatusEnum approveStatusEnum) {
        Project p = projectService.editProject(project, projectInDb);
        p.setStatus(StatusEnum.BUDGET_ACCOUNTS.getStatus());
        p.setStatusDesc(StatusEnum.BUDGET_ACCOUNTS.getStatusDesc());
        p.setApproveStatusBudget(approveStatusEnum.getApproveStatus());
        p = projectRepository.saveAndFlush(p);
        //清空重新保存概算信息
        clearBudget(p);
        saveBudget(p, budgetBean);

        clearBudgetDetail(p);
        saveBudgetDetail(p);
        return p;
    }

    private void saveBudgetDetail(Project p) {
        //临时表中始终是最新的详情，不管是本次修改还是以前保持的
        //收入明细
        List<ProjectBudgetIncomeDetailTemp> projectBudgetIncomeDetailTemps = projectBudgetIncomeDetailTempRepository.findAllByProjectIdEquals(p.getId());
        if(CollectionUtil.isNotEmpty(projectBudgetIncomeDetailTemps)){
            List<ProjectBudgetIncomeDetail> incomeDetails = projectBudgetIncomeDetailTemps.stream()
                    .map(ProjectBudgetIncomeDetailTemp::toProjectBudgetIncomeDetail)
                    .collect(Collectors.toList());
            projectBudgetIncomeDetailRepository.save(incomeDetails);
        }
        //成本明细
        List<ProjectBudgetCostDetailTemp> projectBudgetCostDetailTemps = projectBudgetCostDetailTempRepository.findAllByProjectIdEquals(p.getId());
        if(CollectionUtil.isNotEmpty(projectBudgetCostDetailTemps)){
            List<ProjectBudgetCostDetail> costDetails = projectBudgetCostDetailTemps.stream()
                    .map(ProjectBudgetCostDetailTemp::toProjectBudgetCostDetail)
                    .collect(Collectors.toList());
            projectBudgetCostDetailRepository.save(costDetails);
        }
        //项目管理明细
        List<ProjectBudgetCostProjectManageDetailTemp> projectBudgetCostProjectManageDetailTemps = projectBudgetCostProjectManageDetailTempRepository.findAllByProjectIdEquals(p.getId());
        if(CollectionUtil.isNotEmpty(projectBudgetCostProjectManageDetailTemps)){
            List<ProjectBudgetCostProjectManageDetail> costProjectManageDetails = projectBudgetCostProjectManageDetailTemps.stream()
                    .map(ProjectBudgetCostProjectManageDetailTemp::toProjectBudgetCostProjectManageDetail)
                    .collect(Collectors.toList());
            projectBudgetCostProjectManageDetailRepository.save(costProjectManageDetails);
        }
        //资金计划明细
        List<ProjectBudgetPlanDetailTemp> projectBudgetPlanDetailTemps = projectBudgetPlanDetailTempRepository.findAllByProjectIdEquals(p.getId());
        if(CollectionUtil.isNotEmpty(projectBudgetPlanDetailTemps)){
            List<ProjectBudgetPlanDetail> budgetPlanDetails = projectBudgetPlanDetailTemps.stream()
                    .map(ProjectBudgetPlanDetailTemp::toProjectBudgetPlanDetail)
                    .collect(Collectors.toList());
            projectBudgetPlanDetailRepository.save(budgetPlanDetails);
        }
    }

    private void clearBudgetDetail(Project p) {
        //收入明细
        clearBudgetIncomeDetail(p);
        //成本明细
        clearBudgetCostDetail(p);
        //项目管理明细
        clearBudgetCostProjectManageDetail(p);
        //资金计划明细
        clearBudgetPlanDetail(p);
    }

    public void findAll(Project project) {

    }
}
