package com.zhongshu.card.server.core.controller.orgManager;

import com.github.microservice.auth.client.constant.AuthConstant;
import com.github.microservice.auth.security.annotations.ResourceAuth;
import com.github.microservice.auth.security.type.AuthType;
import com.github.microservice.net.ResultContent;
import com.zhongshu.card.client.model.base.IDParam;
import com.zhongshu.card.client.model.base.OidModel;
import com.zhongshu.card.client.model.org.EditAuthParam;
import com.zhongshu.card.client.model.org.role.*;
import com.zhongshu.card.server.core.service.user.RoleServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author TRX
 * @date 2024/6/5
 */
@RestController
@RequestMapping("role")
@Tag(name = "角色管理")
public class RoleController {

    @Autowired
    RoleServiceImpl roleService;

    @ResourceAuth(value = AuthConstant.SuperAdmin, type = AuthType.Platform, remark = "")
    @ResourceAuth(value = AuthConstant.Admin, type = AuthType.Project, remark = "")
    @ResourceAuth(value = AuthConstant.Admin, type = AuthType.School, remark = "")
    @ResourceAuth(value = AuthConstant.Admin, type = AuthType.BusinessMain, remark = "")
    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "添加-编辑角色", description = "添加-编辑角色")
    @RequestMapping(value = "addRole", method = {RequestMethod.POST})
    public ResultContent addRole(@RequestBody @Valid RoleAddParam param) {
        return this.roleService.addRole(param);
    }

    @ResourceAuth(value = AuthConstant.SuperAdmin, type = AuthType.Platform, remark = "")
    @ResourceAuth(value = AuthConstant.Admin, type = AuthType.Project, remark = "")
    @ResourceAuth(value = AuthConstant.Admin, type = AuthType.School, remark = "")
    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "角色管理列表-分页查询", description = "角色管理列表-分页查询")
    @RequestMapping(value = {"pageManager"}, method = {RequestMethod.POST})
    public ResultContent<Page<RoleModel>> pageManager(@Parameter(hidden = true) @PageableDefault(page = 0, size = 10) Pageable pageable, @Parameter(required = false) RoleSearchParam param) {
        return roleService.page(param, pageable);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "角色列表-分页查询", description = "角色列表-分页查询")
    @RequestMapping(value = {"page"}, method = {RequestMethod.POST})
    public ResultContent<Page<RoleModel>> page(@Parameter(hidden = true) @PageableDefault(page = 0, size = 10) Pageable pageable, @Parameter(required = false) RoleSearchParam param) {
        return roleService.page(param, pageable);
    }

    @ResourceAuth(value = AuthConstant.SuperAdmin, type = AuthType.Platform, remark = "")
    @ResourceAuth(value = AuthConstant.Admin, type = AuthType.Project, remark = "")
    @ResourceAuth(value = AuthConstant.Admin, type = AuthType.School, remark = "")
    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "删除角色", description = "删除角色")
    @RequestMapping(value = "deleteRole", method = {RequestMethod.POST})
    public ResultContent deleteRole(@RequestBody IDParam param) {
        return this.roleService.deleteRole(param.getId());
    }

    @ResourceAuth(value = AuthConstant.SuperAdmin, type = AuthType.Platform, remark = "")
    @ResourceAuth(value = AuthConstant.Admin, type = AuthType.Project, remark = "")
    @ResourceAuth(value = AuthConstant.Admin, type = AuthType.School, remark = "")
    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "角色详情", description = "角色详情")
    @RequestMapping(value = "getRole", method = {RequestMethod.POST})
    public ResultContent<RoleModel> getRole(@RequestBody IDParam param) {
        return this.roleService.getRole(param.getId());
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "查询角色列表(添加用户时使用)", description = "查询角色列表")
    @RequestMapping(value = "queryRoles", method = {RequestMethod.POST})
    public ResultContent queryRoles(@RequestBody RoleQueryParam param) {
        return this.roleService.queryRoles(param);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "得到机构所有角色(根据oid查询)", description = "得到机构所有角色(根据oid查询)")
    @RequestMapping(value = "getAllRoles", method = {RequestMethod.POST})
    public ResultContent getAllRoles(@RequestBody @Valid OidModel param) {
        return this.roleService.getAllRoles(param.getOid());
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "小程序-得到当前用户所有的角色", description = "小程序-得到当前用户所有的角色")
    @RequestMapping(value = {"xcxGetCurrentRoles"}, method = {RequestMethod.GET})
    public ResultContent<List<RoleSimpleModel>> xcxGetCurrentRoles() {
        return roleService.xcxGetCurrentRoles();
    }

    //---------------------------角色权限 start -----------------------

    @Operation(summary = "获取角色的权限列表", description = "获取角色的权限列表")
    @RequestMapping(value = "getRoleAuthList", method = {RequestMethod.GET})
    @ResourceAuth(value = "user", type = AuthType.User)
    public Object getRoleAuthList(@Parameter(name = "roleId", description = "角色数据ID", example = "") @RequestParam("roleId") String roleId) {
        return roleService.getList(roleId);
    }

    @Operation(summary = "修改角色权限", description = "修改角色权限")
    @ResourceAuth(value = "user", type = AuthType.User)
    @RequestMapping(value = "editRoleAuth", method = {RequestMethod.POST})
    public Object editRoleAuth(@RequestBody EditAuthParam param) {
        return roleService.editRoleAuth(param);
    }

    @Operation(summary = "获取用户在当前结机构权限列表 （登录后加载）", description = "获取用户在当前结机构权限列表")
    @RequestMapping(value = "getOrganizationAuth", method = {RequestMethod.GET})
    @ResourceAuth(value = "user", type = AuthType.User)
    public Object getOrganizationAuth(@Parameter(name = "oid", description = "当前用户的机构oid", example = "", required = false) @RequestParam("oid") String oid) {
        return roleService.getOrganizationAuth(oid, null);
    }

    @Operation(summary = "得到用户在项目的权限列表", description = "得到用户在项目的权限列表")
    @RequestMapping(value = "getCurrentUserProjectAuth", method = {RequestMethod.GET})
    @ResourceAuth(value = "user", type = AuthType.User)
    public ResultContent getCurrentUserProjectAuth(@Parameter(name = "projectOid", description = "当前选中的项目", example = "", required = false) @RequestParam("projectOid") String projectOid) {
        return roleService.getCurrentUserProjectAuth(projectOid);
    }

}
