# OvertimeDataPermissionConfigController.java

    package cloud.wconnect.workperformance.controller.mor.overtime;

    import cloud.wconnect.commonlib.annotation.ApiId;
    import cloud.wconnect.commonlib.component.SessionContext;
    import cloud.wconnect.commonlib.cons.*;
    import cloud.wconnect.commonlib.exception.BadRequestException;
    import cloud.wconnect.commonlib.pojo.dto.ListPagedDTO;
    import cloud.wconnect.commonlib.pojo.dto.result.MsgDTO;
    import cloud.wconnect.commonlib.pojo.dto.result.ResultDTO;
    import cloud.wconnect.commonlib.service.DataPermissionConfigInternalService;
    import cloud.wconnect.commonlib.util.I18nMessageUtils;
    import cloud.wconnect.workperformance.entity.mor.OvertimeDataPermissionConfig;
    import cloud.wconnect.workperformance.pojo.dto.mor.MorDataPermissionConfigDTO;
    import cloud.wconnect.workperformance.pojo.query.mor.MorDataPermissionConfigQuery;
    import cloud.wconnect.workperformance.service.mor.IOvertimeDataPermissionConfigService;
    import com.google.common.collect.ImmutableMap;
    import jakarta.annotation.Resource;
    import jakarta.validation.Valid;
    import org.apache.commons.collections.CollectionUtils;
    import org.springframework.web.bind.annotation.*;

    import java.util.Collections;
    import java.util.List;
    import java.util.Map;
    import java.util.Objects;

    /**
     * <p>
     * 数据权限配置 前端控制器
     * </p>
     *
     * @author zc
     * @since 2023-11-22
     */
    @RestController
    @RequestMapping("/mor/overtime/data-permission")
    public class OvertimeDataPermissionConfigController {

        @Resource
        private IOvertimeDataPermissionConfigService dataPermissionConfigService;
        @Resource
        private SessionContext sessionContext;
        @Resource
        private DataPermissionConfigInternalService dataPermissionConfigInternalService;
        @Resource
        private I18nMessageUtils i18nMessageUtils;

        /**
         * 异步刷新数据权限状态
         *
         * @param property 资产标识
         * @return empty map
         * @author zc
         * @date 2023/11/22
         */
        @ApiId(907)
        @PutMapping("/flush/status")
        public Map<Object, Object> dataPermissionFlushStatus(@RequestParam String property) {
            int tenantId = sessionContext.getTenantSymbolBO().getId();
            int propertyId = sessionContext.getPropertyId(property);
            dataPermissionConfigService.dataPermissionFlushStatus(tenantId, propertyId);
            return Collections.emptyMap();
        }

        /**
         * 数据权限集合
         *
         * @param property    资产标识
         * @param configQuery 查询条件
         * @return 数据权限集合
         * @author zc
         * @date 2023/11/22
         */
        @ApiId(908)
        @GetMapping
        public ListPagedDTO<MorDataPermissionConfigDTO> list(@RequestParam String property,
                                                             @Valid MorDataPermissionConfigQuery configQuery) {
            if (Objects.isNull(configQuery.getIdentityType()) && Objects.nonNull(configQuery.getIdentityId())) {
                throw new BadRequestException(CommonErrorI18nConstant.CANNOT_EMPTY,
                        i18nMessageUtils.getErrorParamMapNeedTranslate(CommonFieldI18nConstant.IDENTITY_TYPE),
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        "The identityType cannot be null when query identityId.");
            }
            int tenantId = sessionContext.getTenantSymbolBO().getId();
            int propertyId = sessionContext.getPropertyId(property);
            return dataPermissionConfigService.list(tenantId, propertyId, configQuery);
        }

        /**
         * 创建数据权限
         *
         * @param property  资产标识
         * @param configDTO 数据权限
         * @return 数据 id
         * @author zc
         * @date 2023/11/22
         */
        @ApiId(909)
        @PostMapping
        public Map<String, Integer> add(@RequestParam String property, @RequestBody @Valid MorDataPermissionConfigDTO configDTO) {
            int tenantId = sessionContext.getTenantSymbolBO().getId();
            int propertyId = sessionContext.getPropertyId(property);
            int loginUserId = sessionContext.getLoginUserId();

            // 根据身份数据 id 校验数据是否存在
            dataPermissionConfigInternalService.checkExistForIdentityId(tenantId, propertyId,
                    configDTO.getIdentityType(), configDTO.getIdentityId());

            // 校验数据权限数据是否重复
            dataPermissionConfigService.checkAlreadyExist(tenantId, propertyId, null,
                    configDTO.getIdentityType(), configDTO.getIdentityId());

            // 校验数据权限是否有效
            dataPermissionConfigService.verifyDataPermissionStatus(tenantId, propertyId,
                    configDTO.getIdentityType(), configDTO.getIdentityId());

            return ImmutableMap.of("id", dataPermissionConfigService.save(tenantId, propertyId, configDTO, loginUserId));
        }

        /**
         * 编辑数据权限
         *
         * @param id        数据权限 id
         * @param property  资产标识
         * @param configDTO 数据权限
         * @return empty map
         * @author zc
         * @date 2023/11/22
         */
        @ApiId(910)
        @PutMapping("/{id:\\d+}")
        public Map<Object, Object> edit(@PathVariable int id, @RequestParam String property,
                                        @RequestBody @Valid MorDataPermissionConfigDTO configDTO) {
            int tenantId = sessionContext.getTenantSymbolBO().getId();
            int propertyId = sessionContext.getPropertyId(property);
            int loginUserId = sessionContext.getLoginUserId();

            // 校验数据是否存在
            OvertimeDataPermissionConfig originConfig = dataPermissionConfigService.checkExist(id);
            // 校验数据归属
            dataPermissionConfigService.checkBelong(tenantId, propertyId, originConfig);

            // 根据身份数据 id 校验数据是否存在
            dataPermissionConfigInternalService.checkExistForIdentityId(tenantId, propertyId,
                    configDTO.getIdentityType(), configDTO.getIdentityId());

            // 校验数据权限数据是否重复
            dataPermissionConfigService.checkAlreadyExist(tenantId, propertyId, id,
                    configDTO.getIdentityType(), configDTO.getIdentityId());

            // 校验数据权限是否有效
            dataPermissionConfigService.verifyDataPermissionStatus(tenantId, propertyId,
                    configDTO.getIdentityType(), configDTO.getIdentityId());

            dataPermissionConfigService.update(originConfig, configDTO, loginUserId);

            return Collections.emptyMap();
        }

        /**
         * 删除数据权限
         *
         * @param id       数据权限 id
         * @param property 资产标识
         * @return empty map
         * @author zc
         * @date 2023/11/22
         */
        @ApiId(911)
        @DeleteMapping("/{id:\\d+}")
        public Map<Object, Object> remove(@PathVariable int id, @RequestParam String property) {
            int tenantId = sessionContext.getTenantSymbolBO().getId();
            int propertyId = sessionContext.getPropertyId(property);

            // 校验数据是否存在
            OvertimeDataPermissionConfig originConfig = dataPermissionConfigService.checkExist(id);
            // 校验数据归属
            dataPermissionConfigService.checkBelong(tenantId, propertyId, originConfig);

            dataPermissionConfigService.removeById(id);

            return Collections.emptyMap();
        }

        /**
         * 允许用户申请的数据权限
         *
         * @param id       数据权限 id
         * @param property 资产标识
         * @return empty map
         * @author zc
         * @date 2023/11/22
         */
        @ApiId(912)
        @PutMapping("/{id:\\d+}/pass")
        public Map<Object, Object> pass(@PathVariable int id, @RequestParam String property) {
            int tenantId = sessionContext.getTenantSymbolBO().getId();
            int propertyId = sessionContext.getPropertyId(property);

            // 校验数据是否存在
            OvertimeDataPermissionConfig originConfig = dataPermissionConfigService.checkExist(id);
            if (!originConfig.getStatus().equals(DataPermissionConfigConstant.STATUS_WAIT_PASS)) {
                throw new BadRequestException(CommonErrorI18nConstant.MUST_IN_LIST,
                        i18nMessageUtils.getErrorParamMapNeedTranslate(CommonFieldI18nConstant.STATUS,
                                List.of(DataPermissionStatusEnum.WAITING_FOR_APPROVAL.getI18nCode())),
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        "Only data in state [2: wait pass] can be pass");
            }

            // 校验数据归属
            dataPermissionConfigService.checkBelong(tenantId, propertyId, originConfig);

            dataPermissionConfigService.approval(tenantId, propertyId, originConfig);

            return Collections.emptyMap();
        }

        /**
         * 申请数据权限
         *
         * @param property 资产标识
         * @return cloud.wconnect.commonlib.pojo.dto.result.ResultDTO<java.lang.Void>
         * @author lichen
         * @date 2025/11/12
         */
        @ApiId(913)
        @PutMapping("/apply")
        public ResultDTO<Void> apply(@RequestParam String property) {
            int tenantId = sessionContext.getTenantSymbolBO().getId();
            int propertyId = sessionContext.getPropertyId(property);
            int loginUserId = sessionContext.getLoginUserId();

            // 校验是否已有数据权限
            if (dataPermissionConfigService.verifyDataPermissionAccess(tenantId, propertyId, loginUserId)) {
                List<MsgDTO> msgs = i18nMessageUtils.getAllLanguageMsg(BaseErrorCodeEnum.ALREADY_HAVE_DATA_PERMISSION.getI18nCode());
                return ResultDTO.fail(BaseErrorCodeEnum.ALREADY_HAVE_DATA_PERMISSION.getCode(), msgs);
            }

            // 校验是否已申请过数据权限
            MorDataPermissionConfigQuery configQuery = MorDataPermissionConfigQuery.builder()
                    .identityType(DataPermissionConfigConstant.IDENTITY_TYPE_USER)
                    .identityId(loginUserId)
                    .build();
            List<MorDataPermissionConfigDTO> configList = dataPermissionConfigService.list(tenantId,
                    propertyId, configQuery).getList();

            if (CollectionUtils.isNotEmpty(configList)) {
                List<MsgDTO> msgs = i18nMessageUtils.getAllLanguageMsg(CommonErrorI18nConstant.ALREADY_APPLY_DATA_PERMISSION);
                return ResultDTO.dataValidationError(msgs);
            }

            // 申请数据权限
            dataPermissionConfigService.apply(tenantId, propertyId, loginUserId);

            return ResultDTO.success();
        }

        /**
         * 获取用户数据权限
         *
         * @param property 资产标识
         * @return empty map
         * @author zc
         * @date 2023/11/22
         */
        @ApiId(1617)
        @GetMapping("/user")
        public Map<String, Boolean> userDataPermission(String property) {
            Integer tenantId = sessionContext.getTenantSymbolBO().getId();
            int propertyId = sessionContext.getPropertyId(property);
            int loginUserId = sessionContext.getLoginUserId();
            // 校验当前登录用户是否拥有数据权限
            boolean hasDataPermissionAccess = dataPermissionConfigService.verifyDataPermissionAccess(tenantId, propertyId, loginUserId);
            return Collections.singletonMap("dataPermissionValid", hasDataPermissionAccess);
        }

    }