package cn.ey.biz.module.system.controller.admin.permission;

import cn.ey.biz.common.enums.CommonStatusEnum;
import cn.ey.biz.common.pojo.PageResult;
import cn.ey.biz.common.pojo.R;
import cn.ey.biz.framework.datapermission.core.annotation.DataPermission;
import cn.ey.biz.framework.excel.core.util.ExcelUtils;
import cn.ey.biz.framework.operatelog.core.annotations.OperateLog;
import cn.ey.biz.module.system.controller.admin.permission.vo.role.*;
import cn.ey.biz.module.system.convert.permission.RoleConvert;
import cn.ey.biz.module.system.dal.dataobject.permission.RoleDO;
import cn.ey.biz.module.system.service.permission.DataRoleService;
import cn.ey.biz.module.system.service.permission.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Comparator;
import java.util.List;

import static cn.ey.biz.common.pojo.R.success;
import static cn.ey.biz.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 角色")
@RestController
@RequestMapping("/system/role")
@Validated
public class RoleController {

    @Resource
    private RoleService roleService;

    @Resource
    private DataRoleService dataRoleService;

    @PostMapping("/create")
    @ApiOperation("创建角色")
    @PreAuthorize("@ss.hasPermission('system:role:create')")
    public R<Long> createRole(@Valid @RequestBody RoleCreateReqVO reqVO) {
        return success(roleService.createRole(reqVO, null));
    }

    @GetMapping("/get-max-role-code")
    @ApiOperation("获取角色标识最大序列")
    public R<String> getMaxRoleCodeByAppAndClass(@RequestParam("appCode") String appCode, @RequestParam("roleClass") String roleClass) {
        return success(roleService.getMaxRoleCodeByAppAndClass(appCode, roleClass));
    }

    @PutMapping("/update")
    @ApiOperation("修改角色")
    @PreAuthorize("@ss.hasPermission('system:role:update')")
    public R<Boolean> updateRole(@Valid @RequestBody RoleUpdateReqVO reqVO) {
        roleService.updateRole(reqVO);
        return success(true);
    }

    @PutMapping("/update-status")
    @ApiOperation("修改角色状态")
    @PreAuthorize("@ss.hasPermission('system:role:update')")
    public R<Boolean> updateRoleStatus(@Valid @RequestBody RoleUpdateStatusReqVO reqVO) {
        roleService.updateRoleStatus(reqVO.getId(), reqVO.getStatus());
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除角色")
    @ApiImplicitParam(name = "id", value = "角色编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('system:role:delete')")
    public R<Boolean> deleteRole(@RequestParam("id") Long id) {
        roleService.deleteRole(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得角色信息")
    @PreAuthorize("@ss.hasPermission('system:role:query')")
    public R<RoleRespVO> getRole(@RequestParam("id") Long id) {
        RoleDO role = roleService.getRole(id);
        return success(RoleConvert.INSTANCE.convert(role));
    }

    @GetMapping("/page")
    @ApiOperation("获得角色分页")
    @PreAuthorize("@ss.hasPermission('system:role:query')")
    @DataPermission
    public R<PageResult<RoleDO>> getRolePage(RolePageReqVO reqVO) {
        return success(roleService.getRolePage(reqVO));
    }

    @GetMapping("/list-all-simple")
    @ApiOperation(value = "获取角色精简信息列表", notes = "只包含被开启的角色，主要用于前端的下拉选项")
    @ApiImplicitParam(name = "app", value = "所属应用", required = false, example = "system", dataTypeClass = String.class)
    public R<List<RoleSimpleRespVO>> getSimpleRoles(@RequestParam(required = false, value = "app") String app) {
        // 获得角色列表，只要开启状态的
        // List<RoleDO> list = roleService.getRoles(Collections.singleton(CommonStatusEnum.ENABLE.getStatus()));
        RoleExportReqVO reqVO = new RoleExportReqVO().setApp(app).setStatus(CommonStatusEnum.ENABLE.getStatus());
        List<RoleDO> list = roleService.getRoleList(reqVO);
        // 排序后，返回给前端
        list.sort(Comparator.comparing(RoleDO::getSort));
        return success(RoleConvert.INSTANCE.convertList02(list));
    }

    @GetMapping("/list-all-functional-role")
    @ApiOperation(value = "分配角色-功能角色列表", notes = "只包含被开启的角色，主要用于前端的下拉选项")
    @ApiImplicitParam(name = "appName", value = "所属应用名称", required = false, example = "system", dataTypeClass = String.class)
    public R<List<RoleSimpleFunctionalRespVO>> getFunctionalRoles(@RequestParam(required = false, value = "appName") String appName,
                                                        @RequestParam(required = false, value = "roleName") String roleName) {
        // 获得角色列表，只要开启状态的
        RoleFunctionalReqVO reqVO = new RoleFunctionalReqVO().setAppName(appName).setRoleName(roleName);
        List<RoleSimpleFunctionalRespVO> list = roleService.getFunctionalRoles(reqVO);
        // 排序后，返回给前端
        list.sort(Comparator.comparing(RoleSimpleFunctionalRespVO::getSort));
        return success(list);
    }

    @GetMapping("/list-all-data-role")
    @ApiOperation(value = "分配角色-数据角色列表", notes = "主要用于前端的下拉选项")
    @ApiImplicitParam(name = "appName", value = "所属应用名称", required = false, example = "system", dataTypeClass = String.class)
    public R<List<RoleSimpleDataRespVO>> getDataRoles(@RequestParam(required = false, value = "appName") String appName,
                                                                  @RequestParam(required = false, value = "roleName") String roleName) {
        // 获得角色列表，只要开启状态的
        RoleFunctionalReqVO reqVO = new RoleFunctionalReqVO().setAppName(appName).setRoleName(roleName);
        List<RoleSimpleDataRespVO> dataRoles = dataRoleService.getDataRoles(reqVO);
        return success(dataRoles);
    }

    @GetMapping("/export")
    @OperateLog(type = EXPORT)
    @PreAuthorize("@ss.hasPermission('system:role:export')")
    public void export(HttpServletResponse response, @Validated RoleExportReqVO reqVO) throws IOException {
        List<RoleDO> list = roleService.getRoleList(reqVO);
        List<RoleExcelVO> data = RoleConvert.INSTANCE.convertList03(list);
        // 输出
        ExcelUtils.write(response, "角色数据.xls", "角色列表", RoleExcelVO.class, data);
    }

}
