# SvaProductivityRatioSettingsController.java

    package cloud.wconnect.workperformance.controller.sva;

    import cloud.wconnect.commonlib.annotation.ApiId;
    import cloud.wconnect.commonlib.component.SessionContext;
    import cloud.wconnect.commonlib.cons.BaseErrorCodeEnum;
    import cloud.wconnect.commonlib.cons.CommonErrorI18nConstant;
    import cloud.wconnect.commonlib.exception.BadRequestException;
    import cloud.wconnect.commonlib.exception.ForbiddenException;
    import cloud.wconnect.commonlib.pojo.dto.result.ResultDTO;
    import cloud.wconnect.commonlib.util.UploadUtils;
    import cloud.wconnect.workperformance.entity.sva.SvaProductivityRatioSettings;
    import cloud.wconnect.workperformance.pojo.dto.sva.productivityratiosettings.ProductivityRatioSettingsUploadDTO;
    import cloud.wconnect.workperformance.service.sva.setting.ISvaProductivityRatioSettingsService;
    import com.google.common.collect.Lists;
    import com.google.common.collect.Sets;
    import jakarta.validation.Valid;
    import org.apache.commons.io.FileUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.web.bind.annotation.*;
    import org.springframework.web.multipart.MultipartFile;

    import java.math.BigDecimal;
    import java.math.RoundingMode;
    import java.util.List;

    /**
     * Productivity ratio settings Controller
     *
     * @author lichen
     * @date 2026/3/6
     */
    @RestController
    @RequestMapping("/sva/setting/productivity-ratio")
    public class SvaProductivityRatioSettingsController {

        @Autowired
        private SessionContext sessionContext;
        @Autowired
        private ISvaProductivityRatioSettingsService svaProductivityRatioSettingsService;
        @Value("${file-max-size.import-report.sva}")
        private Long fileMaxSize;

        /**
         * 上传Productivity Ratio Excel
         *
         * @param uploadDTO 上传DTO
         * @return cloud.wconnect.commonlib.pojo.dto.result.ResultDTO<java.lang.Void>
         * @author lichen
         * @date 2026/3/6
         */
        @ApiId(1698)
        @PostMapping
        public ResultDTO<Void> upload(@Valid ProductivityRatioSettingsUploadDTO uploadDTO) {
            List<Integer> propertyIds = uploadDTO.getPropertyIds();
            if (!Sets.newHashSet(sessionContext.getPropertyIds()).containsAll(propertyIds)) {
                throw new ForbiddenException(BaseErrorCodeEnum.DATA_FORBIDDEN.getI18nCode(),
                        BaseErrorCodeEnum.DATA_FORBIDDEN.getCode(),
                        "Cannot be operated others property data; propertyIdRange: " + propertyIds);
            }

            MultipartFile file = uploadDTO.getFile();
            //校验文件后缀
            UploadUtils.checkFileFormat(file, Lists.newArrayList("xls", "xlsx"));

            //校验文件大小
            if (file.getSize() > fileMaxSize) {
                BigDecimal limitSize = BigDecimal.valueOf(fileMaxSize).divide(BigDecimal.valueOf(FileUtils.ONE_MB), 2, RoundingMode.HALF_UP);
                BigDecimal fileSizeBigDecimal = BigDecimal.valueOf(file.getSize()).divide(BigDecimal.valueOf(FileUtils.ONE_MB), 2, RoundingMode.HALF_UP);
                throw new BadRequestException(CommonErrorI18nConstant.FILE_SIZE_ERROR,
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        "Upload file exceeded limit;fileSize:" + fileSizeBigDecimal.stripTrailingZeros().toPlainString() +
                                "MB, maxSize:" + limitSize.stripTrailingZeros().toPlainString() + "MB");
            }
            // 上传文件
            svaProductivityRatioSettingsService.upload(uploadDTO);
            return ResultDTO.success();
        }

        /**
         * 获取指定年份的Productivity Ratio
         *
         * @param property 属性
         * @param year     年份
         * @return cloud.wconnect.commonlib.pojo.dto.result.ResultDTO<java.util.List < cloud.wconnect.workperformance.entity.sva.SvaProductivityRatioSettings>>
         * @author lichen
         * @date 2026/3/6
         */
        @ApiId(1699)
        @GetMapping
        public ResultDTO<List<SvaProductivityRatioSettings>> listByYear(String property, @RequestParam Integer year) {
            int propertyId = sessionContext.getPropertyId(property);
            List<SvaProductivityRatioSettings> svaProductivityRatioSettings = svaProductivityRatioSettingsService.listByYears(propertyId, Sets.newHashSet(year));
            return ResultDTO.success(svaProductivityRatioSettings);
        }

    }