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.ProjectDailyPlan;
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.ProjectDailyPlanMapper;
import com.jeeplus.modules.sg.managementcenter.projectPlanArrage.mapper.ProjectPlanArrageMapper;
import com.jeeplus.modules.sg.managementcenter.projectPlanArrage.util.ImportUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Row;
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.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;

@Service
@Transactional(readOnly = true)
public class ProjectDailyPlanService extends CrudService<ProjectDailyPlanMapper, ProjectDailyPlan> {
    @Autowired
    private ProjectDailyPlanMapper projectDailyPlanMapper;

    @Transactional(readOnly = false)
    public void updateNoMartailDaily(List<String> ids){
        projectDailyPlanMapper.updateNoMartailDaily(ids);
    }

    @Transactional(readOnly = false)
    public void insertList(List<ProjectDailyPlan> projectDailyPlans){
        for (ProjectDailyPlan projectDailyPlan:projectDailyPlans){
            projectDailyPlan.preInsert();
        }
        projectDailyPlanMapper.insertList(projectDailyPlans);
    }

    @Transactional(readOnly = false)
    public void update(ProjectDailyPlan projectDailyPlan){
        projectDailyPlan.preUpdate();
        projectDailyPlanMapper.update(projectDailyPlan);
    }

    //得到可以安排计划数据 统一存放在List<ProjectDailyPlan>集合当中
    //数据库中根据字段匹配 arrageProject 3 -为已经安排过的日计划 0-1-2为月计划
    public List<ProjectDailyPlan> getProjectDailyPlan(String year){
        //初始化
        List<ProjectDailyPlan> projectDailyPlans = new ArrayList<ProjectDailyPlan>();
        MaterialProject materialProject = new MaterialProject();
        materialProject.setArrageProject("1");
        if (null!=year&&!"".equals(year)){
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy");
            materialProject.setAttributionYear(year);
        }
        //查询项目
        List<MaterialProject> listMaterialProject = projectDailyPlanMapper.findListMaterialProject(materialProject);
        //得到立项信息
        if (!listMaterialProject.isEmpty()){
            ProjectDailyPlan projectDailyPlan =  null;
            for (MaterialProject m:listMaterialProject){
                //判断是否存在项目定义号以及项目名称
                if (null!=m.getReserveProjectName()&& null!=m.getProjectId() &&!"".equals(m.getReserveProjectName())&&!"".equals(m.getProjectId())){
                    projectDailyPlan = new ProjectDailyPlan();
                    projectDailyPlan.setProjectName(m.getReserveProjectName());
                    projectDailyPlan.setProjectNumber(m.getProjectId());
                    projectDailyPlan.setFlagYesElectricity(m.getSecondPower());
                    projectDailyPlans.add(projectDailyPlan);
                }
            }
        }
        //查找开关信息
        //得到停电范围和信息数据
        List<ProjectQuantity> listQuantity = new ArrayList<ProjectQuantity>();
        if (!projectDailyPlans.isEmpty()){
            listQuantity = projectDailyPlanMapper.findListQuantity(projectDailyPlans);
        }


        //得到人员信息
        List<ProjectAssignment> projectAssignments = new ArrayList<ProjectAssignment>();
        if (!projectDailyPlans.isEmpty()){
            projectAssignments = projectDailyPlanMapper.findListAssignment(projectDailyPlans);
        }

        //线路 人员整合一起
        List<ProjectDailyPlan> plans = new ArrayList<ProjectDailyPlan>();
        ProjectDailyPlan projectDailyPlan= null;
        if (!listQuantity.isEmpty()&&!projectAssignments.isEmpty()){
            for (ProjectQuantity projectQuantity:listQuantity){
                for (ProjectAssignment projectAssignment:projectAssignments){
                    if (projectQuantity.getReserveProjectName().equals(projectAssignment.getReserveProjectName())){
                        projectDailyPlan = new ProjectDailyPlan();
                        projectDailyPlan.setProjectName(projectQuantity.getReserveProjectName());//项目名称
                        projectDailyPlan.setProjectPowerOut(projectQuantity.getAssignmentStyle());//是否停电
                        projectDailyPlan.setProjectPowerRange(projectQuantity.getOutageRange());//停电范围
                        projectDailyPlan.setConstructionUnit(projectAssignment.getConstructionManage());//施工单位项目经理
                        projectDailyPlan.setDistributionNetwork(projectAssignment.getOwnerManager());//配网项目经理
                        projectDailyPlan.setOperationUnit(projectAssignment.getRepUnit());//运行单位项目经理
                        plans.add(projectDailyPlan);//添加到集合
                        break;
                    }
                }
            }
        }
        if (!projectDailyPlans.isEmpty()&&!plans.isEmpty()){
            for (ProjectDailyPlan projectDailyPlan1:plans){
                for (ProjectDailyPlan projectDailyPlan2:projectDailyPlans){
                    if (projectDailyPlan1.getProjectName().equals(projectDailyPlan2.getProjectName())){
                        projectDailyPlan1.setProjectNumber(projectDailyPlan2.getProjectNumber());
                        projectDailyPlan1.setFlagYesElectricity(projectDailyPlan2.getFlagYesElectricity());//是否二次设电状态
                        break;
                    }
                }
            }
        }
        return plans;
    }

    //筛选条件 -- 操作者输入的项目名称 或者项目id
    public List<ProjectDailyPlan> getSxDailyPlan(List<ProjectDailyPlan> projectDailyPlans,ProjectDailyPlan projectDailyPlan){
        getIdNameHash(projectDailyPlan,projectDailyPlans);
        return projectDailyPlans;
    }

    public void getIdNameHash(ProjectDailyPlan projectDailyPlan,List<ProjectDailyPlan> projectDailyPlans){
        String[] splitNames = null;
        String[] splitIds = null;
        if (null!=projectDailyPlan.getProjectName()&&!"".equals(projectDailyPlan.getProjectName())){
            splitNames = projectDailyPlan.getProjectName().split(",");
        }
        if (null!=projectDailyPlan.getProjectNumber()&&!"".equals(projectDailyPlan.getProjectNumber())){
            splitIds = projectDailyPlan.getProjectNumber().split(",");
        }
        for (ProjectDailyPlan plans:projectDailyPlans){
            plans.setArrangeDailyPlan("0");
            if (null!=splitNames){
                for (String s:splitNames){
                    if (s.equals(plans.getProjectName())){
                        plans.setArrangeDailyPlan("1");
                        break;
                    }
                }
            }
            if (null!=splitIds){
                for (String id:splitIds){
                    if (id.equals(plans.getProjectNumber())){
                        plans.setArrangeDailyPlan("1");
                        break;
                    }
                }
            }
        }
    }

    //安排日计划
    public void addPlanDaily(List<ProjectDailyPlan> projectPlans,List<String> strings){
        for (ProjectDailyPlan plan:projectPlans){
            for (String str:strings){
                if (str.equals(plan.getId())){
                    plan.setArrangeDailyPlan("1");
                    break;
                }
            }
        }
    }
    //取消日计划
    public void cancelPlanDaily(List<ProjectDailyPlan> projectPlans,List<String> strings){
        for (ProjectDailyPlan plan:projectPlans){
            for (String str:strings){
                if (str.equals(plan.getId())){
                    plan.setArrangeDailyPlan("0");
                    break;
                }
            }
        }
    }

    //检验开关是否为同一停电包--获取用户传入的数据功能
    public List<ProjectDailyPlan> getInspectionFunction(String ids){
        //创建list
        List<ProjectDailyPlan> planArrages = new ArrayList<ProjectDailyPlan>();
        String[] idsSplite = null;
        String[] idsSpliteChild = null;
        String id="";
        String powerFlag = "";
        ProjectDailyPlan projectDailyPlan = new ProjectDailyPlan();
        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)){
                    projectDailyPlan = new ProjectDailyPlan();
                    projectDailyPlan.setId(id);
                    projectDailyPlan.setProjectPowerOut(powerFlag);
                    planArrages.add(projectDailyPlan);
                }
            }
        }
        return planArrages;
    }
    //保存
    @Transactional(readOnly = false)
    public void getPlanDaily(List<ProjectDailyPlan> projectPlans,List<ProjectDailyPlan> idsList){
        List<ProjectDailyPlan> surePlan = new ArrayList<ProjectDailyPlan>();
        //得到停电集合
        for (ProjectDailyPlan plan:projectPlans){
           if (plan.getArrangeDailyPlan().equals("1")){
               for (ProjectDailyPlan ids:idsList){
                   if (plan.getId().equals(ids.getId())){
                       plan.setProjectPowerOut(ids.getProjectPowerOut());
                       surePlan.add(plan);
                       break;
                   }
               }
           }
        }
        //保存日计划项目
        insertList(surePlan);
//        //去除月计划项目
        projectDailyPlanMapper.deleteMonthPlan(surePlan);
//        //修改项目状态
        projectDailyPlanMapper.updateMartailDaily(surePlan);
    }


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