# SvaDataPermissionController.java

    package cloud.wconnect.workperformance.controller.sva;

    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.sva.SvaDataPermissionConfig;
    import cloud.wconnect.workperformance.pojo.dto.sva.datapermission.SvaDataPermissionConfigDTO;
    import cloud.wconnect.workperformance.pojo.query.sva.datapermission.SvaDataPermissionConfigQuery;
    import cloud.wconnect.workperformance.service.sva.scheduleactual.ISvaDataPermissionConfigService;
    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>
     * sva 数据权限配置 前端控制器
     * </p>
     *
     * @author zc
     * @since 2023-06-21
     */
    @RestController
    @RequestMapping("/sva/schedule-actual/data-permission")
    public class SvaDataPermissionController {
        @Resource
        private ISvaDataPermissionConfigService svaDataPermissionConfigService;
        @Resource
        private SessionContext sessionContext;
        @Resource
        private DataPermissionConfigInternalService dataPermissionConfigInternalService;
        @Resource
        private I18nMessageUtils i18nMessageUtils;

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

            svaDataPermissionConfigService.dataPermissionFlushStatus(tenantId, propertyId);

            return Collections.emptyMap();
        }

        /**
         * 数据权限集合
         *
         * @param property 资产标识
         * @param query    查询条件
         * @return 数据权限集合
         * @author zc
         * @date 2023/6/21
         */
        @ApiId(370)
        @GetMapping
        public ListPagedDTO<SvaDataPermissionConfigDTO> list(@RequestParam String property,
                                                             @Valid SvaDataPermissionConfigQuery query) {
            if (Objects.isNull(query.getIdentityType()) && Objects.nonNull(query.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 svaDataPermissionConfigService.list(tenantId, propertyId, query);
        }

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

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

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

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

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

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

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

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

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

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

            svaDataPermissionConfigService.update(originConfig, configDTO, loginUserId);

            return Collections.emptyMap();
        }

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

            // 校验数据是否存在
            SvaDataPermissionConfig originConfig = svaDataPermissionConfigService.checkExist(id);

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

            svaDataPermissionConfigService.removeById(id);

            return Collections.emptyMap();
        }

        /**
         * 允许用户申请的数据权限
         *
         * @param id       数据权限 id
         * @param property 资产标识
         * @return empty map
         * @author zc
         * @date 2023/6/22
         */
        @ApiId(374)
        @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);

            // 校验数据是否存在
            SvaDataPermissionConfig originConfig = svaDataPermissionConfigService.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");
            }

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

            svaDataPermissionConfigService.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(385)
        @PutMapping("/category/apply")
        public ResultDTO<Void> apply(@RequestParam String property) {
            int tenantId = sessionContext.getTenantSymbolBO().getId();
            int propertyId = sessionContext.getPropertyId(property);
            int loginUserId = sessionContext.getLoginUserId();

            // 校验是否已有数据权限
            if (svaDataPermissionConfigService.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);
            }

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

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

            svaDataPermissionConfigService.apply(tenantId, propertyId, loginUserId);

            return ResultDTO.success();
        }
    }