# OooRoomTypeConfigServiceImpl.java

    package cloud.wconnect.frontoffice.service.oooroom.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.cons.oooroom.OOORoomCons;
    import cloud.wconnect.frontoffice.entity.oooroom.OooRoom;
    import cloud.wconnect.frontoffice.entity.oooroom.OooRoomTypeConfig;
    import cloud.wconnect.frontoffice.mapper.oooroom.OooRoomTypeConfigMapper;
    import cloud.wconnect.frontoffice.pojo.dto.oooroom.RoomTypeDTO;
    import cloud.wconnect.frontoffice.pojo.query.oooroom.RoomTypeQuery;
    import cloud.wconnect.frontoffice.service.oooroom.IOooRoomService;
    import cloud.wconnect.frontoffice.service.oooroom.IOooRoomTypeConfigService;
    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.collections.CollectionUtils;
    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;

    /**
     * <p>
     * 房间类型-标识 配置 服务实现类
     * </p>
     *
     * @author yyt
     * @since 2023-08-05
     */
    @DS("ratelog_manage")
    @Service
    public class OooRoomTypeConfigServiceImpl extends ServiceImpl<OooRoomTypeConfigMapper, OooRoomTypeConfig> implements IOooRoomTypeConfigService {

        @Autowired
        private IOooRoomService oooRoomService;
        @Autowired
        private BeanMapper beanMapper;

        /**
         * 校验存在
         *
         * @param propertyId  资产 id
         * @param roomTypeDTO 房型信息
         * @param id          排除自身
         * @author yyt
         * @date 2023/8/5
         */
        @Override
        public void checkExist(Integer propertyId, RoomTypeDTO roomTypeDTO, Integer id) {
            List<OooRoomTypeConfig> existData = list(new QueryWrapper<OooRoomTypeConfig>().lambda()
                    .eq(OooRoomTypeConfig::getPropertyId, propertyId)
                    .nested(query -> query.eq(OooRoomTypeConfig::getRoomType, roomTypeDTO.getRoomType())
                            .or().eq(OooRoomTypeConfig::getRoomTypeSymbol, roomTypeDTO.getRoomTypeSymbol()))
                    .ne(Objects.nonNull(id), OooRoomTypeConfig::getId, id));
            if (CollectionUtils.isNotEmpty(existData)) {
                if (existData.stream().anyMatch(item -> item.getRoomType().equals(roomTypeDTO.getRoomType()))) {
                    throw new BadRequestException(OOORoomCons.OOO_ROOM_TYPE_EXIST,
                            BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(), "Room type already exists");
                } else {
                    throw new BadRequestException(OOORoomCons.OOO_ROOM_TYPE_SYMBOL_EXIST,
                            BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(), "Room type symbol already exists");
                }
            }
        }

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

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

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

        /**
         * 删除 room type
         *
         * @param id 房型 id
         * @author yyt
         * @date 2024/11/20
         **/
        @Override
        public void delete(int id) {
            removeById(id);
        }

        /**
         * 获取 room type
         *
         * @param propertyId 资产 id
         * @param query      查询条件
         * @return 房型列表
         * @author yyt
         * @date 2026/1/19
         **/
        @Override
        public ListPagedDTO<RoomTypeDTO> listRoomTypeDTO(Integer propertyId, RoomTypeQuery query) {
            long total;
            List<OooRoomTypeConfig> list;
            if (Objects.nonNull(query.getPage()) && Objects.nonNull(query.getSize())) {
                Page<OooRoomTypeConfig> 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, RoomTypeDTO.class));
        }

        /**
         * 保存 room type
         *
         * @param propertyId  资产 id
         * @param roomTypeDTO 房型信息
         * @param userId      操作人
         * @author yyt
         * @date 2026/1/19
         **/
        @Override
        public void save(Integer propertyId, RoomTypeDTO roomTypeDTO, int userId) {
            save(new OooRoomTypeConfig().setPropertyId(propertyId)
                    .setRoomType(roomTypeDTO.getRoomType())
                    .setRoomTypeSymbol(roomTypeDTO.getRoomTypeSymbol())
                    .setUpdatedBy(userId)
                    .setUpdatedTime(DateTimeUtils.nowTimestamp()));
        }

        /**
         * 更新 room type
         *
         * @param id          房型 id
         * @param roomTypeDTO 房型信息
         * @param userId      操作人
         * @author yyt
         * @date 2026/1/19
         **/
        @Override
        public void update(int id, RoomTypeDTO roomTypeDTO, int userId) {
            updateById(new OooRoomTypeConfig().setId(id)
                    .setRoomType(roomTypeDTO.getRoomType())
                    .setRoomTypeSymbol(roomTypeDTO.getRoomTypeSymbol())
                    .setUpdatedBy(userId)
                    .setUpdatedTime(DateTimeUtils.nowTimestamp()));
        }

        /**
         * 校验 room type 是否被使用
         *
         * @param propertyId 资产 id
         * @param id         房型 id
         * @author yyt
         * @date 2026/1/19
         **/
        @Override
        public void checkUsed(int propertyId, int id) {
            if (Objects.nonNull(oooRoomService.getOne(new QueryWrapper<OooRoom>().lambda()
                    .eq(OooRoom::getPropertyId, propertyId)
                    .eq(OooRoom::getRoomType, id)
                    .last("limit 1")))) {
                throw new BadRequestException(OOORoomCons.OOO_ROOM_TYPE_USED,
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(), "The type has been used and cannot be deleted");
            }
        }

        /**
         * 获取 room type symbols
         *
         * @param propertyId 资产 id
         * @return 房型 symbols
         * @author yyt
         * @date 2026/1/19
         **/
        @Override
        public List<String> listForRoomTypeSymbols(Integer propertyId) {
            return list(new QueryWrapper<OooRoomTypeConfig>().lambda()
                    .eq(OooRoomTypeConfig::getPropertyId, propertyId)
                    .select(OooRoomTypeConfig::getRoomTypeSymbol))
                    .stream().map(item -> item.getRoomTypeSymbol().toLowerCase()).toList();
        }

    }