package com.jeeplus.modules.sg.managementcenter.activiti.utils;

import com.google.common.collect.Maps;
import com.google.gson.internal.$Gson$Preconditions;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.excel.ImportExcel;
import com.jeeplus.modules.sg.managementcenter.activiti.entity.Construction;
import com.jeeplus.modules.sg.managementcenter.activiti.entity.Disclose;
import com.jeeplus.modules.sg.managementcenter.activiti.entity.MyNeedList;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MyImportUtils {
    public static Map<String,Object> errorTips(List<Disclose> discloses) {
        String tips = "";
        String fin = "";
        List<Disclose> newDiscloseList = new ArrayList<>();
        boolean flag = false;

        for (Disclose disclose : discloses) {
            flag = false;
            tips = "";
            //项目名称
            String projectName = disclose.getRequireName();
            //设备基础是否可以扩容
            String capacity = disclose.getCapacity();
            //现场设备尺寸
            String sceneSize = disclose.getSceneSize();
            //最多可扩容尺寸
            String capacitySize = disclose.getCapacitySize();
            //方案安全隐患
            String hiddenDanger = disclose.getHiddenDanger();
            //是否存在低压或通信线等影响施工
            String influenceConstruction = disclose.getInfluenceConstruction();
            //立项重复
            String projectRepetition = disclose.getProjectRepetition();
            //是否已实施
            String implemented = disclose.getImplemented();
            //变压器无负荷
            String transformer = disclose.getTransformer();
            //必要性问题说明
            String explanationOfNecessity = disclose.getExplanationOfNecessity();
            //线路名称一致性
            String lineName = disclose.getLineName();
            //工程量是否一致
            String quantities = disclose.getQuantities();
            //设备、开关、杆号一致性
            String rodNumber = disclose.getRodNumber();
            //图纸现场不一致说明
            String inconsistentDescription = disclose.getInconsistentDescription();
            //政处类型
            String administrationType = disclose.getAdministrationType();
            //前置条件
            String precondition = disclose.getPrecondition();
            //政策处理说明
            String policyHandlingInstructions = disclose.getPolicyHandlingInstructions();
            //二次是否是集成模块
            String integrateModule = disclose.getIntegrateModule();
            //PT是否需要更换
            String needReplace = disclose.getNeedReplace();
            //设备是否有整体改造的必要
            String overallTransformation = disclose.getOverallTransformation();
            if (precondition!=null&&!"".equals(precondition)) {

            }else {
                if ("".equals(projectName) || projectName == null) {
                    flag = true;
                    tips +="项目名称不能为空";
                }
                if ("".equals(capacity) || capacity == null||(!"是".equals(capacity)&&!"否".equals(capacity))) {
                    flag = true;
                    tips += "设备基础是否可以扩容填写有误";
                }
                if("是".equals(capacity)){
                    if("".equals(sceneSize) || sceneSize == null||"".equals(capacitySize) || capacitySize == null){
                        tips += "设备基础是否可以扩容填写有误";
                    }
                }
                if ("".equals(hiddenDanger) || hiddenDanger == null||(!"有".equals(hiddenDanger)&&!"无".equals(hiddenDanger))) {
                    flag = true;
                    tips += "方案安全隐患填写有误";
                }
                if ("".equals(influenceConstruction) || influenceConstruction == null||(!"是".equals(influenceConstruction)&&!"否".equals(influenceConstruction))) {
                    flag = true;
                    tips += "是否存在低压或通信线等影响施工填写有误";
                }
                if ("".equals(projectRepetition) || projectRepetition == null||(!"是".equals(projectRepetition)&&!"否".equals(projectRepetition))) {
                    flag = true;
                    tips += "立项重复填写有误";
                }
                if ("".equals(implemented) || implemented == null||(!"是".equals(implemented)&&!"否".equals(implemented))) {
                    flag = true;
                    tips += "是否已实施填写有误";
                }
                if ("".equals(transformer) || transformer == null||(!"有".equals(transformer)&&!"无".equals(transformer))) {
                    flag = true;
                    tips += "变压器无负荷填写有误";
                }
                if ("有".equals(hiddenDanger) || "是".equals(influenceConstruction)|| "是".equals(projectRepetition)|| "是".equals(implemented)|| "无".equals(transformer)) {
                    if ("".equals(explanationOfNecessity) || explanationOfNecessity == null) {
                        flag = true;
                        tips += "必要性问题说明不能为空";
                    }
                }
                if ("".equals(lineName) || lineName == null||(!"一致".equals(lineName)&&!"不一致".equals(lineName))) {
                    flag = true;
                    tips += "线路名称一致性填写有误";
                }
                if ("".equals(quantities) || quantities == null||(!"是".equals(quantities)&&!"否".equals(quantities))) {
                    flag = true;
                    tips += "工程量是否一致填写有误";
                }
                if ("".equals(rodNumber) || rodNumber == null||(!"一致".equals(rodNumber)&&!"不一致".equals(rodNumber))) {
                    flag = true;
                    tips += "设备、开关、杆号一致性填写有误";
                }
                if ("不一致".equals(lineName)||"否".equals(quantities)||"不一致".equals(rodNumber)) {
                    if ("".equals(inconsistentDescription) || inconsistentDescription == null) {
                        flag = true;
                        tips += "图纸现场不一致说明不能为空";
                    }
                }
                if ("".equals(administrationType) || administrationType == null
                        ||(!"1.无".equals(administrationType)
                            &&!"2.农村政处".equals(administrationType)
                            &&!"3.民事政处/行政政处（公路/水利）".equals(administrationType)
                            &&!"4.行政政处（城区道路）".equals(administrationType)
                            &&!"5.农村政处;民事政处/行政政处（公路/水利）".equals(administrationType)
                            &&!"6.农村政处;行政政处（城区道路）".equals(administrationType)
                            &&!"7.民事政处/行政政处（公路/水利）;行政政处（城区道路）".equals(administrationType)
                            &&!"8.农村政处;民事政处/行政政处（公路/水利）;行政政处（城区道路）".equals(administrationType)
                        )
                ) {
                    flag = true;
                    tips += "政处类型填写有误";
                }else {
                    if (("2.农村政处".equals(administrationType)
                            ||"3.民事政处/行政政处（公路/水利）".equals(administrationType)
                            ||"4.行政政处（城区道路）".equals(administrationType)
                            ||"5.农村政处;民事政处/行政政处（公路/水利）".equals(administrationType)
                            ||"6.农村政处;行政政处（城区道路）".equals(administrationType)
                            ||"7.民事政处/行政政处（公路/水利）;行政政处（城区道路）".equals(administrationType)
                            ||"8.农村政处;民事政处/行政政处（公路/水利）;行政政处（城区道路）".equals(administrationType)
                        )) {
                        if ("".equals(policyHandlingInstructions) || policyHandlingInstructions == null) {
                            flag = true;
                            tips += "政策处理说明不能为空";
                        }
                    }
                }
                if ("".equals(disclose.getPlacementMode())||disclose.getPlacementMode() == null||(!"户外".equals(disclose.getPlacementMode())&&!"户内".equals(disclose.getPlacementMode())&&!"无".equals(disclose.getPlacementMode()))){
                    flag = true;
                    tips+="DTU安放方式核查填写有误";
                }
                if ("".equals(integrateModule) || integrateModule == null||(!"是".equals(integrateModule)&&!"否".equals(integrateModule))) {
                    flag = true;
                    tips += "二次是否是集成模块填写有误";
                }
                if ("".equals(needReplace) || needReplace == null||(!"是".equals(needReplace)&&!"否".equals(needReplace))) {
                    flag = true;
                    tips += "PT是否需要更换填写有误";
                }
                if ("".equals(overallTransformation) || overallTransformation == null||(!"是".equals(overallTransformation)&&!"否".equals(overallTransformation))) {
                    flag = true;
                    tips += "设备是否有整体改造的必要填写有误";
                }
                if ("户外".equals(disclose.getPlacementMode())||"户内".equals(disclose.getPlacementMode())){
                    if ("".equals(disclose.getProductionDate())||disclose.getProductionDate() == null){
                        tips +="核查环网柜生产日期不能为空;";
                    }
                }
            }
            if (flag == true) {
                fin += "<" + projectName + ">" + ":" + tips + "</br>";
            } else {
                newDiscloseList.add(disclose);
            }
        }
        Map<String,Object> reurnMap = new HashMap<>();
        reurnMap.put("fin",fin);
        reurnMap.put("newDiscloseList",newDiscloseList);
        return reurnMap;
    }

    //则现场设备尺寸、最多可扩容尺寸需填写，且默认单位为米
    public static Construction getAddUnit(Construction construction){
        String size = "";
        if (null!=construction.getDisclose()){
            size = construction.getDisclose().getSceneSize();
            if (null!=size&&!"".equals(size)){
                if (!size.contains("米")){
                    construction.getDisclose().setSceneSize(size+"米");
                }
            }
            size = construction.getDisclose().getCapacitySize();
            if (null!=size && !"".equals(size)){
                if (!size.contains("米")){
                    construction.getDisclose().setCapacitySize(size+"米");
                }
            }
        }
        return construction;
    }
    //人工审核：回显
    public static void getTheEcho(Construction construction, Model model){
        //前置条件
        if ((null!=construction.getDisclose().getPrecondition()&&!"".equals(construction.getDisclose().getPrecondition()))//前置条件
                //施工通道
                ||(null!=construction.getDisclose().getChannel()&&!"".equals(construction.getDisclose().getChannel())&&!"无".equals(construction.getDisclose().getChannel()))
                //其它问题描述
                ||(null!=construction.getDisclose().getOtherProblem()&&!"".equals(construction.getDisclose().getOtherProblem()))
        ){
            model.addAttribute("pass_construction6","0");
        }else {
            if ("有".equals(construction.getDisclose().getHiddenDanger())||//方案安全隐患
                    "是".equals(construction.getDisclose().getInfluenceConstruction())||//是否存在低压或通信线等影响施工
                    "是".equals(construction.getDisclose().getProjectRepetition())||//立项重复
                    "是".equals(construction.getDisclose().getImplemented())||//是否已经实施
                    "无".equals(construction.getDisclose().getTransformer())){//变压器无负荷
                model.addAttribute("pass_construction1","0");
            }else {
                //设计变更
                if (null!=construction.getDisclose().getInconsistentDescription()){//图纸现场不一致说明
                    if ("不一致".equals(construction.getDisclose().getLineName())||//线路名称一致性
                            "否".equals(construction.getDisclose().getQuantities())||//工程量是否一致
                            "不一致".equals(construction.getDisclose().getRodNumber())){//设备开关杠杆一致性
                        model.addAttribute("pass_construction2","0");
                    }else {
                        if (construction.getDisclose().getAdministrationType().equals("1.无")){//政处类型
                            model.addAttribute("pass_construction7","0");
                        }
                    }
                }else {
                    if (construction.getDisclose().getAdministrationType().equals("1.无")){//政处类型
                        model.addAttribute("pass_construction7","0");
                    }
                }
                getAdministrationTypeView(construction.getDisclose().getAdministrationType(),model);
//                //农村政处
//                if (construction.getDisclose().getAdministrationType().equals("1.农村政处")){
//                    model.addAttribute("pass_construction3","0");
//                }
//                //2.民事政处/行政政处（公路/水利）
//                if (construction.getDisclose().getAdministrationType().equals("2.民事政处/行政政处（公路/水利）")){
//                    model.addAttribute("pass_construction4","0");
//                }
//                //3.行政政处（城区道路）
//                if (construction.getDisclose().getAdministrationType().equals("3.行政政处（城区道路）")){
//                    model.addAttribute("pass_construction5","0");
//                }
            }
        }
    }
    //政策处理类型判断
    public static void getAdministrationTypeView(String administrationType,Model model){
        if (administrationType.length()>0){
            String[] split = administrationType.split(";");
            for (String str:split){
                if (str.contains("农村政处")){
                    model.addAttribute("pass_construction3","0");
                }
                //2.民事政处/行政政处（公路/水利）
                if (str.contains("民事政处/行政政处（公路/水利）")){
                    model.addAttribute("pass_construction4","0");
                }
                if (str.contains("行政政处（城区道路）")){
                    model.addAttribute("pass_construction5","0");
                }
            }
        }
    }
//     if (str.equals("2.农村政处")){
//        model.addAttribute("pass_construction3","0");
//    }
//    //2.民事政处/行政政处（公路/水利）
//                if (str.equals("3.民事政处/行政政处（公路/水利）")){
//        model.addAttribute("pass_construction4","0");
//    }
//    //3.行政政处（城区道路）
//                if (str.equals("4.行政政处（城区道路）")){
//        model.addAttribute("pass_construction5","0");
//    }
    //政策处理类型判断
    public static String getAdministrationJudge(Disclose disclose,String tips){
        if ("".equals(disclose.getAdministrationType()) || disclose.getAdministrationType() == null) {
            tips += "政处类型填写有误;";
            return tips;
        }
        boolean flag = true;
        String[] split = disclose.getAdministrationType().split(";");
        for (String str : split){
            if (!str.equals("2.农村政处")&&!str.equals("3.民事政处/行政政处（公路/水利）")&&!str.equals("4.行政政处（城区道路）")&&!str.equals("1.无")){
                tips+="政处类型填写有误";
                flag = false;
                break;
            }
        }
        if (flag){
            if (!disclose.getAdministrationType().contains("1.无")){
                if ("".equals(disclose.getPolicyHandlingInstructions()) || disclose.getPolicyHandlingInstructions() == null) {
                    tips += "政策处理说明填写有误;";
                }
            }
        }
        return tips;
    }
    //人工审核：判断
    public static String getHumanReviewJudge(Disclose disclose){
        String tips = "";
        if ("".equals(disclose.getRequireName()) || disclose.getRequireName() == null) {
            tips +="项目名称不能为空;";
        }
        if ("".equals(disclose.getCapacity()) || disclose.getCapacity() == null||(!"是".equals(disclose.getCapacity())&&!"否".equals(disclose.getCapacity()))) {
            tips += "设备基础是否可以扩容填写有误;";
        }else {
            if ("是".equals(disclose.getCapacity())){
                if ("".equals(disclose.getSceneSize())||disclose.getSceneSize() == null){
                    tips+="现场设备尺寸不能为空;";
                }
                if ("".equals(disclose.getCapacitySize())||disclose.getCapacitySize()==null){
                    tips+="最多可扩容尺寸不能为空;";
                }
            }
        }
        if ("".equals(disclose.getHiddenDanger()) || disclose.getHiddenDanger() == null||(!"有".equals(disclose.getHiddenDanger())&&!"无".equals(disclose.getHiddenDanger()))) {
            tips += "方案安全隐患填写有误;";
        }
        if ("".equals(disclose.getInfluenceConstruction()) || disclose.getInfluenceConstruction() == null||(!"是".equals(disclose.getInfluenceConstruction())&&!"否".equals(disclose.getInfluenceConstruction()))) {
            tips += "是否存在低压或通信线等影响施工填写有误;";
        }
        if ("".equals(disclose.getProjectRepetition()) || disclose.getProjectRepetition() == null||(!"是".equals(disclose.getProjectRepetition())&&!"否".equals(disclose.getProjectRepetition()))) {
            tips += "立项重复填写有误;";
        }
        if ("".equals(disclose.getImplemented()) || disclose.getImplemented() == null||(!"是".equals(disclose.getImplemented())&&!"否".equals(disclose.getImplemented()))) {
            tips += "是否已实施填写有误;";
        }
        if ("".equals(disclose.getTransformer()) || disclose.getTransformer() == null||(!"有".equals(disclose.getTransformer())&&!"无".equals(disclose.getTransformer()))) {
            tips += "变压器无负荷填写有误;";
        }
        if ("有".equals(disclose.getHiddenDanger()) || "是".equals(disclose.getInfluenceConstruction())|| "是".equals(disclose.getProjectRepetition())|| "是".equals(disclose.getImplemented())|| "无".equals(disclose.getTransformer())) {
            if ("".equals(disclose.getExplanationOfNecessity()) || disclose.getExplanationOfNecessity() == null) {
                tips += "必要性问题说明不能为空;";
            }
        }
        if ("".equals(disclose.getLineName()) || disclose.getLineName() == null||(!"一致".equals(disclose.getLineName())&&!"不一致".equals(disclose.getLineName()))) {
            tips += "线路名称一致性填写有误;";
        }
        if ("".equals(disclose.getQuantities()) || disclose.getQuantities() == null||(!"是".equals(disclose.getQuantities())&&!"否".equals(disclose.getQuantities()))) {
            tips += "工程量是否一致填写有误;";
        }
        if ("".equals(disclose.getRodNumber()) || disclose.getRodNumber() == null||(!"一致".equals(disclose.getRodNumber())&&!"不一致".equals(disclose.getRodNumber()))) {
            tips += "设备、开关、杆号一致性填写有误;";
        }
        if ("不一致".equals(disclose.getLineName())||"否".equals(disclose.getQuantities())||"不一致".equals(disclose.getRodNumber())) {
            if ("".equals(disclose.getInconsistentDescription()) || disclose.getInconsistentDescription() == null) {
                tips += "图纸现场不一致说明不能为空;";
            }
        }
//        if ("".equals(disclose.getAdministrationType()) || disclose.getAdministrationType() == null||(!"1.农村政处".equals(disclose.getAdministrationType())&&!"2.民事政处/行政政处（公路/水利）".equals(disclose.getAdministrationType())&&!"3.行政政处（城区道路）".equals(disclose.getAdministrationType())&&!"4.无".equals(disclose.getAdministrationType()))) {
////            tips += "政处类型填写有误;";
////        }else {
////            if (("1.农村政处".equals(disclose.getAdministrationType())||"2.民事政处/行政政处（公路/水利）".equals(disclose.getAdministrationType())||"3.行政政处（城区道路）".equals(disclose.getAdministrationType()))) {
////                if ("".equals(disclose.getPolicyHandlingInstructions()) || disclose.getPolicyHandlingInstructions() == null) {
////                    tips += "政策处理说明填写有误;";
////                }
////            }
////        }
        tips = getAdministrationJudge(disclose, tips);
        //DTU  安放方式检查
        if ("".equals(disclose.getPlacementMode())||disclose.getPlacementMode() == null||(!"户外".equals(disclose.getPlacementMode())&&!"户内".equals(disclose.getPlacementMode())&&!"无".equals(disclose.getPlacementMode()))){
            tips+="DTU安放方式核查填写有误";
        }else {
            if ("户外".equals(disclose.getPlacementMode())||"户内".equals(disclose.getPlacementMode())){
                if ("".equals(disclose.getProductionDate())||disclose.getProductionDate() == null){
                    tips +="核查环网柜生产日期不能为空;";
                }
            }
        }
        if ("".equals(disclose.getIntegrateModule()) || disclose.getIntegrateModule() == null) {
            tips += "二次是否是集成模块不能为空;";
        }else {
            if (!"是".equals(disclose.getIntegrateModule())&&!"否".equals(disclose.getIntegrateModule())){
                tips += "二次是否是集成模块填写有误;";
            }
        }
        if ("".equals(disclose.getNeedReplace()) || disclose.getNeedReplace() == null) {
            tips += "PT是否需要更换不能为空;";
        }else {
            if (!"是".equals(disclose.getNeedReplace())&&!"否".equals(disclose.getNeedReplace())){
                tips += "PT是否需要更换填写有误;";
            }
        }
        if ("".equals(disclose.getOverallTransformation()) || disclose.getOverallTransformation() == null) {
            tips += "设备是否有整体改造的必要不能为空;";
        }else {
            if (!"是".equals(disclose.getOverallTransformation())&&!"否".equals(disclose.getOverallTransformation())){
                tips += "设备是否有整体改造的必要填写有误;";
            }
        }
        return tips;
    }
}
