package com.manage.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.manage.annotation.OptionalLog;
import com.manage.annotation.RequiresPermissions;
import com.manage.entity.Power_Menu;
import com.manage.entity.Power_User;
import com.manage.service.cache.CacheManager;
import com.manage.service.PowerService;
import com.manage.service.Power_UserService;
import com.manage.util.Msg;
import com.manage.vo.PowerTree;
import com.manage.vo.Power_UserAndRoleTree;
import com.manage.vo.Power_UserVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

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

/**
 * @ProjectName: power
 * @Package: com.manage.controller
 * @ClassName: RoleAndUserPowerController
 * @Description: java类作用描述
 * @Author: 曾文和
 * @CreateDate: 2019/4/17 15:36
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/4/17 15:36
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
@Controller
@RequestMapping("menuPower/")
public class RoleAndUserPowerController {
    @Autowired
    PowerService powerService;
    @Autowired
    Power_UserService userService;
    /**
      * @ProjectName:    getMenuBySysIdAndRoleIdPowerTree
      * @Description:    根据系统id和角色id获取角色菜单权限
      * @Param           dictId:系统id，roleId角色权限
      * @Return          返回
      * @Author:         曾文和
      * @CreateDate:     2019/4/17 15:37
      * @UpdateUser:     曾文和
      * @UpdateDate:     2019/4/21 15:37
      * @UpdateRemark:   整合角色权限
      * @Version:        1.0
     */
    @RequestMapping(value = "getMenuBySysIdAndUerIdPowerTree",produces = {"text/json;charset=UTF-8"})
    @ResponseBody
    public String getMenuBySysIdAndUerIdPowerTree(String sysFlag, Integer userId, HttpServletRequest request){
        if(StringUtils.isNoneBlank(sysFlag)){
            try {
                List<Power_Menu> menus = powerService.getMenuBySysIdAndUerIdPowerTree(sysFlag,userId);
                if(null != menus && !menus.isEmpty()){
                    Power_User user = (Power_User)request.getSession().getAttribute("CURRENT_USER");
                    for (Power_Menu menu:menus){
                        if(StringUtils.isNotBlank(menu.getMenuName())){
                            if("菜单管理".equals(menu.getMenuName()) && user.getRoleId() != 0){
                                menus.remove(menu);
                                break;
                            }
                        }
                    }
                }
                ObjectMapper mapper = new ObjectMapper();
                String json = mapper.writeValueAsString(menus);
                CacheManager.addExcCount("noExc");
                return json;
            }catch(Exception e){
                CacheManager.addExcCount("exc");
                e.printStackTrace();
                return null;
            }
        }else{
            return null;
        }
    }

    /**
      * @ProjectName:    getMenuBySysIdAndRoleIdPowerTree
      * @Description:    根据系统id和角色id获取角色菜单权限
      * @Param           dictId:系统id，roleId角色权限
      * @Return          返回
      * @Author:         曾文和
      * @CreateDate:     2019/4/17 15:37
      * @UpdateUser:     曾文和
      * @UpdateDate:     2019/4/21 15:37
      * @UpdateRemark:   整合角色权限
      * @Version:        1.0
     */
    @RequestMapping(value = "getMenuBySysIdAndRoleIdPowerTree",produces = {"text/json;charset=UTF-8"})
    @ResponseBody
    public String getMenuBySysIdAndRoleIdPowerTree(String sysFlag,Integer roleId,HttpServletRequest request){
        try {
            List<Power_Menu> menus = powerService.getMenuBySysIdAndRoleIdPowerTree(sysFlag,roleId);
            ObjectMapper mapper = new ObjectMapper();
            String json = mapper.writeValueAsString(menus);
            CacheManager.addExcCount("noExc");
            return json;
        }catch(Exception e){
            CacheManager.addExcCount("exc");
            e.printStackTrace();
            return null;
        }
    }

    /**
      * @ProjectName:    getMethodByMenuIdPowerTree
      * @Description:    根据菜单id获取方法
      * @Param           menuId:菜单id
      * @Return          返回
      * @Author:         曾文和
      * @CreateDate:     2019/4/17 15:37
      * @UpdateUser:     更新者
      * @UpdateDate:     2019/4/17 15:37
      * @UpdateRemark:   更新说明
      * @Version:        1.0
     */
    @RequestMapping("getMethodByMenuIdPowerTree/{menuId}")
    @ResponseBody
    public String getMethodByMenuIdPowerTree(@PathVariable("menuId")Integer menuId){
        try {
            List<Power_Menu> menus = powerService.getMethodByMenuIdPowerTree(menuId);
            ObjectMapper mapper = new ObjectMapper();
            String json = mapper.writeValueAsString(menus);
            CacheManager.addExcCount("noExc");
            return json;
        }catch(Exception e){
            CacheManager.addExcCount("exc");
            e.printStackTrace();
            return null;
        }
    }

   /**
     * @ProjectName:    rolePowerList
     * @Description:    跳转到角色分配页面
     * @Param           无
     * @Return          返回
     * @Author:         曾文和
     * @CreateDate:     2019/4/21 9:39
     * @UpdateUser:     更新者
     * @UpdateDate:     2019/4/21 9:39
     * @UpdateRemark:   更新说明
     * @Version:        1.0
    */
   @OptionalLog(module = "查看",methods = "角色分配菜单页面")
   @RequiresPermissions(value="/menuPower/rolePowerUI")
    @RequestMapping("rolePowerUI")
    public String rolePowerList(){
       CacheManager.addExcCount("noExc");
       return "menu/rolePowerList";
    }

    /**
      * @ProjectName:    getRolePowerTree
      * @Description:    获取系统角色树
      * @Param           无
      * @Return          PowerTree
      * @Author:         曾文和
      * @CreateDate:     2019/4/21 10:00
      * @UpdateUser:     更新者
      * @UpdateDate:     2019/4/21 10:00
      * @UpdateRemark:   更新说明
      * @Version:        1.0
     */
    @RequiresPermissions(value="/menuPower/rolePowerUI")
    @RequestMapping(value = "getRolePowerTree",produces = {"text/json;charset=UTF-8"})
    @ResponseBody
    public String getRolePowerTree(String key){
        try {
            List<Power_UserAndRoleTree> dicts = powerService.getRolePowerTree(key);
            ObjectMapper mapper = new ObjectMapper();
            String json = mapper.writeValueAsString(dicts);
            CacheManager.addExcCount("noExc");
            return json;
        }catch(Exception e){
            CacheManager.addExcCount("exc");
            e.printStackTrace();
            return null;
        }
    }

    /**
     * @ProjectName:    addRoleMenu
     * @Description:    保存角色菜单表
     * @Param           Integer userId:用户id,Integer dictId:系统id,String menus:菜单id集合
     * @Return          PowerTree
     * @Author:         曾文和
     * @CreateDate:     2019/4/21 10:00
     * @UpdateUser:     更新者
     * @UpdateDate:     2019/4/21 10:00
     * @UpdateRemark:   更新说明
     * @Version:        1.0
     */
    @OptionalLog(module = "修改分配",methods = "角色分配菜单",fieldName = "roleName",tableName = "power_role")
    @RequiresPermissions(value="/menuPower/addRoleMenu")
    @RequestMapping("/addRoleMenu")
    @ResponseBody
    public Msg addRoleMenu(Integer roleId,String sysFlag,String menus){
        try {
            //医院管理员不需要保存
            if(roleId != -100){
                powerService.addRoleMenu(roleId,sysFlag,menus);
            }
            CacheManager.addExcCount("noExc");
            return Msg.success();
        }catch(Exception e){
            CacheManager.addExcCount("exc");
            e.printStackTrace();
            return Msg.fail("保存失败,请联系系统管理员!");
        }
    }
/**************************************用户菜单权限操作************************************************/
    /**
      * @ProjectName:    userPowerList
      * @Description:    跳转到用户分配权限页面
      * @Param           传输参数
      * @Return          返回
      * @Author:         曾文和
      * @CreateDate:     2019/4/17 15:37
      * @UpdateUser:     更新者
      * @UpdateDate:     2019/4/17 15:37
      * @UpdateRemark:   更新说明
      * @Version:        1.0
     */
    @OptionalLog(module = "查看",methods = "用户分配菜单页面")
    @RequiresPermissions(value="/menuPower/userPowerUI")
    @RequestMapping("userPowerUI")
    public String userPowerList(){
        CacheManager.addExcCount("noExc");
        return "menu/userPowerList";
    }

    /**
      * @ProjectName:    userPowerList
      * @Description:    获取用户树
      * @Param           传输参数
      * @Return          返回
      * @Author:         曾文和
      * @CreateDate:     2019/4/17 15:37
      * @UpdateUser:     更新者
      * @UpdateDate:     2019/4/17 15:37
      * @UpdateRemark:   更新说明
      * @Version:        1.0
     */
    @RequiresPermissions(value="/menuPower/userPowerUI")
    @RequestMapping(value = "getUserPowerTree",produces = {"text/json;charset=UTF-8"})
    @ResponseBody
    public String getUserPowerTree(String key){
        try {
            List<Power_UserAndRoleTree> dicts = powerService.getUserPowerTree(key);
            ObjectMapper mapper = new ObjectMapper();
            String json = mapper.writeValueAsString(dicts);
            CacheManager.addExcCount("noExc");
            return json;
        }catch(Exception e){
            CacheManager.addExcCount("exc");
            e.printStackTrace();
            return null;
        }
    }

    /**
      * @ProjectName:    getUserTreeByHospitalId
      * @Description:    根据医院id查询用户
      * @Param           传输参数
      * @Return          返回
      * @Author:         曾文和
      * @CreateDate:     2020/02/18
      * @UpdateUser:     更新者
      * @UpdateDate:     2020/02/18
      * @UpdateRemark:   更新说明
      * @Version:        1.0
     */
    @RequestMapping(value = "getUserTreeByHospitalId")
    @ResponseBody
    public String getUserTreeByHospitalId(Integer hospitalId){
        try {
            List<Power_UserAndRoleTree> userListTree = powerService.getUserTreeByHospitalId(hospitalId);
            ObjectMapper mapper = new ObjectMapper();
            String json = mapper.writeValueAsString(userListTree);
            CacheManager.addExcCount("noExc");
            return json;
        }catch(Exception e){
            CacheManager.addExcCount("exc");
            e.printStackTrace();
            return null;
        }
    }

    /**
      * @ProjectName:    getUserTreeByDeptId
      * @Description:    根据科室id查询用户
      * @Param           传输参数
      * @Return          返回
      * @Author:         曾文和
      * @CreateDate:     2020/02/18
      * @UpdateUser:     更新者
      * @UpdateDate:     2020/02/18
      * @UpdateRemark:   更新说明
      * @Version:        1.0
     */
    @RequestMapping(value = "getUserTreeByDeptId")
    @ResponseBody
    public String getUserTreeByDeptId(Integer deptId){
        try {
            List<Power_UserAndRoleTree> userListTree = powerService.getUserTreeByDeptId(deptId);
            ObjectMapper mapper = new ObjectMapper();
            String json = mapper.writeValueAsString(userListTree);
            CacheManager.addExcCount("noExc");
            return json;
        }catch(Exception e){
            CacheManager.addExcCount("exc");
            e.printStackTrace();
            return null;
        }
    }

    /**
     * @ProjectName:    addUserMenu
     * @Description:    保存用户菜单表
     * @Param           Integer userId:用户id,Integer dictId:系统id,String menus:菜单id集合
     * @Return          PowerTree
     * @Author:         曾文和
     * @CreateDate:     2019/4/22 10:00
     * @UpdateUser:     更新者
     * @UpdateDate:     2019/4/22 10:00
     * @UpdateRemark:   更新说明
     * @Version:        1.0
     */
    @OptionalLog(module = "修改分配",methods = "用户分配菜单",fieldName = "userName",tableName = "power_user")
    @RequiresPermissions(value="/menuPower/addUserMenu")
    @RequestMapping("addUserMenu")
    @ResponseBody
    public Msg addUserMenu(Integer userId, String sysFlag, String menus){
        try {
            Power_UserVo user = userService.selectByPrimaryKey(userId);
            //医院管理员不需要保存
            if(user.getRoleId() != -100){
                powerService.addUserMenu(userId,sysFlag,menus);
            }
            CacheManager.addExcCount("noExc");
            return Msg.success();
        }catch(Exception e){
            CacheManager.addExcCount("exc");
            e.printStackTrace();
            return Msg.fail("保存失败,请联系系统管理员!");
        }
    }
/******************************************归属管理操作*********************************************************/
    /**
     * @ProjectName:    getDictPowerTree
     * @Description:    获取系统树
     * @Param           传输参数
     * @Return          返回
     * @Author:         曾文和
     * @CreateDate:     2019/08/05
     * @UpdateUser:     更新者
     * @UpdateDate:     2019/08/05
     * @UpdateRemark:   更新说明
     * @Version:        1.0
     */
    @RequestMapping(value = "getDictPowerTree",produces = {"text/json;charset=UTF-8"})
    @ResponseBody
    public String getDictPowerTree(String key){
        try {
            List<PowerTree> dicts = powerService.getDictPowerTree(key);
            ObjectMapper mapper = new ObjectMapper();
            String json = mapper.writeValueAsString(dicts);
            CacheManager.addExcCount("noExc");
            return json;
        }catch(Exception e){
            CacheManager.addExcCount("exc");
            e.printStackTrace();
            return null;
        }
    }
}
