/**
 * 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.*;
import com.jeeplus.modules.sg.managementcenter.project.entity.Project1;
import org.apache.shiro.authz.annotation.RequiresPermissions;
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 org.springframework.web.multipart.MultipartFile;

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


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

	@Autowired
	private ConstructionProjectService constructionProjectService;
	@Autowired
	private ConstructionProjectTemporaryService constructionProjectTemporaryService;
	
	@ModelAttribute
	public ConstructionProject get(@RequestParam(required=false) String id) {
		ConstructionProject entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = constructionProjectService.get(id);
		}
		if (entity == null){
			entity = new ConstructionProject();
		}
		return entity;
	}
	
	/**
	 * 项目列表页面
	 */
	@RequiresPermissions("sg:managementCenter:constructionProject")
	@RequestMapping(value = {"list", ""})
	public String list(ConstructionProject constructionProject, Model model) {
		model.addAttribute("constructionProject", constructionProject);
		return "modules/sg/managementcenter/constructionProject/constructionList";
	}
	//树形列表页面--首页
	@ResponseBody
	@RequestMapping(value = "bootstrapTreeTable")
	public List<Map<String,Object>> getBootstrapTreeTable(ConstructionProjectTemporary constructionProjectTemporary){
		List<Map<String,Object>> objects = Lists.newArrayList();
        List<ConstructionProjectTemporary> listTemporary = constructionProjectTemporaryService.findListTemporary(constructionProjectTemporary);
        return TreeTableUtil.getBootstrapTreeTable(objects,listTemporary);
	}

		/**
	 * 列表数据
	 */
	@ResponseBody
//	@RequiresPermissions("managementcenter:materialproject:list")
	@RequestMapping(value = "data")
	public Map<String, Object> data(ConstructionProject constructionProject, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ConstructionProject> page = constructionProjectService.findPage(new Page<ConstructionProject>(request, response), constructionProject);
		return getBootstrapData(page);
	}

	/**
	 * 查看，增加，编辑表单页面
	 */
//	@RequiresPermissions(value={"managementcenter:materialproject:view")
	@RequestMapping(value = "form")
	public String form(ConstructionProject constructionProject, Model model) {
		model.addAttribute("constructionProject", constructionProject);
        List<String> projectNumber = constructionProjectService.getProjectNumber(constructionProject);
        List<Project1> projectList = constructionProjectService.allListProject(constructionProject);
        //1
        List<Project1> listProject = constructionProjectService.findListAllProject(projectList,constructionProject);
        //2
        List<Project1> allProjectNo = constructionProjectService.getAllProjectNo(projectList,constructionProject);
        constructionProjectTemporaryService.deleteAll();
        if (null!= listProject && listProject.size()>0) {
            //1
            List<ConstructionProjectTemporary> constructionProjectTemporary = constructionProjectTemporaryService.getConstructionProjectPowerLine(listProject,constructionProject.getArrangeTime(),"2","1");
            //2
            List<ConstructionProjectTemporary> constructionProjectPowerLine = constructionProjectTemporaryService.getConstructionProjectPowerLine(allProjectNo, constructionProject.getArrangeTime(), "0", "0");
            //1
            constructionProjectTemporaryService.insertList(constructionProjectTemporary,projectNumber);
            //2
            constructionProjectTemporaryService.insertNoList(constructionProjectPowerLine);
        }
		return "modules/sg/managementcenter/constructionProject/constructionForm";
	}

	//设计时间页面
	@RequestMapping(value = "formDate")
	public String formDate(ConstructionProject constructionProject, Model model) {
		return "modules/sg/managementcenter/constructionProject/constructionForm";
	}

	//选择月份页面
    @RequiresPermissions("sg:managementCenter:constructionProject:add")
	@RequestMapping(value = "formDateMonth")
	public String formDateMonth(ConstructionProjectTemporary constructionProject,Model model){
		return "modules/sg/managementcenter/constructionProject/constructionDateForm";
	}
	/**
	 * 保存
	 */
	@ResponseBody
	@RequestMapping(value = "save")
	public AjaxJson save(ConstructionProject constructionProject, Model model) throws Exception{
		AjaxJson j = new AjaxJson();
		ConstructionProjectTemporary constructionProjectTemporary = new ConstructionProjectTemporary();
		constructionProjectTemporary.setStatus("1");
		List<ConstructionProjectTemporary> list = constructionProjectTemporaryService.findList(constructionProjectTemporary);
		List<ConstructionProject> constructionProjectList = new ArrayList<ConstructionProject>();
		for (ConstructionProjectTemporary projectTemporary:list){
			constructionProject = new ConstructionProject();
			constructionProject.setProjectNumber(projectTemporary.getProjectNumber());
			constructionProject.setLineName(projectTemporary.getLineName());
			constructionProject.setArrangeTime(projectTemporary.getArrangeTime());
			constructionProject.setSpecificTime(projectTemporary.getPlanTime());
			constructionProject.setProjectName(projectTemporary.getProjectName());
			constructionProject.setProjectNote(projectTemporary.getPowerScope());
			constructionProject.setInstructions(projectTemporary.getFlagPower());
			constructionProject.setRemarks(projectTemporary.getHtmlStatus());
			constructionProjectList.add(constructionProject);
		}
		Project1 project = null;
		List<Project1> projectList = new ArrayList<Project1>();
		for (ConstructionProject constructionProject1:constructionProjectList){
		    project = new Project1();
		    project.setProjectNumber(constructionProject1.getProjectNumber());
		    project.setRezkey("1");
		    projectList.add(project);
        }
		/**
		 * 后台hibernate-validation插件校验
		 */
		//新增或编辑表单保存
//		constructionProjectService.save(constructionProject);//保存
		if (null!=constructionProjectList&&constructionProjectList.size()>0){
			j.setSuccess(true);
			j.setMsg("保存项目成功");
			constructionProjectService.savaList(constructionProjectList);
			constructionProjectTemporaryService.deleteAll();
//			constructionProjectService.updateListProject(projectList);
		}else {
			j.setSuccess(false);
			j.setMsg("无项目");
		}
		return j;
	}
	
	/**
	 * 删除项目
	 */
	@ResponseBody
	@RequiresPermissions("managementcenter:materialproject:del")
	@RequestMapping(value = "delete")
	public AjaxJson delete(ConstructionProject constructionProject) {
		AjaxJson j = new AjaxJson();
		constructionProjectService.delete(constructionProject);
		j.setMsg("删除项目成功");
		return j;
	}
	
	/**
	 * 批量删除
	 */
	@ResponseBody
	@RequiresPermissions("sg:managementCenter:constructionProject:del")
	@RequestMapping(value = "deleteAll")
	public AjaxJson deleteAll(String ids) {
		AjaxJson j = new AjaxJson();
		String idArray[] =ids.split(",");
		for(String id : idArray){
			constructionProjectService.delete(constructionProjectService.get(id));
		}
		j.setMsg("删除项目成功");
		return j;
	}
	


	/**
	 * 导入Excel数据

	 */
	@ResponseBody
//	@RequiresPermissions("managementcenter:materialproject:import")
	@RequestMapping(value = "import")
	public AjaxJson importFile(@RequestParam("file")MultipartFile file, HttpServletResponse response, HttpServletRequest request){
		AjaxJson j = new AjaxJson();
		try {
			ImportUtil importUtil = new ImportUtil(file, 0, 0);
			List<ConstructionProject> constructionProjects = ImportProUtil.getProjectUtil(importUtil);
			constructionProjectService.savaList(constructionProjects);
			j.setMsg( "已成功导入 "+constructionProjects.size()+" 条项目表单记录");
		} catch (Exception e) {
			j.setSuccess(false);
			j.setMsg("导入项目失败！失败信息："+e.getMessage());
		}
		return j;
	}

	/**
	 * 安排计划
	 * @return ajax
	 */
	@ResponseBody
	@RequestMapping(value = "unplanned")
	public Result getUnplanned(String ids,ConstructionProjectTemporary constructionProjectTemporary,String time){
		Result result = new Result();
		try{
			String idArray[] = ids.split(",");//获取传过来的id
			constructionProjectTemporary = new ConstructionProjectTemporary();
			//获取未安排的计划
			List<ConstructionProjectTemporary> list = constructionProjectTemporaryService.findAllList(constructionProjectTemporary);
//			//修改状态的数据集合
//			List<ConstructionProjectTemporary> addTreeTable = constructionProjectTemporaryService.getAddTreeTable(list, idArray);
			List<ConstructionProjectTemporary> addPlanList = RecursiveUtil.getAddPlanList(list, idArray);
			//获取需要修改的状态
			List<ConstructionProjectTemporary> updateConstruction = TreeTableUtil.getUpdateConstruction(addPlanList);
			constructionProjectTemporaryService.updateConstruction(updateConstruction);

			//得到左边树集合 -- 修改过后
			constructionProjectTemporary.setStatus("0");
			List<ConstructionProjectTemporary> allList = constructionProjectTemporaryService.findAllList(constructionProjectTemporary);
			List<Map<String,Object>> objectsTree = Lists.newArrayList();
			result.setSuccess(true);
			result.setMapListTree(TreeTableUtil.getBootstrapTreeTable(objectsTree,allList));
			result.setMessage("安排计划成功");
		}catch (Exception e){
			result.setSuccess(false);
			result.setMessage("安排计划失败");
		}
		return result;
	}

    @ResponseBody
    @RequestMapping(value = "monthTime")
    public AjaxJson monthTimeController(ConstructionProject constructionProject){
        AjaxJson ajaxJson = new AjaxJson();
        try {
            List<ConstructionProject> list = constructionProjectService.findList(constructionProject);
            if (list.size()>0){
                ajaxJson.setSuccess(false);
                ajaxJson.setMsg("该日期已经存在项目");
                return ajaxJson;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        ajaxJson.setSuccess(true);
        ajaxJson.setMsg("操作成功");
        return ajaxJson;
    }
}