# ClcGuestInhFilterConfigServiceImpl.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.ClcGuestInhFilterConfig;
    import cloud.wconnect.frontoffice.mapper.clc.ClcGuestInhFilterConfigMapper;
    import cloud.wconnect.frontoffice.pojo.dto.clc.ImportGuestConfigDTO;
    import cloud.wconnect.frontoffice.pojo.query.clc.ImportGuestConfigQuery;
    import cloud.wconnect.frontoffice.service.clc.IClcGuestInhFilterConfigService;
    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.util.List;
    import java.util.Objects;
    import java.util.stream.Collectors;

    /**
     * <p>
     * 导入入住信息过滤配置表 服务实现类
     * </p>
     *
     * @author yyt
     * @since 2023-07-24
     */
    @Service
    @DS("logbook")
    public class ClcGuestInhFilterConfigServiceImpl extends ServiceImpl<ClcGuestInhFilterConfigMapper, ClcGuestInhFilterConfig> implements IClcGuestInhFilterConfigService {

        @Autowired
        private BeanMapper beanMapper;

        /**
         * 校验存在
         *
         * @param propertyId 资产 id
         * @param rateCode   rate code
         * @param id         排除自身
         * @author yyt
         * @date 2023/7/25
         */
        @Override
        public void checkExist(Integer propertyId, String rateCode, Integer id) {
            ClcGuestInhFilterConfig config = getOne(new QueryWrapper<ClcGuestInhFilterConfig>().lambda()
                    .eq(ClcGuestInhFilterConfig::getPropertyId, propertyId)
                    .eq(ClcGuestInhFilterConfig::getRateCode, rateCode)
                    .ne(Objects.nonNull(id), ClcGuestInhFilterConfig::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 ClcGuestInhFilterConfig checkExist(int id) {
            ClcGuestInhFilterConfig 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<ClcGuestInhFilterConfig> listPage(Integer propertyId, ImportGuestConfigQuery query) {
            return page(new Page<>(query.getPage(), query.getSize()), new QueryWrapper<ClcGuestInhFilterConfig>().lambda()
                    .eq(ClcGuestInhFilterConfig::getPropertyId, propertyId)
                    .like(StringUtils.isNotEmpty(query.getRateCode()), ClcGuestInhFilterConfig::getRateCode, query.getRateCode())
                    .orderByDesc(ClcGuestInhFilterConfig::getId));
        }

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

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

        /**
         * 获取导入 guest in house 报告配置规则
         *
         * @param propertyId 资产 id
         * @param query      查询条件
         * @return 配置规则
         * @author yyt
         * @date 2026/1/19
         **/
        @Override
        public ListPagedDTO<ImportGuestConfigDTO> listImportGuestConfigDTO(Integer propertyId, ImportGuestConfigQuery query) {
            long total;
            List<ClcGuestInhFilterConfig> list;
            if (Objects.nonNull(query.getPage()) && Objects.nonNull(query.getSize())) {
                Page<ClcGuestInhFilterConfig> 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, ImportGuestConfigDTO.class));
        }

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

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

        /**
         * 获取资产下所有 rate code
         *
         * @param propertyId 资产 id
         * @return rate code 列表
         * @author yyt
         * @date 2026/1/29
         **/
        @Override
        public List<String> listForRateCode(Integer propertyId) {
            return list(new QueryWrapper<ClcGuestInhFilterConfig>().lambda()
                    .eq(ClcGuestInhFilterConfig::getPropertyId, propertyId)
                    .select(ClcGuestInhFilterConfig::getRateCode))
                    .stream().map(ClcGuestInhFilterConfig::getRateCode).collect(Collectors.toList());
        }

    }