package com.saye.hospitalgd.controller.system;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.saye.hospitalgd.commons.JsonResult;
import com.saye.hospitalgd.commons.log.ExceptionDUtil;
import com.saye.hospitalgd.commons.log.LogUtil;
import com.saye.hospitalgd.model.MenuRole;
import com.saye.hospitalgd.model.ResourceInfo;
import com.saye.hospitalgd.model.Role;
import com.saye.hospitalgd.model.Users;
import com.saye.hospitalgd.service.system.DepartService;
import com.saye.hospitalgd.service.system.MenuService;
import com.saye.hospitalgd.service.system.RoleService;
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.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

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

@Controller
public class RoleController {

	@Autowired
	private RoleService roleService;
	
	@Autowired
	private MenuService menuService;

	@Autowired
	private DepartService departService;
	/**
	 * @description 到角色管理页面
	 * @author thuang       
	 * @created 2019年11月11日 上午10:50:40 
	 * @return
	 */
	@RequestMapping("/toRoleManager")
	public String toRoleManager(ModelMap modelMap){
		
		HashMap<Object,Object> responseMap = new HashMap<Object,Object>();
		responseMap.put("firstNode", "0");
		ResourceInfo resourceInfo=null;
		try {
			resourceInfo = this.menuService.getMenuByRole(responseMap);
		} catch (Exception e) {
			e.printStackTrace();
		}
		modelMap.addAttribute("resourceInfo", resourceInfo);

		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("parent_id","10330701");
		List<HashMap<Object, Object>> departList = this.departService.findDepartByParentId(map);
		for (HashMap<Object, Object> departMap : departList) {
			departMap.put("id",departMap.get("DEPART_ID"));
			departMap.put("title",departMap.get("DEPART_NAME"));
		}
		modelMap.put("departList",departList);
		return "system/roleManager";
	}

	/**
	 * 
	 * @description 查询列表
	 * @author qfqi       
	 * @created 2019年11月5日 下午3:07:41 
	 * @return
	 */
	@RequestMapping("/searchRole")
	@ResponseBody
	public HashMap<String, Object> searchRole(String roleName,String date,Integer page, Integer limit){
		HashMap<String,String> map=new HashMap<String,String>();
		map.put("roleName", roleName);
		map.put("date", date);
		PageHelper.startPage(page, limit);
		PageInfo<Role> appsPageInfo = new PageInfo<Role>(roleService.searchRole(map));
		HashMap<String, Object> map1 = new HashMap<String,Object>();
		map1.put("code", 0);
		map1.put("msg", "你好");
		map1.put("count", appsPageInfo.getTotal());
		map1.put("data", appsPageInfo.getList());

		return map1;
	}


	/**
	 * @description 添加角色
	 * @author thuang       
	 * @created 2019年11月11日 下午3:58:24 
	 * @return
	 */
	@RequestMapping("/insertRoles")
	@ResponseBody
	public HashMap<Object, Object> insertRoles(String roleName,String menuId){
		
		HashMap<String, String> map=new HashMap<String, String>();
		HashMap<Object, Object> response=new HashMap<Object, Object>();
		
		String errCode="0";
		String errMsg="";
		
		Users user = (Users) SecurityUtils.getSubject().getPrincipal();
		
		map.put("roleName",roleName);
		map.put("menuId",menuId);
		String userName = user.getUserName();
		String userTrueName = user.getTrueName();
		map.put("userName", userName);
		map.put("userTrueName", userTrueName);
		
		//新增角色
		try {
			String status = roleService.addRole(map);
			LogUtil.info(this.getClass(),"用户"+ user.getTrueName()+"添加角色：map:"+map.toString());
			if ("true".equals(status)) {
				
			}else {
				errCode="998";
				errMsg=status;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			errCode="999";
			errMsg=e.getMessage();
			LogUtil.error(this.getClass(), "新增角色失败，原因:"+ExceptionDUtil.getDetailExceptionMsg(e));
		}
		
		response.put("errCode", errCode);
		response.put("errMsg", errMsg);
		return response;
	}

	/**
	 * 
	 * @description 删除用户角色
	 * @author qfqi       
	 * @created 2019年11月5日 下午3:07:54 
	 * @return
	 */
	@RequestMapping("/deleteRole")
	@ResponseBody
	public JsonResult deleteRole(String roleId){
		Users user = (Users) SecurityUtils.getSubject().getPrincipal();
		int result=roleService.deleteRole(roleId);
		LogUtil.info(this.getClass(),"用户"+ user.getTrueName()+"删除角色：roleId:"+roleId);
		JsonResult j=new JsonResult();
		if(result>0){
			 j.setState(true);
			 j.setMessage("删除成功！");
		}else{
			j.setState(false);
			j.setMessage("删除失败！");
		}
		return j;
	}
	
	/**
	 * @description 根据角色id获取角色权限菜单id
	 * @author thuang       
	 * @created 2019年11月12日 上午9:51:08 
	 * @return
	 */
	@RequestMapping("/getSelectMenuId")
	@ResponseBody
	public List<String> getSelectMenuId(String roleId){
		
		HashMap<Object, Object> map=new HashMap<Object, Object>();
		map.put("roleId",roleId);
		List<MenuRole> menuIdByRoleId = menuService.getMenuIdByRoleId(map);
		
		List<String> menuIdList=new ArrayList<String>();
		
		for (int i = 0; i < menuIdByRoleId.size(); i++) {
			MenuRole menuRole = menuIdByRoleId.get(i);
			menuIdList.add(menuRole.getMenuId());
		}
		
		return menuIdList;
	}

	/**
	 * 
	 * @description 修改用户角色权限信息
	 * @author qfqi       
	 * @created 2019年11月5日 下午3:08:05 
	 * @return
	 */
	@RequestMapping("/modifyRoleResource")
	@ResponseBody
	public JsonResult modifyRoleResource(String roleId,String roleName,String menuId,String departId,String pointDelete) {
		
		HashMap<String,String> map=new HashMap<String,String>();
		map.put("roleName", roleName);
		map.put("roleId", roleId);
		map.put("menuId", menuId);
		map.put("departId", departId);
		map.put("pointDelete", pointDelete);
		Users user = (Users) SecurityUtils.getSubject().getPrincipal();
		int result=roleService.modifyRoleResource(map);
		LogUtil.info(this.getClass(),"用户"+ user.getTrueName()+"修改用户角色权限信息：map:"+map.toString());
		JsonResult j=new JsonResult();
		if(result>0){
			 j.setState(true);
			 j.setMessage("修改成功！");
		}else{
			j.setState(false);
			j.setMessage("修改失败！");
		}
		return j;
	}
	
	
}
