# TaCompareOtaContractPriceConfigController.java

    package cloud.wconnect.finance.controller.ta;

    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.finance.cons.ta.TaCompareConfigCons;
    import cloud.wconnect.finance.entity.ta.TaCompareOtaContractPriceConfig;
    import cloud.wconnect.finance.pojo.dto.ta.compare.TaCompareListDTO;
    import cloud.wconnect.finance.pojo.dto.ta.compare.TaCompareOtaContractPriceConfigDTO;
    import cloud.wconnect.finance.pojo.query.ta.TaCompareListQuery;
    import cloud.wconnect.finance.service.ta.ITaCompareOtaContractPriceConfigService;
    import com.google.common.collect.ImmutableMap;
    import com.google.common.collect.Lists;
    import org.springframework.context.i18n.LocaleContextHolder;
    import org.springframework.web.bind.annotation.*;

    import jakarta.annotation.Resource;
    import jakarta.validation.Valid;

    import java.util.*;

    /**
     * <p>
     * TA Report Compare OTA Contract Price Config 前端控制器
     * </p>
     *
     * @author zc
     * @since 2023-07-07
     */
    @RestController
    @RequestMapping("/ta/report-compare/ota-contract-price/config")
    public class TaCompareOtaContractPriceConfigController {
        @Resource
        private ITaCompareOtaContractPriceConfigService taCompareOtaContractPriceConfigService;
        @Resource
        private SessionContext sessionContext;
        @Resource
        private I18nMessageUtils i18nMessageUtils;

        /**
         * 获取目标日期的 OTA 合约价配置
         *
         * @param property   资产标识
         * @param targetDate 目标日期
         * @param isGreater  0: 获取大于目标日期的数据；1: 获取小于目标日期的数据；2: 获取等于目标日期的数据
         * @return TaCompareOtaContractPriceConfigDTO
         * @author zc
         * @date 2023/7/7
         */
        @ApiId(429)
        @GetMapping
        public TaCompareOtaContractPriceConfigDTO getByTargetDate(@RequestParam String property, @RequestParam long targetDate,
                                                                  @RequestParam(required = false) Integer isGreater) {
            int tenantId = sessionContext.getTenantSymbolBO().getId();
            int propertyId = sessionContext.getPropertyId(property);

            if (targetDate == 0L) {
                return taCompareOtaContractPriceConfigService.getLatest(tenantId, propertyId);
            }
            if (Objects.isNull(isGreater)) {
                return taCompareOtaContractPriceConfigService.getCurrent(tenantId, propertyId, targetDate);
            }
            if (!Lists.newArrayList(TaCompareConfigCons.LESS_THAN, TaCompareConfigCons.GREATER_THAN,
                    TaCompareConfigCons.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 taCompareOtaContractPriceConfigService.getEffective(tenantId, propertyId, targetDate, isGreater);
        }

        /**
         * 保存 OTA 合约价配置
         *
         * @param property               资产标识
         * @param contractPriceConfigDTO OTA 合约价配置数据
         * @return id
         * @author zc
         * @date 2023/7/7
         */
        @ApiId(430)
        @PostMapping
        public Map<String, Long> save(@RequestParam String property, @RequestBody @Valid TaCompareOtaContractPriceConfigDTO contractPriceConfigDTO) {
            int tenantId = sessionContext.getTenantSymbolBO().getId();
            int propertyId = sessionContext.getPropertyId(property);

            if (Objects.isNull(contractPriceConfigDTO.getId())) {
                // 新增数据
                return ImmutableMap.of("id", taCompareOtaContractPriceConfigService.insert(tenantId, propertyId, contractPriceConfigDTO, sessionContext.getLoginUserId()));
            }

            // 校验是否存在
            TaCompareOtaContractPriceConfig originContractPriceConfig = taCompareOtaContractPriceConfigService
                    .checkExist(contractPriceConfigDTO.getId());

            // 校验数据归属
            taCompareOtaContractPriceConfigService.checkBelong(tenantId, propertyId, originContractPriceConfig);

            // 更新数据
            taCompareOtaContractPriceConfigService.update(tenantId, propertyId, originContractPriceConfig, contractPriceConfigDTO, sessionContext.getLoginUserId());

            return ImmutableMap.of("id", contractPriceConfigDTO.getId());
        }

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

            // 校验是否存在
            TaCompareOtaContractPriceConfig originContractPriceConfig = taCompareOtaContractPriceConfigService.checkExist(id);

            // 校验数据归属
            taCompareOtaContractPriceConfigService.checkBelong(tenantId, propertyId, originContractPriceConfig);

            taCompareOtaContractPriceConfigService.removeById(id);

            return Collections.emptyMap();
        }

        /**
         * 获取 OTA 合约价配置数据集合
         *
         * @param property           资产标识
         * @param taCompareListQuery 查询条件
         * @return TaCompareListDTO 集合
         * @author zc
         * @date 2023/7/7
         */
        @ApiId(432)
        @GetMapping("/list")
        public ListPagedDTO<TaCompareListDTO> list(@RequestParam String property, @Valid TaCompareListQuery taCompareListQuery) {
            int tenantId = sessionContext.getTenantSymbolBO().getId();
            int propertyId = sessionContext.getPropertyId(property);

            return taCompareOtaContractPriceConfigService.list(tenantId, propertyId, taCompareListQuery);
        }
    }