# SvaDepartmentIgnoreConfigServiceImpl.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.SvaDepartmentIgnoreConfig;
    import cloud.wconnect.workperformance.mapper.sva.employees.SvaDepartmentIgnoreConfigMapper;
    import cloud.wconnect.workperformance.pojo.dto.sva.employee.SvaDepartmentIgnoreConfigDTO;
    import cloud.wconnect.workperformance.pojo.query.sva.employee.SvaDepartmentIgnoreConfigQuery;
    import cloud.wconnect.workperformance.service.sva.employees.ISvaDepartmentIgnoreConfigService;
    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 employees 导入报告忽略更新部门配置 服务实现类
     * </p>
     *
     * @author zc
     * @since 2024-02-23
     */
    @Service
    @DS("work_performance")
    public class SvaDepartmentIgnoreConfigServiceImpl extends ServiceImpl<SvaDepartmentIgnoreConfigMapper, SvaDepartmentIgnoreConfig> implements ISvaDepartmentIgnoreConfigService {
        @Resource
        private BeanMapper beanMapper;

        /**
         * 获取忽略更新部门数据配置
         *
         * @param propertyId        资产 id
         * @param ignoreConfigQuery 查询条件
         * @return SvaDepartmentIgnoreConfigDTO 集合
         * @author zc
         * @date 2023/11/25
         */
        @Override
        public ListPagedDTO<SvaDepartmentIgnoreConfigDTO> list(int propertyId, SvaDepartmentIgnoreConfigQuery ignoreConfigQuery) {
            LambdaQueryWrapper<SvaDepartmentIgnoreConfig> queryWrapper = new QueryWrapper<SvaDepartmentIgnoreConfig>().lambda()
                    .eq(SvaDepartmentIgnoreConfig::getPropertyId, propertyId);

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

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

            return new ListPagedDTO<>(total, beanMapper.mapList(filterConfigs, SvaDepartmentIgnoreConfigDTO.class,
                    (item, dto) -> dto.setCode(item.getCode())));
        }

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

        /**
         * 新增配置
         *
         * @param propertyId 资产 id
         * @param operatorId 操作人 id
         * @param code       部门 code
         * @author zc
         * @date 2023/11/25
         */
        @Override
        public void add(int propertyId, int operatorId, String code) {
            save(new SvaDepartmentIgnoreConfig()
                    .setPropertyId(propertyId)
                    .setCode(code)
                    .setUpdatedBy(operatorId)
                    .setUpdatedTime(DateTimeUtils.nowTimestamp()));
        }

        /**
         * 校验配置是否存在
         *
         * @param id 配置 id
         * @return SvaDepartmentIgnoreConfig
         * @author zc
         * @date 2023/11/25
         */
        @Override
        public SvaDepartmentIgnoreConfig checkExist(int id) {
            SvaDepartmentIgnoreConfig 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 ignoreConfig 配置数据
         * @author zc
         * @date 2023/11/27
         */
        @Override
        public void checkBelong(int propertyId, SvaDepartmentIgnoreConfig ignoreConfig) {
            if (!ignoreConfig.getPropertyId().equals(propertyId)) {
                throw new ForbiddenException(BaseErrorCodeEnum.DATA_FORBIDDEN.getI18nCode(),
                        BaseErrorCodeEnum.DATA_FORBIDDEN.getCode(),
                        MessageFormat.format("The ignoreConfig does not belong to this property; propertyId:{0}; ignoreConfigPropertyId:{1}",
                                propertyId, ignoreConfig.getPropertyId()));
            }
        }

        /**
         * 编辑配置
         *
         * @param operatorId 操作人 id
         * @param id         配置数据 id
         * @param code       部门 code
         * @author zc
         * @date 2023/11/27
         */
        @Override
        public void update(int operatorId, int id, String code) {
            updateById(new SvaDepartmentIgnoreConfig()
                    .setId(id)
                    .setCode(code)
                    .setUpdatedBy(operatorId)
                    .setUpdatedTime(DateTimeUtils.nowTimestamp()));
        }

        /**
         * 获取配置
         *
         * @param propertyId 资产 id
         * @return SvaDepartmentIgnoreConfig 集合
         * @author zc
         * @date 2023/11/27
         */
        @Override
        public List<SvaDepartmentIgnoreConfig> listConfig(int propertyId) {
            return list(new QueryWrapper<SvaDepartmentIgnoreConfig>().lambda()
                    .eq(SvaDepartmentIgnoreConfig::getPropertyId, propertyId));
        }
    }