package com.xdap.self_development.controller;


import com.definesys.mpaas.common.http.Response;
import com.xdap.self_development.controller.request.ModelRolesRequest;
import com.xdap.self_development.controller.request.PreAndRolePageRequest;
import com.xdap.self_development.controller.request.RolePerJoinRequest;
import com.xdap.self_development.controller.request.UserRoleBindingRequest;
import com.xdap.self_development.domain.dto.PageResultDTO;
import com.xdap.self_development.domain.pojo.ModelRolesPojo;
import com.xdap.self_development.domain.view.MenuListView;
import com.xdap.self_development.domain.view.MenuParamView;
import com.xdap.self_development.domain.view.RoleInfoView;
import com.xdap.self_development.exception.CommonException;
import com.xdap.self_development.service.ModelMenuPermissionService;
import com.xdap.self_development.service.ModelRoleService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import java.util.HashMap;
import java.util.List;

/**
 * 权限控制器
 * 提供权限管理相关的功能，包括角色管理、用户权限分配、角色权限管理等
 *
 * @version 1.0
 * @since 2025
 */
@RestController
@RequestMapping("/custom/permission")
public class PermissionController {
    @Resource
    ModelRoleService modelRoleService;
    @Resource
    ModelMenuPermissionService modelMenuPermissionService;

    /**
     * 添加角色
     *
     * @param request 角色请求对象
     * @return 操作结果响应
     */
    @PostMapping("/addRoles")
    Response addRoles(@RequestBody ModelRolesRequest request) {
        try {
            modelRoleService.addRoles(request);
        } catch (Exception e) {
            throw new CommonException("添加失败:检查是否存在重复角色（代码）" + e.getMessage());
        }
        return Response.ok();
    }

    /**
     * 查看角色
     *
     * @return 角色列表
     */
    @GetMapping("/selectRoles")
    Response selectRoles() {
        try {
            List<ModelRolesPojo> modelRolesPojos = modelRoleService.selectRoles();
            return Response.ok().data(modelRolesPojos);
        } catch (Exception e) {
            throw new CommonException(e.getMessage());
        }
    }

    /**
     * 删除角色
     *
     * @param roleID 角色ID
     * @param userID 用户ID
     * @return 操作结果响应
     */
    @GetMapping("/removeRoles")
    Response removeRoles(@RequestParam String roleID, String userID) {
        try {
            modelRoleService.removeRoles(roleID, userID);
            return Response.ok();
        } catch (Exception e) {
            throw new CommonException(e.getMessage());
        }
    }


    /**
     * 获取用户权限
     *
     * @param userOrDepID 用户或部门ID
     * @return 菜单参数视图列表
     */
    @GetMapping("/selectPersByUser")
    Response selectPersByUser(@RequestParam String userOrDepID) {

        List<MenuParamView> views = modelRoleService.selectPersByUser(userOrDepID);
        return Response.ok().data(views);

    }

    /**
     * 获取角色权限
     *
     * @param roleID 角色ID
     * @return 菜单参数视图列表
     */
    @GetMapping("/selectPersByRole")
    Response selectPersByRole(@RequestParam String roleID) {
        List<MenuParamView> views = modelRoleService.selectPersByRole(roleID);
        if (ObjectUtils.isEmpty(views)) {
            return Response.ok().data("角色无权限");
        }
        return Response.ok().data(views);
    }


    /**
     * 为用户绑定角色
     *
     * @param userRoleBindingRequest 用户角色绑定请求
     * @return 操作结果响应
     */
    @PostMapping("/userRoleBinding")
    public Response userRoleBinding(@RequestBody UserRoleBindingRequest userRoleBindingRequest) {
        try {
            modelRoleService.userRoleBinding(userRoleBindingRequest);
            return Response.ok();
        } catch (Exception e) {
            throw new CommonException(e.getMessage());
        }
    }


    /**
     * 修改角色权限(关联权限修改 增加，修改，删除)
     *
     * @param request 角色权限关联请求列表
     * @return 操作结果响应
     */
    @PostMapping("/updatePerForRole")
    public Response updatePerForRole(@RequestBody List<RolePerJoinRequest> request) {
        try {
            modelRoleService.updatePerForRole(request);
            return Response.ok();
        } catch (Exception e) {
            throw new CommonException(e.getMessage());
        }
    }

    /**
     * 修改单个角色信息(基础信息)
     *
     * @param modelRolesPojo 角色POJO对象
     * @param userID         用户ID
     * @return 操作结果响应
     */
    @PostMapping("/updateRole")
    public Response updateRole(@RequestBody ModelRolesPojo modelRolesPojo, @RequestParam String userID) {
        modelRoleService.updateRole(modelRolesPojo, userID);
        return Response.ok();
    }


    /**
     * 菜单查询
     *
     * @return 菜单列表视图
     */
    @GetMapping("/selectMenus")
    Response selectMenus() {
        try {
            List<MenuListView> modelRolesPojos = modelMenuPermissionService.selectMenus();
            return Response.ok().data(modelRolesPojos);
        } catch (Exception e) {
            throw new CommonException(e.getMessage());
        }
    }
    /**
     * 查看角色绑定的人员或部门列表
     *
     * @param roleId 角色ID
     * @return 用户或部门映射
     */
    @GetMapping("/selectUserOrDeptByRole")
    public Response selectUserOrDeptByRole(
            @NotBlank String roleId
    ) {
        HashMap<String, List<String>> map = modelRoleService.selectUserOrDeptByRole(roleId);
        if (ObjectUtils.isEmpty(map)) {
            return Response.ok().data(new HashMap<>());
        }
        return Response.ok().data(map);
    }

    /**
     * 根据条件获取权限和角色
     *
     * @param request 条件
     * @return 前置角色和当前角色映射
     */
    @PostMapping("/getPreAndRoleByCondition")
    public Response getPreAndRoleByCondition(
            @RequestBody PreAndRolePageRequest request
    ) {
        PageResultDTO<RoleInfoView> result = modelRoleService.getPreAndRoleByCondition(request);
        return Response.ok().data(result);
    }

    @GetMapping("/exportPreAndRole")
    public void exportPreAndRole(
            HttpServletResponse response
    ) {
        modelRoleService.writePreAndRole(response);
    }
}
