package com.jeeplus.modules.sg.managementcenter.projectPlanArrage.service;

import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.sg.managementcenter.materialproject.entity.MaterialProject;
import com.jeeplus.modules.sg.managementcenter.projectPlanArrage.entity.ProjectAssignment;
import com.jeeplus.modules.sg.managementcenter.projectPlanArrage.entity.ProjectPlanArrage;
import com.jeeplus.modules.sg.managementcenter.projectPlanArrage.entity.ProjectQuantity;
import com.jeeplus.modules.sg.managementcenter.projectPlanArrage.mapper.ProjectPlanArrageMapper;
import com.jeeplus.modules.sg.managementcenter.projectPlanArrage.util.ImportUtil;
import com.jeeplus.modules.sg.managementcenter.projectPlanArrage.util.PackagingUtil;
import com.jeeplus.modules.sys.utils.UserUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.poi.ss.usermodel.Row;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Transactional(readOnly = true)
public class ProjectPlanArrageService extends CrudService<ProjectPlanArrageMapper, ProjectPlanArrage> {
    @Autowired
    private ProjectPlanArrageMapper projectPlanArrageMapper;

    //保存项目计划安排
    @Transactional(readOnly = false)
    public void insertList(List<ProjectPlanArrage> projectPlanArrages){
        for (ProjectPlanArrage planArrage:projectPlanArrages){
            planArrage.preInsert();
        }
        projectPlanArrageMapper.insertList(projectPlanArrages);
    }

    //查询项目计划的里，查询状态未安排的计划的项目
    public List<MaterialProject> findListMaterialProject(MaterialProject materialProject) {
        return projectPlanArrageMapper.findListMaterialProject(materialProject);
    }

    //根据项目名称查找项目
    public List<ProjectQuantity> findListQuantity(List<ProjectPlanArrage> list) {
        return projectPlanArrageMapper.findListQuantity(list);
    }

    //根据项目名称查找人员表
    public List<ProjectAssignment> findListAssignment(List<ProjectPlanArrage> projectPlanArrages) {
        return projectPlanArrageMapper.findListAssignment(projectPlanArrages);
    }

    //主页面删除
    @Transactional(readOnly = false)
    public void deleteMainAll(List<String> list){
        projectPlanArrageMapper.deleteMainAll(list);
    }

    //获取两张表相同的数据
    public List<ProjectPlanArrage> getPlanArrageList(List<ProjectPlanArrage> projectPlanArrageList, List<ProjectAssignment> listAssignment) {
        List<ProjectPlanArrage> planArrages = new ArrayList<ProjectPlanArrage>();
        for (ProjectAssignment projectAssignment : listAssignment) {
            for (ProjectPlanArrage planArrage : projectPlanArrageList) {
                if (projectAssignment.getReserveProjectName().equals(planArrage.getProjectName())) {
                    planArrage.setDistributionNetwork(projectAssignment.getOwnerManager());
                    planArrage.setConstructionUnit(projectAssignment.getConstructionManage());
                    planArrage.setOperationUnit(projectAssignment.getRepUnit());
                    planArrages.add(planArrage);
                    break;
                }
            }
        }
        return planArrages;
    }

    //获取最终待安排的项目list  --树形
    public List<ProjectPlanArrage> getProjectPlanArrageList(MaterialProject materialProject){
        //初始list
        List<ProjectPlanArrage> projectPlanArrages = new ArrayList<ProjectPlanArrage>();
        //得到所有未安排的计划
        List<MaterialProject> listMaterialProject = findListMaterialProject(materialProject);
        //得到所有立项信息的项目--有项目定义号：项目名称
        ProjectPlanArrage projectPlanArrage = new ProjectPlanArrage();
        for (MaterialProject m:listMaterialProject){
            //判断是否存在项目定义号以及项目名称
            if (null!=m.getReserveProjectName()&& null!=m.getProjectId() &&!"".equals(m.getReserveProjectName())&&!"".equals(m.getProjectId())){
                projectPlanArrage = new ProjectPlanArrage();
                projectPlanArrage.setProjectName(m.getReserveProjectName());//获取项目定义号
                projectPlanArrage.setProjectNumber(m.getProjectId());//获取项目id
                projectPlanArrages.add(projectPlanArrage);//添加入集合
            }
        }
        //得到停电范围和信息数据
        List<ProjectQuantity> listQuantity = new ArrayList<ProjectQuantity>();
        if (!projectPlanArrages.isEmpty()){
            listQuantity = findListQuantity(projectPlanArrages);
        }
        //合并未一个实体类集合获得未安排的计划
        List<ProjectPlanArrage> list = new ArrayList<ProjectPlanArrage>();
        String projectName = "";//项目定义号
        String projectId = "";//项目id
        ProjectPlanArrage planArrage = null;
        for (ProjectPlanArrage p: projectPlanArrages){
            projectName = p.getProjectName();//获取项目定义号
            projectId = p.getProjectNumber();//项目定义号
            for (ProjectQuantity q:listQuantity){
                if (q.getReserveProjectName().equals(projectName)){
                    planArrage = new ProjectPlanArrage();
                    planArrage.setProjectName(projectName);//获取名称
                    planArrage.setProjectNumber(projectId);//项目id
                    planArrage.setProjectLine(q.getBlackoutLine());//项目线路
                    planArrage.setProjectPowerRange(q.getOutageRange());//停电范围
                    list.add(planArrage);//添加集合
                    break;
                }
            }
        }
        return list;
    }

    //定义树形基本List状态-包含开关的list--同处一级
    public List<ProjectPlanArrage> getProjectList(List<ProjectPlanArrage> projectPlanArrages){
        //获取所有开关集合
        HashSet<String> outageRanges = new HashSet<String>();
        //获取所有的停电开关
        String[] split;
        List<ProjectPlanArrage> planArrages = new ArrayList<ProjectPlanArrage>();
        ProjectPlanArrage projectPlanArrage = null;
        for (ProjectPlanArrage pr:projectPlanArrages){
            pr.setArrageFlag("0");
            pr.setWhetherStatus("0");//子类
            planArrages.add(pr);
            split = pr.getProjectPowerRange().split(",");
            for (String str:split){
                outageRanges.add(str);
            }
        }
        //加入集合
        for (String range:outageRanges){
            projectPlanArrage = new ProjectPlanArrage();//定义新的对象
            projectPlanArrage.preInsert();
            projectPlanArrage.setProjectName(range);
            projectPlanArrage.setArrageFlag("0");//0-未安排 1-已安排
            projectPlanArrage.setWhetherStatus("1");//父类
            planArrages.add(projectPlanArrage);
        }
        return planArrages;
    }


    //安排项目计划 -- 按钮功能 获取相同id相同以及包含开关的方法--配网项目经理初始安排计划
    public void getUnplannedService(List<ProjectPlanArrage> projectPlanArrages,List<String> idList){
        List<Integer> integerList = new ArrayList<Integer>();
        for (ProjectPlanArrage planArrage:projectPlanArrages){
            if (null!=planArrage.getAssociatedSort()){
                integerList.add(planArrage.getAssociatedSort());//添加进入集合
            }
        }
        Integer assInter = 1;
        if (null!=integerList&&!integerList.isEmpty()){
            Collections.sort(integerList);
            assInter = integerList.get(integerList.size()-1)+1;
        }
        //复制所有数据集合,以供使用
        List<ProjectPlanArrage> copyProjectPlanArrage = PackagingUtil.getCopyProjectPlanArrage(projectPlanArrages);
        //获取用户选择安排计划项目定义号的项目
        List<ProjectPlanArrage> planArragesChild = new ArrayList<ProjectPlanArrage>();//子类集合

        HashSet<String> hashSet = new HashSet<String>();
        //得到集合
        for (String id:idList){
            for (ProjectPlanArrage projectPlanArrage:copyProjectPlanArrage){
                if (id.equals(projectPlanArrage.getId())){
                    if (projectPlanArrage.getWhetherStatus().equals("0")){
                        planArragesChild.add(projectPlanArrage);
                        PackagingUtil.getSplice(projectPlanArrage.getProjectPowerRange(),hashSet);
                        break;
                    }
                }
            }
        }
        PackagingUtil.getRecursive(copyProjectPlanArrage,hashSet);
        String assPackName = "";
        Integer assPackSort = null;
        if (!planArragesChild.isEmpty()){
            for (ProjectPlanArrage pa:projectPlanArrages){
                if (!pa.getWhetherStatus().equals("1")){
                    for (String s:hashSet){
                        if (pa.getProjectPowerRange().contains(s)){
                            if (pa.getArrageFlag().equals("1")){
                                assPackName = pa.getAssociatedBag();
                                assPackSort = pa.getAssociatedSort();
                                break;
                            }
                        }
                    }
                }
            }
        }
        //处理子类集合 -- 如果子类集合不为空
        if (!planArragesChild.isEmpty()){
            for (ProjectPlanArrage pa:projectPlanArrages){
                if (!pa.getWhetherStatus().equals("1")){
                    for (String s:hashSet){
                        if (pa.getProjectPowerRange().contains(s)){
                            if (!"".equals(assPackName)){
                                pa.setArrageFlag("1");
                                pa.setAssociatedBag(assPackName);
                                pa.setAssociatedSort(assPackSort);
                            }else {
                                pa.setArrageFlag("1");
                                pa.setAssociatedBag("关联包项目"+assInter.toString());
                                pa.setAssociatedSort(assInter);
                            }
                            break;
                        }
                    }
                }
            }
        }
    }

    //安排项目计划 -- 按钮功能 获取相同id相同以及包含开关的方法--配网项目经理初始安排计划
    public void getNetWorkUnplannedService(List<ProjectPlanArrage> projectPlanArrages,List<String> idList){
        Date mouth = null;
        for (ProjectPlanArrage planArrage:projectPlanArrages){
            if (null!=planArrage.getProjectMonthDate()){
                mouth = planArrage.getProjectMonthDate();
                break;
            }
        }
        List<Integer> integerList = new ArrayList<Integer>();
        for (ProjectPlanArrage planArrage:projectPlanArrages){
            if (null!=planArrage.getAssociatedSort()){
                integerList.add(planArrage.getAssociatedSort());//添加进入集合
            }
        }
        Integer assInter = 1;
        if (null!=integerList&&!integerList.isEmpty()){
            Collections.sort(integerList);
            assInter = integerList.get(integerList.size()-1)+1;
        }
        //复制所有数据集合,以供使用
        List<ProjectPlanArrage> copyProjectPlanArrage = PackagingUtil.getCopyProjectPlanArrage(projectPlanArrages);
        //获取用户选择安排计划项目定义号的项目
        List<ProjectPlanArrage> planArragesChild = new ArrayList<ProjectPlanArrage>();//子类集合

        HashSet<String> hashSet = new HashSet<String>();
        //得到集合
        for (String id:idList){
            for (ProjectPlanArrage projectPlanArrage:copyProjectPlanArrage){
                if (id.equals(projectPlanArrage.getId())){
                    if (projectPlanArrage.getWhetherStatus().equals("0")){
                        planArragesChild.add(projectPlanArrage);
                        PackagingUtil.getSplice(projectPlanArrage.getProjectPowerRange(),hashSet);
                        break;
                    }
                }
            }
        }
        PackagingUtil.getRecursive(copyProjectPlanArrage,hashSet);
        String assPackName = "";
        Integer assPackSort = null;
        if (!planArragesChild.isEmpty()){
            for (ProjectPlanArrage pa:projectPlanArrages){
                if (!pa.getWhetherStatus().equals("1")){
                    for (String s:hashSet){
                        if (pa.getProjectPowerRange().contains(s)){
                            if (pa.getArrageFlag().equals("1")){
                                assPackName = pa.getAssociatedBag();
                                assPackSort = pa.getAssociatedSort();
                                break;
                            }
                        }
                    }
                }
            }
        }
        //处理子类集合 -- 如果子类集合不为空
        if (!planArragesChild.isEmpty()){
            for (ProjectPlanArrage pa:projectPlanArrages){
                if (!pa.getWhetherStatus().equals("1")){
                    for (String s:hashSet){
                        if (pa.getProjectPowerRange().contains(s)){
                            if (!pa.getArrageFlag().equals("1")){
                                pa.setBackToStauts("2");
                                pa.setArrageFlag("1");
                                pa.setProjectMonthDate(mouth);
                                if (null!=pa.getProjectPowerOut()&&!"".equals(pa.getProjectPowerOut())){
                                    pa.setProjectPowerOut(pa.getProjectPowerOut());
                                }else {
                                    pa.setProjectPowerOut("停电");
                                }
                                if (!"".equals(assPackName)){
                                    pa.setAssociatedBag(assPackName);
                                    pa.setAssociatedSort(assPackSort);
                                }else {
                                    pa.setAssociatedBag("关联包项目"+assInter.toString());
                                    pa.setAssociatedSort(assInter);
                                }
                            }
                            break;
                        }
                    }
                }
            }
        }
    }
    //取消项目计划 -- 按钮功能 获取相同id相同以及包含开关的方法--配网项目经理初始安排计划
    public void getCancelPowerPackage(List<ProjectPlanArrage> projectPlanArrages,List<String> idList){
        for(String id:idList){
            for (ProjectPlanArrage planArrage:projectPlanArrages){
                if (id.equals(planArrage.getId())){
                    planArrage.setPowerPackageSort(null);
                    planArrage.setPowerPackage(null);
                    break;
                }
            }
        }
    }
    //安排项目计划 -- 按钮功能 获取相同id相同以及包含开关的方法--配网项目经理初始安排计划
    public void getUnplannedPowerPackage(List<ProjectPlanArrage> projectPlanArrages,List<String> idList){
        //得到包的数量
        List<Integer> integerList = new ArrayList<Integer>();
        for (ProjectPlanArrage planArrage:projectPlanArrages){
            if (null!=planArrage.getPowerPackageSort()){
                integerList.add(planArrage.getPowerPackageSort());//添加进入集合
            }
        }
        Integer powerInter = 1;
        if (null!=integerList&&!integerList.isEmpty()){
            Collections.sort(integerList);
            powerInter = integerList.get(integerList.size()-1)+1;
        }
        //打停电包
        boolean flag = true;//定义开关
        for (ProjectPlanArrage planArrage:projectPlanArrages){
            flag = false;
            for (String id:idList){
                if (id.equals(planArrage.getId())){
                    flag = true;
                    break;
                }
            }
            if (flag){
                planArrage.setPowerPackage("停电包"+powerInter);
                planArrage.setPowerPackageSort(powerInter);
            }
        }
    }

    //退回操作--第二次配网项目经理
    public void getNetWorkSecond(List<ProjectPlanArrage> projectPlanArrages,List<String> idList){
        for (String id:idList){
            for (ProjectPlanArrage planArrage:projectPlanArrages){
                if (planArrage.getId().equals(id)){
                    planArrage.setAssociatedSort(null);
                    planArrage.setArrageFlag("0");
                    break;
                }
            }
        }
    }
    //发起人备注
    public void getRemarksBack(List<ProjectPlanArrage> projectPlanArrages,List<String> idList,ProjectPlanArrage projectPlanArrage){
        for (ProjectPlanArrage planArrage:projectPlanArrages){
            for (String id:idList){
                if (planArrage.getId().equals(id)){
                    planArrage.setRemarks(projectPlanArrage.getRemarks());
                    break;
                }
            }
        }
    }
    //退回项目计划 -- 单个项目 -- 按钮功能 获取相同id相同以及包含开关的方法
    public void getTheRoBackOneService(List<ProjectPlanArrage> projectPlanArrages,List<String> idList){
       for (String id:idList){
           for (ProjectPlanArrage planArrage:projectPlanArrages){
               if (id.equals(planArrage.getId())){
                   planArrage.setArrageFlag("0");
                   planArrage.setAssociatedSort(null);
                   planArrage.setPowerPackage("");
                   planArrage.setPowerPackageSort(null);
                   break;
               }
           }
       }
    }
    //退回项目计划 -- 按钮功能 获取相同id相同以及包含开关的方法
    public void getTheRoBackService(List<ProjectPlanArrage> projectPlanArrages,List<String> idList){
        //复制所有数据集合,以供使用
        List<ProjectPlanArrage> copyProjectPlanArrage = PackagingUtil.getCopyProjectPlanArrage(projectPlanArrages);
        //获取用户选择安排计划项目定义号的项目
        List<ProjectPlanArrage> planArragesChild = new ArrayList<ProjectPlanArrage>();//子类集合

        HashSet<String> hashSet = new HashSet<String>();
        //得到集合
        for (String id:idList){
            for (ProjectPlanArrage projectPlanArrage:copyProjectPlanArrage){
                if (id.equals(projectPlanArrage.getId())){
                    if (projectPlanArrage.getWhetherStatus().equals("0")){
                        planArragesChild.add(projectPlanArrage);
                        PackagingUtil.getSplice(projectPlanArrage.getProjectPowerRange(),hashSet);
                        break;
                    }
                }
            }
        }
        PackagingUtil.getRecursive(copyProjectPlanArrage,hashSet);
        //处理子类集合 -- 如果子类集合不为空
        if (!planArragesChild.isEmpty()){
            for (ProjectPlanArrage pa:projectPlanArrages){
                if (!pa.getWhetherStatus().equals("1")){
                    for (String s:hashSet){
                        if (pa.getProjectPowerRange().contains(s)){
                            pa.setArrageFlag("0");
                            break;
                        }
                    }
                }
            }
        }
    }

    //检验开关是否为同一停电包--检验功能
    public List<ProjectPlanArrage> inspectionService(String ids,List<ProjectPlanArrage> projectPlans){
        //根据id进行截取
        List<ProjectPlanArrage> inspectionFunction = getInspectionFunction(ids);
        //复制给现有集合
        for (ProjectPlanArrage planArrage1:projectPlans){
            for (ProjectPlanArrage planArrage2:inspectionFunction){
                if (planArrage1.getId().equals(planArrage2.getId())){
                    planArrage1.setProjectPowerOut(planArrage2.getProjectPowerOut());
                }
            }
        }
        //带电
        List<ProjectPlanArrage> planYesArrageList = new ArrayList<ProjectPlanArrage>();
        //停电
        List<ProjectPlanArrage> planNoArrageList = new ArrayList<ProjectPlanArrage>();

        //循环打包
        for (ProjectPlanArrage planArrage:projectPlans){
            //安排的计划
            if (planArrage.getArrageFlag().equals("1")&&planArrage.getWhetherStatus().equals("0")){
                if (null!=planArrage.getProjectPowerOut()&&planArrage.getProjectPowerOut().equals("停电")){
                    planNoArrageList.add(planArrage);
                }else {
                    planYesArrageList.add(planArrage);
                }
            }
        }
        HashSet<String> powerString = getPowerString(planNoArrageList);
        List<ProjectPlanArrage> judgeProject = getJudgeProject(projectPlans, powerString);
        return judgeProject;
    }

    //检验开关是否为同一停电包 -- 定义检验状态
    public List<ProjectPlanArrage> getJudgeProject(List<ProjectPlanArrage> projectPlans,HashSet<String> powerString){
        boolean flag = false;
        for (ProjectPlanArrage planArrage:projectPlans){
            flag = true;
            if (planArrage.getArrageFlag().equals("1")&&planArrage.getWhetherStatus().equals("0")){
                if (planArrage.getProjectPowerOut().equals("带电")){
                    for (String s:powerString){
                        if (planArrage.getProjectPowerRange().contains(s)){
                            planArrage.setHtmlStatus("1");
                            flag = false;
                            break;
                        }
                    }
                }
            }
            if (flag){
                planArrage.setHtmlStatus("0");
            }
        }

//        boolean flag = true;
//        for (String s:powerString){
//            for (ProjectPlanArrage planArrage:projectPlans){
//                //安排的计划
//                flag = true;
//                if (planArrage.getArrageFlag().equals("1")&&planArrage.getWhetherStatus().equals("0")){
//                    if (planArrage.getProjectPowerOut().equals("带电")){
//                        if (planArrage.getProjectPowerRange().contains(s)){
//                            planArrage.setHtmlStatus("1");
//                            flag = false;
//                        }
//                    }
//                }
//                if (flag){
//                    planArrage.setHtmlStatus("0");
//                }
//            }
//        }
        return projectPlans;
    }

    //检验开关是否为同一停电包 -- 停电范围
    public HashSet<String> getPowerString(List<ProjectPlanArrage> projectPlanArrages){
        HashSet<String> hashSet = new HashSet<String>();
        for (ProjectPlanArrage planArrage:projectPlanArrages){
            PackagingUtil.getSplice(planArrage.getProjectPowerRange(),hashSet);
        }
        return hashSet;
    }

    //检验开关是否为同一停电包--获取用户传入的数据功能
    public List<ProjectPlanArrage> getInspectionFunction(String ids){
        //创建list
        List<ProjectPlanArrage> planArrages = new ArrayList<ProjectPlanArrage>();
        String[] idsSplite = null;
        String[] idsSpliteChild = null;
        String id="";
        String powerFlag = "";
        ProjectPlanArrage projectPlanArrage = new ProjectPlanArrage();
        if (null!=ids){
            idsSplite = ids.split(";");
            for (String str : idsSplite){
                id="";
                powerFlag="";
                idsSpliteChild = str.split(",");
                for (String strChild:idsSpliteChild){
                    if (strChild.equals("停电")||strChild.equals("带电")){
                        powerFlag = strChild;
                    }else {
                        id = strChild;
                    }
                }
                if (!"".equals(id)){
                    projectPlanArrage = new ProjectPlanArrage();
                    projectPlanArrage.setId(id);
                    projectPlanArrage.setProjectPowerOut(powerFlag);
                    planArrages.add(projectPlanArrage);
                }
            }
        }
        return planArrages;
    }


    //安排项目计划 -- 按钮功能 获取相同id相同以及包含开关的方法--施工项目经理初始安排计划
    public void getConsPlanArranageService(List<ProjectPlanArrage> projectPlanArrages, List<String> idList, Date mouthDate){
        List<Integer> integerList = new ArrayList<Integer>();
        for (ProjectPlanArrage planArrage:projectPlanArrages){
            if (null!=planArrage.getAssociatedSort()){
                integerList.add(planArrage.getAssociatedSort());//添加进入集合
            }
        }
        Integer assInter = 1;
        if (null!=integerList&&!integerList.isEmpty()){
            Collections.sort(integerList);
            assInter = integerList.get(integerList.size()-1)+1;
        }

        //复制所有数据集合,以供使用
        List<ProjectPlanArrage> copyProjectPlanArrage = PackagingUtil.getCopyProjectPlanArrage(projectPlanArrages);
        //获取用户选择安排计划项目定义号的项目
        List<ProjectPlanArrage> planArragesChild = new ArrayList<ProjectPlanArrage>();//子类集合

        HashSet<String> hashSet = new HashSet<String>();//存放停电范围
        //得到集合
        for (String id:idList){
            for (ProjectPlanArrage projectPlanArrage:copyProjectPlanArrage){
                if (id.equals(projectPlanArrage.getId())){
                    if (projectPlanArrage.getWhetherStatus().equals("0")){
                        planArragesChild.add(projectPlanArrage);
                        PackagingUtil.getSplice(projectPlanArrage.getProjectPowerRange(),hashSet);
                        break;
                    }
                }
            }
        }
        PackagingUtil.getRecursive(copyProjectPlanArrage,hashSet);
        //获取存在的关联包
        String assPackName = "";
        Integer assPackSort = null;
        if (!planArragesChild.isEmpty()){
            for (ProjectPlanArrage pa:projectPlanArrages){
                if (!pa.getWhetherStatus().equals("1")){
                    for (String s:hashSet){
                        if (pa.getProjectPowerRange().contains(s)){
                            if (pa.getArrageFlag().equals("1")){
                                assPackName = pa.getAssociatedBag();
                                assPackSort = pa.getAssociatedSort();
                                break;
                            }
                        }
                    }
                }
            }
        }
        String name = UserUtils.getUser().getName();
        //处理子类集合 -- 如果子类集合不为空
        if (!planArragesChild.isEmpty()){
            for (ProjectPlanArrage pa:projectPlanArrages){
                if (!pa.getWhetherStatus().equals("1")){
                    for (String s:hashSet){
                       if (pa.getConstructionUnit().equals(name)){
                           if (pa.getProjectPowerRange().contains(s)){
                               if (!pa.getArrageFlag().equals("1")){
                                   pa.setArrageFlag("1");
                                   pa.setBackToStauts("2");//说明这是施工单位项目经理新增的状态
                                   pa.setOperationStatus("0");//循序操作
                                   pa.setProjectMonthDate(mouthDate);//自动配置月份
                                   if (null!=pa.getProjectPowerRange()&&pa.getProjectPowerRange().contains("非涉电")){
                                       pa.setProjectPowerOut("带电");
                                   }else {
                                       pa.setProjectPowerOut("停电");
                                   }
                                   //分配关联包
                                   if (!"".equals(assPackName)){
                                       pa.setAssociatedBag("关联包项目"+assPackSort);
                                       pa.setAssociatedSort(assPackSort);
                                   }else {
                                       pa.setAssociatedBag("关联包项目"+assInter);
                                       pa.setAssociatedSort(assInter);
                                   }
                               }
                               break;
                           }
                       }
                    }
                }
            }
        }
    }

    //删除已经安排的项目-----施工单位项目经理 --添加备注
    public void deletePlanArrange(List<ProjectPlanArrage> projectPlans,List<String> strings,String backToText){
        for (String id:strings){
            for (ProjectPlanArrage planArrage:projectPlans){
                if (id.equals(planArrage.getId())){
                    if (null!=planArrage.getBackToStauts()&&planArrage.getBackToStauts().equals("2")){
                        planArrage.setBackToStauts("0");
                        planArrage.setArrageFlag("0");
                    }else {
                        planArrage.setBackToStauts("0");
                        planArrage.setBackToText(backToText);
                    }
                    break;
                }
            }
        }
    }

    //删除已经安排的项目-----施工单位项目经理 --添加备注 --直接删除
    public boolean deletePlanArrangeAll(List<ProjectPlanArrage> projectPlans,List<String> strings){
        boolean flag = true;
        for (String id:strings){
            for (ProjectPlanArrage planArrage:projectPlans){
                if (id.equals(planArrage.getId())){
                    if (null!=planArrage.getBackToStauts()&&planArrage.getBackToStauts().equals("2")){
                    }else {
                        flag = false;
                        break;
                    }
                }
            }
            if (!flag){
                break;
            }
        }
        if (flag){
            for (String id:strings){
                for (ProjectPlanArrage planArrage:projectPlans){
                    if (id.equals(planArrage.getId())){
                        if (null!=planArrage.getBackToStauts()&&planArrage.getBackToStauts().equals("2")){
                            planArrage.setBackToStauts("0");
                            planArrage.setArrageFlag("0");
                        }
                        break;
                    }
                }
            }
        }
        return flag;
    }

    //删除已经安排的项目-----施工单位项目经理 --添加备注
    public boolean deleteJudgePlanArrange(List<ProjectPlanArrage> projectPlans,List<String> strings){
        String name = UserUtils.getUser().getName();
        boolean flag = true;
        for (String id:strings){
            for (ProjectPlanArrage planArrage:projectPlans){
                if (id.equals(planArrage.getId())){
                    if (!planArrage.getConstructionUnit().equals(name)){
                        flag = false;
                        break;
                    }
                }
            }
            if (!flag){
                break;
            }
        }
        return flag;
    }



    //导入数据
    /**
     * 获取表格数据
     * @param importUtil
     * @return List<ErpAccount> 导入表格数据
     */
    public List<ProjectPlanArrage> getMaterProUtil(ImportUtil importUtil) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        int lastRow = importUtil.getLastDataRowNum();
        List<ProjectPlanArrage> list = new ArrayList<ProjectPlanArrage>();
        ProjectPlanArrage projectPlanArrage = null;
        String projectId="";//物料编码
        for(int i=2; i<lastRow;i++){
            projectPlanArrage = new ProjectPlanArrage();
            Row row = importUtil.getRow(i);
            projectId = (String)importUtil.getCellValue(row,0);
            if (StringUtils.isNotBlank(projectId)){
                projectPlanArrage.setProjectNumber(projectId);
                projectPlanArrage.setProjectName((String)importUtil.getCellValue(row,1));
                projectPlanArrage.setProjectPowerRange((String)importUtil.getCellValue(row,2));
                if (null!=(String)importUtil.getCellValue(row,3)&&!"".equals((String)importUtil.getCellValue(row,3))){
                    projectPlanArrage.setProjectDate(format.parse((String)importUtil.getCellValue(row,3)));
                }
                if (null!=(String)importUtil.getCellValue(row,4)&&!"".equals((String)importUtil.getCellValue(row,4))){
                    projectPlanArrage.setProjectMonthDate(format.parse((String)importUtil.getCellValue(row,4)));
                }
                projectPlanArrage.setProjectPowerOut((String)importUtil.getCellValue(row,5));
                projectPlanArrage.setAssociatedBag((String)importUtil.getCellValue(row,6));
                projectPlanArrage.setPowerPackage((String)importUtil.getCellValue(row,7));
                list.add(projectPlanArrage);
            }
        }
        return list;
    }


    //初始安排的计划项目--右侧表格数据 展示计划安排的项目--根据管理包进行排序 -- 以关联包排序
    public void getSortAssPackage(List<ProjectPlanArrage> projectPlanArrages){
        Collections.sort(projectPlanArrages, new Comparator<ProjectPlanArrage>() {
            @Override
            public int compare(ProjectPlanArrage o1, ProjectPlanArrage o2) {
                if (o1.getAssociatedSort()<o2.getAssociatedSort()){
                    return 1;
                }
                if (o1.getAssociatedSort()==o2.getAssociatedSort()){
                    return 0;
                }
                return -1;
            }
        });
    }
    //初始安排的计划项目--右侧表格数据 打停电包验证是否是统一关联包 true 验证通过
    public Boolean getPowerOutagesInspection(List<String> strings,List<ProjectPlanArrage> projectPlanArrages){
        boolean flag = true;
        String assPackage = "";
        for (ProjectPlanArrage planArrage:projectPlanArrages){
            //计划安排的项目
            if (planArrage.getArrageFlag().equals("1")){
                //循环id集合
                for (String s:strings){
                    if (s.equals(planArrage.getId())){ //如果id相等
                        if (!"".equals(assPackage)){
                            if (!assPackage.equals(planArrage.getAssociatedBag())){
                                flag = false;
                                return flag;
                            }
                        }else {
                            assPackage = planArrage.getAssociatedBag();//赋初始值
                        }
                    }
                }
            }
        }
        return flag;
    }
    //初始项目经理发布流程后  --  以停电包进行排序，如果为null 则放到最后
    public void getSortPowerPackage(List<ProjectPlanArrage> projectPlanArrages){
        //最后计划安排：存放  停电包为空的 数据
        List<ProjectPlanArrage> endPlan = new ArrayList<ProjectPlanArrage>();
        for (ProjectPlanArrage projectPlanArrage:projectPlanArrages){
            if (null==projectPlanArrage.getPowerPackageSort()){
                endPlan.add(projectPlanArrage);
            }
        }
        for (int i = 0; i < projectPlanArrages.size(); i++) {
            if (null == projectPlanArrages.get(i).getPowerPackageSort()){
                projectPlanArrages.remove(i);
                i--;
            }
        }
        Collections.sort(projectPlanArrages, new Comparator<ProjectPlanArrage>() {
            @Override
            public int compare(ProjectPlanArrage o1, ProjectPlanArrage o2) {
                if (o1.getPowerPackageSort()>o2.getPowerPackageSort()){
                    return 1;
                }
                if (o1.getPowerPackageSort()==o2.getPowerPackageSort()){
                    return 0;
                }
                return -1;
            }
        });
        for (ProjectPlanArrage planArrage:endPlan){
            projectPlanArrages.add(planArrage);
        }
    }
}
