package cn.palmte.work.service;

import cn.palmte.work.bean.*;
import cn.palmte.work.model.*;
import cn.palmte.work.utils.InterfaceUtil;
import org.springframework.beans.factory.annotation.Autowired;
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;

@Service
public class ProjectFinalSevice {

    @Autowired
    private ProjectFinalCostRepository projectFinalCostRepository;

    @Autowired
    private ProjectFinalCashFluxRepository projectFinalCashFluxRepository;

    @Autowired
    private ProjectFinalCostManageRepository projectFinalCostManageRepository;

    @Autowired
    private ProjectFinalIncomeReposiry projectFinalIncomeReposiry;

    @Autowired
    private ProjectEstimateService projectEstimateService;

    @Autowired
    private ProjectBudgetService projectBudgetService;

    @Autowired
    private ProjectSettleIncomeRepository projectSettleIncomeRepository;

    @Autowired
    private ProjectSettleCostRepository projectSettleCostRepository;

    @Autowired
    private ProjectSettleCostManageRepository projectSettleCostManageRepository;

    @Autowired
    private ProjectSettleCashFlowRepository projectSettleCashFlowRepository;

    @Autowired
    private ProjectService projectService;

    @Autowired
    private ProjectInstanceService projectInstanceService;

    @Autowired
    private ProjectSettleService projectSettleService;

    @Autowired
    private ProjectFinalProfitMarginRepository projectFinalProfitMarginRepository;

    @Transactional
    public void save(Project project, FinalBean finalBean) {
        clearAndSave(project, finalBean);
        projectService.updateStatusAndApproveStatus(project.getId(), StatusEnum.FINAL_ACCOUNTS, ApproveStatusEnum.APPROVAL_UNCOMMIT, project.getOtherName());
    }

    @Transactional
    public void saveAndApprove(Project project, FinalBean finalBean) throws Exception {
        clearAndSave(project, finalBean);
        //更新项目和审批状态
        projectService.updateStatusAndApproveStatus(project.getId(), StatusEnum.FINAL_ACCOUNTS, ApproveStatusEnum.APPROVAL_PENDING, project.getOtherName());
        //发起流程
        projectInstanceService.startFinalProcessInstance(project.getId(), InterfaceUtil.getAdmin());
    }

    public void clearAndSave(Project project, FinalBean finalBean) {
        //清楚旧数据
        clearFinal(project);

        //预算表数据
        EstimateBean estimate = projectEstimateService.getEstimate(project);
        //概算表数据
        BudgetBean budget = projectBudgetService.getBudget(project);

        FormerBean settle = projectSettleService.getCurrentSettle(project, null);
        //现金流量表数据
        List<ProjectBudgetPlanDetail> projectBudgetPlanDetails = projectBudgetService.getProjectBudgetPlanDetails(project);
        CashFlowBean cashFlowBean = projectBudgetService.getCashFlowBean(project, projectBudgetPlanDetails);

        //保存项目决算收入信息
        saveProjectFinalIncome(project, finalBean, estimate, budget, settle);

        //保存项目决算成本信息
        saveProjectFinalCost(project, finalBean, estimate, budget, settle);

        //保存项目结算管理成本信息
        saveProjectFinalCostManage(project, finalBean, estimate, budget, settle);

        //保存项目决算利润率
        saveProjectFinalProfitMargin(project, finalBean, estimate, budget, settle);

        //保存项目结算现金流量信息
        saveProjectFinalCashFlux(project, finalBean, cashFlowBean, settle);
    }

    private void clearFinal(Project project) {
        List<ProjectFinalCost> projectFinalCosts = projectFinalCostRepository.findByProjectIdEquals(project.getId());
        if (CollectionUtil.isNotEmpty(projectFinalCosts)) {
            projectFinalCostRepository.deleteInBatch(projectFinalCosts);
        }

        List<ProjectFinalCostManage> projectFinalCostManages = projectFinalCostManageRepository.findByProjectIdEquals(project.getId());
        if (CollectionUtil.isNotEmpty(projectFinalCostManages)) {
            projectFinalCostManageRepository.deleteInBatch(projectFinalCostManages);
        }

        List<ProjectFinalIncome> projectFinalIncomes = projectFinalIncomeReposiry.findByProjectIdEquals(project.getId());
        if (CollectionUtil.isNotEmpty(projectFinalIncomes)) {
            projectFinalIncomeReposiry.deleteInBatch(projectFinalIncomes);
        }

        List<ProjectFinalProfitMargin> projectFinalProfitMargins = projectFinalProfitMarginRepository.findByProjectIdEquals(project.getId());
        if (CollectionUtil.isNotEmpty(projectFinalProfitMargins)) {
            projectFinalProfitMarginRepository.deleteInBatch(projectFinalProfitMargins);
        }

        List<ProjectFinalCashFlux> projectFinalCashFluxes = projectFinalCashFluxRepository.findByProjectIdEquals(project.getId());
        if (CollectionUtil.isNotEmpty(projectFinalCashFluxes)) {
            projectFinalCashFluxRepository.deleteInBatch(projectFinalCashFluxes);
        }
    }

    private void saveProjectFinalProfitMargin(Project project, FinalBean finalBean, EstimateBean estimate, BudgetBean budget, FormerBean settle) {
        ProjectFinalProfitMargin typeGrossProfit = new ProjectFinalProfitMargin();
        typeGrossProfit.setProjectId(project.getId());
        typeGrossProfit.setType(ProjectFinalProfitMargin.TYPE_GROSS_PROFIT);
        typeGrossProfit.setEstimateTotalProfitMargin(estimate.getProjectGrossProfit());
        typeGrossProfit.setBudgetTotalProfitMargin(budget.getProjectGrossProfit());
        typeGrossProfit.setSettleTotalProfitMargin(settle.getGrossProfit());
        typeGrossProfit.setFinalTotalProfitMargin(finalBean.getGrossProfitFinalTotal());
        typeGrossProfit.setProfitMargin(finalBean.getGrossProfitProfitMargin());
        projectFinalProfitMarginRepository.saveAndFlush(typeGrossProfit);


        ProjectFinalProfitMargin typeContributionMargin = new ProjectFinalProfitMargin();
        typeContributionMargin.setProjectId(project.getId());
        typeContributionMargin.setType(ProjectFinalProfitMargin.TYPE_CONTRIBUTION_MARGIN);
        typeContributionMargin.setEstimateTotalProfitMargin(estimate.getProjectContributionProfit());
        typeContributionMargin.setBudgetTotalProfitMargin(budget.getProjectContributionProfit());
        typeContributionMargin.setSettleTotalProfitMargin(settle.getContributionProfit());
        typeContributionMargin.setFinalTotalProfitMargin(finalBean.getContributionMarginFinalTotal());
        typeContributionMargin.setProfitMargin(finalBean.getContributionMarginProfitMargin());
        projectFinalProfitMarginRepository.saveAndFlush(typeContributionMargin);

        ProjectFinalProfitMargin typeNetMargin = new ProjectFinalProfitMargin();
        typeNetMargin.setProjectId(project.getId());
        typeNetMargin.setType(ProjectFinalProfitMargin.TYPE_NET_MARGIN);
        typeNetMargin.setSettleTotalProfitMargin(settle.getNetProfit());
        typeNetMargin.setFinalTotalProfitMargin(finalBean.getNetMarginFinalTotal());
        typeNetMargin.setProfitMargin(finalBean.getNetMarginProfitMargin());
        projectFinalProfitMarginRepository.saveAndFlush(typeNetMargin);

    }

    public void saveProjectFinalCostManage(Project project, FinalBean finalBean, EstimateBean estimate, BudgetBean budget, FormerBean settle) {
        ProjectFinalCostManage expropriationManage = new ProjectFinalCostManage();
        expropriationManage.setProjectId(project.getId());
        expropriationManage.setType(ProjectFinalCostManage.TYPE_EXPROPRIATION);
        expropriationManage.setEstimateTotalManageCost(estimate.getCostExpropriationTaxExclude());
        expropriationManage.setBudgetTotalManageCost(budget.getCostExpropriationTaxExclude());
        expropriationManage.setSettleTotalManageCost(settle.getCostExpropriationTaxExclude());
        expropriationManage.setFinalTotalManageCost(finalBean.getCostExpropriationFinalTotal());
        projectFinalCostManageRepository.saveAndFlush(expropriationManage);

        ProjectFinalCostManage companyManage = new ProjectFinalCostManage();
        companyManage.setProjectId(project.getId());
        companyManage.setType(ProjectFinalCostManage.TYPE_COMPANY_MANAGE);
        companyManage.setEstimateTotalManageCost(estimate.getCostCompanyManageTaxExclude());
        companyManage.setBudgetTotalManageCost(budget.getCostCompanyManageTaxExclude());
        companyManage.setSettleTotalManageCost(settle.getCostCompanyManageTaxExclude());
        companyManage.setFinalTotalManageCost(finalBean.getCostCompanyManageFinalTotal());
        projectFinalCostManageRepository.saveAndFlush(companyManage);

        ProjectFinalCostManage incomeTax = new ProjectFinalCostManage();
        incomeTax.setProjectId(project.getId());
        incomeTax.setType(ProjectFinalCostManage.TYPE_INCOME_TAX);
        incomeTax.setSettleTotalManageCost(new BigDecimal(0));
        incomeTax.setFinalTotalManageCost(finalBean.getCostIncomeTaxFinalTotal());
        projectFinalCostManageRepository.saveAndFlush(incomeTax);

    }

    private void saveProjectFinalIncome(Project project, FinalBean finalBean, EstimateBean estimate, BudgetBean budget, FormerBean settle) {
        ProjectFinalIncome projectFinalIncomeDevice = new ProjectFinalIncome();
        projectFinalIncomeDevice.setProjectId(project.getId());
        projectFinalIncomeDevice.setType(ProjectFinalIncome.TYPE_DEVICE);
        projectFinalIncomeDevice.setEstimateTotalIncome(estimate.getIncomeDeviceTaxExclude());
        projectFinalIncomeDevice.setBudgetTotalIncome(budget.getIncomeDeviceTaxExclude());
        projectFinalIncomeDevice.setSettleTotalIncome(settle.getIncomeDeviceTaxExclude());
        projectFinalIncomeDevice.setFinalTotalIncome(finalBean.getIncomeDeviceFinalTotal());
        projectFinalIncomeReposiry.saveAndFlush(projectFinalIncomeDevice);

        ProjectFinalIncome projectFinalIncomeEngineer = new ProjectFinalIncome();
        projectFinalIncomeEngineer.setProjectId(project.getId());
        projectFinalIncomeEngineer.setType(ProjectFinalIncome.TYPE_ENGINEER);
        projectFinalIncomeEngineer.setEstimateTotalIncome(estimate.getIncomeEngineerTaxExclude());
        projectFinalIncomeEngineer.setBudgetTotalIncome(budget.getIncomeEngineerTaxExclude());
        projectFinalIncomeEngineer.setSettleTotalIncome(settle.getIncomeEngineerTaxExclude());
        projectFinalIncomeEngineer.setFinalTotalIncome(finalBean.getIncomeEngineerFinalTotal());
        projectFinalIncomeReposiry.saveAndFlush(projectFinalIncomeEngineer);

        ProjectFinalIncome projectFinalIncomeService = new ProjectFinalIncome();
        projectFinalIncomeService.setProjectId(project.getId());
        projectFinalIncomeService.setType(ProjectFinalIncome.TYPE_SERVICE);
        projectFinalIncomeService.setEstimateTotalIncome(estimate.getIncomeServiceTaxExclude());
        projectFinalIncomeService.setBudgetTotalIncome(budget.getIncomeServiceTaxExclude());
        projectFinalIncomeService.setSettleTotalIncome(settle.getIncomeServiceTaxExclude());
        projectFinalIncomeService.setFinalTotalIncome(finalBean.getIncomeServiceFinalTotal());
        projectFinalIncomeReposiry.saveAndFlush(projectFinalIncomeService);
    }

    public void saveProjectFinalCost(Project project, FinalBean finalBean, EstimateBean estimate, BudgetBean budget, FormerBean settle) {
        ProjectFinalCost projectFinalCostDevice = new ProjectFinalCost();
        projectFinalCostDevice.setProjectId(project.getId());
        projectFinalCostDevice.setFee(ProjectFinalCost.FEE_PURCHASE);
        projectFinalCostDevice.setType(ProjectFinalCost.TYPE_DEVICE);
        projectFinalCostDevice.setEstimateTotalCost(estimate.getCostPurchaseDeviceTaxExclude());
        projectFinalCostDevice.setBudgetTotalCost(budget.getCostPurchaseDeviceTaxExclude());
        projectFinalCostDevice.setSettleTotalCost(settle.getCostPurchaseDeviceTaxExclude());
        projectFinalCostDevice.setFinalTotalCost(finalBean.getCostPurchaseDeviceFinalTotal());
        projectFinalCostRepository.saveAndFlush(projectFinalCostDevice);


        ProjectFinalCost projectFinalCostBuild = new ProjectFinalCost();
        projectFinalCostBuild.setProjectId(project.getId());
        projectFinalCostBuild.setFee(ProjectFinalCost.FEE_PURCHASE);
        projectFinalCostBuild.setType(ProjectFinalCost.TYPE_BUILDING);
        projectFinalCostBuild.setEstimateTotalCost(estimate.getCostPurchaseBuildTaxExclude());
        projectFinalCostBuild.setBudgetTotalCost(budget.getCostPurchaseBuildTaxExclude());
        projectFinalCostBuild.setSettleTotalCost(settle.getCostPurchaseBuildTaxExclude());
        projectFinalCostBuild.setFinalTotalCost(finalBean.getCostPurchaseBuildFinalTotal());
        projectFinalCostRepository.saveAndFlush(projectFinalCostBuild);

        ProjectFinalCost projectFinalCostService = new ProjectFinalCost();
        projectFinalCostService.setProjectId(project.getId());
        projectFinalCostService.setFee(ProjectFinalCost.FEE_PURCHASE);
        projectFinalCostService.setType(ProjectFinalCost.TYPE_SERVICE);
        projectFinalCostService.setEstimateTotalCost(estimate.getCostPurchaseServiceTaxExclude());
        projectFinalCostService.setBudgetTotalCost(budget.getCostPurchaseServiceTaxExclude());
        projectFinalCostService.setSettleTotalCost(settle.getCostPurchaseServiceTaxExclude());
        projectFinalCostService.setFinalTotalCost(finalBean.getCostPurchaseServiceFinalTotal());
        projectFinalCostRepository.saveAndFlush(projectFinalCostService);

        ProjectFinalCost projectFinalCostOther = new ProjectFinalCost();
        projectFinalCostOther.setProjectId(project.getId());
        projectFinalCostOther.setFee(ProjectFinalCost.FEE_PURCHASE);
        projectFinalCostOther.setType(ProjectFinalCost.TYPE_OTHER);
        projectFinalCostOther.setEstimateTotalCost(estimate.getCostPurchaseOtherTaxExclude());
        projectFinalCostOther.setBudgetTotalCost(budget.getCostPurchaseOtherTaxExclude());
        projectFinalCostOther.setSettleTotalCost(settle.getCostPurchaseOtherTaxExclude());
        projectFinalCostOther.setFinalTotalCost(finalBean.getCostPurchaseOtherFinalTotal());
        projectFinalCostRepository.saveAndFlush(projectFinalCostOther);

        ProjectFinalCost projectFinalCostProject = new ProjectFinalCost();
        projectFinalCostProject.setProjectId(project.getId());
        projectFinalCostProject.setFee(ProjectFinalCost.FEE_PROJECT_MANAGE);
        projectFinalCostProject.setType(ProjectFinalCost.TYPE_PROJECT_MANAGE);
        projectFinalCostProject.setEstimateTotalCost(estimate.getCostProjectManageTaxExclude());
        projectFinalCostProject.setBudgetTotalCost(budget.getCostProjectManageTaxExclude());
        projectFinalCostProject.setSettleTotalCost(settle.getCostProjectManageTaxExclude());
        projectFinalCostProject.setFinalTotalCost(finalBean.getCostProjectManageFinalTotal());
        projectFinalCostRepository.saveAndFlush(projectFinalCostProject);

        ProjectFinalCost projectFinalCostOtherOther = new ProjectFinalCost();
        projectFinalCostOtherOther.setProjectId(project.getId());
        projectFinalCostOtherOther.setFee(ProjectFinalCost.FEE_OTHER);
        projectFinalCostOtherOther.setType(ProjectFinalCost.TYPE_OTHER_OTHER);
        projectFinalCostOtherOther.setEstimateTotalCost(estimate.getCostOtherOtherTaxExclude());
        projectFinalCostOtherOther.setBudgetTotalCost(budget.getCostOtherOtherTaxExclude());
        projectFinalCostOtherOther.setSettleTotalCost(settle.getCostOtherOtherTaxExclude());
        projectFinalCostOtherOther.setFinalTotalCost(finalBean.getCostOtherFinalTotal());
        projectFinalCostRepository.saveAndFlush(projectFinalCostOtherOther);
    }

    public void saveProjectFinalCashFlux(Project project, FinalBean finalBean, CashFlowBean cashFlowBean, FormerBean settle) {
        List<ProjectFinalCashFlux> list = new ArrayList<>();

        ProjectFinalCashFlux projectFinalCashFlux1 = new ProjectFinalCashFlux();
        projectFinalCashFlux1.setProjectId(project.getId());
        projectFinalCashFlux1.setType(ProjectFinalCashFlux.TYPE1);
        projectFinalCashFlux1.setBudgetTotalCashFlux(cashFlowBean.getSaleIncomeCash());
        projectFinalCashFlux1.setSettleTotalCashFlux(settle.getSaleIncomeCash());
        projectFinalCashFlux1.setFinalTotalCashFlux(finalBean.getSaleIncomeCash());
        projectFinalCashFluxRepository.saveAndFlush(projectFinalCashFlux1);

        ProjectFinalCashFlux projectFinalCashFlux2 = new ProjectFinalCashFlux();
        projectFinalCashFlux2.setProjectId(project.getId());
        projectFinalCashFlux2.setType(ProjectFinalCashFlux.TYPE2);
        projectFinalCashFlux2.setBudgetTotalCashFlux(cashFlowBean.getTaxReturn());
        projectFinalCashFlux2.setSettleTotalCashFlux(settle.getTaxReturn());
        projectFinalCashFlux2.setFinalTotalCashFlux(finalBean.getTaxReturn());
        projectFinalCashFluxRepository.saveAndFlush(projectFinalCashFlux2);

        ProjectFinalCashFlux projectFinalCashFlux3 = new ProjectFinalCashFlux();
        projectFinalCashFlux3.setProjectId(project.getId());
        projectFinalCashFlux3.setType(ProjectFinalCashFlux.TYPE3);
        projectFinalCashFlux3.setBudgetTotalCashFlux(cashFlowBean.getEarnestMoneyIncome());
        projectFinalCashFlux3.setSettleTotalCashFlux(settle.getEarnestMoneyIncome());
        projectFinalCashFlux3.setFinalTotalCashFlux(finalBean.getEarnestMoneyIncome());
        projectFinalCashFluxRepository.saveAndFlush(projectFinalCashFlux3);

        ProjectFinalCashFlux projectFinalCashFlux4 = new ProjectFinalCashFlux();
        projectFinalCashFlux4.setProjectId(project.getId());
        projectFinalCashFlux4.setType(ProjectFinalCashFlux.TYPE4);
        projectFinalCashFlux4.setBudgetTotalCashFlux(cashFlowBean.getPurchaseCost());
        projectFinalCashFlux4.setSettleTotalCashFlux(settle.getPurchaseCost());
        projectFinalCashFlux4.setFinalTotalCashFlux(finalBean.getPurchaseCost());
        projectFinalCashFluxRepository.saveAndFlush(projectFinalCashFlux4);

        ProjectFinalCashFlux projectFinalCashFlux5 = new ProjectFinalCashFlux();
        projectFinalCashFlux5.setProjectId(project.getId());
        projectFinalCashFlux5.setType(ProjectFinalCashFlux.TYPE5);
        projectFinalCashFlux5.setBudgetTotalCashFlux(cashFlowBean.getTaxCost());
        projectFinalCashFlux5.setSettleTotalCashFlux(settle.getTaxCost());
        projectFinalCashFlux5.setFinalTotalCashFlux(finalBean.getTaxCost());
        projectFinalCashFluxRepository.saveAndFlush(projectFinalCashFlux5);

        ProjectFinalCashFlux projectFinalCashFlux6 = new ProjectFinalCashFlux();
        projectFinalCashFlux6.setProjectId(project.getId());
        projectFinalCashFlux6.setType(ProjectFinalCashFlux.TYPE6);
        projectFinalCashFlux6.setBudgetTotalCashFlux(cashFlowBean.getEarnestMoneyCost());
        projectFinalCashFlux6.setSettleTotalCashFlux(settle.getEarnestMoneyCost());
        projectFinalCashFlux6.setFinalTotalCashFlux(finalBean.getEarnestMoneyCost());
        projectFinalCashFluxRepository.saveAndFlush(projectFinalCashFlux6);

        ProjectFinalCashFlux projectFinalCashFlux7 = new ProjectFinalCashFlux();
        projectFinalCashFlux7.setProjectId(project.getId());
        projectFinalCashFlux7.setType(ProjectFinalCashFlux.TYPE7);
        projectFinalCashFlux7.setBudgetTotalCashFlux(cashFlowBean.getNetCashFlow());
        projectFinalCashFlux7.setSettleTotalCashFlux(settle.getNetCashFlow());
        projectFinalCashFlux7.setFinalTotalCashFlux(finalBean.getNetCashFlow());
        projectFinalCashFluxRepository.saveAndFlush(projectFinalCashFlux7);

        ProjectFinalCashFlux projectFinalCashFlux8 = new ProjectFinalCashFlux();
        projectFinalCashFlux8.setProjectId(project.getId());
        projectFinalCashFlux8.setType(ProjectFinalCashFlux.TYPE8);
        projectFinalCashFlux8.setBudgetTotalCashFlux(cashFlowBean.getCashInflowFromInvestingActivities());
        projectFinalCashFlux8.setSettleTotalCashFlux(settle.getCashInflowFromInvestingActivities());
        projectFinalCashFlux8.setFinalTotalCashFlux(finalBean.getCashInflowFromInvestingActivities());
        projectFinalCashFluxRepository.saveAndFlush(projectFinalCashFlux8);

        ProjectFinalCashFlux projectFinalCashFlux9 = new ProjectFinalCashFlux();
        projectFinalCashFlux9.setProjectId(project.getId());
        projectFinalCashFlux9.setType(ProjectFinalCashFlux.TYPE9);
        projectFinalCashFlux9.setBudgetTotalCashFlux(cashFlowBean.getCashOutflowFromInvestingActivities());
        projectFinalCashFlux9.setSettleTotalCashFlux(settle.getCashOutflowFromInvestingActivities());
        projectFinalCashFlux9.setFinalTotalCashFlux(finalBean.getCashOutflowFromInvestingActivities());
        projectFinalCashFluxRepository.saveAndFlush(projectFinalCashFlux9);

        ProjectFinalCashFlux projectFinalCashFlux10 = new ProjectFinalCashFlux();
        projectFinalCashFlux10.setProjectId(project.getId());
        projectFinalCashFlux10.setType(ProjectFinalCashFlux.TYPE10);
        projectFinalCashFlux10.setBudgetTotalCashFlux(cashFlowBean.getNetCashFromInvestingActivities());
        projectFinalCashFlux10.setSettleTotalCashFlux(settle.getNetCashFromInvestingActivities());
        projectFinalCashFlux10.setFinalTotalCashFlux(finalBean.getNetCashFromInvestingActivities());
        projectFinalCashFluxRepository.saveAndFlush(projectFinalCashFlux10);

        ProjectFinalCashFlux projectFinalCashFlux11 = new ProjectFinalCashFlux();
        projectFinalCashFlux11.setProjectId(project.getId());
        projectFinalCashFlux11.setType(ProjectFinalCashFlux.TYPE11);
        projectFinalCashFlux11.setBudgetTotalCashFlux(cashFlowBean.getFinancingCapitalInflow());
        projectFinalCashFlux11.setSettleTotalCashFlux(settle.getFinancingCapitalInflow());
        projectFinalCashFlux11.setFinalTotalCashFlux(finalBean.getFinancingCapitalInflow());
        projectFinalCashFluxRepository.saveAndFlush(projectFinalCashFlux11);

        ProjectFinalCashFlux projectFinalCashFlux12 = new ProjectFinalCashFlux();
        projectFinalCashFlux12.setProjectId(project.getId());
        projectFinalCashFlux12.setType(ProjectFinalCashFlux.TYPE12);
        projectFinalCashFlux12.setBudgetTotalCashFlux(cashFlowBean.getFinancingCapitalOutflow());
        projectFinalCashFlux12.setSettleTotalCashFlux(settle.getFinancingCapitalOutflow());
        projectFinalCashFlux12.setFinalTotalCashFlux(finalBean.getFinancingCapitalOutflow());
        projectFinalCashFluxRepository.saveAndFlush(projectFinalCashFlux12);

        ProjectFinalCashFlux projectFinalCashFlux13 = new ProjectFinalCashFlux();
        projectFinalCashFlux13.setProjectId(project.getId());
        projectFinalCashFlux13.setType(ProjectFinalCashFlux.TYPE13);
        projectFinalCashFlux13.setBudgetTotalCashFlux(cashFlowBean.getFinancingCapitalCashflow());
        projectFinalCashFlux13.setSettleTotalCashFlux(settle.getFinancingCapitalCashflow());
        projectFinalCashFlux13.setFinalTotalCashFlux(finalBean.getFinancingCapitalCashflow());
        projectFinalCashFluxRepository.saveAndFlush(projectFinalCashFlux13);

        ProjectFinalCashFlux projectFinalCashFlux14 = new ProjectFinalCashFlux();
        projectFinalCashFlux14.setProjectId(project.getId());
        projectFinalCashFlux14.setType(ProjectFinalCashFlux.TYPE14);
        projectFinalCashFlux14.setBudgetTotalCashFlux(cashFlowBean.getNetIncreaseMonetaryFunds());
        projectFinalCashFlux14.setSettleTotalCashFlux(settle.getNetIncreaseMonetaryFunds());
        projectFinalCashFlux14.setFinalTotalCashFlux(finalBean.getNetIncreaseMonetaryFunds());
        projectFinalCashFluxRepository.saveAndFlush(projectFinalCashFlux14);

    }


    public FinalBean getFinal(Project project) {
        FinalBean finalBean = new FinalBean();
        List<ProjectFinalIncome> projectFinalIncomes = projectFinalIncomeReposiry.findByProjectIdEquals(project.getId());

        if (CollectionUtil.isNotEmpty(projectFinalIncomes)) {
            for (ProjectFinalIncome projectFinalIncome : projectFinalIncomes) {
                if (ProjectFinalIncome.TYPE_DEVICE == projectFinalIncome.getType()) {
                    finalBean.setIncomeDeviceFinalTotal(projectFinalIncome.getFinalTotalIncome());
                } else if (ProjectFinalIncome.TYPE_ENGINEER == projectFinalIncome.getType()) {
                    finalBean.setIncomeEngineerFinalTotal(projectFinalIncome.getFinalTotalIncome());
                } else if (ProjectFinalIncome.TYPE_SERVICE == projectFinalIncome.getType()) {
                    finalBean.setIncomeServiceFinalTotal(projectFinalIncome.getFinalTotalIncome());
                }
            }
        }

        List<ProjectFinalCost> projectFinalCosts = projectFinalCostRepository.findByProjectIdEquals(project.getId());
        if (CollectionUtil.isNotEmpty(projectFinalCosts)) {
            for (ProjectFinalCost projectFinalCost : projectFinalCosts) {
                if (ProjectFinalCost.TYPE_DEVICE == projectFinalCost.getType()) {
                    finalBean.setCostPurchaseDeviceFinalTotal(projectFinalCost.getFinalTotalCost());
                } else if (ProjectFinalCost.TYPE_BUILDING == projectFinalCost.getType()) {
                    finalBean.setCostPurchaseBuildFinalTotal(projectFinalCost.getFinalTotalCost());
                } else if (ProjectFinalCost.TYPE_SERVICE == projectFinalCost.getType()) {
                    finalBean.setCostPurchaseServiceFinalTotal(projectFinalCost.getFinalTotalCost());
                } else if (ProjectFinalCost.TYPE_OTHER == projectFinalCost.getType()) {
                    finalBean.setCostPurchaseOtherFinalTotal(projectFinalCost.getFinalTotalCost());
                } else if (ProjectFinalCost.TYPE_PROJECT_MANAGE == projectFinalCost.getType()) {
                    finalBean.setCostProjectManageFinalTotal(projectFinalCost.getFinalTotalCost());
                } else if (ProjectFinalCost.TYPE_OTHER_OTHER == projectFinalCost.getType()) {
                    finalBean.setCostOtherFinalTotal(projectFinalCost.getFinalTotalCost());
                }
            }
        }

        List<ProjectFinalCostManage> projectFinalCostManages = projectFinalCostManageRepository.findByProjectIdEquals(project.getId());

        if (CollectionUtil.isNotEmpty(projectFinalCostManages)) {
            for (ProjectFinalCostManage projectFinalCostManage : projectFinalCostManages) {
                if (ProjectFinalCostManage.TYPE_EXPROPRIATION == projectFinalCostManage.getType()) {
                    finalBean.setCostExpropriationFinalTotal(projectFinalCostManage.getFinalTotalManageCost());
                } else if (ProjectFinalCostManage.TYPE_COMPANY_MANAGE == projectFinalCostManage.getType()) {
                    finalBean.setCostCompanyManageFinalTotal(projectFinalCostManage.getFinalTotalManageCost());
                } else if (ProjectFinalCostManage.TYPE_INCOME_TAX == projectFinalCostManage.getType()) {
                    finalBean.setCostIncomeTaxFinalTotal(projectFinalCostManage.getFinalTotalManageCost());
                }
            }
        }


        List<ProjectFinalCashFlux> projectFinalCashFluxes = projectFinalCashFluxRepository.findByProjectIdEquals(project.getId());
        if (CollectionUtil.isNotEmpty(projectFinalCashFluxes)) {
            for (ProjectFinalCashFlux projectFinalCashFlux : projectFinalCashFluxes) {
                if (ProjectFinalCashFlux.TYPE1 == projectFinalCashFlux.getType()) {
                    finalBean.setSaleIncomeCash(projectFinalCashFlux.getFinalTotalCashFlux());
                }
                if (ProjectFinalCashFlux.TYPE2 == projectFinalCashFlux.getType()) {
                    finalBean.setTaxReturn(projectFinalCashFlux.getFinalTotalCashFlux());
                }
                if (ProjectFinalCashFlux.TYPE3 == projectFinalCashFlux.getType()) {
                    finalBean.setEarnestMoneyIncome(projectFinalCashFlux.getFinalTotalCashFlux());
                }
                if (ProjectFinalCashFlux.TYPE4 == projectFinalCashFlux.getType()) {
                    finalBean.setPurchaseCost(projectFinalCashFlux.getFinalTotalCashFlux());
                }
                if (ProjectFinalCashFlux.TYPE5 == projectFinalCashFlux.getType()) {
                    finalBean.setTaxCost(projectFinalCashFlux.getFinalTotalCashFlux());
                }
                if (ProjectFinalCashFlux.TYPE6 == projectFinalCashFlux.getType()) {
                    finalBean.setEarnestMoneyCost(projectFinalCashFlux.getFinalTotalCashFlux());
                }
                if (ProjectFinalCashFlux.TYPE7 == projectFinalCashFlux.getType()) {
                    finalBean.setNetCashFlow(projectFinalCashFlux.getFinalTotalCashFlux());
                }
                if (ProjectFinalCashFlux.TYPE8 == projectFinalCashFlux.getType()) {
                    finalBean.setCashInflowFromInvestingActivities(projectFinalCashFlux.getFinalTotalCashFlux());
                }
                if (ProjectFinalCashFlux.TYPE9 == projectFinalCashFlux.getType()) {
                    finalBean.setCashOutflowFromInvestingActivities(projectFinalCashFlux.getFinalTotalCashFlux());
                }
                if (ProjectFinalCashFlux.TYPE10 == projectFinalCashFlux.getType()) {
                    finalBean.setNetCashFromInvestingActivities(projectFinalCashFlux.getFinalTotalCashFlux());
                }
                if (ProjectFinalCashFlux.TYPE11 == projectFinalCashFlux.getType()) {
                    finalBean.setFinancingCapitalInflow(projectFinalCashFlux.getFinalTotalCashFlux());
                }
                if (ProjectFinalCashFlux.TYPE12 == projectFinalCashFlux.getType()) {
                    finalBean.setFinancingCapitalOutflow(projectFinalCashFlux.getFinalTotalCashFlux());
                }
                if (ProjectFinalCashFlux.TYPE13 == projectFinalCashFlux.getType()) {
                    finalBean.setFinancingCapitalCashflow(projectFinalCashFlux.getFinalTotalCashFlux());
                }
                if (ProjectFinalCashFlux.TYPE14 == projectFinalCashFlux.getType()) {
                    finalBean.setNetIncreaseMonetaryFunds(projectFinalCashFlux.getFinalTotalCashFlux());
                }
            }
        }

        //finalBean.setGrossProfitProfitMargin(finalBean.getGrossProfitFinalTotal().divide(finalBean.getIncomeTotal(),4, RoundingMode.HALF_UP).multiply(new BigDecimal(100)));
        //finalBean.setContributionMarginProfitMargin(finalBean.getContributionMarginFinalTotal().divide(finalBean.getIncomeTotal(),4, RoundingMode.HALF_UP).multiply(new BigDecimal(100)));
        //finalBean.setNetMarginProfitMargin(finalBean.getNetMarginFinalTotal().divide(finalBean.getIncomeTotal(),4, RoundingMode.HALF_UP).multiply(new BigDecimal(100)));
        return finalBean;
    }

    public SettleBean getSettle(Project project) {
        SettleBean settleBean = new SettleBean();
        BigDecimal incomeDivice = projectSettleIncomeRepository.incomeTaxExcludeSum(project.getId(), ProjectSettleIncome.TYPE_DEVICE);
        BigDecimal incomeEngineer = projectSettleIncomeRepository.incomeTaxExcludeSum(project.getId(), ProjectSettleIncome.TYPE_ENGINEER);
        BigDecimal incomeService = projectSettleIncomeRepository.incomeTaxExcludeSum(project.getId(), ProjectSettleIncome.TYPE_SERVICE);

        settleBean.setIncomeDevice(incomeDivice);
        settleBean.setIncomeEngineer(incomeEngineer);
        settleBean.setIncomeService(incomeService);

        BigDecimal costDevice = projectSettleCostRepository.costTaxExcludeSum(project.getId(), ProjectSettleCost.TYPE_DEVICE);
        BigDecimal costBuilding = projectSettleCostRepository.costTaxExcludeSum(project.getId(), ProjectSettleCost.TYPE_BUILDING);
        BigDecimal costService = projectSettleCostRepository.costTaxExcludeSum(project.getId(), ProjectSettleCost.TYPE_SERVICE);
        BigDecimal costOther = projectSettleCostRepository.costTaxExcludeSum(project.getId(), ProjectSettleCost.TYPE_OTHER);
        BigDecimal costProjectManage = projectSettleCostRepository.costTaxExcludeSum(project.getId(), ProjectSettleCost.TYPE_PROJECT_MANAGE);
        BigDecimal costOtherOther = projectSettleCostRepository.costTaxExcludeSum(project.getId(), ProjectSettleCost.TYPE_OTHER_OTHER);

        settleBean.setCostPurchaseDevice(costDevice);
        settleBean.setCostPurchaseBuild(costBuilding);
        settleBean.setCostPurchaseService(costService);
        settleBean.setCostOther(costOther);
        settleBean.setCostProjectManage(costProjectManage);
        settleBean.setCostPurchaseOther(costOtherOther);

        BigDecimal costExpropriation = projectSettleCostManageRepository.costTaxExcludeSum(project.getId(), ProjectSettleCostManage.TYPE_EXPROPRIATION);
        BigDecimal costCompanyMange = projectSettleCostManageRepository.costTaxExcludeSum(project.getId(), ProjectSettleCostManage.TYPE_COMPANY_MANAGE);
        BigDecimal costIncomeTax = projectSettleCostManageRepository.costTaxExcludeSum(project.getId(), ProjectSettleCostManage.TYPE_INCOME_TAX);

        settleBean.setCostExpropriation(costExpropriation);
        settleBean.setCostCompanyManage(costCompanyMange);
        settleBean.setCostIncomeTax(costIncomeTax);

        BigDecimal type1 = projectSettleCashFlowRepository.costAmountSum(project.getId(), ProjectSettleCashFlow.SALE_INCOME_CASH);
        BigDecimal type2 = projectSettleCashFlowRepository.costAmountSum(project.getId(), ProjectSettleCashFlow.TAX_RETURN);
        BigDecimal type3 = projectSettleCashFlowRepository.costAmountSum(project.getId(), ProjectSettleCashFlow.EARNEST_MONEY_INCOME);
        BigDecimal type4 = projectSettleCashFlowRepository.costAmountSum(project.getId(), ProjectSettleCashFlow.PURCHASE_COST);
        BigDecimal type5 = projectSettleCashFlowRepository.costAmountSum(project.getId(), ProjectSettleCashFlow.TAX_COST);
        BigDecimal type6 = projectSettleCashFlowRepository.costAmountSum(project.getId(), ProjectSettleCashFlow.EARNEST_MONEY_COST);
        BigDecimal type7 = projectSettleCashFlowRepository.costAmountSum(project.getId(), ProjectSettleCashFlow.NET_CASH_FLOW);
        BigDecimal type8 = projectSettleCashFlowRepository.costAmountSum(project.getId(), ProjectSettleCashFlow.CASH_INFLOW_FROM_INVESTING_ACTIVITIES);
        BigDecimal type9 = projectSettleCashFlowRepository.costAmountSum(project.getId(), ProjectSettleCashFlow.CASH_OUTFLOW_FROM_INVESTING_ACTIVITIES);
        BigDecimal type10 = projectSettleCashFlowRepository.costAmountSum(project.getId(), ProjectSettleCashFlow.NET_CASH_FROM_INVESTING_ACTIVITIES);
        BigDecimal type11 = projectSettleCashFlowRepository.costAmountSum(project.getId(), ProjectSettleCashFlow.FINANCING_CAPITAL_INFLOW);
        BigDecimal type12 = projectSettleCashFlowRepository.costAmountSum(project.getId(), ProjectSettleCashFlow.FINANCING_CAPITAL_OUTFLOW);
        BigDecimal type13 = projectSettleCashFlowRepository.costAmountSum(project.getId(), ProjectSettleCashFlow.FINANCING_CAPITAL_CASHFLOW);
        BigDecimal type14 = projectSettleCashFlowRepository.costAmountSum(project.getId(), ProjectSettleCashFlow.NET_INCREASE_MONETARY_FUNDS);

        settleBean.setSaleIncomeCash(type1);
        settleBean.setTaxReturn(type2);
        settleBean.setEarnestMoneyIncome(type3);
        settleBean.setPurchaseCost(type4);
        settleBean.setTaxCost(type5);
        settleBean.setEarnestMoneyCost(type6);
        settleBean.setNetCashFlow(type7);
        settleBean.setCashInflowFromInvestingActivities(type8);
        settleBean.setCashOutflowFromInvestingActivities(type9);
        settleBean.setNetCashFromInvestingActivities(type10);
        settleBean.setFinancingCapitalInflow(type11);
        settleBean.setFinancingCapitalOutflow(type12);
        settleBean.setFinancingCapitalCashflow(type13);
        settleBean.setNetIncreaseMonetaryFunds(type14);
        return settleBean;
    }
}
