# SvaDepartmentController.java

    package cloud.wconnect.workperformance.controller.sva;

    import cloud.wconnect.commonlib.annotation.ApiId;
    import cloud.wconnect.commonlib.component.SessionContext;
    import cloud.wconnect.commonlib.cons.BaseConstant;
    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.pojo.dto.result.ResultDTO;
    import cloud.wconnect.commonlib.redis.PropertyI18nRedisService;
    import cloud.wconnect.commonlib.util.DateTimeUtils;
    import cloud.wconnect.workperformance.entity.sva.SvaProductivityRatioSettings;
    import cloud.wconnect.workperformance.entity.sva.setting.DepCodeToCategory;
    import cloud.wconnect.workperformance.pojo.dto.sva.category.DepartmentDTO;
    import cloud.wconnect.workperformance.pojo.dto.sva.recommendhours.RecommendHoursDTO;
    import cloud.wconnect.workperformance.service.sva.setting.IDepCategoryRecommendHourService;
    import cloud.wconnect.workperformance.service.sva.setting.IDepCodeToCategoryService;
    import cloud.wconnect.workperformance.service.sva.setting.ISvaProductivityRatioSettingsService;
    import com.google.common.collect.Sets;
    import jakarta.annotation.Resource;
    import org.apache.commons.collections.CollectionUtils;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;

    import java.util.*;

    /**
     * 控制器类：部门
     *
     * @author lichen
     * @date 2026/3/6
     */
    @RestController
    @RequestMapping("/sva")
    public class SvaDepartmentController {

        @Resource
        private IDepCategoryRecommendHourService depCategoryRecommendHourService;
        @Resource
        private IDepCodeToCategoryService depCodeToCategoryService;
        @Resource
        private SessionContext sessionContext;
        @Resource
        private PropertyI18nRedisService propertyI18nRedisService;
        @Resource
        private ISvaProductivityRatioSettingsService svaProductivityRatioSettingsService;

        /**
         * 获取所有部门
         *
         * @param property 资产标识
         * @param year     年份
         * @return 部门列表
         * @author lichen
         * @date 2026/3/6
         */
        @ApiId(1700)
        @GetMapping("/all-department")
        public ResultDTO<List<DepartmentDTO>> listAllDepartments(String property, @RequestParam int year) {
            int propertyId = sessionContext.getPropertyId(property);
            PropertyDTO propertyDTO = propertyI18nRedisService.get(propertyId);

            // 获取最新的Recommend Hour版本
            RecommendHoursDTO latest = depCategoryRecommendHourService.getLatest(propertyId);
            List<DepCodeToCategory> depCodeToCategorys = Collections.emptyList();
            if (Objects.nonNull(latest)) {
                // 获取版本
                int version = Integer.parseInt(DateTimeUtils.localDateToStr(DateTimeUtils.longToLocalDate(latest.getStartingDate(), propertyDTO.getTimezone()),
                        BaseConstant.DATE_FORMAT_YYYYMMDD));
                // 查询指定版本的ADP部门
                depCodeToCategorys = depCodeToCategoryService.listByVersion(propertyId, version);
            }

            List<SvaProductivityRatioSettings> settingsList = svaProductivityRatioSettingsService.listByYears(propertyId, Sets.newHashSet(year));

            if (CollectionUtils.isEmpty(settingsList) && CollectionUtils.isEmpty(depCodeToCategorys)) {
                return ResultDTO.success(Collections.emptyList());
            }

            // 合并去重部门
            List<DepartmentDTO> departmentDTOS = depCodeToCategoryService.distinctMergeDepartment(depCodeToCategorys, settingsList);

            return ResultDTO.success(departmentDTOS);
        }

    }