package com.saye.hrs.controller.system;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.saye.hrs.commons.JsonResult;
import com.saye.hrs.commons.date.DateDUtil;
import com.saye.hrs.commons.log.ExceptionDUtil;
import com.saye.hrs.commons.log.LogUtil;
import com.saye.hrs.commons.page.PageUtil;
import com.saye.hrs.commons.page.TemplatePage;
import com.saye.hrs.commons.string.StringDUtil;
import com.saye.hrs.commons.uuid.UUIDGenerator;
import com.saye.hrs.model.Dicinfo;
import com.saye.hrs.model.ResourceInfo;
import com.saye.hrs.model.Users;
import com.saye.hrs.service.system.DicinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class DicinfoController {
	
	@Autowired
	private DicinfoService dicinfoService;
	
	/**
	 * 
	 * @description 跳转到字典管理页面
	 * @author qfqi       
	 * @created 2019年12月23日 下午1:47:21 
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("/toDicinfoManager")
	public String toDepartManager(ModelMap modelMap){
		
		String parentCode="0";
		List<Dicinfo> list= dicinfoService.findDicinfoTreeNodeList(parentCode);
		  ResourceInfo resource = new ResourceInfo();
		    resource.setTitle("字典");
			resource.setId("0");
			for (Dicinfo dicinfo : list) {
				//生成主节点
				 ResourceInfo resourceInfo = new ResourceInfo();
				resourceInfo.setTitle(dicinfo.getDicname());
				resourceInfo.setId(dicinfo.getDiccode());
				resourceInfo.setSysid(dicinfo.getParentCode());
				resource.getChildren().add(resourceInfo);
			}
		String jsonString = JSONObject.toJSONString(resource);
		modelMap.addAttribute("departInfo", jsonString);
		return "system/dicinfoManage";
	}
	
	/**
	 * 
	 * @description 新增字典
	 * @author qfqi       
	 * @created 2019年12月19日 下午5:20:28 
	 * @param requestMap
	 * @return
	 */
	@RequestMapping("/dicinfoManager/addDicinfoManager")
	@ResponseBody
	public JsonResult addDicinfoManager(HttpServletRequest request, String dicname,String diccode,String dicvalue,String parentCode,String sortNo){
		HashMap<String,String> map=new HashMap<String,String>();
		JsonResult j=new JsonResult();
		map.put("dicname", dicname);
		map.put("dicvalue", dicvalue);
		map.put("parentCode", parentCode);
		map.put("sortNo", sortNo);
		
		
		if(diccode!=null && !"".equals(diccode)){
			//代表新增的是字典树父级 需要验证字点编码是否重复
			map.put("diccode", diccode);
			List<Dicinfo> list= dicinfoService.findDicinfoBydiccode(diccode);
			if(list.size()>0){
				//代表在相同的父类下有相同的value值，
				j.setState(false);
				j.setMessage("字典编码重复，请重新输入！");
				return j;
			}
		}else{
			//代表新增的是子类，需要查询该父类下是否存在相同的val值
			map.put("diccode", UUIDGenerator.getUUID());
			List<Dicinfo> list= dicinfoService.findDicinfoTreeByCode(map);
			if(list.size()>0){
				//代表在相同的父类下有相同的value值，
				j.setState(false);
				j.setMessage("字典值重复，请重新输入！");
				return j;
			}
		}
		int result=0;
	
		try {
			Users user = (Users) request.getSession().getAttribute("user");
			result = dicinfoService.addDicinfo(map);
			LogUtil.debug(this.getClass(),"用户"+ user.getTrueName()+"新增字典：map:"+map.toString());
		} catch (Exception e) {
			e.printStackTrace();
			LogUtil.error(this.getClass(), "新增字典失败，原因:"+ExceptionDUtil.getDetailExceptionMsg(e));
		}
		if(result>0){
			 j.setState(true);
			 j.setMessage("新增成功！");
		}else{
			j.setState(false);
			j.setMessage("新增失败！");
		}
		return j;
	}
	/**
	 * 
	 * @description 修改字典名称
	 * @author qfqi       
	 * @created 2019年12月19日 下午5:50:45 
	 * @param requestMap
	 * @return
	 */
	@RequestMapping("/dicinfoManager/updateDicinfoManager")
	@ResponseBody
	public JsonResult updateDicinfoManager(HttpServletRequest request, String diccode,String dicname,String parentCode,String dicvalue,String sortNo){
		HashMap<String,String> map=new HashMap<String,String>();
		JsonResult j=new JsonResult();
		map.put("dicname", dicname);
		map.put("diccode", diccode);
		map.put("modifyTime", DateDUtil.getTheCurrentTime());
		map.put("dicvalue", dicvalue);
		map.put("parentCode", parentCode);
		map.put("sortNo", sortNo);
		
		
		int result=0;
		
		try {
			Users user = (Users) request.getSession().getAttribute("user");
			result = dicinfoService.modifyDicinfo(map);
			LogUtil.debug(this.getClass(),"用户"+ user.getTrueName()+"修改字典：map:"+map.toString());
		} catch (Exception e) {
			e.printStackTrace();
			LogUtil.error(this.getClass(), "修改字典失败，原因:"+ExceptionDUtil.getDetailExceptionMsg(e));
		}
		if(result>0){
			 j.setState(true);
			 j.setMessage("修改成功！");
		}else{
			j.setState(false);
			j.setMessage("修改失败！");
		}
		return j;
	}
	
	/**
	 * 
	 * @description 删除字典
	 * @author qfqi       
	 * @created 2019年12月19日 下午5:50:25 
	 * @param requestMap
	 * @return
	 */
	@RequestMapping("/dicinfoManager/deleteDicinfoManager")
	@ResponseBody
	public JsonResult deleteDicinfoManager(HttpServletRequest request, @RequestBody Map requestMap){
		String diccode= StringDUtil.changeNullToEmpty(requestMap.get("diccode"));
		int result=0;
		JsonResult j=new JsonResult();
		try {
			Users user = (Users) request.getSession().getAttribute("user");
			result = dicinfoService.deleteDicinfo(diccode);
			LogUtil.debug(this.getClass(),"用户"+ user.getTrueName()+"删除字典：diccode:"+diccode);
		} catch (Exception e) {
			e.printStackTrace();
			LogUtil.error(this.getClass(), "删除字典失败，原因:"+ExceptionDUtil.getDetailExceptionMsg(e));
		}
		if(result>0){
			 j.setState(true);
			 j.setMessage("删除成功！");
		}else{
			j.setState(false);
			j.setMessage("删除失败！");
		}
		return j;
	}
	//根据父id分页查询字典
	@RequestMapping("/dicinfoManager/selectDicinfoManager")
	@ResponseBody
	public TemplatePage selectDicinfoManager(String parentCode,Integer page,Integer limit){
		PageHelper.startPage(page, limit);
		PageInfo<Dicinfo> appsPageInfo = new PageInfo<Dicinfo>(dicinfoService.findDicinfoTreeNodeList(parentCode));
		return PageUtil.loadJsonPage(appsPageInfo);
	}
	
	//根据上级编码查询所有数据
	@RequestMapping("/dicinfoManager/selectDicinfoListByCode")
	@ResponseBody
	public List<HashMap<Object, Object>> selectDicinfoListByCode(String parent_code){
		
		List<HashMap<Object, Object>> list = this.dicinfoService.selectDicinfoListByCode(parent_code);
		return list;
	}
	
	//根据条件分页查询字典
	@RequestMapping("/dicinfoManager/selectDicinfoPageListByCondition")
	@ResponseBody
	public TemplatePage selectDicinfoPageListByCondition(String parentCode,String dicname,Integer page,Integer limit){
		
		HashMap<String, String> map = new HashMap<String,String>();
		map.put("parentCode", parentCode);
		map.put("dicname", dicname);
		PageHelper.startPage(page, limit);
		PageInfo<HashMap<Object, Object>> appsPageInfo = new PageInfo<HashMap<Object, Object>>(dicinfoService.selectDicinfoListByCondition(map));
		return PageUtil.loadJsonPage(appsPageInfo);
	}
	
	//根据条件查询所有字典
	@RequestMapping("/dicinfoManager/selectDicinfoListByCondition")
	@ResponseBody
	public List<HashMap<Object, Object>> selectDicinfoListByCondition(@RequestBody HashMap<String, String> map){
		
		List<HashMap<Object, Object>> list = this.dicinfoService.selectDicinfoListByCondition(map);
		return list;
	}
	/**
	 * 
	 * @description 获取最大字典顺序
	 * @author qfqi       
	 * @created 2021年1月13日 下午2:56:03 
	 * @param requestMap
	 * @return
	 */
	@RequestMapping("/dicinfoManager/getMaxDicValue")
	@ResponseBody
	public JsonResult getMaxDicValue(@RequestBody Map requestMap){
		String diccode= StringDUtil.changeNullToEmpty(requestMap.get("diccode"));
		
		JsonResult j=new JsonResult();
		try {
			HashMap<String, String> map = new HashMap<String,String>();
			map.put("parentCode", diccode);
			HashMap<Object, Object> result = dicinfoService.getMaxDicValue(map);
			j.setState(true);
			j.setData(result.get("MAXDICVALUE"));
			j.setMessage("成功！");
		} catch (Exception e) {
			j.setState(false);
			j.setMessage("查询字典最大值失败！");
			e.printStackTrace();
			LogUtil.error(this.getClass(), "查询字典最大值失败，原因:"+ExceptionDUtil.getDetailExceptionMsg(e));
		}
	
		return j;
	}
	
}
