package com.app.admin.framework.system.permission.authorization.controller;

import com.app.admin.framework.system.module.view.SysModuleTreeNode;
import com.app.admin.framework.system.permission.authorization.service.SysAdministratorRoleService;
import com.app.admin.framework.system.permission.authorization.service.SysAuthorityService;
import com.app.admin.framework.system.permission.authorization.view.SysAdministratorRoleView;
import com.app.admin.framework.system.permission.role.convert.SysRoleTreeConvert;
import com.app.admin.framework.system.permission.role.model.SysRole;
import com.app.admin.framework.system.permission.role.service.SysRoleService;
import com.app.framework.core.support.auth.LoginUser;
import com.app.framework.core.support.view.form.AjaxResult;
import com.app.framework.core.support.view.tree.ZTreeNode;
import com.app.framework.core.utils.request.SessionUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 权限管理
 */
@Controller
@RequestMapping("/authoriz")
public class AuthorityController {

    @Resource
    SysRoleService sysRoleService;

    @Resource
    SysAdministratorRoleService sysAdministratorRoleService;

    @Resource
    SysAuthorityService sysAuthorityService;

    /**
     * 跳转到列表页面
     */
    @RequestMapping(value = "/main")
    public ModelAndView main(HttpServletRequest request) {
        return new ModelAndView("admin/framework/system/authorization/main.html");
    }

    /**
     * 根据角色id获取模块树
     */
    @RequestMapping("/loadModuleTree")
    public @ResponseBody List<SysModuleTreeNode> loadModuleTree(HttpServletRequest request, Long roleId) {
        LoginUser loginUser = SessionUtil.getCurrentUser(request);
        return sysAuthorityService.selectModuleByRoleId(loginUser, roleId);
    }

    /**
     * 保存角色模块对应关系
     */
    @RequestMapping("/saveRoleModuleRelation")
    public @ResponseBody AjaxResult saveRoleModuleRelation(Long roleId, Long[] moduleIds) {
        sysAuthorityService.saveRoleModuleRelation(roleId, moduleIds);
        return AjaxResult.wrap(true);
    }

    /**
     * 跳转到编辑页面
     */
    @RequestMapping(value = "/editUserRole")
    public ModelAndView editUserRole(HttpServletRequest request, Long userId) {
        request.setAttribute("userId", userId);
        return new ModelAndView("admin/framework/system/authorization/bind_role.html");
    }

    /**
     * 根据用户id获取角色树
     */
    @RequestMapping("/loadUserRoleTree")
    public @ResponseBody List<ZTreeNode> loadUserRoleTree(HttpServletRequest request, Long userId) {
        LoginUser loginUser = SessionUtil.getCurrentUser(request);
        //查询用户已有角色
        List<SysAdministratorRoleView> list = sysAdministratorRoleService.selectByUserId(userId, null);
        List<Long> selectRoleIdList = new ArrayList<>(list.size());
        for (Iterator<SysAdministratorRoleView> iterator = list.iterator(); iterator.hasNext(); ) {
            SysAdministratorRoleView r = iterator.next();
            selectRoleIdList.add(r.getRoleId());
        }
        //所有角色
        List<SysRole> roleList = sysRoleService.selectByEnable(loginUser.getOrganizationId());
        return new SysRoleTreeConvert().convertTree(roleList, selectRoleIdList);
    }

    /**
     * 保存用户角色对应关系
     *
     * @param userId  用户ID
     * @param roleIds 角色ID数组
     */
    @RequestMapping("/saveUserRoleRelation")
    public @ResponseBody AjaxResult saveUserRoleRelation(HttpServletRequest request, Long userId, Long[] roleIds) {
        sysAuthorityService.saveUserRoleRelation(userId, roleIds);
        return AjaxResult.wrap(true);
    }

    /**
     * 保存角色用户对应关系
     *
     * @param roleId  角色ID
     * @param userIds 用户ID数组
     */
    @RequestMapping("/saveRoleUserRelation")
    public @ResponseBody AjaxResult saveRoleUserRelation(HttpServletRequest request, Long roleId, Long[] userIds) {
        sysAuthorityService.saveRoleUserRelation(roleId, userIds);
        return AjaxResult.wrap(true);
    }

    /**
     * 删除角色
     * 同时删除与用户对应关系
     * 同时删除角色模块关联关系
     *
     * @param roleId 角色ID
     */
    @RequestMapping("/deleteRole")
    public @ResponseBody AjaxResult deleteRole(Long roleId) {
        sysAuthorityService.deleteRole(roleId);
        return AjaxResult.wrap(true);
    }
}
