package com.fzm.mall.server.admin.auth_center.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fzm.mall.server.admin.annotation.AdminLog;
import com.fzm.mall.server.admin.auth_center.entity.dto.RoleDTO;
import com.fzm.mall.server.admin.auth_center.entity.dto.RoleEditDTO;
import com.fzm.mall.server.admin.auth_center.entity.vo.AdminRoleDetailVO;
import com.fzm.mall.server.admin.auth_center.entity.vo.AdminRoleVO;
import com.fzm.mall.server.admin.auth_center.entity.vo.RoleAuthMenuVO;
import com.fzm.mall.server.admin.auth_center.service.IAdminRoleService;
import com.fzm.mall.server.admin.auth_center.service.IAuthMenuService;
import com.fzm.mall.server.admin.enums.log.OperateModuleEnum;
import com.fzm.mall.server.admin.enums.log.OperateTypeEnum;
import com.fzm.mall.server.admin.home_page.enums.type.AdminTypeEnum;
import com.fzm.mall.server.admin.home_page.service.IAdminService;
import com.fzm.mall.server.admin.repo.ResponseFactory;
import com.fzm.mall.server.admin.repo.ResponseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * @author wangp
 * @date 2021/4/8 13:41
 * @description 角色管理
 * @since JDK 1.8
 */
@Api(tags = "权限中心-角色管理")
@RestController
@RequestMapping("/roleManage")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class RoleManageController {

    private final IAuthMenuService authMenuService;
    private final IAdminRoleService adminRoleService;
    private final IAdminService adminService;
    private final ResponseFactory resFac;

    @ApiOperation(value = "全部权限")
    @GetMapping("/totalAuth")
    public ResponseVO totalAuth(@ApiParam(name = "type", value = "角色归属") @RequestParam Integer type, @RequestHeader String adminId) {
        adminService.adminAuthChecks(adminId, AdminTypeEnum.getPlatFormTypeArr());
        List<RoleAuthMenuVO> authMenu = authMenuService.getRoleAuthMenu(type, null);
        return resFac.getSuccessResponseWithData(authMenu);
    }

    @ApiOperation(value = "添加")
    @AdminLog(description = "权限中心-添加角色", module = OperateModuleEnum.AUTH_CENTER, opType = OperateTypeEnum.ADD_ROLE)
    @PostMapping("/add")
    public ResponseVO add(@RequestBody @Valid RoleDTO roleDTO, @RequestHeader String adminId) {
        adminService.adminAuthChecks(adminId, AdminTypeEnum.getPlatFormTypeArr());
        adminRoleService.add(roleDTO);
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation(value = "编辑")
    @AdminLog(description = "权限中心-编辑角色", module = OperateModuleEnum.AUTH_CENTER, opType = OperateTypeEnum.EDIT_ROLE)
    @PutMapping("/edit")
    public ResponseVO edit(@RequestBody @Valid RoleEditDTO roleEditDTO, @ApiParam(name = "adminId", value = "管理员id")
    @RequestHeader String adminId) {
        adminService.adminAuthChecks(adminId, AdminTypeEnum.getPlatFormTypeArr());
        adminRoleService.edit(roleEditDTO);
        adminService.offLineByRoleId(roleEditDTO.getRoleId());
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation(value = "分页查询")
    @GetMapping("/page")
    public ResponseVO page(
            @ApiParam(name = "roleId", value = "角色id")
            @RequestParam(required = false) String roleId,
            @ApiParam(name = "roleName", value = "角色名称")
            @RequestParam(required = false) String roleName,
            @ApiParam(name = "pageNum", value = "当前页数")
            @RequestParam Integer pageNum,
            @ApiParam(name = "pageSize", value = "每页个数")
            @RequestParam Integer pageSize,
            @ApiParam(name = "adminId", value = "管理员id")
            @RequestHeader String adminId) {
        adminService.adminAuthChecks(adminId, AdminTypeEnum.getPlatFormTypeArr());
        IPage<AdminRoleVO> page = adminRoleService.page(roleId, roleName, pageNum, pageSize);
        return resFac.getSuccessResponseWithData(page);
    }

    @ApiOperation(value = "角色详情")
    @GetMapping("/detail")
    public ResponseVO detail(@ApiParam(name = "roleId", value = "角色id") @RequestParam String roleId, @RequestHeader String adminId) {
        adminService.adminAuthChecks(adminId, AdminTypeEnum.getPlatFormTypeArr());
        AdminRoleDetailVO detail = adminRoleService.detail(roleId);
        return resFac.getSuccessResponseWithData(detail);
    }

    @ApiOperation(value = "启用")
//    @AdminLog(description = "权限中心-禁用角色", module = OperateModuleEnum.AUTH_CENTER, opType = OperateTypeEnum.DISABLE_ROLE)
    @GetMapping("/able/{roleId}")
    public ResponseVO able(@ApiParam(name = "roleId", value = "角色id") @PathVariable String roleId,
                           @RequestHeader String adminId) {
        adminService.adminAuthChecks(adminId, AdminTypeEnum.getPlatFormTypeArr());
        adminRoleService.able(roleId);
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation(value = "禁用")
    @AdminLog(description = "权限中心-禁用角色", module = OperateModuleEnum.AUTH_CENTER, opType = OperateTypeEnum.DISABLE_ROLE)
    @GetMapping("/disable/{roleId}")
    public ResponseVO disable(@ApiParam(name = "roleId", value = "角色id") @PathVariable String roleId,
                              @RequestHeader String adminId) {
        adminService.adminAuthChecks(adminId, AdminTypeEnum.getPlatFormTypeArr());
        adminRoleService.disable(roleId);
        adminService.offLineByRoleId(roleId);
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation(value = "删除")
    @AdminLog(description = "权限中心-删除角色", module = OperateModuleEnum.AUTH_CENTER, opType = OperateTypeEnum.DEL_ROLE)
    @GetMapping("/delete/{roleId}")
    public ResponseVO delete(@ApiParam(name = "roleId", value = "角色id") @PathVariable String roleId,
                             @RequestHeader String adminId) {
        adminService.adminAuthChecks(adminId, AdminTypeEnum.getPlatFormTypeArr());
        adminRoleService.delete(roleId);
        adminService.offLineByRoleId(roleId);
        return resFac.getSimpleSuccessResponse();
    }

}
