# AdjPmsConfigServiceImpl.java

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

    import cloud.wconnect.commonlib.component.BeanMapper;
    import cloud.wconnect.commonlib.cons.BaseConstant;
    import cloud.wconnect.commonlib.cons.BaseErrorCodeEnum;
    import cloud.wconnect.commonlib.exception.BadRequestException;
    import cloud.wconnect.commonlib.pojo.dto.ListPagedDTO;
    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.redis.PropertyI18nRedisService;
    import cloud.wconnect.commonlib.service.UserInternalService;
    import cloud.wconnect.commonlib.util.DateTimeUtils;
    import cloud.wconnect.frontoffice.cons.adjustment.AdjErrorI18nConstant;
    import cloud.wconnect.frontoffice.entity.adjustment.AdjPmsConfig;
    import cloud.wconnect.frontoffice.mapper.adjustment.AdjPmsAccountMapper;
    import cloud.wconnect.frontoffice.mapper.adjustment.AdjPmsConfigMapper;
    import cloud.wconnect.frontoffice.pojo.dto.adjustment.AdjPmsConfigDTO;
    import cloud.wconnect.frontoffice.pojo.query.adjustment.PmsConfigQuery;
    import cloud.wconnect.frontoffice.service.adjustment.IAdjPmsConfigService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
    import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import com.google.common.collect.Lists;
    import lombok.extern.slf4j.Slf4j;
    import org.apache.commons.lang3.StringUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;

    import java.util.*;
    import java.util.stream.Collectors;

    /**
     * <p>
     * Adjustment Requests Opera 配置表 服务实现类
     * </p>
     *
     * @author lichen
     * @since 2023-03-31
     */
    @Service
    @DS("logbook")
    @Slf4j
    public class AdjPmsConfigServiceImpl extends ServiceImpl<AdjPmsConfigMapper, AdjPmsConfig> implements IAdjPmsConfigService {

        @Autowired
        private BeanMapper beanMapper;
        @Autowired
        private AdjPmsAccountMapper adjPmsAccountMapper;
        @Autowired
        private PropertyI18nRedisService propertyI18nRedisService;
        @Autowired
        private UserInternalService userInternalService;

        /**
         * 保存pmsAccountConfig
         *
         * @param propertyId      资产ID
         * @param adjPmsConfigDTO pmsAccountConfig DTO
         * @return long
         * @author lichen
         * @date 2025/12/3
         */
        @Override
        @Transactional(rollbackFor = Exception.class)
        public long save(int propertyId, AdjPmsConfigDTO adjPmsConfigDTO) {
            AdjPmsConfig entity = beanMapper.map(adjPmsConfigDTO, AdjPmsConfig.class);
            entity.setPropertyId(propertyId);
            entity.setCreateTime(DateTimeUtils.nowTimestamp());
            //新增配置
            save(entity);

            PropertyDTO propertyDTO = propertyI18nRedisService.get(propertyId);

            //新增pmsAccount
            adjPmsAccountMapper.insertOrUpdateByUnique(propertyDTO, Lists.newArrayList(adjPmsConfigDTO.getPmsAccount()));

            return entity.getId();
        }

        /**
         * 校验pmsAccount是否已经存在
         *
         * @param propertyId 资产ID
         * @param pmsAccount pms 账户
         * @author lichen
         * @date 2025/12/3
         */
        @Override
        public void checkExist(Integer propertyId, String pmsAccount) {
            long count = count(new LambdaQueryWrapper<AdjPmsConfig>().eq(AdjPmsConfig::getPropertyId, propertyId)
                    .eq(AdjPmsConfig::getPmsAccount, pmsAccount));
            if (count > 0) {
                throw new BadRequestException(AdjErrorI18nConstant.PMS_ACCOUNT_ALREADY_EXIST,
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        "The current PMS Account can't be associated to more than one user");
            }
        }

        /**
         * 获取pmsAccountConfig列表
         *
         * @param tenantId   租户id
         * @param propertyId 资产id
         * @param query      查询对象
         * @return cloud.wconnect.commonlib.pojo.dto.ListPagedDTO<cloud.wconnect.frontoffice.pojo.dto.adjustment.AdjPmsConfigDTO>
         * @author lichen
         * @date 2025/12/3
         */
        @Override
        public ListPagedDTO<AdjPmsConfigDTO> pageList(Integer tenantId, int propertyId, PmsConfigQuery query) {
            List<AdjPmsConfig> pmsConfigs;
            long total = 0;
            if (Objects.nonNull(query.getPage()) && Objects.nonNull(query.getSize())) {
                //分页查询
                Page<AdjPmsConfig> pageResult = page(new Page<>(query.getPage(), query.getSize()),
                        new LambdaQueryWrapper<AdjPmsConfig>()
                                .eq(AdjPmsConfig::getPropertyId, propertyId)
                                .eq(Objects.nonNull(query.getDataType()), AdjPmsConfig::getDataType, query.getDataType())
                                .like(StringUtils.isNotBlank(query.getPmsAccount()), AdjPmsConfig::getPmsAccount, query.getPmsAccount())
                                .orderByDesc(AdjPmsConfig::getId));
                pmsConfigs = pageResult.getRecords();
                total = pageResult.getTotal();
            } else {
                pmsConfigs = list(new LambdaQueryWrapper<AdjPmsConfig>()
                        .eq(AdjPmsConfig::getPropertyId, propertyId)
                        .eq(Objects.nonNull(query.getDataType()), AdjPmsConfig::getDataType, query.getDataType())
                        .eq(StringUtils.isNotBlank(query.getPmsAccount()), AdjPmsConfig::getPmsAccount, query.getPmsAccount())
                        .orderByDesc(AdjPmsConfig::getId));
                total = pmsConfigs.size();
            }

            if (total == BaseConstant.ZERO) {
                return new ListPagedDTO<>();
            }

            //查询用户信息
            List<Integer> uids = pmsConfigs.stream().map(AdjPmsConfig::getUserId).distinct().collect(Collectors.toList());
            Map<Integer, String> userId2Name = userInternalService.getNameMapByIds(tenantId, uids);

            List<AdjPmsConfigDTO> pmsConfigDTOS = beanMapper.mapList(pmsConfigs, AdjPmsConfigDTO.class, (re, dto) -> {
                dto.setUserName(userId2Name.get(dto.getUserId()));
            });
            return new ListPagedDTO<>(total, pmsConfigDTOS);
        }

        /**
         * 根据资产id获取pmsAccountConfig列表
         *
         * @param propertyId 资产id
         * @return java.util.List<cloud.wconnect.frontoffice.entity.adjustment.AdjPmsConfig>
         * @author lichen
         * @date 2025/12/3
         */
        @Override
        public List<AdjPmsConfig> listByPropertyId(int propertyId) {
            return list(new LambdaQueryWrapper<AdjPmsConfig>().eq(AdjPmsConfig::getPropertyId, propertyId));
        }

        /**
         * 根据资产id和数据类型查询的PMS Account配置
         *
         * @param propertyId 资产id
         * @param dataType   数据类型
         * @return java.util.List<cloud.wconnect.frontoffice.entity.adjustment.AdjPmsConfig>
         * @author lichen
         * @date 2026/2/5
         */
        @Override
        public List<AdjPmsConfig> listByPropertyIdAndDataType(int propertyId, int dataType) {
            return list(new LambdaQueryWrapper<AdjPmsConfig>()
                    .eq(AdjPmsConfig::getPropertyId, propertyId)
                    .eq(AdjPmsConfig::getDataType, dataType));
        }
    }