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

import com.google.common.collect.Lists;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.web.BaseController;
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.service.ConstructionProjectService;
import com.jeeplus.modules.sg.managementcenter.constructionProject.service.ConstructionProjectTemporaryService;
import com.jeeplus.modules.sg.managementcenter.constructionProject.util.Result;
import com.jeeplus.modules.sg.managementcenter.constructionProject.util.TreeTableUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;


@Controller
@RequestMapping(value = "${adminPath}/sg/managementCenter/constructionProjectTemporaryController")
public class ConstructionProjectTemporaryController extends BaseController {

	@Autowired
	private ConstructionProjectService constructionProjectService;
	@Autowired
	private ConstructionProjectTemporaryService constructionProjectTemporaryService;
	
	@ModelAttribute
	public ConstructionProjectTemporary get(@RequestParam(required=false) String id) {
		ConstructionProjectTemporary entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = constructionProjectTemporaryService.get(id);
		}
		if (entity == null){
			entity = new ConstructionProjectTemporary();
		}
		return entity;
	}
	
	/**
	 * 项目列表页面
	 */
//	@RequiresPermissions("managementcenter:materialproject:list")
	@RequestMapping(value = {"list", ""})
	public String list(ConstructionProject constructionProject, Model model) {
		model.addAttribute("constructionProject", constructionProject);
		return "modules/sg/managementcenter/constructionProject/constructionDateList";
	}

	@RequestMapping(value = {"listPower", ""})
	public String listPower(ConstructionProject constructionProject, Model model) {
		model.addAttribute("constructionProject", constructionProject);
		return "modules/sg/managementcenter/constructionProject/constructionPowerList";
	}

	/**
	 * 列表数据
	 */
	@ResponseBody
//	@RequiresPermissions("managementcenter:materialproject:list")
	@RequestMapping(value = "data")
	public Map<String, Object> data(ConstructionProjectTemporary constructionProjectTemporary, HttpServletRequest request, HttpServletResponse response, Model model) {
		constructionProjectTemporary.setStatus("1");
		Page<ConstructionProjectTemporary> page = constructionProjectTemporaryService.findPageList(new Page<ConstructionProjectTemporary>(request, response), constructionProjectTemporary);
		return getBootstrapData(page);
	}
	//选择树形列表数据
	@ResponseBody
	@RequestMapping(value = "dataPower")
	public Map<String, Object> dataPower(ConstructionProjectTemporary constructionProjectTemporary, HttpServletRequest request, HttpServletResponse response, Model model) {
		constructionProjectTemporary.setStatus("1");
		Page<ConstructionProjectTemporary> page = constructionProjectTemporaryService.findPageList(new Page<ConstructionProjectTemporary>(request, response), constructionProjectTemporary);
		return getBootstrapData(page);
	}

	@ResponseBody
//	@RequiresPermissions("managementcenter:materialproject:list")
	@RequestMapping(value = "dataPlan")
	public Map<String, Object> dataPlan(ConstructionProjectTemporary constructionProjectTemporary, HttpServletRequest request, HttpServletResponse response, Model model) {
		constructionProjectTemporary.setStatus("1");
		Page<ConstructionProjectTemporary> page = constructionProjectTemporaryService.findPageList(new Page<ConstructionProjectTemporary>(request, response), constructionProjectTemporary);
		return getBootstrapData(page);
	}

	@ResponseBody
	@RequestMapping(value = "addtime")
	public AjaxJson addTime(ConstructionProjectTemporary constructionProjectTemporary){
		AjaxJson j = new AjaxJson();
		try {
			String ids = constructionProjectTemporary.getId();
			if (ids.contains(",")){
				String[] split = ids.split(",");
				ConstructionProjectTemporary cons = new ConstructionProjectTemporary();
				for (String str:split){
					cons = new ConstructionProjectTemporary();
					cons.setId(str);
					cons.setPlanTime(constructionProjectTemporary.getPlanTime());
					constructionProjectTemporaryService.updateStatus(cons);
				}
			}else {
				constructionProjectTemporaryService.updateTime(constructionProjectTemporary);
			}
			j.setSuccess(true);
			j.setMsg("更新成功");
			return j;
		}catch (Exception e){
			e.printStackTrace();
		}
		j.setSuccess(false);
		j.setMsg("时间不符合规范");
		return j;
	}

	@ResponseBody
	@RequestMapping(value = "deleteProject")
	public AjaxJson deleteProject(ConstructionProjectTemporary constructionProjectTemporary){
		AjaxJson j = new AjaxJson();
		try {
			j.setSuccess(true);
			constructionProjectTemporaryService.deleteAll();
			return j;
		}catch (Exception e){
			e.printStackTrace();
		}
		return j;
	}

	/**
	 * 批量删除
	 */
	@ResponseBody
	@RequestMapping(value = "deleteAll")
	public Result deleteAll(String ids) {
		String idArray[] = ids.split(",");
		Result j = new Result();
		try {
			ConstructionProjectTemporary constructionProjectTemporary = new ConstructionProjectTemporary();
			for (String str : idArray){
				constructionProjectTemporary = new ConstructionProjectTemporary();
				constructionProjectTemporary.setId(str);
				constructionProjectTemporary.setStatus("0");
				constructionProjectTemporaryService.updateStatus(constructionProjectTemporary);
			}
			constructionProjectTemporary = new ConstructionProjectTemporary();
			constructionProjectTemporary.setStatus("0");
			List<ConstructionProjectTemporary> allList = constructionProjectTemporaryService.findAllList(constructionProjectTemporary);
			List<Map<String,Object>> objectsTree = Lists.newArrayList();
			j.setSuccess(true);
//			result.setMapList(TreeTableUtil.getBootstrapTreeTable(objects,list1));
			j.setMapListTree(TreeTableUtil.getBootstrapTreeTable(objectsTree,allList));
			j.setSuccess(true);
			j.setMessage("退回成功!");
		}catch (Exception e){
			e.printStackTrace();
		}
		return j;
	}
	//获取停电线路
	@ResponseBody
//	@RequiresPermissions(value={"managementcenter:materialproject:add","managementcenter:materialproject:edit"},logical=Logical.OR)
	@RequestMapping(value = "powerSave")
	public AjaxJson save(String ids,ConstructionProjectTemporary constructionProjectTemporary, Model model) throws Exception{
		AjaxJson ajaxJson = new AjaxJson();
		getJugde(ids,constructionProjectTemporary,model);
		ajaxJson.setSuccess(true);
		return ajaxJson;
	}
	//获取停电线路
	@ResponseBody
//	@RequiresPermissions(value={"managementcenter:materialproject:add","managementcenter:materialproject:edit"},logical=Logical.OR)
	@RequestMapping(value = "powerSaveSure")
	public AjaxJson powerSaveSure(String ids,ConstructionProjectTemporary constructionProjectTemporary, Model model) throws Exception{
		AjaxJson ajaxJson = new AjaxJson();
		getJugde(ids,constructionProjectTemporary,model);
		ajaxJson.setSuccess(true);
		ConstructionProjectTemporary constructionProjectTemporary1 = new ConstructionProjectTemporary();
		constructionProjectTemporary1.setStatus("1");
		List<ConstructionProjectTemporary> list = constructionProjectTemporaryService.findList(constructionProjectTemporary1);
		boolean flag = false;
		for (ConstructionProjectTemporary projectTemporary:list){
			if ("1".equals(projectTemporary.getHtmlStatus())){
				flag = true;
				break;
			}
		}
		if (flag){
			ajaxJson.setSuccess(true);
			ajaxJson.setMsg("1");
		}else {
			ajaxJson.setSuccess(false);
			ajaxJson.setMsg("1");
		}
		return ajaxJson;
	}

	//baocun
	public void getJugde(String ids,ConstructionProjectTemporary constructionProjectTemporary, Model model){
		List<ConstructionProjectTemporary> constructionProjectTemporaryList = new ArrayList<ConstructionProjectTemporary>();
		String[] idsSplite = null;
		String[] idsSpliteChild = null;
		String id="";
		String powerFlag = "";
		ConstructionProjectTemporary constructionProject = new ConstructionProjectTemporary();
		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)){
					constructionProject=new ConstructionProjectTemporary();
					constructionProject.setId(id);
					constructionProject.setFlagPower(powerFlag);
					constructionProjectTemporaryList.add(constructionProject);
				}
			}
			ConstructionProjectTemporary projectTemporary = new ConstructionProjectTemporary();
			projectTemporary.setStatus("1");
			List<ConstructionProjectTemporary> list = constructionProjectTemporaryService.findList(projectTemporary);
			//得到集合
			for (ConstructionProjectTemporary cons1:list){
				for (ConstructionProjectTemporary cons2:constructionProjectTemporaryList){
					if (cons1.getId().equals(cons2.getId())){
						cons1.setFlagPower(cons2.getFlagPower());
						break;
					}
				}
			}
			//停电
			List<ConstructionProjectTemporary> constructionYes = new ArrayList<ConstructionProjectTemporary>();
			//带电
			List<ConstructionProjectTemporary> constructionNo = new ArrayList<ConstructionProjectTemporary>();
			for (ConstructionProjectTemporary project:list){
				if (project.getFlagPower().equals("停电")){
					constructionYes.add(project);//停电
				}else {
					constructionNo.add(project);//带电
				}
				project.setHtmlStatus("0");
				constructionProjectTemporaryService.updatePower(project);
			}
			HashSet<String> powerString = getPowerString(constructionYes);//停电范围
			List<ConstructionProjectTemporary> powerStringList = getPowerStringList(constructionNo, powerString);
//			for (ConstructionProjectTemporary cons:powerStringList){
//				constructionYes.add(cons);
//			}

		}
	}
	//得到停电范围
	public HashSet<String>  getPowerString(List<ConstructionProjectTemporary> constructionProjectTemporaries){
		HashSet<String> hashSet = new HashSet<String>();//开关集合
		String[] splite = null;
		for (ConstructionProjectTemporary constructionProjectTemporary:constructionProjectTemporaries){
			if (null != constructionProjectTemporary.getPowerScope()){
				if (null!=constructionProjectTemporary.getPowerScope()&&!"".equals(constructionProjectTemporary.getPowerScope())) {
					if (constructionProjectTemporary.getPowerScope().contains("；")) {
						splite = constructionProjectTemporary.getPowerScope().split("；");
						for (String s : splite) {
							hashSet.add(s);
						}
					}else if (constructionProjectTemporary.getPowerScope().contains(";")){
						splite = constructionProjectTemporary.getPowerScope().split(";");
						for (String s1 : splite) {
							hashSet.add(s1);
						}
					}else {
						hashSet.add(constructionProjectTemporary.getPowerScope());
					}
				}
			}
		}
		return hashSet;
	}
	//得到带电集合状态
	public List<ConstructionProjectTemporary> getPowerStringList(List<ConstructionProjectTemporary> constructionProjectTemporaries,HashSet<String> strings){
		for (String power:strings){
			for (ConstructionProjectTemporary constructionProjectTemporary:constructionProjectTemporaries){
				if (constructionProjectTemporary.getPowerScope().contains(power)){
					constructionProjectTemporary.setHtmlStatus("1");
					constructionProjectTemporaryService.updatePower(constructionProjectTemporary);
				}
			}
		}
		return constructionProjectTemporaries;
	}

}