# MorOvertimeReportFilterConfigServiceImpl.java

    package cloud.wconnect.workperformance.service.mor.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.mor.MorOvertimeReportFilterConfig;
    import cloud.wconnect.workperformance.mapper.mor.MorOvertimeReportFilterConfigMapper;
    import cloud.wconnect.workperformance.pojo.dto.mor.overtime.filterconfig.MorOvertimeReportFilterConfigDTO;
    import cloud.wconnect.workperformance.pojo.query.mor.overtime.MorOvertimeReportFilterConfigQuery;
    import cloud.wconnect.workperformance.service.mor.IMorOvertimeReportFilterConfigService;
    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.Collections;
    import java.util.List;
    import java.util.Objects;

    /**
     * <p>
     * Monthly OT Report - Overtime 报告过滤配置 服务实现类
     * </p>
     *
     * @author zc
     * @since 2025-03-14
     */
    @Service
    @DS("work_performance")
    public class MorOvertimeReportFilterConfigServiceImpl extends ServiceImpl<MorOvertimeReportFilterConfigMapper, MorOvertimeReportFilterConfig> implements IMorOvertimeReportFilterConfigService {
        @Resource
        private BeanMapper beanMapper;

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

            List<MorOvertimeReportFilterConfig> configList;
            long total;
            if (Objects.nonNull(configQuery.getPage()) && Objects.nonNull(configQuery.getSize())) {
                queryWrapper.like(StringUtils.isNotBlank(configQuery.getPayCode()),
                                MorOvertimeReportFilterConfig::getPayCode, configQuery.getPayCode())
                        .orderByDesc(MorOvertimeReportFilterConfig::getUpdatedTime);
                Page<MorOvertimeReportFilterConfig> page = page(new Page<>(configQuery.getPage(),
                        configQuery.getSize()), queryWrapper);
                configList = page.getRecords();
                total = page.getTotal();
            } else {
                configList = list(queryWrapper.eq(StringUtils.isNotBlank(configQuery.getPayCode()),
                        MorOvertimeReportFilterConfig::getPayCode, configQuery.getPayCode()));
                total = configList.size();
            }
            if (CollectionUtils.isEmpty(configList)) {
                return new ListPagedDTO<>();
            }
            return new ListPagedDTO<>(total, beanMapper.mapList(configList, MorOvertimeReportFilterConfigDTO.class));
        }

        /**
         * 校验配置信息是否重复
         *
         * @param propertyId 资产 id
         * @param configId   配置 id
         * @param payCode    pay code
         * @author zc
         * @date 2025/3/14
         */
        @Override
        public void checkRepeat(int propertyId, Integer configId, String payCode) {
            long count = count(new QueryWrapper<MorOvertimeReportFilterConfig>().lambda()
                    .eq(MorOvertimeReportFilterConfig::getPropertyId, propertyId)
                    .ne(Objects.nonNull(configId), MorOvertimeReportFilterConfig::getId, configId)
                    .eq(MorOvertimeReportFilterConfig::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/3/14
         */
        @Override
        public int add(int propertyId, String payCode, int operatorId) {
            MorOvertimeReportFilterConfig config = new MorOvertimeReportFilterConfig()
                    .setPropertyId(propertyId)
                    .setPayCode(payCode)
                    .setUpdatedBy(operatorId)
                    .setUpdatedTime(DateTimeUtils.nowTimestamp());
            save(config);
            return config.getId();
        }

        /**
         * 校验配置是否存在
         *
         * @param id 配置 id
         * @return MorOvertimeReportFilterConfig
         * @author zc
         * @date 2025/3/14
         */
        @Override
        public MorOvertimeReportFilterConfig checkExist(int id) {
            MorOvertimeReportFilterConfig 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/3/14
         */
        @Override
        public void checkBelong(int propertyId, MorOvertimeReportFilterConfig 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/3/14
         */
        @Override
        public void update(int id, String payCode, int operatorId) {
            updateById(new MorOvertimeReportFilterConfig()
                    .setId(id)
                    .setPayCode(payCode)
                    .setUpdatedBy(operatorId)
                    .setUpdatedTime(DateTimeUtils.nowTimestamp()));
        }

        /**
         * 获取过滤配置
         *
         * @param propertyIds 资产 id 集合
         * @return MorOvertimeReportFilterConfig 集合
         * @author zc
         * @date 2025/3/14
         */
        @Override
        public List<MorOvertimeReportFilterConfig> listFilterPayCode(List<Integer> propertyIds) {
            if (CollectionUtils.isEmpty(propertyIds)) {
                return Collections.emptyList();
            }
            return list(new LambdaQueryWrapper<MorOvertimeReportFilterConfig>()
                    .in(MorOvertimeReportFilterConfig::getPropertyId, propertyIds));
        }
    }