package com.zcloud.controller.statistics;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zcloud.controller.base.BaseController;
import com.zcloud.entity.PageData;
import com.zcloud.logs.LogAnno;
import com.zcloud.service.statistics.StatisticsService;
import com.zcloud.util.Jurisdiction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/statistics")
public class StatisticsController extends BaseController {
	@Autowired
	private StatisticsService statisticsService;

	@RequestMapping(value="/gostatistics")
	@ResponseBody
	@LogAnno(menuType= "双重预防",menuServer= "统计分析",instructionsOperate = "隐患统计",instructionsType = "列表")
	public Object goEdit() throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		List<JSONObject> checkedIdslist = (List<JSONObject>) JSON.parse(pd.get("checkedIds").toString());
		String checkedIds="";
		if(checkedIdslist.size()>0) {

			for(int i=0;i<checkedIdslist.size();i++) {
				checkedIds+="'"+checkedIdslist.get(i)+"',";
			}
			checkedIds=checkedIds.substring(0, checkedIds.length()-1);
		}
		List<PageData> retultlist =new ArrayList<PageData>();
		PageData namepd=new PageData();
		namepd.put("dataItem", "name");
		namepd.put("dataName", "部门");
		retultlist.add(namepd);
		String sql=" SELECT ";
		String sql1="";
			sql+=" oadep.DEPARTMENT_ID,oadep.PARENT_ID,oadep.name,";
			List<JSONObject> statelist = (List<JSONObject>) JSON.parse(pd.get("statecheckedCities").toString());
			if(statelist.size()>0) {

			for(int i=0;i<statelist.size();i++) {
				PageData deppd=new PageData();
				deppd.put("dataItem", "c"+i);
				deppd.put("dataName", statelist.get(i).getString("name"));
				retultlist.add(deppd);
				if(i+1==statelist.size()) {
					sql+="ifnull(ch"+i+".c,0) as c"+i;
				}else {
					sql+="ifnull(ch"+i+".c,0) as c"+i+",";
				}
				sql1+=" LEFT JOIN (	SELECT count(1) c,HIDDENFINDDEPT FROM vi_hidden_all WHERE STATE = '"+statelist.get(i).getString("id")+"'";

				if(null!=pd.getString("STARTTIME")&&!"".equals(pd.getString("STARTTIME"))) {
					sql1+="and CREATTIME >= '"+pd.getString("STARTTIME");
				}
					if(null!=pd.getString("ENDTIME")&&!"".equals(pd.getString("ENDTIME"))) {
					sql1+="' and CREATTIME  <= '"+pd.getString("ENDTIME")+"'";
				}
				sql1+=" AND CORPINFO_ID = '"+ Jurisdiction.getCORPINFO_ID()+"' GROUP BY HIDDENFINDDEPT) ch"+i+" on ch"+i+".HIDDENFINDDEPT = oadep.DEPARTMENT_ID ";
				}

			sql+=" FROM  oa_department oadep ";
			sql=sql+sql1;
			sql+="where oadep.CORPINFO_ID = '"+ Jurisdiction.getCORPINFO_ID()+"' ";
			if(checkedIdslist.size()>0) {
				sql+=" and oadep.DEPARTMENT_ID in ("+checkedIds+")";
			}


			pd.put("sql", sql);
			}
			List<JSONObject> levellist = (List<JSONObject>) JSON.parse(pd.get("checkedCities").toString());
			if(levellist.size()>0) {//隐患级别
				for(int i=0;i<levellist.size();i++) {
					PageData deppd=new PageData();
					deppd.put("dataItem", "c"+i);
					deppd.put("dataName", levellist.get(i).getString("NAME"));
					retultlist.add(deppd);
					if(i+1==levellist.size()) {
						sql+="ifnull(ch"+i+".c,0) as c"+i;
					}else {
						sql+="ifnull(ch"+i+".c,0) as c"+i+",";
					}
				sql1+=" LEFT JOIN (	SELECT count(1) c,HIDDENFINDDEPT FROM vi_hidden_all WHERE HIDDENLEVEL = '"+levellist.get(i).getString("BIANMA")+"'";
				if(null!=pd.getString("STARTTIME")&&!"".equals(pd.getString("STARTTIME"))) {
					sql1+="and CREATTIME >= '"+pd.getString("STARTTIME");
				}
					if(null!=pd.getString("ENDTIME")&&!"".equals(pd.getString("ENDTIME"))) {
					sql1+="' and CREATTIME  <= '"+pd.getString("ENDTIME")+"'";
				}
				sql1+=" AND CORPINFO_ID = '"+ Jurisdiction.getCORPINFO_ID()+"' GROUP BY HIDDENFINDDEPT) ch"+i+"  on ch"+i+".HIDDENFINDDEPT = oadep.DEPARTMENT_ID ";
				}
				sql+=" FROM  oa_department oadep ";
				sql=sql+sql1;
				sql+="where oadep.CORPINFO_ID = '"+ Jurisdiction.getCORPINFO_ID()+"' ";
				if(checkedIdslist.size()>0) {
					sql+=" and oadep.DEPARTMENT_ID in ("+checkedIds+")";
				}
				pd.put("sql", sql);
			}

			List<JSONObject> typelist = (List<JSONObject>) JSON.parse(pd.get("hiddentypecheckedCities").toString());
				if(typelist.size()>0) {//隐患级别
					for(int i=0;i<typelist.size();i++) {
						PageData deppd=new PageData();
						deppd.put("dataItem", "c"+i);
						deppd.put("dataName", typelist.get(i).getString("NAME"));
						retultlist.add(deppd);
						if(i+1==typelist.size()) {
							sql+="ifnull(ch"+i+".c,0) as c"+i;
						}else {
							sql+="ifnull(ch"+i+".c,0) as c"+i+",";
						}
				sql1+=" LEFT JOIN (	SELECT count(1) c,HIDDENFINDDEPT FROM vi_hidden_all WHERE HIDDENTYPE =  '"+typelist.get(i).getString("BIANMA")+"'";
				if(null!=pd.getString("STARTTIME")&&!"".equals(pd.getString("STARTTIME"))) {
					sql1+="and CREATTIME >= '"+pd.getString("STARTTIME");
				}
					if(null!=pd.getString("ENDTIME")&&!"".equals(pd.getString("ENDTIME"))) {
					sql1+="' and CREATTIME  <= '"+pd.getString("ENDTIME")+"'";
				}
				sql1+=" AND CORPINFO_ID = '"+ Jurisdiction.getCORPINFO_ID()+"' GROUP BY HIDDENFINDDEPT) ch"+i+"  on ch"+i+".HIDDENFINDDEPT = oadep.DEPARTMENT_ID ";
				}
				sql+=" FROM  oa_department oadep ";
				sql=sql+sql1;
				sql+="where oadep.CORPINFO_ID = '"+ Jurisdiction.getCORPINFO_ID()+"'";
				if(checkedIdslist.size()>0) {
					sql+=" and oadep.DEPARTMENT_ID in ("+checkedIds+")";
				}
				pd.put("sql", sql);
			}







//		if(pd.getString("str").equals("2")) {
//			sql+="oadep.dictionaries_id,oadep.name,";
//			if(pd.getString("std").equals("1")) {
//				String td[]=pd.getString("state").split(",");
//			for(int i=0;i<td.length;i++) {
//				sql+="ifnull(ch"+i+".c,0),";
//				sql1+="LEFT JOIN (	SELECT count(1) c,HIDDENFINDDEPT FROM bus_hidden WHERE STATE = '"+td[i]+"'";
//
//				if(pd.getString("STARTTIME")!=null&&pd.getString("STARTTIME")!="") {
//					sql1+="and f.CREATTIME &gt;= "+pd.getString("STARTTIME");
//				}
//				if(pd.getString("ENDTIME")!=null&&pd.getString("ENDTIME")!="") {
//					sql1+="and f.CREATTIME  &lt;= "+pd.getString("ENDTIME");
//				}
//				sql1+=" AND CORPINFO_ID = '"+Jurisdiction.getCORPINFO_ID()+"' GROUP BY HIDDENFINDDEPT) ch on ch.HIDDENFINDDEPT = oadep.BIANMA";
//				}
//
//			sql+=" FROM  oa_department oadep ";
//			sql=sql+sql1;
//			sql+="where oadep.CORPINFO_ID = '"+Jurisdiction.getCORPINFO_ID()+"' and DEPARTMENT_ID in ("+pd.getString("d")+")";
//			pd.put("sql", sql);
//			}
//			if(pd.getString("std").equals("2")) {//隐患级别
//				String td[]=pd.getString("HIDDENLEVEL").split(",");
//				for(int i=0;i<td.length;i++) {
//				sql+="ifnull(ch"+i+".c,0),";
//
//				sql1+="LEFT JOIN (	SELECT count(1) c,HIDDENFINDDEPT FROM bus_hidden WHERE HIDDENLEVEL = '"+td[i]+"'";
//				if(pd.getString("STARTTIME")!=null&&pd.getString("STARTTIME")!="") {
//					sql1+="and f.CREATTIME &gt;= "+pd.getString("STARTTIME");
//				}
//				if(pd.getString("ENDTIME")!=null&&pd.getString("ENDTIME")!="") {
//					sql1+="and f.CREATTIME  &lt;= "+pd.getString("ENDTIME");
//				}
//
//				sql1+=" AND CORPINFO_ID = '"+Jurisdiction.getCORPINFO_ID()+"' GROUP BY HIDDENFINDDEPT) ch on ch.HIDDENFINDDEPT = oadep.DEPARTMENT_ID";
//				}
//				sql+=" FROM  oa_department oadep ";
//				sql=sql+sql1;
//				sql+="where oadep.CORPINFO_ID = '"+Jurisdiction.getCORPINFO_ID()+"' and DEPARTMENT_ID in ("+pd.getString("d")+")";
//				pd.put("sql", sql);
//			}
//			if(pd.getString("std").equals("3")) {//隐患级别
//				String td[]=pd.getString("HIDDENTYPE").split(",");
//				for(int i=0;i<td.length;i++) {
//				sql+="ifnull(ch"+i+".c,0),";
//
//				sql1+="LEFT JOIN (	SELECT count(1) c,HIDDENFINDDEPT FROM bus_hidden WHERE HIDDENTYPE = '"+td[i]+"'";
//				if(pd.getString("STARTTIME")!=null&&pd.getString("STARTTIME")!="") {
//					sql1+="and f.CREATTIME &gt;= "+pd.getString("STARTTIME");
//				}
//				if(pd.getString("ENDTIME")!=null&&pd.getString("ENDTIME")!="") {
//					sql1+="and f.CREATTIME  &lt;= "+pd.getString("ENDTIME");
//				}
//
//				sql1+=" AND CORPINFO_ID = '"+Jurisdiction.getCORPINFO_ID()+"' GROUP BY HIDDENTYPE) ch on ch.HIDDENTYPE = oadep.BIANMA";
//				}
//				sql+=" FROM  sys_dictionaries oadep ";
//				sql=sql+sql1;
//				sql+="where oadep.CORPINFO_ID = '"+Jurisdiction.getCORPINFO_ID()+"' and DEPARTMENT_ID ='5e7cf8620ba54ad89719d0be62133c7a'";
//				pd.put("sql", sql);
//			}
//
//		}
//
//
//
//
//
//		if(pd.getString("str").equals("3")) {
//			sql+="oadep.dictionaries_id,oadep.name,";
//			if(pd.getString("std").equals("1")) {
//				String td[]=pd.getString("state").split(",");
//			for(int i=0;i<td.length;i++) {
//				sql+="ifnull(ch"+i+".c,0),";
//				sql1+="LEFT JOIN (	SELECT count(1) c,HIDDENFINDDEPT FROM bus_hidden WHERE STATE = '"+td[i]+"'";
//
//				if(pd.getString("STARTTIME")!=null&&pd.getString("STARTTIME")!="") {
//					sql1+="and f.CREATTIME &gt;= "+pd.getString("STARTTIME");
//				}
//				if(pd.getString("ENDTIME")!=null&&pd.getString("ENDTIME")!="") {
//					sql1+="and f.CREATTIME  &lt;= "+pd.getString("ENDTIME");
//				}
//				sql1+=" AND CORPINFO_ID = '"+Jurisdiction.getCORPINFO_ID()+"' GROUP BY HIDDENFINDDEPT) ch on ch.HIDDENFINDDEPT = oadep.DEPARTMENT_ID";
//				}
//
//			sql+=" FROM  oa_department oadep ";
//			sql=sql+sql1;
//			sql+="where oadep.CORPINFO_ID = '"+Jurisdiction.getCORPINFO_ID()+"' and PARENT_ID in ("+pd.getString("d")+")";
//			pd.put("sql", sql);
//			}
//			if(pd.getString("std").equals("2")) {//隐患级别
//				String td[]=pd.getString("HIDDENLEVEL").split(",");
//				for(int i=0;i<td.length;i++) {
//				sql+="ifnull(ch"+i+".c,0),";
//
//				sql1+="LEFT JOIN (	SELECT count(1) c,HIDDENFINDDEPT FROM bus_hidden WHERE HIDDENLEVEL = '"+td[i]+"'";
//				if(pd.getString("STARTTIME")!=null&&pd.getString("STARTTIME")!="") {
//					sql1+="and f.CREATTIME &gt;= "+pd.getString("STARTTIME");
//				}
//				if(pd.getString("ENDTIME")!=null&&pd.getString("ENDTIME")!="") {
//					sql1+="and f.CREATTIME  &lt;= "+pd.getString("ENDTIME");
//				}
//
//				sql1+=" AND CORPINFO_ID = '"+Jurisdiction.getCORPINFO_ID()+"' GROUP BY HIDDENFINDDEPT) ch on ch.HIDDENFINDDEPT = oadep.DEPARTMENT_ID";
//				}
//				sql+=" FROM  oa_department oadep ";
//				sql=sql+sql1;
//				sql+="where oadep.CORPINFO_ID = '"+Jurisdiction.getCORPINFO_ID()+"' and DEPARTMENT_ID in ("+pd.getString("d")+")";
//				pd.put("sql", sql);
//			}
//			if(pd.getString("std").equals("3")) {//隐患级别
//				String td[]=pd.getString("HIDDENTYPE").split(",");
//				for(int i=0;i<td.length;i++) {
//				sql+="ifnull(ch"+i+".c,0),";
//
//				sql1+="LEFT JOIN (	SELECT count(1) c,HIDDENFINDDEPT FROM bus_hidden WHERE HIDDENTYPE = '"+td[i]+"'";
//				if(pd.getString("STARTTIME")!=null&&pd.getString("STARTTIME")!="") {
//					sql1+="and f.CREATTIME &gt;= "+pd.getString("STARTTIME");
//				}
//				if(pd.getString("ENDTIME")!=null&&pd.getString("ENDTIME")!="") {
//					sql1+="and f.CREATTIME  &lt;= "+pd.getString("ENDTIME");
//				}
//
//				sql1+=" AND CORPINFO_ID = '"+Jurisdiction.getCORPINFO_ID()+"' GROUP BY HIDDENFINDDEPT) ch on ch.HIDDENFINDDEPT = oadep.DEPARTMENT_ID";
//				}
//				sql+=" FROM  sys_dictionaries oadep ";
//				sql=sql+sql1;
//				sql+="where oadep.CORPINFO_ID = '"+Jurisdiction.getCORPINFO_ID()+"' and PARENT_ID in ("+pd.getString("d")+")";
//				pd.put("sql", sql);
//			}
//
//		}



		System.out.println(pd.getString("sql"));
		List<PageData>	varList = statisticsService.listAll(pd);	//根据ID读取

		List<PageData>	newList =new ArrayList<PageData>();
		List<PageData>	cList =new ArrayList<PageData>();
		for(PageData var :varList) {
			PageData newpd=new PageData();
			if(var.getString("PARENT_ID").equals("0")) {//一级部门
				newpd=var;
				for(PageData pvar :varList) {//二级部门
				if(var.getString("DEPARTMENT_ID").equals(pvar.getString("PARENT_ID"))) {
					List<PageData>	pList =new ArrayList<PageData>();
					for(PageData svar :varList) {//三级部门
						if(pvar.getString("DEPARTMENT_ID").equals(svar.getString("PARENT_ID"))) {
							List<PageData>	fList =new ArrayList<PageData>();
							for(PageData fvar :varList) {//四级部门
								if(svar.getString("DEPARTMENT_ID").equals(fvar.getString("PARENT_ID"))) {
									List<PageData>	fiList =new ArrayList<PageData>();
									for(PageData fivar :varList) {//五级部门
										if(fvar.getString("DEPARTMENT_ID").equals(fivar.getString("PARENT_ID"))) {
											List<PageData>	seList =new ArrayList<PageData>();
											for(PageData sevar :varList) {//六级部门
												if(fivar.getString("DEPARTMENT_ID").equals(sevar.getString("PARENT_ID"))) {

													seList.add(sevar);

												}
												}
											fivar.put("children", seList);
											fiList.add(fivar);

										}
										}
									fvar.put("children", fiList);
									fList.add(fvar);

								}
								}
							svar.put("children", fList);
							pList.add(svar);

						}
						}
					pvar.put("children", pList);
					cList.add(pvar);

				}
				}
				newpd.put("children", cList);
				newList.add(newpd);
			}
		}

		map.put("pd", pd);
		map.put("varList", newList);
		map.put("retultlist", retultlist);
		map.put("result", errInfo);
		return map;
	}
}
