package com.saye.hrs.controller.system;

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.model.MenuRole;
import com.saye.hrs.model.ResourceInfo;
import com.saye.hrs.model.Role;
import com.saye.hrs.model.Users;
import com.saye.hrs.service.system.MenuService;
import com.saye.hrs.service.system.RoleService;
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.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
public class RoleController {

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

	/**
	 * @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);
		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(HttpServletRequest request,@RequestBody HashMap<String, String> map){
		
		String errCode="0";
		String errMsg="";
		Users user = (Users) request.getSession().getAttribute("user");
		//新增角色
		try {
			map.put("createTime", DateDUtil.getTheCurrentTime());
			roleService.addRole(map);
			LogUtil.info(this.getClass(),"用户"+ user.getTrueName()+"添加角色：map:"+map.toString());
		} catch (Exception e) {
			errCode="999";
			errMsg=e.getMessage();
			LogUtil.error(this.getClass(), "新增角色失败，原因:"+ExceptionDUtil.getDetailExceptionMsg(e));
		}
		HashMap<Object, Object> resultMap = new HashMap<Object, Object>();
		resultMap.put("errCode", errCode);
		resultMap.put("errMsg", errMsg);
		return resultMap;
	}

	/**
	 * 
	 * @description 删除用户角色
	 * @author qfqi       
	 * @created 2019年11月5日 下午3:07:54 
	 * @return
	 */
	@RequestMapping("/deleteRole")
	@ResponseBody
	public HashMap<Object, Object> deleteRole(HttpServletRequest request, String roleId){
		Users user = (Users) request.getSession().getAttribute("user");
		String errCode="0";
		String errMsg="";
		try {
			roleService.deleteRole(roleId);
			LogUtil.info(this.getClass(),"用户"+ user.getTrueName()+"删除角色：roleId:"+roleId);
		} catch (Exception e) {
			errCode = "999";
			errMsg = "删除失败";
			LogUtil.error(this.getClass(), "删除角色失败，原因:"+ExceptionDUtil.getDetailExceptionMsg(e));
		}
		HashMap<Object, Object> resultMap = new HashMap<Object, Object>();
		resultMap.put("errCode", errCode);
		resultMap.put("errMsg", errMsg);
		return resultMap;
	}
	
	/**
	 * @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;
	}

	@RequestMapping("/getMenuList")
	@ResponseBody
	public ArrayList<ResourceInfo> getMenuList() throws Exception {

		ResourceInfo resourceInfo=null;
		resourceInfo = menuService.getMenuByRole(new HashMap<Object, Object>());
		return resourceInfo.getChildren();
	}

	/**
	 * 
	 * @description 修改用户角色权限信息
	 * @author qfqi       
	 * @created 2019年11月5日 下午3:08:05 
	 * @return
	 */
	@RequestMapping("/modifyRoleResource")
	@ResponseBody
	public JsonResult modifyRoleResource(HttpServletRequest request,@RequestBody HashMap<String, String> map) {
		
		Users user = (Users) request.getSession().getAttribute("user");
		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;
	}
	
	
}
