/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.sg.managementcenter.constructionProject.service;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.sg.managementcenter.constructionProject.entity.ConstructionProject;
import com.jeeplus.modules.sg.managementcenter.constructionProject.entity.ConstructionProjectTemporary;
import com.jeeplus.modules.sg.managementcenter.constructionProject.mapper.ConstructionProjectTemporaryMapper;
import com.jeeplus.modules.sg.managementcenter.constructionProject.util.RecursiveUtil;
import com.jeeplus.modules.sg.managementcenter.project.entity.Project1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


@Service
@Transactional(readOnly = true)
public class ConstructionProjectTemporaryService extends CrudService<ConstructionProjectTemporaryMapper, ConstructionProjectTemporary> {

	@Autowired
	private ConstructionProjectTemporaryMapper constructionProjectTemporaryMapper;

	@Transactional
	public void insertList(List<ConstructionProjectTemporary> constructionProjectTemporaries,List<String> projectNumbers){
		for (int i = 0; i < constructionProjectTemporaries.size(); i++) {
			for (String s:projectNumbers){
				if (constructionProjectTemporaries.get(i).getProjectNumber().equals(s)){
					constructionProjectTemporaries.get(i).setHtmlStatus("2");
					break;
				}
			}
		}
		constructionProjectTemporaryMapper.insertList(constructionProjectTemporaries);
	}

	@Transactional
	public void insertNoList(List<ConstructionProjectTemporary> constructionProjectTemporaries){
		constructionProjectTemporaryMapper.insertList(constructionProjectTemporaries);
	}
	/*
		安排计划时间：修改状态
	 */
	@Transactional(readOnly = false)
	public void updateStatus(ConstructionProjectTemporary constructionProjectTemporary){
		constructionProjectTemporaryMapper.updateStatus(constructionProjectTemporary);
	}
	@Transactional(readOnly = false)
	public void updatePower(ConstructionProjectTemporary constructionProjectTemporary){
		constructionProjectTemporaryMapper.updatePower(constructionProjectTemporary);
	}
	@Transactional
	public void updateConstruction(List<ConstructionProjectTemporary> constructionProjectTemporaries){
		for (ConstructionProjectTemporary constructionProjectTemporary:constructionProjectTemporaries){
			constructionProjectTemporary.preUpdate();
			constructionProjectTemporaryMapper.updateStatus(constructionProjectTemporary);
		}
	}

	@Transactional(readOnly = false)
	public void updateTime(ConstructionProjectTemporary constructionProjectTemporary){
		constructionProjectTemporary.preUpdate();
		constructionProjectTemporaryMapper.updateTime(constructionProjectTemporary);
	}

	//清空表中所有数据
	@Transactional(readOnly = false)
	public void deleteAll(){
		constructionProjectTemporaryMapper.deleteAll();
	}


	public List<ConstructionProjectTemporary> findTreeList(ConstructionProjectTemporary constructionProjectTemporary){
		return constructionProjectTemporaryMapper.findTreeList(constructionProjectTemporary);
	}

	//新增加操作
	public List<ConstructionProjectTemporary> getAddTreeTable(List<ConstructionProjectTemporary> constructionProjectTemporaries,String ids[]){
		//获取  目前用户当前操作工程的id
		List<ConstructionProjectTemporary> constructionProjectTemporaryList = new ArrayList<ConstructionProjectTemporary>();
		for (ConstructionProjectTemporary constructionProjectTemporary:constructionProjectTemporaries){
			if (!"0".equals(constructionProjectTemporary.getParentId())){
				for (String str:ids){
					if (str.equals(constructionProjectTemporary.getId())){
						constructionProjectTemporaryList.add(constructionProjectTemporary);
						break;
					}
				}
			}
		}
		List<ConstructionProjectTemporary> newCon = new ArrayList<ConstructionProjectTemporary>();
		List<String> powerLineList = getPowerLineList(constructionProjectTemporaryList);//获取停电范围集合
		boolean flag = false;
		for (ConstructionProjectTemporary constructionProjectTemporary:constructionProjectTemporaries){
			flag = false;
			if (!"0".equals(constructionProjectTemporary.getParentId())){
				for (String s:powerLineList){
					if (s.equals(constructionProjectTemporary.getPowerScope())){
						flag = true;
					}
				}
				if (flag){
					newCon.add(constructionProjectTemporary);
				}
			}
		}
		for (ConstructionProjectTemporary constructionProjectTemporary:constructionProjectTemporaries){
			for (ConstructionProjectTemporary construction:newCon){
				if ("0".equals(constructionProjectTemporary.getParentId())){
					if (constructionProjectTemporary.getId().equals(construction.getParentId())){
						newCon.add(constructionProjectTemporary);
						break;
					}
				}
			}
		}
		return newCon;
	}
	//得到目前线路集合
	public List<String> getPowerLineList(List<ConstructionProjectTemporary> constructionProjectTemporaryList){
		List<String> powerLines = new ArrayList<String>();//得到目前存在的线路集合
		String powerLine = "";//停电线路
		String[] powerStr = null;
		boolean flag = false;
		for (ConstructionProjectTemporary constructionProjectTemporary:constructionProjectTemporaryList){
			if (null!=constructionProjectTemporary.getPowerScope()){
				flag = true;
				if (constructionProjectTemporary.getPowerScope().contains(",")){
					powerStr = constructionProjectTemporary.getPowerScope().split(",");
					for (String str : powerStr){
						flag = true;
						for (String strList:powerLines){//如果已经存在
							if (str.equals(strList)){
								flag = false;
							}
						}
						if (flag){
							powerLines.add(str);
						}
					}
				}else {
					flag = true;
					for (String strList1:powerLines){
						if (strList1.equals(constructionProjectTemporary.getPowerScope())){
							flag = false;
						}
					}
					if (flag){
						powerLines.add(constructionProjectTemporary.getPowerScope());
					}
				}
			}
		}
		return powerLines;
	}
	//新增加操作

	//根据项目定义号查询
	public List<ConstructionProjectTemporary> findListTemporary(ConstructionProjectTemporary constructionProjectTemporary){
		List<ConstructionProjectTemporary> list = constructionProjectTemporaryMapper.findAllList(constructionProjectTemporary);
//		String ids = "";
//		List<ConstructionProjectTemporary> constructionProjectTemporaries = null;
//		if (null!=constructionProject && null!=constructionProject.getProjectNumber()){
//			ids = constructionProject.getProjectNumber();
//		}
//		if (!"".equals(ids)){
//			List<String> stringList = new ArrayList<String>();
//			String[] split = ids.split(",");
//			for (String str : split){
//				stringList.add(str);
//			}
//			constructionProjectTemporaries = constructionProjectTemporaryMapper.findListTemporary(stringList);
//		}
		return list;
	}

	//获取临时表存储对象
	public List<ConstructionProjectTemporary> getConstructionProjectPowerLine(List<Project1> projects, Date date, String parentStatus, String childStatus){
		List<Project1> projectList = new ArrayList<Project1>();
		for (Project1 project : projects){
			projectList.add(project);
		}
		List<ConstructionProjectTemporary> stringPowerList = new ArrayList<ConstructionProjectTemporary>();
		HashSet<String> hashSet = new HashSet<String>();//开关集合
		String[] splite = null;
		for (Project1 project : projects){
			if (null!= project.getPowerScope()&&!"".equals(project.getPowerScope())) {
				if (project.getPowerScope().contains("；")) {
					splite = project.getPowerScope().split("；");
					for (String s : splite) {
						hashSet.add(s);
					}
				}else if (project.getPowerScope().contains(";")){
					splite = project.getPowerScope().split(";");
					for (String s1 : splite) {
						hashSet.add(s1);
					}
				}else if (project.getPowerScope().contains(",")){
					splite = project.getPowerScope().split(",");
					for (String s1 : splite) {
						hashSet.add(s1);
					}
				}else if (project.getPowerScope().contains("，")){
					splite = project.getPowerScope().split("，");
					for (String s1 : splite) {
						hashSet.add(s1);
					}
				}else {
					hashSet.add(project.getPowerScope());
				}
			}
		}
		ConstructionProjectTemporary constructionProjectTemporary = new ConstructionProjectTemporary();
		//循环hashset
		for (String set:hashSet){
			//添加父级
			constructionProjectTemporary = new ConstructionProjectTemporary();
			constructionProjectTemporary.setProjectName(set);
			constructionProjectTemporary.setLineName(set);
			constructionProjectTemporary.setProjectNumber(set+"ProjectNumber");
			constructionProjectTemporary.setParentId("0");
			constructionProjectTemporary.setParentIds("0,");
			constructionProjectTemporary.setStatus(parentStatus);
			constructionProjectTemporary.setLineStatus("0");
			stringPowerList.add(constructionProjectTemporary);
			//添加子集
			for (int i = 0; i < projectList.size(); i++) {
				if (null!= projectList.get(i).getPowerScope()){
					if (projectList.get(i).getPowerScope().contains(set)){
						constructionProjectTemporary = new ConstructionProjectTemporary();
						constructionProjectTemporary.setProjectName(projectList.get(i).getProjectName());
						constructionProjectTemporary.setLineName(projectList.get(i).getLineName());
						constructionProjectTemporary.setProjectNumber(projectList.get(i).getProjectNumber());
						constructionProjectTemporary.setPowerFlagPackage(projectList.get(i).getPowerFlagPackage());
						constructionProjectTemporary.setPowerLine(projectList.get(i).getPowerLine());
						constructionProjectTemporary.setPowerScope(projectList.get(i).getPowerScope());
						constructionProjectTemporary.setArrangeTime(date);
						constructionProjectTemporary.setPlanTime(date);
						constructionProjectTemporary.setStatus(childStatus);
						constructionProjectTemporary.setParentId("1");
						constructionProjectTemporary.setLineStatus("1");
						constructionProjectTemporary.setHtmlStatus("0");
						stringPowerList.add(constructionProjectTemporary);
						projectList.remove(i);
						i--;
					}
				}
			}
		}
		//获取id
		for (ConstructionProjectTemporary projectTemporary:stringPowerList){
			projectTemporary.preInsert();
		}
		//获取父级线路集合
		List<ConstructionProject> constructionProjectList = new ArrayList<ConstructionProject>();
		ConstructionProject constructionProject = null;
		//获取项目
		for (ConstructionProjectTemporary projectTemporary:stringPowerList){
			if ("0".equals(projectTemporary.getLineStatus())){
				constructionProject = new ConstructionProject();
				constructionProject.setId(projectTemporary.getId());
				constructionProject.setProjectName(projectTemporary.getProjectName());
				constructionProjectList.add(constructionProject);
			}
		}
		List<ConstructionProjectTemporary> constructionProjectPowerLineChild = getConstructionProjectPowerLineChild(constructionProjectList, stringPowerList);
		List<ConstructionProjectTemporary> qcConstruction = RecursiveUtil.getQcConstruction(constructionProjectPowerLineChild);
		return qcConstruction;
	}

	//补充方法子集
	public List<ConstructionProjectTemporary> getConstructionProjectPowerLineChild(List<ConstructionProject> constructionProjects,List<ConstructionProjectTemporary> constructionProjectTemporaries){
		//线路循环
		String judge = "";
		String conLineName="";
		String proLineName = "";
		for (ConstructionProject construction:constructionProjects){
			conLineName= construction.getProjectName();//获取名字
			for (ConstructionProjectTemporary temporary:constructionProjectTemporaries){//
				proLineName = temporary.getPowerScope();//获取线路
				if ("1".equals(temporary.getLineStatus())){//如果不是线路
					if (proLineName.contains(conLineName)){
						judge = temporary.getParentId();
						if ("1".equals(judge)){
							temporary.setParentId(construction.getId());
							temporary.setParentIds("0,"+construction.getId()+',');
						}else {
							temporary.setParentId(construction.getId()+","+judge);
						}
					}
				}
			}
		}
		return constructionProjectTemporaries;
	}
	public Page<ConstructionProjectTemporary> findPageList(Page<ConstructionProjectTemporary> page, ConstructionProjectTemporary entity) {
		dataRuleFilter(entity);
		entity.setPage(page);
		List<ConstructionProjectTemporary> list = constructionProjectTemporaryMapper.findList(entity);
//		List<ConstructionProjectTemporary> qcList = getQCList(list);
		page.setList(list);
		return page;
	}
	//去重
	public List<ConstructionProjectTemporary> getQCList(List<ConstructionProjectTemporary> constructionProjectTemporaries){
		List<ConstructionProjectTemporary> projectTemporaries = new ArrayList<ConstructionProjectTemporary>();
		boolean flag = false;
		String id = "";
		String idParent = "";
		for (ConstructionProjectTemporary constructionProjectTemporary:constructionProjectTemporaries){
			idParent = constructionProjectTemporary.getId();
			flag = true;
			for (ConstructionProjectTemporary projectTemporary:projectTemporaries){
				if (constructionProjectTemporary.getProjectNumber().equals(projectTemporary.getProjectNumber())){
					id = projectTemporary.getId();
					projectTemporary.setId(id+","+idParent);
					flag = false;
				}
			}
			if (flag){
				projectTemporaries.add(constructionProjectTemporary);
			}
		}
		return projectTemporaries;
	}
}