package com.saye.hospitalgd.controller.system;

import com.saye.hospitalgd.commons.JsonResult;
import com.saye.hospitalgd.commons.date.DateDUtil;
import com.saye.hospitalgd.commons.log.ExceptionDUtil;
import com.saye.hospitalgd.commons.log.LogUtil;
import com.saye.hospitalgd.commons.string.StringDUtil;
import com.saye.hospitalgd.model.Users;
import com.saye.hospitalgd.service.system.DepartService;
import org.apache.shiro.SecurityUtils;
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 java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class DepartController {
	
	@Autowired
	private DepartService departService;
	
	/**
	 * @description 到部门页面
	 * @author thuang       
	 * @created 2019年11月13日 下午5:26:53 
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("/toDepartManager")
	public String toDepartManager(ModelMap modelMap){
		
		String parentId="10330001";
		List<HashMap<Object, Object>> resourceInfo = this.departService.findDepartTreeNodeList(parentId);

		modelMap.addAttribute("departInfo", resourceInfo);
		
		return "system/departManager";
	}

	/**
	 * 
	 * @description 新增部门
	 * @author qfqi       
	 * @created 2019年11月5日 下午3:42:26 
	 * @return
	 */
	@RequestMapping("/addDepart")
	@ResponseBody
	public JsonResult addDepart(@RequestBody Map requestMap){
		HashMap<String,String> map=new HashMap<String,String>();
		map.put("parentId", StringDUtil.changeNullToEmpty(requestMap.get("parentId")));
		map.put("departName", StringDUtil.changeNullToEmpty(requestMap.get("departName")));
		map.put("departId", StringDUtil.changeNullToEmpty(requestMap.get("departId")));

		Users user = (Users) SecurityUtils.getSubject().getPrincipal();
		
		String userName = user.getUserName();
		String userTrueName = user.getTrueName();
		map.put("modifyUserName", userName);
		map.put("modifyTrueName", userTrueName);
		
		String createTime = DateDUtil.getCurrentDate(DateDUtil.yyyy_MM_dd_HH_mm_ss);
		String createDate = DateDUtil.getCurrentDate();
		map.put("createTime", createTime);
		map.put("createDate", createDate);
		map.put("modifyTime",createTime);
		int result=0;
		
		JsonResult j=new JsonResult();
		try {
			result = departService.addDepart(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年11月5日 下午3:45:34 
	 * @return
	 */
	@RequestMapping("/modifyDepart")
	@ResponseBody
	public JsonResult modifyDepart(@RequestBody Map requestMap){
		HashMap<String,String> map=new HashMap<String,String>();
		map.put("departId", StringDUtil.changeNullToEmpty(requestMap.get("departId")));
		map.put("departName", StringDUtil.changeNullToEmpty(requestMap.get("departName")));
		
		Users user = (Users) SecurityUtils.getSubject().getPrincipal();
		
		String userName = user.getUserName();
		String userTrueName = user.getTrueName();
		map.put("modifyUserName", userName);
		map.put("modifyTrueName", userTrueName);
		
		String modifyTime = DateDUtil.getCurrentDate(DateDUtil.yyyy_MM_dd_HH_mm_ss);
		map.put("modifyTime",modifyTime);
		int result=0;
		try {
			result=departService.modifyDepart(map);
			LogUtil.debug(this.getClass(),"用户"+ user.getTrueName()+"修改部门：map:"+map.toString());
		} catch (Exception e) {
			e.printStackTrace();
			LogUtil.error(this.getClass(), "修改部门失败，原因:"+ExceptionDUtil.getDetailExceptionMsg(e));
		
		}
		JsonResult j=new JsonResult();
		if(result>0){
			 j.setState(true);
			 j.setMessage("修改成功！");
		}else{
			j.setState(false);
			j.setMessage("修改失败！");
		}
		return j;
	}
	


	/**
	 * 
	 * @description 删除某部门及其下的所有子结点,并更新其父部门的是否叶子结点信息
	 * @author qfqi       
	 * @created 2019年11月5日 下午3:48:04 
	 * @return
	 */
	@RequestMapping("/removeDepart")
	@ResponseBody
	public JsonResult removeDepart(@RequestBody Map requestMap){
		
		HashMap<String,String> map=new HashMap<String,String>();
		map.put("departId", StringDUtil.changeNullToEmpty(requestMap.get("departId")));
		
		Users user = (Users) SecurityUtils.getSubject().getPrincipal();
		
		String userName = user.getUserName();
		String userTrueName = user.getTrueName();
		map.put("modifyUserName", userName);
		map.put("modifyTrueName", userTrueName);

		String modifyTime = DateDUtil.getCurrentDate(DateDUtil.yyyy_MM_dd_HH_mm_ss);
		map.put("modifyTime",modifyTime);
		int result=0;
		try {
			result=departService.removeDepart(map);
			LogUtil.debug(this.getClass(),"用户"+ user.getTrueName()+"删除部门：map:"+map.toString());
		} catch (Exception e) {
			e.printStackTrace();
			LogUtil.error(this.getClass(), "删除部门失败，原因:"+ExceptionDUtil.getDetailExceptionMsg(e));
		
		}
		
		JsonResult j=new JsonResult();
		if(result>0){
			 j.setState(true);
			 j.setMessage("删除成功！");
		}else{
			j.setState(false);
			j.setMessage("删除失败！");
		}
		return j;
	}

	/**
	 * @description: 根据上级部门查询部门
	 * @author thuang
	 * @date 2021/7/8 10:10
	 * @version 1.0
	 */
	@RequestMapping("/findDepartByParentId")
	@ResponseBody
	public HashMap<Object,Object> findDepartByParentId(@RequestBody HashMap<Object,Object> map){
		HashMap<Object,Object> responseMap=new HashMap<>();
		String errCode="0";
		String errMsg="";

		try {
			String parentId=StringDUtil.changeNullToEmpty(map.get("parentId"));
			List<HashMap<Object, Object>> resourceInfo = this.departService.findDepartTreeNodeList(parentId);

			responseMap.put("resourceInfo",resourceInfo);
		}catch (Exception e){
			e.printStackTrace();
		}

		responseMap.put("errCode",errCode);
		responseMap.put("errMsg",errMsg);

		return  responseMap;
	}
}
