# CompareConfigController.java

    package cloud.wconnect.frontoffice.controller.clc;

    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.frontoffice.entity.clc.ClcCompareConfig;
    import cloud.wconnect.frontoffice.pojo.dto.clc.CompareConfigDTO;
    import cloud.wconnect.frontoffice.pojo.query.clc.CompareConfigQuery;
    import cloud.wconnect.frontoffice.service.clc.IClcCompareConfigService;
    import jakarta.validation.Valid;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;

    import java.util.Collections;
    import java.util.Map;

    /**
     * 控制层：Compare Config
     *
     * @author yyt
     * 2023/7/25
     */
    @RestController
    @RequestMapping("/clc/compare-config")
    public class CompareConfigController {

        @Autowired
        private IClcCompareConfigService clcCompareConfigService;
        @Autowired
        private SessionContext sessionContext;

        /**
         * 获取报告对比配置规则
         *
         * @param property 资产标识
         * @param query    查询条件
         * @return 配置规则
         * @author yyt
         * @date 2023/7/25
         */
        @ApiId(489)
        @GetMapping
        public ListPagedDTO<CompareConfigDTO> list(String property, CompareConfigQuery query) {
            Integer propertyId = sessionContext.getPropertyId(property);
            return clcCompareConfigService.listCompareConfigDTO(propertyId, query);
        }

        /**
         * 新增、编辑配置规则
         *
         * @param id        配置 id
         * @param property  资产标识
         * @param configDTO 配置规则
         * @return {}
         * @author yyt
         * @date 2023/7/25
         */
        @ApiId(490)
        @PostMapping("/{id:\\d+}")
        public Map save(@PathVariable int id, String property, @Valid @RequestBody CompareConfigDTO configDTO) {
            Integer propertyId = sessionContext.getPropertyId(property);
            int userId = sessionContext.getLoginUserId();
            if (id == 0) {
                clcCompareConfigService.checkExist(propertyId, configDTO.getRoomType(), configDTO.getRate(), null);

                clcCompareConfigService.save(propertyId, configDTO, userId);
            } else {
                ClcCompareConfig config = clcCompareConfigService.checkExist(id);

                clcCompareConfigService.checkExist(propertyId, configDTO.getRoomType(), configDTO.getRate(), id);

                checkBelong(config, propertyId);

                clcCompareConfigService.update(id, configDTO, userId);
            }

            return Collections.emptyMap();
        }

        /**
         * 删除配置规则
         *
         * @param id       配置 id
         * @param property 资产标识
         * @return {}
         * @author yyt
         * @date 2023/7/25
         */
        @ApiId(491)
        @DeleteMapping("/{id:\\d+}")
        public Map delete(@PathVariable int id, String property) {
            ClcCompareConfig config = clcCompareConfigService.checkExist(id);

            checkBelong(config, sessionContext.getPropertyId(property));

            clcCompareConfigService.delete(id);

            return Collections.emptyMap();
        }

        /**
         * 校验数据归属
         *
         * @param compareConfig 配置规则
         * @param propertyId    资产 id
         * @author yyt
         * @date 2023/7/25
         */
        private void checkBelong(ClcCompareConfig compareConfig, Integer propertyId) {
            if (!compareConfig.getPropertyId().equals(propertyId)) {
                throw new ForbiddenException(BaseErrorCodeEnum.DATA_FORBIDDEN.getI18nCode(),
                        BaseErrorCodeEnum.DATA_FORBIDDEN.getCode(), "the property id parameter is different from " +
                        "the property id of the compareConfig;propertyId:" + propertyId + ",compareConfigPropertyId:" +
                        compareConfig.getPropertyId() + ",compareConfigId:" + compareConfig.getId());
            }
        }

    }