# ClcCompareConfigServiceImpl.java

    package cloud.wconnect.frontoffice.service.clc.impl;

    import cloud.wconnect.commonlib.component.BeanMapper;
    import cloud.wconnect.commonlib.cons.BaseErrorCodeEnum;
    import cloud.wconnect.commonlib.cons.CommonErrorI18nConstant;
    import cloud.wconnect.commonlib.exception.BadRequestException;
    import cloud.wconnect.commonlib.pojo.dto.ListPagedDTO;
    import cloud.wconnect.commonlib.util.DateTimeUtils;
    import cloud.wconnect.frontoffice.entity.clc.ClcCompareConfig;
    import cloud.wconnect.frontoffice.mapper.clc.ClcCompareConfigMapper;
    import cloud.wconnect.frontoffice.pojo.dto.clc.CompareConfigDTO;
    import cloud.wconnect.frontoffice.pojo.query.clc.CompareConfigQuery;
    import cloud.wconnect.frontoffice.service.clc.IClcCompareConfigService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
    import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import org.apache.commons.lang3.StringUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;

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

    /**
     * <p>
     * 入住信息 与 clc 对比配置表 服务实现类
     * </p>
     *
     * @author yyt
     * @since 2023-07-24
     */
    @Service
    @DS("logbook")
    public class ClcCompareConfigServiceImpl extends ServiceImpl<ClcCompareConfigMapper, ClcCompareConfig> implements IClcCompareConfigService {

        @Autowired
        private BeanMapper beanMapper;

        /**
         * 查询配置规则
         *
         * @param propertyId 资产 id
         * @return 配置规则
         * @author yyt
         * @date 2023/7/24
         */
        @Override
        public List<ClcCompareConfig> list(Integer propertyId) {
            return list(new QueryWrapper<ClcCompareConfig>().lambda().eq(ClcCompareConfig::getPropertyId, propertyId));
        }

        /**
         * 校验存在
         *
         * @param propertyId 资产 id
         * @param roomType   房型
         * @param rate       房价
         * @param id         排除自身
         * @author yyt
         * @date 2023/7/25
         */
        @Override
        public void checkExist(Integer propertyId, String roomType, BigDecimal rate, Integer id) {
            ClcCompareConfig config = getOne(new QueryWrapper<ClcCompareConfig>().lambda()
                    .eq(ClcCompareConfig::getPropertyId, propertyId)
                    .eq(ClcCompareConfig::getRoomType, roomType).eq(ClcCompareConfig::getRate, rate)
                    .ne(Objects.nonNull(id), ClcCompareConfig::getId, id));
            if (Objects.nonNull(config)) {
                throw new BadRequestException(CommonErrorI18nConstant.DATA_EXISTED,
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(), "the config is exist");
            }
        }

        /**
         * 校验存在
         *
         * @param id 配置 id
         * @return 配置
         * @author yyt
         * @date 2023/7/25
         */
        @Override
        public ClcCompareConfig checkExist(int id) {
            ClcCompareConfig config = getById(id);
            if (Objects.isNull(config)) {
                throw new BadRequestException(CommonErrorI18nConstant.DATA_NOT_FOUND,
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(), "the config is not found;id:" + id);
            }
            return config;
        }

        /**
         * 分页查询数据
         *
         * @param propertyId 资产 id
         * @param query      查询条件
         * @return 配置规则
         * @author yyt
         * @date 2024/11/19
         **/
        @Override
        public Page<ClcCompareConfig> listPage(Integer propertyId, CompareConfigQuery query) {
            return page(new Page<>(query.getPage(), query.getSize()), new QueryWrapper<ClcCompareConfig>().lambda()
                    .eq(ClcCompareConfig::getPropertyId, propertyId)
                    .like(StringUtils.isNotEmpty(query.getRoomType()), ClcCompareConfig::getRoomType, query.getRoomType())
                    .like(Objects.nonNull(query.getRate()), ClcCompareConfig::getRate, query.getRate())
                    .orderByDesc(ClcCompareConfig::getId));
        }

        /**
         * 查询数据
         *
         * @param propertyId 资产 id
         * @param query      查询条件
         * @return 配置规则
         * @author yyt
         * @date 2024/11/19
         **/
        @Override
        public List<ClcCompareConfig> list(Integer propertyId, CompareConfigQuery query) {
            return list(new QueryWrapper<ClcCompareConfig>().lambda()
                    .eq(ClcCompareConfig::getPropertyId, propertyId)
                    .eq(StringUtils.isNotEmpty(query.getRoomType()), ClcCompareConfig::getRoomType, query.getRoomType())
                    .eq(Objects.nonNull(query.getRate()), ClcCompareConfig::getRate, query.getRate()));
        }

        /**
         * 删除配置
         *
         * @param id 配置 id
         * @author yyt
         * @date 2024/11/19
         **/
        @Override
        public void delete(int id) {
            removeById(id);
        }

        /**
         * 查询配置
         *
         * @param propertyId 资产 id
         * @param query      查询条件
         * @return 配置规则
         * @author yyt
         * @date 2026/1/19
         **/
        @Override
        public ListPagedDTO<CompareConfigDTO> listCompareConfigDTO(Integer propertyId, CompareConfigQuery query) {
            long total;
            List<ClcCompareConfig> list;
            if (Objects.nonNull(query.getPage()) && Objects.nonNull(query.getSize())) {
                Page<ClcCompareConfig> page = listPage(propertyId, query);
                total = page.getTotal();
                list = page.getRecords();
            } else {
                list = list(propertyId, query);
                total = list.size();
            }

            return new ListPagedDTO<>(total, beanMapper.mapList(list, CompareConfigDTO.class));
        }

        /**
         * 保存配置规则
         *
         * @param propertyId 资产 id
         * @param configDTO  配置规则
         * @param userId     操作人
         * @author yyt
         * @date 2026/1/19
         **/
        @Override
        public void save(Integer propertyId, CompareConfigDTO configDTO, int userId) {
            save(new ClcCompareConfig().setPropertyId(propertyId)
                    .setRoomType(configDTO.getRoomType())
                    .setRate(configDTO.getRate())
                    .setUpdatedBy(userId)
                    .setUpdatedTime(DateTimeUtils.nowTimestamp()));
        }

        /**
         * 更新配置规则
         *
         * @param id        配置 id
         * @param configDTO 配置规则
         * @param userId    操作人
         * @author yyt
         * @date 2026/1/19
         **/
        @Override
        public void update(int id, CompareConfigDTO configDTO, int userId) {
            updateById(new ClcCompareConfig().setId(id)
                    .setRoomType(configDTO.getRoomType())
                    .setRate(configDTO.getRate())
                    .setUpdatedBy(userId)
                    .setUpdatedTime(DateTimeUtils.nowTimestamp()));
        }

    }