package jnpf.authority.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jnpf.authority.service.FtbPermissionRoleAuthorizePersonService;
import jnpf.authority.utils.PermissionsUtils;
import jnpf.base.ActionResult;
import jnpf.base.vo.PageListVO;
import jnpf.model.authority.dto.authorize.FtbPermissionAuthorizeVO;
import jnpf.model.authority.dto.menu.FuntionMenuDTO;
import jnpf.model.authority.dto.person.FtbPermissionRoleBatchDeleteDTO;
import jnpf.model.authority.dto.person.FtbPermissionRolePersonAddDTO;
import jnpf.model.authority.dto.person.FtbPermissionRolePersonRelationDTO;
import jnpf.model.authority.vo.person.FtbAuthorizedPersonnelVO;
import jnpf.model.authority.vo.person.FtbEmployeePermissionPersonnelVO;
import jnpf.model.authority.vo.person.FtbRoleListDropDownVO;
import jnpf.model.authority.vo.person.FunctionVO;
import jnpf.model.cultivate.CultivatePage;
import jnpf.util.NoDataSourceBind;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * web权限管理角色人员授权模块
 *
 * @author xxxxx
 */
@RestController
@RequestMapping("/web/permission-role-authorize-person")
public class FtbPermissionRoleAuthorizePersonController {

    @Resource
    private FtbPermissionRoleAuthorizePersonService ftbPermissionRoleAuthorizePersonService;

    @Resource
    private PermissionsUtils permissionsUtils;

    /**
     * 添加人员授权
     */
    @PostMapping("/add")
    public ActionResult<Void> add(@RequestBody @Validated FtbPermissionRolePersonAddDTO rolePersonAddDTO) {
        ftbPermissionRoleAuthorizePersonService.add(rolePersonAddDTO);
        return ActionResult.success("操作成功");
    }

    /**
     * 批量删除人员授权
     */
    @DeleteMapping("batch-delete")
    public ActionResult<Void> batchDelete(@RequestBody @Validated FtbPermissionRoleBatchDeleteDTO rolePersonDeleteDTO) {
        ftbPermissionRoleAuthorizePersonService.batchDelete(rolePersonDeleteDTO);
        return ActionResult.success("操作成功");
    }

    /**
     * 授权人员列表
     *
     * @param roleId 角色主键id(必传)
     */
    @GetMapping("/authorized-personnel")
    public ActionResult<PageListVO<FtbAuthorizedPersonnelVO>> authorizedPersonnel(CultivatePage cultivatePage, @RequestParam(value = "roleId") String roleId) {
        Page<FtbAuthorizedPersonnelVO> page = cultivatePage.coverCultivatePage();
        PageListVO<FtbAuthorizedPersonnelVO> result = ftbPermissionRoleAuthorizePersonService.authorizedPersonnel(page, roleId);
        return ActionResult.success(result);
    }

    /**
     * 员工权限列表
     *
     * @param userName 员工姓名
     */
    @GetMapping("/employee-permission")
    public ActionResult<PageListVO<FtbEmployeePermissionPersonnelVO>> employeePermission(CultivatePage cultivatePage, String userName) {
        Page<FtbEmployeePermissionPersonnelVO> page = cultivatePage.coverCultivatePage();
        PageListVO<FtbEmployeePermissionPersonnelVO> result = ftbPermissionRoleAuthorizePersonService.employeePermission(page, userName, null);
        return ActionResult.success(result);
    }
    /**
     *  添加个人权限
     */
    @PostMapping("/add-personal-permission")
    public ActionResult<Void> addPersonalPermission(@RequestBody @Validated FtbPermissionRolePersonRelationDTO rolePersonAddDTO) {
        ftbPermissionRoleAuthorizePersonService.addPersonalPermission(rolePersonAddDTO);
        return ActionResult.success("操作成功");
    }
    /**
     *  根据userId查询个人权限
     */
    @GetMapping("/getPersonalPermission")
    public ActionResult<List<FunctionVO>> getPersonalPermission(@RequestParam String userId) {
        List<FunctionVO> list = ftbPermissionRoleAuthorizePersonService.getPersonalPermission(userId);
        return ActionResult.success(list);
    }

    /**
     * 角色列表下拉
     */
    @GetMapping("/role-list-drop")
    public ActionResult<List<FtbRoleListDropDownVO>> roleListDropDown(@RequestParam String userId) {
        List<FtbRoleListDropDownVO> list = ftbPermissionRoleAuthorizePersonService.roleListDropDown(userId);
        return ActionResult.success(list);
    }

    /**
     * 批量编辑员工角色权限
     *
     * @param roleIds 角色主键id集合(必传)
     */
    @PostMapping("/edit-employee-permission/{userId}")
    public ActionResult<Void> editEmployeePermission(@RequestBody @Validated List<String> roleIds, @PathVariable("userId") String userId) {
        ftbPermissionRoleAuthorizePersonService.editEmployeePermission(roleIds, userId);
        return ActionResult.success("操作成功");
    }
    /**
     * 根据userId返回角色未绑定userId集合
     * @param vo userId集合(必传)
     */
    @PostMapping("/getRoleIdsByUserId")
    public ActionResult<List<String>> getRoleIdsByUserId(@RequestBody FtbPermissionAuthorizeVO vo) {
        return ActionResult.success(ftbPermissionRoleAuthorizePersonService.getRoleIdsByUserId(vo));
    }

    /**
     * 权限用户、组织、按钮等内容获取（内部测试）
     */
    @GetMapping("/permission-user-test")
    public List<String> permissionUserTest(String userId, String permissionModule, String category, Integer type) {
        if (type == 1) {
            return permissionsUtils.obtainPersonnelUserIdDataPermissions(userId, permissionModule, category);
        } else if (type == 2) {
            return permissionsUtils.obtainPersonnelOrganizationIdDataPermissions(userId, permissionModule, category);
        } else {
            return permissionsUtils.queryCollectionOfUserButtonPermissions(userId, null, category, permissionModule);
        }
    }

    /**
     * 当前人员所有角色
     */
    @GetMapping("/allRolesOfTheCurrentPerson")
    @NoDataSourceBind
    public ActionResult<List<FtbRoleListDropDownVO>> allRolesOfTheCurrentPerson(@RequestParam("userId") String userId, @RequestParam("tenantId") String tenantId) {
        List<FtbRoleListDropDownVO> list = ftbPermissionRoleAuthorizePersonService.allRolesOfTheCurrentPerson(userId, tenantId);
        return ActionResult.success(list);
    }

    /**
     * 当前登录用户所有权限用户id
     * @return 用户id集合
     */
    @GetMapping("/permission-user")
    public ActionResult<List<String>> permissionUser(@RequestParam("userId") String userId, @RequestParam("permissionModule") String permissionModule, @RequestParam("category") String category) {
        List<String> userIds = permissionsUtils.obtainPersonnelUserIdDataPermissions(userId, permissionModule, category);
        return ActionResult.success(userIds);
    }
    /**
     * 查询那些人员具有当前按钮权限
     */
    @PostMapping("/queryButtonPermission")
    public List<String> queryButtonPermission(@RequestBody FuntionMenuDTO funtion) {
        try {
            return ftbPermissionRoleAuthorizePersonService.queryButtonPermission(funtion);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ArrayList<>();
    }

    /**
     * 根据用户Id获取用户权限的用户Id集合
     * @return 用户id->所具有的权限用户id集合
     */
    @PostMapping("/collection-of-user")
    public Map<String,List<String>> getUserPermissionUserCollection(@RequestBody List<String> userIds) {
        if (CollUtil.isNotEmpty(userIds)) {
            return new HashMap<>();
        }
        Map<String,List<String>> result = new HashMap<>(userIds.size());
        String moduleId = permissionsUtils.getRequestModule();
        for (String userId : userIds) {
            List<String> userIdDataPermissions = permissionsUtils.obtainPersonnelUserIdDataPermissions(userId, moduleId);
            result.put(userId, userIdDataPermissions);
        }
        return result;
    }
}
