# SvaEmployeesImportFilterConfigServiceImpl.java

    package cloud.wconnect.workperformance.service.sva.employees.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.employees.AttendanceEmployee;
    import cloud.wconnect.workperformance.entity.sva.employees.SvaEmployeesImportFilterConfig;
    import cloud.wconnect.workperformance.mapper.sva.employees.SvaEmployeesImportFilterConfigMapper;
    import cloud.wconnect.workperformance.pojo.dto.sva.employee.EmployeeFilterConfigDTO;
    import cloud.wconnect.workperformance.pojo.query.sva.employee.SvaEmployeesFilterConfigQuery;
    import cloud.wconnect.workperformance.service.sva.employees.ISvaEmployeesImportFilterConfigService;
    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.compress.utils.Lists;
    import org.springframework.lang.Nullable;
    import org.springframework.stereotype.Service;

    import jakarta.annotation.Resource;

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

    /**
     * <p>
     * sva employees 员工报告导入过滤条件配置 服务实现类
     * </p>
     *
     * @author zc
     * @since 2023-10-18
     */
    @Service
    @DS("work_performance")
    public class SvaEmployeesImportFilterConfigServiceImpl extends ServiceImpl<SvaEmployeesImportFilterConfigMapper, SvaEmployeesImportFilterConfig> implements ISvaEmployeesImportFilterConfigService {
        @Resource
        private BeanMapper beanMapper;

        /**
         * 获取员工报告导入过滤配置
         *
         * @param propertyId        资产 id
         * @param filterConfigQuery 查询条件
         * @return EmployeeFilterConfigDTO 集合
         * @author zc
         * @date 2023/10/18
         */
        @Override
        public ListPagedDTO<EmployeeFilterConfigDTO> list(int propertyId, SvaEmployeesFilterConfigQuery filterConfigQuery) {
            LambdaQueryWrapper<SvaEmployeesImportFilterConfig> queryWrapper = new QueryWrapper<SvaEmployeesImportFilterConfig>().lambda()
                    .eq(SvaEmployeesImportFilterConfig::getPropertyId, propertyId);

            List<SvaEmployeesImportFilterConfig> filterConfigs;
            long total;
            if (Objects.nonNull(filterConfigQuery.getPage()) && Objects.nonNull(filterConfigQuery.getSize())) {
                queryWrapper.like(Objects.nonNull(filterConfigQuery.getPositionId()),
                                SvaEmployeesImportFilterConfig::getPositionId, filterConfigQuery.getPositionId())
                        .orderByDesc(SvaEmployeesImportFilterConfig::getUpdatedTime);
                Page<SvaEmployeesImportFilterConfig> page = page(new Page<>(filterConfigQuery.getPage(),
                        filterConfigQuery.getSize()), queryWrapper);
                filterConfigs = page.getRecords();
                total = page.getTotal();
            } else {
                filterConfigs = list(queryWrapper.eq(Objects.nonNull(filterConfigQuery.getPositionId()),
                        SvaEmployeesImportFilterConfig::getPositionId, filterConfigQuery.getPositionId()));
                total = filterConfigs.size();
            }

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

            return new ListPagedDTO<>(total, beanMapper.mapList(filterConfigs, EmployeeFilterConfigDTO.class,
                    (item, dto) -> dto.setPositionId(item.getPositionId())));
        }

        /**
         * 校验过滤配置信息是否重复
         *
         * @param propertyId 资产 id
         * @param configId   配置 id
         * @param positionId positionId
         * @author zc
         * @date 2023/10/18
         */
        @Override
        public void checkRepeat(int propertyId, @Nullable Integer configId, String positionId) {
            long count = count(new QueryWrapper<SvaEmployeesImportFilterConfig>().lambda()
                    .eq(SvaEmployeesImportFilterConfig::getPropertyId, propertyId)
                    .ne(Objects.nonNull(configId), SvaEmployeesImportFilterConfig::getId, configId)
                    .eq(SvaEmployeesImportFilterConfig::getPositionId, positionId));
            if (count > BaseConstant.LONG_ZERO) {
                throw new BadRequestException(CommonErrorI18nConstant.DATA_EXISTED,
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        MessageFormat.format("The positionId is already exist in current property; propertyId:{0}, positionId:{1}",
                                propertyId, positionId));
            }
        }

        /**
         * 新增配置
         *
         * @param propertyId 资产 id
         * @param operatorId 操作人 id
         * @param positionId positionId
         * @author zc
         * @date 2023/10/18
         */
        @Override
        public void add(int propertyId, int operatorId, String positionId) {
            save(new SvaEmployeesImportFilterConfig()
                    .setPropertyId(propertyId)
                    .setPositionId(positionId)
                    .setUpdatedBy(operatorId)
                    .setUpdatedTime(DateTimeUtils.nowTimestamp()));
        }

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

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

        /**
         * 编辑配置
         *
         * @param operatorId 操作人 id
         * @param id         配置数据 id
         * @param positionId positionId
         * @author zc
         * @date 2023/10/18
         */
        @Override
        public void update(int operatorId, int id, String positionId) {
            updateById(new SvaEmployeesImportFilterConfig()
                    .setId(id)
                    .setPositionId(positionId)
                    .setUpdatedBy(operatorId)
                    .setUpdatedTime(DateTimeUtils.nowTimestamp()));
        }

        /**
         * 过滤数据 for employees
         *
         * @param employees 员工数据
         * @author zc
         * @date 2023/10/18
         */
        @Override
        public void filterEmployees(List<AttendanceEmployee> employees) {
            if (CollectionUtils.isEmpty(employees)) {
                return;
            }

            Set<Integer> propertyIds = employees.stream().map(AttendanceEmployee::getPropertyId).collect(Collectors.toSet());
            List<SvaEmployeesImportFilterConfig> configList = list(new QueryWrapper<SvaEmployeesImportFilterConfig>().lambda()
                    .in(SvaEmployeesImportFilterConfig::getPropertyId, propertyIds));
            if (CollectionUtils.isEmpty(configList)) {
                return;
            }

            Map<Integer, List<String>> propertyId2PositionIds = configList.stream()
                    .collect(Collectors.groupingBy(SvaEmployeesImportFilterConfig::getPropertyId,
                            Collectors.mapping(SvaEmployeesImportFilterConfig::getPositionId, Collectors.toList())));

            List<AttendanceEmployee> needRemoveEmployees = Lists.newArrayList();
            employees.forEach(item -> {
                List<String> configs = propertyId2PositionIds.getOrDefault(item.getPropertyId(), Collections.emptyList());
                if (configs.contains(item.getPositionId())) {
                    needRemoveEmployees.add(item);
                }
            });
            employees.removeAll(needRemoveEmployees);
        }

        /**
         * 获取资产配置的忽略的 positionId 集合
         *
         * @param propertyId 资产 id
         * @return 忽略的 positionId 集合
         * @author zc
         * @date 2025/4/21
         */
        @Override
        public List<String> listPositionId(int propertyId) {
            return list(new QueryWrapper<SvaEmployeesImportFilterConfig>().lambda()
                    .eq(SvaEmployeesImportFilterConfig::getPropertyId, propertyId))
                    .stream().map(SvaEmployeesImportFilterConfig::getPositionId).collect(Collectors.toList());
        }
    }