# SvaPayCodeExcludeConfigServiceImpl.java

    package cloud.wconnect.workperformance.service.sva.actualschedulehour.impl;

    import cloud.wconnect.commonlib.component.BeanMapper;
    import cloud.wconnect.commonlib.cons.BaseConstant;
    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.DateTimeUtils;
    import cloud.wconnect.workperformance.entity.sva.avhours.SvaPayCodeExcludeConfig;
    import cloud.wconnect.workperformance.mapper.sva.avhours.SvaPayCodeExcludeConfigMapper;
    import cloud.wconnect.workperformance.pojo.dto.sva.avhours.SvaPayCodeExcludeConfigDTO;
    import cloud.wconnect.workperformance.pojo.query.sva.avhours.SvaPayCodeExcludeConfigQuery;
    import cloud.wconnect.workperformance.service.sva.actualschedulehour.ISvaPayCodeExcludeConfigService;
    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.collections4.CollectionUtils;
    import org.apache.commons.lang3.StringUtils;
    import org.springframework.stereotype.Service;

    import jakarta.annotation.Resource;
    import java.text.MessageFormat;
    import java.util.List;
    import java.util.Objects;

    /**
     * <p>
     * sva 排除 Pay Code配置 服务实现类
     * </p>
     *
     * @author zc
     * @since 2025-02-06
     */
    @Service
    @DS("work_performance")
    public class SvaPayCodeExcludeConfigServiceImpl extends ServiceImpl<SvaPayCodeExcludeConfigMapper, SvaPayCodeExcludeConfig> implements ISvaPayCodeExcludeConfigService {
        @Resource
        private BeanMapper beanMapper;

        /**
         * 获取排除PayCode配置
         *
         * @param propertyId  资产 id
         * @param configQuery 查询条件
         * @return 配置集合
         * @author zc
         * @date 2025/2/6
         */
        @Override
        public ListPagedDTO<SvaPayCodeExcludeConfigDTO> list(int propertyId, SvaPayCodeExcludeConfigQuery configQuery) {
            LambdaQueryWrapper<SvaPayCodeExcludeConfig> queryWrapper = new QueryWrapper<SvaPayCodeExcludeConfig>().lambda()
                    .eq(SvaPayCodeExcludeConfig::getPropertyId, propertyId);

            List<SvaPayCodeExcludeConfig> configList;
            long total;
            if (Objects.nonNull(configQuery.getPage()) && Objects.nonNull(configQuery.getSize())) {
                queryWrapper.like(StringUtils.isNotBlank(configQuery.getPayCode()),
                                SvaPayCodeExcludeConfig::getPayCode, configQuery.getPayCode())
                        .orderByDesc(SvaPayCodeExcludeConfig::getUpdatedTime);
                Page<SvaPayCodeExcludeConfig> page = page(new Page<>(configQuery.getPage(),
                        configQuery.getSize()), queryWrapper);
                configList = page.getRecords();
                total = page.getTotal();
            } else {
                configList = list(queryWrapper.eq(StringUtils.isNotBlank(configQuery.getPayCode()),
                        SvaPayCodeExcludeConfig::getPayCode, configQuery.getPayCode()));
                total = configList.size();
            }

            if (CollectionUtils.isEmpty(configList)) {
                return new ListPagedDTO<>();
            }

            return new ListPagedDTO<>(total, beanMapper.mapList(configList, SvaPayCodeExcludeConfigDTO.class));
        }

        /**
         * 校验配置信息是否重复
         *
         * @param propertyId 资产 id
         * @param configId   配置 id
         * @param payCode    pay code
         * @author zc
         * @date 2025/2/6
         */
        @Override
        public void checkRepeat(int propertyId, Integer configId, String payCode) {
            long count = count(new QueryWrapper<SvaPayCodeExcludeConfig>().lambda()
                    .eq(SvaPayCodeExcludeConfig::getPropertyId, propertyId)
                    .ne(Objects.nonNull(configId), SvaPayCodeExcludeConfig::getId, configId)
                    .eq(SvaPayCodeExcludeConfig::getPayCode, payCode));
            if (count > BaseConstant.ZERO) {
                throw new BadRequestException(CommonErrorI18nConstant.DATA_EXISTED,
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        MessageFormat.format("The payCode is already exist in current property; propertyId:{0}, payCode:{1}",
                                propertyId, payCode));
            }
        }

        /**
         * 新增配置
         *
         * @param propertyId 资产 id
         * @param payCode    pay code
         * @param operatorId 操作人 id
         * @return 配置 id
         * @author zc
         * @date 2025/2/6
         */
        @Override
        public int add(int propertyId, String payCode, int operatorId) {
            SvaPayCodeExcludeConfig config = new SvaPayCodeExcludeConfig()
                    .setPropertyId(propertyId)
                    .setPayCode(payCode)
                    .setUpdatedBy(operatorId)
                    .setUpdatedTime(DateTimeUtils.nowTimestamp());
            save(config);
            return config.getId();
        }

        /**
         * 校验配置是否存在
         *
         * @param id 配置 id
         * @return SvaPayCodeExcludeConfig
         * @author zc
         * @date 2025/2/6
         */
        @Override
        public SvaPayCodeExcludeConfig checkExist(int id) {
            SvaPayCodeExcludeConfig config = getById(id);
            if (Objects.isNull(config)) {
                throw new BadRequestException(CommonErrorI18nConstant.DATA_NOT_FOUND,
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        "The config is not found; configId: " + id);
            }
            return config;
        }

        /**
         * 校验归属
         *
         * @param propertyId 资产 id
         * @param config     配置数据
         * @author zc
         * @date 2025/2/6
         */
        @Override
        public void checkBelong(int propertyId, SvaPayCodeExcludeConfig config) {
            if (!config.getPropertyId().equals(propertyId)) {
                throw new ForbiddenException(BaseErrorCodeEnum.DATA_FORBIDDEN.getI18nCode(),
                        BaseErrorCodeEnum.DATA_FORBIDDEN.getCode(),
                        MessageFormat.format("The config does not belong to this property; propertyId:{0}; configPropertyId:{1}",
                                propertyId, config.getPropertyId()));
            }
        }

        /**
         * 编辑配置
         *
         * @param id         配置数据 id
         * @param payCode    pay code
         * @param operatorId 操作人 id
         * @author zc
         * @date 2025/2/6
         */
        @Override
        public void update(int id, String payCode, int operatorId) {
            updateById(new SvaPayCodeExcludeConfig()
                    .setId(id)
                    .setPayCode(payCode)
                    .setUpdatedBy(operatorId)
                    .setUpdatedTime(DateTimeUtils.nowTimestamp()));
        }

        /**
         * 获取排除的 Pay Code
         *
         * @param propertyId 资产 id
         * @return Pay Code 集合
         * @author zc
         * @date 2025/2/7
         */
        @Override
        public List<String> listExcludePayCode(int propertyId) {
            return list(new LambdaQueryWrapper<SvaPayCodeExcludeConfig>()
                    .eq(SvaPayCodeExcludeConfig::getPropertyId, propertyId)).stream()
                    .map(SvaPayCodeExcludeConfig::getPayCode).toList();
        }
    }