package cn.hnthyy.thmz.controller.mz;

import cn.hnthyy.thmz.Utils.TokenUtil;
import cn.hnthyy.thmz.comment.UserLoginToken;
import cn.hnthyy.thmz.entity.his.mz.Employee;
import cn.hnthyy.thmz.entity.his.zd.ZdUnitCode;
import cn.hnthyy.thmz.entity.thmz.Menu;
import cn.hnthyy.thmz.entity.thmz.Role;
import cn.hnthyy.thmz.entity.thmz.RoleMenuRelation;
import cn.hnthyy.thmz.entity.thmz.User;
import cn.hnthyy.thmz.service.his.mz.EmployeeService;
import cn.hnthyy.thmz.service.his.zd.ZdUnitCodeService;
import cn.hnthyy.thmz.service.thmz.MenuService;
import cn.hnthyy.thmz.service.thmz.RoleMenuRelationService;
import cn.hnthyy.thmz.service.thmz.RoleService;
import cn.hnthyy.thmz.service.thmz.UserService;
import cn.hnthyy.thmz.vo.RoleMenuRelationVo;
import cn.hnthyy.thmz.vo.RoleMenuVo;
import cn.hnthyy.thmz.vo.RoleVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

@Slf4j
@RestController
public class RoleController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private UserService userService;
    @Autowired
    private RoleMenuRelationService roleMenuRelationService;
    @Autowired
    private MenuService menuService;

    /**
     * 获取所有的角色
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getAllRole", method = {RequestMethod.POST})
    public Map<String, Object> getAllRole() {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            List<RoleVo> roles = roleService.queryAllRole();
            for (RoleVo role : roles) {
                User user = userService.queryUserById(role.getUpdateId());
                role.setUpdateName(user.getUserName());
            }
            resultMap.put("code", 0);
            resultMap.put("message", "查询所有角色成功");
            resultMap.put("data", roles);
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询所有角色失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "查询所有角色失败");
            return resultMap;
        }
    }

    /**
     * 根据主键查询角色
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getRole", method = {RequestMethod.GET})
    public Map<String, Object> getRole(@RequestParam Long id) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            Role role = roleService.queryById(id);
            if (role == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "根据主键查询角色失败，不存在该角色");
                return resultMap;
            }
            resultMap.put("code", 0);
            resultMap.put("message", "根据主键查询角色成功");
            resultMap.put("data", role);
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("根据主键查询角色失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "根据主键查询角色失败");
            return resultMap;
        }
    }


    /**
     * 保存角色信息和与菜单的关系
     *
     * @param roleMenuRelationVo
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/saveRoleMenuRelation", method = {RequestMethod.POST})
    public Map<String, Object> saveRoleMenuRelation(@RequestBody RoleMenuRelationVo roleMenuRelationVo, HttpServletRequest httpServletRequest) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (roleMenuRelationVo == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "保存角色与菜单关系失败,参数为空");
                return resultMap;
            }
            User tokenUser = TokenUtil.getUser(httpServletRequest);
            //保存角色信息
            Role role = roleMenuRelationVo.getRole();
            role.setUpdateId(tokenUser.getId());
            if (role.getId() == null) {
                role.setCreateId(tokenUser.getId());
            }
            roleService.saveRole(role);
            //保存角色菜单信息
            roleMenuRelationService.saveMenuRelation(roleMenuRelationVo, tokenUser.getId());
            resultMap.put("code", 0);
            resultMap.put("message", "保存用户与菜单关系成功");
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("保存用户与菜单关系失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "保存用户与菜单关系失败");
            return resultMap;
        }
    }

    /**
     * 查询角色菜单权限树
     *
     * @param role_id
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getRoleMenuTree", method = {RequestMethod.GET})
    public Map<String, Object> getRoleMenuTree(@RequestParam(required = false) Long role_id) {
        Map<String, Object> resultMap = new HashMap<>();
        List<RoleMenuVo> trees = new ArrayList<RoleMenuVo>();
        try {
            List<RoleMenuRelation> roleMenus = new ArrayList<RoleMenuRelation>();
            if (null != role_id) {//查询角色现有菜单
                roleMenus = roleMenuRelationService.queryByRoleId(role_id);
            }
            List<Menu> menus = menuService.queryAllMenu(null);
            Map<Long, Map<String, Object>> nodesMap = new HashMap<>();
            for (Menu m : menus) {
                RoleMenuVo rm = new RoleMenuVo();
                rm.setId(m.getId());
                rm.setIcon(m.getClassCode());
                rm.setName(m.getName());
                rm.setPId(m.getParentId() == null ? 0 : m.getParentId());
                for (RoleMenuRelation roleMenu : roleMenus) {
                    if(m.getId() == roleMenu.getMenuId()){
                        rm.setChecked(true);
                    }
                }
                trees.add(rm);
            }
            resultMap.put("code", 0);
            resultMap.put("message", "查询菜单树成功");
            resultMap.put("data", trees);
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询菜单树失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "查询菜单树失败");
            return resultMap;
        }
    }

    /**
     * 删除角色
     *
     * @param role_id
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/deleteRole", method = {RequestMethod.GET})
    public Map<String, Object> deleteRole(@RequestParam Long role_id) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            roleService.removeById(role_id);
            resultMap.put("code", 0);
            resultMap.put("message", "删除角色成功");
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询角色失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "删除角色失败");
            return resultMap;
        }
    }
}
