# TaCompareFilterRoomNoConfigServiceImpl.java

    package cloud.wconnect.finance.service.ta.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.exception.ForbiddenException;
    import cloud.wconnect.commonlib.pojo.dto.ListPagedDTO;
    import cloud.wconnect.commonlib.util.UpdateLogUtil;
    import cloud.wconnect.finance.cons.ta.TaCompareConfigCons;
    import cloud.wconnect.finance.entity.ta.TaCompareFilterRoomNoConfig;
    import cloud.wconnect.finance.mapper.ta.TaCompareFilterRoomNoConfigMapper;
    import cloud.wconnect.finance.pojo.dto.ta.compare.TaCompareFilterRoomNoConfigDTO;
    import cloud.wconnect.finance.pojo.dto.ta.compare.TaCompareListDTO;
    import cloud.wconnect.finance.pojo.query.ta.TaCompareListQuery;
    import cloud.wconnect.finance.service.ta.ITaCompareFilterRoomNoConfigService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
    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.collections.CollectionUtils;
    import org.springframework.stereotype.Service;

    import jakarta.annotation.Resource;

    import java.text.MessageFormat;
    import java.util.Objects;

    /**
     * <p>
     * TA 报告比对过滤房间号配置 服务实现类
     * </p>
     *
     * @author zc
     * @since 2023-07-07
     */
    @Service
    @DS("finance")
    public class TaCompareFilterRoomNoConfigServiceImpl extends ServiceImpl<TaCompareFilterRoomNoConfigMapper, TaCompareFilterRoomNoConfig> implements ITaCompareFilterRoomNoConfigService {
        @Resource
        private UpdateLogUtil updateLogUtil;
        @Resource
        private BeanMapper beanMapper;

        /**
         * 获取最新版本的过滤房间号配置数据
         *
         * @param tenantId   租户 id
         * @param propertyId 资产 id
         * @return TaCompareFilterRoomNoConfigDTO
         * @author zc
         * @date 2023/7/7
         */
        @Override
        public TaCompareFilterRoomNoConfigDTO getLatest(int tenantId, int propertyId) {
            TaCompareFilterRoomNoConfig roomNoConfig = getOne(new QueryWrapper<TaCompareFilterRoomNoConfig>().lambda()
                    .eq(TaCompareFilterRoomNoConfig::getTenantId, tenantId)
                    .eq(TaCompareFilterRoomNoConfig::getPropertyId, propertyId)
                    .orderByDesc(TaCompareFilterRoomNoConfig::getStartingDate)
                    .last("limit 1"));
            if (Objects.isNull(roomNoConfig)) {
                return null;
            }

            return beanMapper.map(roomNoConfig, TaCompareFilterRoomNoConfigDTO.class);
        }

        /**
         * 获取当前应用的过滤房间号配置数据
         *
         * @param tenantId   租户 id
         * @param propertyId 资产 id
         * @param targetDate 目标日期
         * @return TaCompareFilterRoomNoConfigDTO
         * @author zc
         * @date 2023/7/7
         */
        @Override
        public TaCompareFilterRoomNoConfigDTO getCurrent(int tenantId, int propertyId, long targetDate) {
            TaCompareFilterRoomNoConfig roomNoConfig = getOne(new QueryWrapper<TaCompareFilterRoomNoConfig>().lambda()
                    .eq(TaCompareFilterRoomNoConfig::getTenantId, tenantId)
                    .eq(TaCompareFilterRoomNoConfig::getPropertyId, propertyId)
                    .le(TaCompareFilterRoomNoConfig::getStartingDate, targetDate)
                    .orderByDesc(TaCompareFilterRoomNoConfig::getStartingDate)
                    .last("limit 1"));
            if (Objects.isNull(roomNoConfig)) {
                return null;
            }

            return beanMapper.map(roomNoConfig, TaCompareFilterRoomNoConfigDTO.class);
        }

        /**
         * 获取有效的过滤房间号配置数据
         *
         * @param tenantId   租户 id
         * @param propertyId 资产 id
         * @param targetDate 目标日期
         * @param isGreater  0: 获取大于目标日期的数据；1: 获取小于目标日期的数据；2: 获取等于目标日期的数据
         * @return TaCompareFilterRoomNoConfigDTO
         * @author zc
         * @date 2023/7/7
         */
        @Override
        public TaCompareFilterRoomNoConfigDTO getEffective(int tenantId, int propertyId, long targetDate, Integer isGreater) {
            LambdaQueryWrapper<TaCompareFilterRoomNoConfig> queryWrapper = new QueryWrapper<TaCompareFilterRoomNoConfig>().lambda()
                    .eq(TaCompareFilterRoomNoConfig::getTenantId, tenantId)
                    .eq(TaCompareFilterRoomNoConfig::getPropertyId, propertyId);
            if (TaCompareConfigCons.GREATER_THAN == isGreater) {
                queryWrapper.gt(TaCompareFilterRoomNoConfig::getStartingDate, targetDate)
                        .orderByAsc(TaCompareFilterRoomNoConfig::getStartingDate);
            } else if (TaCompareConfigCons.LESS_THAN == isGreater) {
                queryWrapper.lt(TaCompareFilterRoomNoConfig::getStartingDate, targetDate)
                        .orderByDesc(TaCompareFilterRoomNoConfig::getStartingDate);
            } else if (TaCompareConfigCons.EQUAL_TO == isGreater) {
                queryWrapper.eq(TaCompareFilterRoomNoConfig::getStartingDate, targetDate);
            }

            TaCompareFilterRoomNoConfig roomNoConfig = getOne(queryWrapper.last("limit 1"));
            if (Objects.isNull(roomNoConfig)) {
                return null;
            }

            return beanMapper.map(roomNoConfig, TaCompareFilterRoomNoConfigDTO.class);
        }

        /**
         * 新增过滤房间号配置数据
         *
         * @param tenantId        租户 id
         * @param propertyId      资产 id
         * @param roomNoConfigDTO 过滤房间号配置数据
         * @param loginUserId     用户id
         * @return id
         * @author zc
         * @date 2023/7/7
         */
        @Override
        public long insert(int tenantId, int propertyId, TaCompareFilterRoomNoConfigDTO roomNoConfigDTO, int loginUserId) {
            // 校验指定生效日期的过滤房间号配置数据是否已存在
            checkExistByStartingDate(tenantId, propertyId, roomNoConfigDTO);

            TaCompareFilterRoomNoConfig roomNoConfig = beanMapper.map(roomNoConfigDTO, TaCompareFilterRoomNoConfig.class);
            roomNoConfig.setTenantId(tenantId);
            roomNoConfig.setPropertyId(propertyId);
            roomNoConfig.setUpdateLog(updateLogUtil.get(loginUserId));
            save(roomNoConfig);

            return roomNoConfig.getId();
        }

        /**
         * 更新过滤房间号配置数据
         *
         * @param tenantId           租户 id
         * @param propertyId         资产 id
         * @param originRoomNoConfig 原 过滤房间号配置数据
         * @param roomNoConfigDTO    过滤房间号配置数据
         * @param loginUserId        用户id
         * @author zc
         * @date 2023/7/7
         */
        @Override
        public void update(int tenantId, int propertyId,
                           TaCompareFilterRoomNoConfig originRoomNoConfig,
                           TaCompareFilterRoomNoConfigDTO roomNoConfigDTO,
                           int loginUserId) {
            // 校验指定生效日期的过滤房间号配置数据是否已存在
            checkExistByStartingDate(tenantId, propertyId, roomNoConfigDTO);

            updateById(TaCompareFilterRoomNoConfig.builder()
                    .id(originRoomNoConfig.getId())
                    .startingDate(roomNoConfigDTO.getStartingDate())
                    .roomNo(roomNoConfigDTO.getRoomNo())
                    .updateLog(updateLogUtil.append(originRoomNoConfig.getUpdateLog(), loginUserId))
                    .build());
        }

        /**
         * 校验过滤房间号配置数据是否存在
         *
         * @param id 过滤房间号配置数据 id
         * @return TaCompareFilterRoomNoConfig
         * @author zc
         * @date 2023/7/7
         */
        @Override
        public TaCompareFilterRoomNoConfig checkExist(long id) {
            TaCompareFilterRoomNoConfig roomNoConfig = getById(id);
            if (Objects.isNull(roomNoConfig)) {
                throw new BadRequestException(CommonErrorI18nConstant.DATA_NOT_FOUND,
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        "The filter room no config is not found; filterRoomNoConfigId: " + id);
            }
            return roomNoConfig;
        }

        /**
         * 校验数据归属
         *
         * @param tenantId     租户 id
         * @param propertyId   资产 id
         * @param roomNoConfig 过滤房间号配置数据
         * @author zc
         * @date 2023/7/7
         */
        @Override
        public void checkBelong(int tenantId, int propertyId, TaCompareFilterRoomNoConfig roomNoConfig) {
            if (!roomNoConfig.getTenantId().equals(tenantId)) {
                throw new ForbiddenException(BaseErrorCodeEnum.DATA_FORBIDDEN.getI18nCode(),
                        BaseErrorCodeEnum.DATA_FORBIDDEN.getCode(),
                        MessageFormat.format("The filter room no config does not belong to this tenant; tenantId:{0}; roomNoConfigId:{1}",
                                tenantId, roomNoConfig.getId()));
            }

            if (!roomNoConfig.getPropertyId().equals(propertyId)) {
                throw new ForbiddenException(BaseErrorCodeEnum.DATA_FORBIDDEN.getI18nCode(),
                        BaseErrorCodeEnum.DATA_FORBIDDEN.getCode(),
                        MessageFormat.format("The filter room no config does not belong to this property; propertyId:{0}; roomNoConfigId:{1}",
                                propertyId, roomNoConfig.getId()));
            }
        }

        /**
         * 获取过滤房间号配置数据集合
         *
         * @param tenantId           租户 id
         * @param propertyId         资产 id
         * @param taCompareListQuery 查询条件
         * @return TaCompareListDTO 集合
         * @author zc
         * @date 2023/7/7
         */
        @Override
        public ListPagedDTO<TaCompareListDTO> list(int tenantId, int propertyId, TaCompareListQuery taCompareListQuery) {
            LambdaQueryWrapper<TaCompareFilterRoomNoConfig> queryWrapper = new QueryWrapper<TaCompareFilterRoomNoConfig>().lambda()
                    .eq(TaCompareFilterRoomNoConfig::getTenantId, tenantId)
                    .eq(TaCompareFilterRoomNoConfig::getPropertyId, propertyId)
                    .orderByDesc(TaCompareFilterRoomNoConfig::getStartingDate);
            Page<TaCompareFilterRoomNoConfig> paged = page(new Page<>(taCompareListQuery.getPage(), taCompareListQuery.getSize()), queryWrapper);
            if (CollectionUtils.isEmpty(paged.getRecords())) {
                return new ListPagedDTO<>();
            }

            return new ListPagedDTO<>(paged.getTotal(), beanMapper.mapList(paged.getRecords(), TaCompareListDTO.class));
        }

        /**
         * 校验指定生效日期的过滤房间号配置数据是否已存在
         *
         * @param tenantId        租户 id
         * @param propertyId      资产 id
         * @param roomNoConfigDTO 过滤房间号配置数据
         * @author zc
         * @date 2023/7/7
         */
        private void checkExistByStartingDate(int tenantId, int propertyId, TaCompareFilterRoomNoConfigDTO roomNoConfigDTO) {
            long count = count(new QueryWrapper<TaCompareFilterRoomNoConfig>().lambda()
                    .eq(TaCompareFilterRoomNoConfig::getTenantId, tenantId)
                    .eq(TaCompareFilterRoomNoConfig::getPropertyId, propertyId)
                    .eq(TaCompareFilterRoomNoConfig::getStartingDate, roomNoConfigDTO.getStartingDate())
                    .ne(Objects.nonNull(roomNoConfigDTO.getId()), TaCompareFilterRoomNoConfig::getId, roomNoConfigDTO.getId()));
            if (count > 0) {
                throw new BadRequestException(CommonErrorI18nConstant.DATA_EXISTED,
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        "The filter room no config startingDate has existed in current property; startingDate: " + roomNoConfigDTO.getStartingDate());
            }
        }
    }