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

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.module.system.api.permission.dto.CodeBlockDataPermissionRespDTO;
import cn.ey.biz.module.system.controller.admin.permission.vo.dataRole.ConfigValueVO;
import cn.ey.biz.module.system.controller.admin.permission.vo.dataRole.DataRoleVO;
import cn.ey.biz.module.system.dal.dataobject.user.AdminUserDO;
import cn.ey.biz.module.system.enums.permission.DataRoleConfigTypeEnum;
import cn.ey.biz.module.system.service.permission.DataRoleService;
import cn.ey.biz.module.system.service.permission.PermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static cn.ey.biz.common.pojo.R.success;

@Api(tags = "管理后台 - 数据角色")
@RestController
@RequestMapping("/system/datarole")
@Validated
@Slf4j
public class DataRoleController {

    @Resource
    private DataRoleService dataRoleService;

    @Resource
    private PermissionService permissionService;

    @PostMapping("/create")
    @ApiOperation("创建角色")
    @PreAuthorize("@ss.hasPermission('system:datarole:create')")
    public R<Long> createRole(@Validated(value = DataRoleVO.Save.class) @RequestBody DataRoleVO roleVO) {
        return success(dataRoleService.createRole(roleVO));
    }

    @PutMapping("/update")
    @ApiOperation("修改角色")
    @PreAuthorize("@ss.hasPermission('system:datarole:update')")
    public R<Boolean> updateRole(@Validated(value = DataRoleVO.Update.class) @RequestBody DataRoleVO roleVO) {
        log.info("进入修改角色方法updateRole[1]");
        dataRoleService.updateRole(roleVO);
        return success(true);
    }

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

    @PostMapping("/page")
    @ApiOperation("获得角色分页")
    @DataPermission
    @PreAuthorize("@ss.hasPermission('system:datarole:query')")
    public R<PageResult<DataRoleVO>> getRolePage(@RequestBody DataRoleVO roleVO) {
        return success(dataRoleService.getRolePage(roleVO));
    }

    @GetMapping("/list")
    @ApiOperation("根据用户ID, 获取角色列表")
    R<List<DataRoleVO>> getRoleListByUserId(@RequestParam("id") Long id) {
        return success(dataRoleService.getRoleListByUserId(id));
    }

    @GetMapping("/list/all")
    @ApiOperation("获取全部数据角色列表")
    R<List<DataRoleVO>> getAllRoleList() {
        return success(dataRoleService.getAllRoleList());
    }

    @GetMapping("/users")
    @ApiOperation("获取数据角色对应人员列表")
    R<List<AdminUserDO>> getUsersByDataRoleId(@RequestParam("id") Long dataRoleId) {
        return success(dataRoleService.getUsersByDataRoleId(dataRoleId));
    }

    @GetMapping("/configType")
    @ApiOperation("配置方式")
    public R<List<ConfigValueVO>> getTaskRulePullDownList() {
        List<ConfigValueVO> contentTypeOption = Arrays.stream(DataRoleConfigTypeEnum.values()).map(
                (item) -> new ConfigValueVO(item.getValue(), item.getName())
        ).collect(Collectors.toList());
        return success(contentTypeOption);
    }

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


    @GetMapping("/getCodeBlockDataPermission")
    @ApiOperation("获取codeBlock权限")
    public R<CodeBlockDataPermissionRespDTO> getCodeBlockDataPermission(@RequestParam("userId") Long userId) {
        CodeBlockDataPermissionRespDTO codeBlockDataPermission = permissionService.getCodeBlockDataPermission(userId);
        return success(codeBlockDataPermission);
    }
}
