# SalaryPayConfigController.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.cons.CommonFieldI18nConstant;
    import cloud.wconnect.commonlib.exception.BadRequestException;
    import cloud.wconnect.commonlib.pojo.dto.ListPagedDTO;
    import cloud.wconnect.commonlib.util.I18nMessageUtils;
    import cloud.wconnect.workperformance.entity.sva.setting.SalaryPayConfig;
    import cloud.wconnect.workperformance.pojo.dto.sva.salarypay.SalaryPayDTO;
    import cloud.wconnect.workperformance.pojo.dto.sva.salarypay.SalaryPayHistoryDTO;
    import cloud.wconnect.workperformance.service.sva.setting.ISalaryPayConfigService;
    import com.google.common.collect.ImmutableMap;
    import com.google.common.collect.Lists;
    import jakarta.annotation.Resource;
    import jakarta.validation.Valid;
    import org.springframework.context.i18n.LocaleContextHolder;
    import org.springframework.util.Assert;
    import org.springframework.web.bind.annotation.*;

    import java.util.*;

    /**
     * <p>
     * 控制器类：Salary Pay 配置
     * </p>
     *
     * @author zc
     * @since 2023-03-10
     */
    @RestController
    @RequestMapping("/sva/setting/salary/pay")
    public class SalaryPayConfigController {
        @Resource
        private ISalaryPayConfigService salaryPayConfigService;
        @Resource
        private SessionContext sessionContext;
        @Resource
        private I18nMessageUtils i18nMessageUtils;

        /**
         * 保存 salary pay 数据
         *
         * @param property     资产标识
         * @param salaryPayDTO salary pay 数据
         * @return salary pay id
         * @author zc
         * @date 2023/3/11
         */
        @ApiId(83)
        @PostMapping
        public Map<Object, Object> save(@RequestParam String property, @RequestBody @Valid SalaryPayDTO salaryPayDTO) {
            int propertyId = sessionContext.getPropertyId(property);
            if (Objects.isNull(salaryPayDTO.getId())) {
                return ImmutableMap.of("id", salaryPayConfigService.insert(propertyId,
                        sessionContext.getLoginUserId(), salaryPayDTO));
            }

            SalaryPayConfig originSalaryPay = salaryPayConfigService.getById(salaryPayDTO.getId());
            if (Objects.isNull(originSalaryPay)) {
                throw new BadRequestException(CommonErrorI18nConstant.DATA_NOT_FOUND,
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        "The salary pay to be update is not found; salaryPayId: " + salaryPayDTO.getId());
            }
            // 校验数据归属
            salaryPayConfigService.checkBelong(propertyId, originSalaryPay);
            return ImmutableMap.of("id", salaryPayConfigService.update(propertyId,
                    sessionContext.getLoginUserId(), salaryPayDTO));
        }

        /**
         * 删除 salary pay 数据
         *
         * @param property 资产标识
         * @param id       salary pay id
         * @return empty map
         * @author zc
         * @date 2023/3/12
         */
        @ApiId(85)
        @DeleteMapping("/{id:\\d+}")
        public Map<Object, Object> remove(@RequestParam String property, @PathVariable int id) {
            SalaryPayConfig originSalaryPay = salaryPayConfigService.getById(id);
            if (Objects.isNull(originSalaryPay)) {
                throw new BadRequestException(CommonErrorI18nConstant.DATA_NOT_FOUND,
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        "The salary pay to be remove is not found; salaryPayId: " + id);
            }
            int propertyId = sessionContext.getPropertyId(property);
            // 校验数据归属
            salaryPayConfigService.checkBelong(propertyId, originSalaryPay);
            salaryPayConfigService.removeById(id);
            return Collections.emptyMap();
        }

        /**
         * 获取 Salary Pay 历史版本列表
         *
         * @param property 资产标识
         * @param page     当前页码
         * @param size     每页条数
         * @return Salary Pay 历史数据
         * @author zc
         * @date 2023/3/12
         */
        @ApiId(86)
        @GetMapping("/history")
        public ListPagedDTO<SalaryPayHistoryDTO> listByPaged(@RequestParam String property, @RequestParam int page, @RequestParam int size) {
            Assert.isTrue(page > 0, "The page must greater than 0");
            Assert.isTrue(size > 0, "The size must greater than 0");

            int propertyId = sessionContext.getPropertyId(property);
            return salaryPayConfigService.listHistories(propertyId, page, size);
        }

        /**
         * 获取目标日期的 Salary Pay 数据
         *
         * @param property   资产标识
         * @param targetDate 目标日期
         * @param isGreater  0: 获取大于目标日期的数据；1: 获取小于目标日期的数据；2: 获取等于目标日期的数据
         * @return Salary Pay
         * @author zc
         * @date 2023/3/12
         */
        @ApiId(87)
        @GetMapping
        public SalaryPayDTO getByTargetDate(@RequestParam String property, @RequestParam long targetDate,
                                            @RequestParam(required = false) Integer isGreater) {
            int propertyId = sessionContext.getPropertyId(property);

            if (targetDate == 0L) {
                return salaryPayConfigService.getLatest(propertyId);
            }
            if (Objects.isNull(isGreater)) {
                return salaryPayConfigService.getCurrent(propertyId, targetDate);
            }
            if (!Lists.newArrayList(SalaryPayDTO.LESS_THAN, SalaryPayDTO.GREATER_THAN,
                    SalaryPayDTO.EQUAL_TO).contains(isGreater)) {
                throw new BadRequestException(CommonErrorI18nConstant.MUST_IN_LIST,
                        i18nMessageUtils.getErrorParamMapNotNeedTranslate("isGreater",
                                List.of("less-than", "greater-than", "equal-to")),
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        "The isGreater must be in [0:less than; 1:greater than; 2:equal to]");
            }
            return salaryPayConfigService.getEffective(propertyId, targetDate, isGreater);
        }
    }