package com.jeeplus.modules.sg.financial.erpcredit.util;

import com.jeeplus.modules.sg.audit.information.entity.Information;
import com.jeeplus.modules.sg.audit.information.utils.RandomUtil;
import com.jeeplus.modules.sg.financial.erpcredit.entity.ErpCredit;
import com.jeeplus.modules.sg.financial.erpcredit.entity.ErpJudgeAccount;
import com.jeeplus.modules.sg.financial.erpcredit.entity.ErpJudgeArtificial;
import com.jeeplus.modules.sg.financial.erpcredit.entity.ErpJudgeExport;
import com.jeeplus.modules.sg.financial.expense.entity.Expense;
import com.jeeplus.modules.sg.financial.settlement.entity.MaintainData;
import com.jeeplus.modules.sg.managementcenter.project.entity.Project;
import io.swagger.models.auth.In;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

public class ValidationUtils {

    //验证概预算信息是否存在
    public static String getDataMainJudge(List<ErpCredit> erpCredits, List<MaintainData> maintainDatas){
        List<ErpJudgeAccount> list = new ArrayList<ErpJudgeAccount>();
        ErpJudgeAccount erpJudgeDataMain = new ErpJudgeAccount();
        boolean flag = true;
        for (ErpCredit erpCredit1:erpCredits){
            flag = true;
            for (MaintainData maintainData:maintainDatas){
                if (erpCredit1.getItemId().equals(maintainData.getProjectId())){
                    flag = false;
                }
            }
            if (flag){
                erpJudgeDataMain = new ErpJudgeAccount();
                erpJudgeDataMain.setItemId(erpCredit1.getItemId());
                erpJudgeDataMain.setItemName(erpCredit1.getItemName());
                erpJudgeDataMain.setFlagItem("无");
                erpJudgeDataMain.setItemSupplierName(erpCredit1.getSupplierName());
                list.add(erpJudgeDataMain);
            }
        }
        if (null!=list && list.size()>0){
            return getJoiningTogether(list);
        }
        return "";
    }

    //验证费用入账是否存在
    public static String getExpenseJudge(List<ErpCredit> erpCredits, List<Expense> expenses){
        List<ErpJudgeAccount> list = new ArrayList<ErpJudgeAccount>();
        ErpJudgeAccount erpExpense = new ErpJudgeAccount();
        boolean flag = true;
        for (ErpCredit erpCredit1:erpCredits){
            flag = true;
            for (Expense expense:expenses){
                if (erpCredit1.getItemId().equals(expense.getProjectId())){
                    flag = false;
                }
            }
            if (flag){
                erpExpense = new ErpJudgeAccount();
                erpExpense.setItemId(erpCredit1.getItemId());
                erpExpense.setItemName(erpCredit1.getItemName());
                erpExpense.setFlagItem("无");
                erpExpense.setItemSupplierName(erpCredit1.getSupplierName());
                list.add(erpExpense);
            }
        }
        if (null!=list && list.size()>0){
            return getJoiningTogether(list);
        }
        return "";
    }
    //验证需求基本信息
    public static String getProjectJudge(List<ErpCredit> erpCredits, List<Project> projects){
        List<ErpJudgeAccount> list = new ArrayList<ErpJudgeAccount>();
        ErpJudgeAccount erpProject = new ErpJudgeAccount();
        boolean flag = true;
        for (ErpCredit erpCredit1:erpCredits){
            flag = true;
            for (Project project:projects){
                if (erpCredit1.getItemId().equals(project.getProjectNumber())){
                    flag = false;
                }
            }
            if (flag){
                erpProject = new ErpJudgeAccount();
                erpProject.setItemId(erpCredit1.getItemId());
                erpProject.setItemName(erpCredit1.getItemName());
                erpProject.setFlagItem("无");
                erpProject.setItemSupplierName(erpCredit1.getSupplierName());
                list.add(erpProject);
            }
        }
        if (null!=list && list.size()>0){
            return getJoiningTogether(list);
        }
        return "";
    }

    //验证是否甲供材存在
    public static String getInformationJudge(List<ErpCredit> erpCredits, List<Information> informationList){
        List<ErpJudgeAccount> list = new ArrayList<ErpJudgeAccount>();
        ErpJudgeAccount erpProject = new ErpJudgeAccount();
        boolean flag = true;
        for (ErpCredit erpCredit1:erpCredits){
            flag = true;
            for (Information information:informationList){
                if (erpCredit1.getItemId().equals(information.getProjectId())){
                    flag = false;
                    break;
                }
            }
            if (flag){
                erpProject = new ErpJudgeAccount();
                erpProject.setItemId(erpCredit1.getItemId());
                erpProject.setItemName(erpCredit1.getItemName());
                erpProject.setFlagItem("无");
                erpProject.setItemSupplierName(erpCredit1.getSupplierName());
                list.add(erpProject);
            }
        }
        if (null!=list && list.size()>0){
            return getJoiningTogether(list);
        }
        return "";
    }
    //验证是否甲供材存在
    public static String getInformationJudge1(List<ErpCredit> erpCredits, List<Information> informationList){
        List<ErpJudgeAccount> list = new ArrayList<ErpJudgeAccount>();
        ErpJudgeAccount erpProject = new ErpJudgeAccount();
        for (ErpCredit erpCredit:erpCredits){
            for (Information information:informationList){
                if (erpCredit.getItemId().equals(information.getProjectId())){
                    erpProject = new ErpJudgeAccount();
                    erpProject.setItemId(erpCredit.getItemId());
                    erpProject.setItemName(erpCredit.getItemName());
                    erpProject.setFlagItem("否");
                    erpProject.setItemSupplierName(erpCredit.getSupplierName());
                    list.add(erpProject);
                    break;
                }
            }
        }
        if (null!=list && list.size()>0){
            return getJoiningTogether(list);
        }
        return "";
    }
    //拼接
    public static String getJoiningTogether(List<ErpJudgeAccount> erpJudgeAccounts){
        String str = "";
        for (ErpJudgeAccount erpJudgeAccount:erpJudgeAccounts){
            str+=erpJudgeAccount.getItemId()+","+erpJudgeAccount.getItemName()+","+erpJudgeAccount.getFlagItem()+","+erpJudgeAccount.getItemSupplierName()+",";
        }
        str = str.substring(0,str.length()-1);
        return str;
    }

    //若应付金额（不含税价）>实付金额（不含税价）,需要进入人工判断，若人工判断通过，则费用入账核实无问题，若人工判断不通过则费用入账核实存在问题
    public static String getJudgeTwo(List<ErpCredit> listTwo){
        DecimalFormat decimalFormat   = new DecimalFormat("#0.00");
        List<ErpJudgeArtificial> erpJudgeArtificials = new ArrayList<ErpJudgeArtificial>();
        ErpJudgeArtificial erpJudgeArtificial = null;
        String str = "";
        for (ErpCredit erpCredit:listTwo){
            erpJudgeArtificial = new ErpJudgeArtificial();
            erpJudgeArtificial.setItemId(erpCredit.getItemId());
            //施工费
            if (null!=erpCredit.getConstructionStatus()){
               if (erpCredit.getConstructionStatus().equals("2")){
                   str+="施工费应付金额为"+decimalFormat.format(erpCredit.getConstructionFeePayable())+
                           ";实际金额"+decimalFormat.format(erpCredit.getConsTotalFee())+";";
               }
            }
            //设计费
            if (null!=erpCredit.getDesignStatus()){
                if (erpCredit.getDesignStatus().equals("2")){
                    str+="设计费应付金额为"+decimalFormat.format(erpCredit.getDesignFeePayable())+
                            ";实际金额为"+decimalFormat.format(erpCredit.getOtherDesignFee())+";";
                }
            }
            //监理费
            if (null!=erpCredit.getSupervisorStatus()){
               if (erpCredit.getSupervisorStatus().equals("2")){
                   str+="监理费应付金额为"+decimalFormat.format(erpCredit.getSupervisorFeePayable())+
                           ";实际金额为"+decimalFormat.format(erpCredit.getOtherSupervisorFee())+";";
               }
            }
            erpJudgeArtificial.setNotTheSame(str);
            erpJudgeArtificials.add(erpJudgeArtificial);
            str = "";
        }
        String string = "";
        for (ErpJudgeArtificial erpJudgeArtificial1:erpJudgeArtificials){
            string+=erpJudgeArtificial1.getItemId()+","+erpJudgeArtificial1.getNotTheSame()+",";
        }
        if (!"".equals(string)){
            string = string.substring(0,string.length()-1);
            return string;
        }
        return "";
    }
    //若应付金额（不含税价）<实付金额（不含税价）,需要导出差异情况，同时费用入账核实存在问题。
    public static String getJudgeThree(List<ErpCredit> listThree){
        DecimalFormat decimalFormat   = new DecimalFormat("#0.00");
        List<ErpJudgeExport> erpJudgeExports = new ArrayList<ErpJudgeExport>();
        ErpJudgeExport erpJudgeExport = null;
        for (ErpCredit erpCredit:listThree){
            erpJudgeExport = new ErpJudgeExport();
            erpJudgeExport.setItemId(erpCredit.getItemId());
            erpJudgeExport.setItemName(erpCredit.getItemName());
            erpJudgeExport.setConsTotalFee("");
            erpJudgeExport.setConstructionFeePayable("");
            erpJudgeExport.setOtherDesignFee("");
            erpJudgeExport.setDesignFeePayable("");
            erpJudgeExport.setOtherSupervisorFee("");
            erpJudgeExport.setSupervisorFeePayable("");
            //施工费
            if (null!=erpCredit.getConstructionStatus()){
                erpJudgeExport.setConsTotalFee(decimalFormat.format(erpCredit.getConsTotalFee()));
                erpJudgeExport.setConstructionFeePayable(decimalFormat.format(erpCredit.getConstructionFeePayable()));
            }
            //设计费
            if (null!=erpCredit.getDesignStatus()){
               erpJudgeExport.setOtherDesignFee(decimalFormat.format(erpCredit.getOtherDesignFee()));
               erpJudgeExport.setDesignFeePayable(decimalFormat.format(erpCredit.getDesignFeePayable()));
            }
            //监理费
            if (null!=erpCredit.getSupervisorStatus()){
                erpJudgeExport.setOtherSupervisorFee(decimalFormat.format(erpCredit.getOtherSupervisorFee()));
                erpJudgeExport.setSupervisorFeePayable(decimalFormat.format(erpCredit.getSupervisorFeePayable()));
            }
            erpJudgeExports.add(erpJudgeExport);
        }
        String str = "";
        for (ErpJudgeExport erpJudgeExport1:erpJudgeExports){
            str+=erpJudgeExport1.getItemId()+","+erpJudgeExport1.getItemName()+","
                    +erpJudgeExport1.getConstructionFeePayable()+","+erpJudgeExport1.getConsTotalFee()+","
                    +erpJudgeExport1.getDesignFeePayable()+","+erpJudgeExport1.getOtherDesignFee()+","
                    +erpJudgeExport1.getSupervisorFeePayable()+","+erpJudgeExport1.getOtherSupervisorFee()+",";
        }
        str = str.substring(0,str.length()-1);
        return str;
    }
    //导出差异
    public static List<ErpJudgeExport> getErpJudgeExport(String string){
        String[] split = string.split(",");
        List<ErpJudgeExport> erpJudgeExports = new ArrayList<ErpJudgeExport>();
        ErpJudgeExport erpJudgeExport = null;
        for (int i = 0; i < split.length; i+=8) {
            erpJudgeExport = new ErpJudgeExport();
            erpJudgeExport.setItemId(split[i]);
            erpJudgeExport.setItemName(split[i+1]);
            erpJudgeExport.setConstructionFeePayable(split[i+2]);
            erpJudgeExport.setConsTotalFee(split[i+3]);
            erpJudgeExport.setDesignFeePayable(split[i+4]);
            erpJudgeExport.setOtherDesignFee(split[i+5]);
            erpJudgeExport.setSupervisorFeePayable(split[i+6]);
            erpJudgeExport.setOtherSupervisorFee(split[i+7]);
            erpJudgeExports.add(erpJudgeExport);
        }
        return erpJudgeExports;
    }
    //所有erp信息
    public static String getAllListErpCredit(List<ErpCredit> erpCredits){
        String string = "";
        for (ErpCredit erpCredit:erpCredits){
            string +=erpCredit.getItemId()+",";
        }
        if (!"".equals(string)){
            string = string.substring(0,string.length()-1);
        }
        return string;
    }

    //验证资本和成本
    public static String getTheCostOfCapital(List<ErpCredit> erpCredits,List<Project> projects){
        String type = "";
        String strFlag1 = "0";
        String strFlag2 = "0";
        for (ErpCredit erpCredit:erpCredits){
            for (Project project:projects){
                if (erpCredit.getItemId().equals(project.getProjectNumber())){
                    if (project.getType().equals("配网资本")){
                        strFlag1 = "1";
                    }
                    if (project.getType().equals("配网成本")){
                        strFlag2 = "1";
                    }
                    break;
                }
            }
        }
        if (strFlag1.equals("1")&&!strFlag2.equals("1")){
            return "1";//配网资本
        }
        if (strFlag2.equals("1")&&!strFlag1.equals("1")){
            return "0";//配网成本
        }
        return "3";
    }
}
