# ServiceRequestCenterController.java

    package cloud.wconnect.it.controller.servicerequestcenter;

    import cloud.wconnect.commonlib.annotation.ApiId;
    import cloud.wconnect.commonlib.component.SessionContext;
    import cloud.wconnect.commonlib.cons.BaseErrorCodeEnum;
    import cloud.wconnect.commonlib.exception.ForbiddenException;
    import cloud.wconnect.commonlib.pojo.dto.ListPagedDTO;
    import cloud.wconnect.commonlib.pojo.dto.UserBasicInfoDTO;
    import cloud.wconnect.commonlib.pojo.dto.UserNameDTO;
    import cloud.wconnect.commonlib.util.I18nMessageUtils;
    import cloud.wconnect.it.entity.servicerequestcenter.ServiceRequestCenter;
    import cloud.wconnect.it.pojo.dto.servicerequestcenter.AllocateToOneSelfDTO;
    import cloud.wconnect.it.pojo.dto.servicerequestcenter.ServiceRequestCenterDTO;
    import cloud.wconnect.it.pojo.dto.servicerequestcenter.ServiceRequestCenterStatisticsDTO;
    import cloud.wconnect.it.pojo.dto.servicerequestcenter.StatisticsCurrentDepartmentDTO;
    import cloud.wconnect.it.pojo.query.servicerequestcenter.*;
    import cloud.wconnect.it.service.servicerequestcenter.IServiceRequestCenterCheckService;
    import cloud.wconnect.it.service.servicerequestcenter.IServiceRequestCenterService;
    import jakarta.validation.Valid;
    import org.apache.commons.lang3.StringUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;

    import java.util.Arrays;
    import java.util.List;

    /**
     * <p>
     *  前端控制器
     * </p>
     *
     * @author yyt
     * @since 2025-12-16
     */
    @RestController
    @RequestMapping("/dashboard/service-request-center")
    public class ServiceRequestCenterController {

        @Autowired
        private IServiceRequestCenterService serviceRequestCenterService;
        @Autowired
        private IServiceRequestCenterCheckService serviceRequestCenterCheckService;
        @Autowired
        private SessionContext sessionContext;
        @Autowired
        private I18nMessageUtils i18nMessageUtils;

        /**
         * 1668 - my request 列表
         *
         * @param query 查询条件
         * @return my request 列表
         * @author yyt
         * @date 2025/12/17
         **/
        @ApiId(1668)
        @GetMapping("/my-request")
        public ListPagedDTO<ServiceRequestCenterDTO> myRequest(@Valid ServiceRequestCenterQuery query) {
            return serviceRequestCenterService.myRequest(sessionContext.getTenantSymbolBO().getId(), sessionContext.getPropertyIds(), query, sessionContext.getLoginUserId());
        }

        /**
         * 1673 - 统计分配给自己的 request
         *
         * @return 统计结果
         * @author yyt
         * @date 2025/12/17
         **/
        @ApiId(1673)
        @GetMapping("/allocate-statistics-to-oneself")
        public AllocateToOneSelfDTO countAllocateToOneSelf() {
            int userId = sessionContext.getLoginUserId();

            // 校验用户是否是 员工 / 管理员
            Integer tenantId = sessionContext.getTenantSymbolBO().getId();
            serviceRequestCenterCheckService.checkUserIsStaffOrManager(tenantId, userId);

            return serviceRequestCenterService.countAllocateToOneSelf(tenantId, sessionContext.getPropertyIds(), userId);
        }

        /**
         * 1674 - 获取分配给自己的 request 列表
         *
         * @return request 列表
         * @author yyt
         * @date 2025/12/17
         **/
        @ApiId(1674)
        @GetMapping("/assign-to-oneself")
        public ListPagedDTO<ServiceRequestCenterDTO> allocateToOneSelf(@Valid AllocateToOneSelfQuery query) {
            int userId = sessionContext.getLoginUserId();

            // 校验用户是否是 员工 / 管理员
            Integer tenantId = sessionContext.getTenantSymbolBO().getId();
            int identity = serviceRequestCenterCheckService.checkUserIsStaffOrManager(tenantId, userId);
            return serviceRequestCenterService.allocateToOneSelf(tenantId, sessionContext.getPropertyIds(), userId, query, identity);
        }

        /**
         * 1677 - 查询可分配的人员列表
         *
         * @return 人员列表
         * @author yyt
         * @date 2025/12/18
         **/
        @ApiId(1677)
        @GetMapping("/assignable-personnel")
        public ListPagedDTO<UserBasicInfoDTO> assignablePersonnel(@Valid AssignablePersonnelQuery query) {
            int userId = sessionContext.getLoginUserId();
            ServiceRequestCenter request = serviceRequestCenterCheckService.checkExist(query.getRequestId());
            Integer tenantId = sessionContext.getTenantSymbolBO().getId();
            serviceRequestCenterCheckService.checkProperty(tenantId, request, sessionContext.getPropertyIds());
            // 校验用户是否是 员工 / 管理员
            serviceRequestCenterCheckService.checkUserIsStaffOrManager(tenantId, userId, request.getType());
            return serviceRequestCenterService.assignablePersonnel(tenantId, userId, query, request);
        }

        /**
         * 1679 - all request 列表
         *
         * @param query    查询条件
         * @return all request 列表
         * @author yyt
         * @date 2025/12/18
         **/
        @ApiId(1679)
        @GetMapping("/all-request")
        public ListPagedDTO<ServiceRequestCenterDTO> allRequest(@Valid ServiceRequestCenterQuery query) {
            String propertyIds = query.getPropertyIds();
            if (StringUtils.isNotEmpty(propertyIds)) {
                List<Integer> propertyIdList = Arrays.stream(query.getPropertyIds().split(",")).map(Integer::parseInt).toList();
                serviceRequestCenterCheckService.checkPropertyIds(propertyIdList, sessionContext.getPropertyIds());
            }

            // 校验用户身份（管理员）
            Integer tenantId = sessionContext.getTenantSymbolBO().getId();
            checkUserIsManager(tenantId, sessionContext.getLoginUserId());

            return serviceRequestCenterService.allRequest(tenantId, sessionContext.getPropertyIds(), query, sessionContext.getLoginUserId());
        }

        /**
         * 1684 - 查询创建人列表
         *
         * @param query 查询条件
         * @return 创建人列表
         * @author yyt
         * @date 2025/12/18
         **/
        @ApiId(1684)
        @GetMapping("/creator")
        public ListPagedDTO<UserNameDTO> listCreator(@Valid ListCreatorQuery query) {
            List<Integer> propertyIds = Arrays.stream(query.getPropertyIds().split(",")).map(Integer::parseInt).toList();
            serviceRequestCenterCheckService.checkPropertyIds(propertyIds, sessionContext.getPropertyIds());
            query.setPropertyIdList(propertyIds);

            // 校验用户身份（管理员）
            Integer tenantId = sessionContext.getTenantSymbolBO().getId();
            checkUserIsManager(tenantId, sessionContext.getLoginUserId());

            return serviceRequestCenterService.listCreator(tenantId, propertyIds, query);
        }

        /**
         * 1680 - statistics 信息
         *
         * @param query 查询条件
         * @return statistics 信息
         * @author yyt
         * @date 2025/12/18
         **/
        @ApiId(1680)
        @GetMapping("/statistics")
        public ServiceRequestCenterStatisticsDTO statistics(@Valid ServiceRequestCenterStatisticsQuery query) {
            List<Integer> propertyIdsForLoginUser = sessionContext.getPropertyIds();
            String propertyIds = query.getPropertyIds();
            if (StringUtils.isNotEmpty(propertyIds)) {
                List<Integer> selectPropertyIds = Arrays.stream(propertyIds.split(",")).map(Integer::parseInt).toList();
                serviceRequestCenterCheckService.checkPropertyIds(selectPropertyIds, propertyIdsForLoginUser);
                query.setPropertyIdList(selectPropertyIds);
            }

            // 校验用户身份（管理员）
            Integer tenantId = sessionContext.getTenantSymbolBO().getId();
            int userId = sessionContext.getLoginUserId();
            checkUserIsManager(tenantId, userId);

            return serviceRequestCenterService.statistics(tenantId, propertyIdsForLoginUser, userId, query);
        }

        /**
         * 1681 - 当前部门下人员 statistics 信息
         *
         * @param query 查询条件
         * @return statistics 信息
         * @author yyt
         * @date 2025/12/19
         **/
        @ApiId(1681)
        @GetMapping("/statistics/current-department-people")
        public ListPagedDTO<StatisticsCurrentDepartmentDTO> departmentStatistics(@Valid StatisticsCurrentDepartmentQuery query) {
            List<Integer> propertyIdsForLoginUser = sessionContext.getPropertyIds();
            String propertyIds = query.getPropertyIds();
            if (StringUtils.isNotEmpty(propertyIds)) {
                List<Integer> selectPropertyIds = Arrays.stream(propertyIds.split(",")).map(Integer::parseInt).toList();
                serviceRequestCenterCheckService.checkPropertyIds(selectPropertyIds, propertyIdsForLoginUser);
                query.setPropertyIdList(selectPropertyIds);
            }

            // 校验用户身份（管理员）
            Integer tenantId = sessionContext.getTenantSymbolBO().getId();
            int userId = sessionContext.getLoginUserId();
            checkUserIsManager(tenantId, userId);

            return serviceRequestCenterService.departmentStatistics(tenantId, propertyIdsForLoginUser, userId, query);
        }

        /**
         * 校验用户是否是管理员
         *
         * @param tenantId 租户id
         * @param userId   用户id
         * @author yyt
         * @date 2025/12/18
         **/
        private void checkUserIsManager(int tenantId, int userId) {
            if (!serviceRequestCenterCheckService.checkUserIsManager(tenantId, userId)) {
                throw new ForbiddenException(BaseErrorCodeEnum.DATA_FORBIDDEN.getI18nCode(),
                        BaseErrorCodeEnum.DATA_FORBIDDEN.getCode(), "you are not a manager");
            }
        }

    }