# ClcArrivalDetailedFilterConfigServiceImpl.java

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

    import cloud.wconnect.commonlib.component.BeanMapper;
    import cloud.wconnect.commonlib.cons.BaseErrorCodeEnum;
    import cloud.wconnect.commonlib.cons.CommonErrorI18nConstant;
    import cloud.wconnect.commonlib.exception.BadRequestException;
    import cloud.wconnect.commonlib.pojo.dto.ListPagedDTO;
    import cloud.wconnect.commonlib.util.DateTimeUtils;
    import cloud.wconnect.frontoffice.entity.clc.ClcArrivalDetailedFilterConfig;
    import cloud.wconnect.frontoffice.mapper.clc.ClcArrivalDetailedFilterConfigMapper;
    import cloud.wconnect.frontoffice.pojo.dto.clc.ImportArrivalConfigDTO;
    import cloud.wconnect.frontoffice.pojo.query.clc.ImportArrivalConfigQuery;
    import cloud.wconnect.frontoffice.service.clc.IClcArrivalDetailedFilterConfigService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    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.lang3.StringUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;

    import java.util.List;
    import java.util.Objects;
    import java.util.stream.Collectors;

    /**
     * <p>
     *  服务实现类
     * </p>
     *
     * @author yyt
     * @since 2023-07-25
     */
    @DS("logbook")
    @Service
    public class ClcArrivalDetailedFilterConfigServiceImpl extends ServiceImpl<ClcArrivalDetailedFilterConfigMapper, ClcArrivalDetailedFilterConfig> implements IClcArrivalDetailedFilterConfigService {

        @Autowired
        private BeanMapper beanMapper;

        /**
         * 校验存在
         *
         * @param propertyId  资产 id
         * @param companyName company name
         * @param id          排除自身
         * @author yyt
         * @date 2023/7/25
         */
        @Override
        public void checkExist(Integer propertyId, String companyName, Integer id) {
            ClcArrivalDetailedFilterConfig config = getOne(new QueryWrapper<ClcArrivalDetailedFilterConfig>().lambda()
                    .eq(ClcArrivalDetailedFilterConfig::getPropertyId, propertyId)
                    .eq(ClcArrivalDetailedFilterConfig::getCompanyName, companyName)
                    .ne(Objects.nonNull(id), ClcArrivalDetailedFilterConfig::getId, id));
            if (Objects.nonNull(config)) {
                throw new BadRequestException(CommonErrorI18nConstant.DATA_EXISTED,
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(), "the config is exist");
            }
        }

        /**
         * 校验存在
         *
         * @param id 配置 id
         * @return 配置
         * @author yyt
         * @date 2023/7/25
         */
        @Override
        public ClcArrivalDetailedFilterConfig checkExist(int id) {
            ClcArrivalDetailedFilterConfig config = getById(id);
            if (Objects.isNull(config)) {
                throw new BadRequestException(CommonErrorI18nConstant.DATA_NOT_FOUND,
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(), "the config is not found;id:" + id);
            }
            return config;
        }

        /**
         * 分页查询数据
         *
         * @param propertyId 资产 id
         * @param query      查询条件
         * @return 配置规则
         * @author yyt
         * @date 2024/11/19
         **/
        @Override
        public Page<ClcArrivalDetailedFilterConfig> listPage(Integer propertyId, ImportArrivalConfigQuery query) {
            return page(new Page<>(query.getPage(), query.getSize()), new QueryWrapper<ClcArrivalDetailedFilterConfig>().lambda()
                    .eq(ClcArrivalDetailedFilterConfig::getPropertyId, propertyId)
                    .like(StringUtils.isNotEmpty(query.getCompanyName()), ClcArrivalDetailedFilterConfig::getCompanyName,
                            query.getCompanyName())
                    .orderByDesc(ClcArrivalDetailedFilterConfig::getId));
        }

        /**
         * 查询数据
         *
         * @param propertyId 资产 id
         * @param query      查询条件
         * @return 配置规则
         * @author yyt
         * @date 2024/11/19
         **/
        @Override
        public List<ClcArrivalDetailedFilterConfig> list(Integer propertyId, ImportArrivalConfigQuery query) {
            return list(new QueryWrapper<ClcArrivalDetailedFilterConfig>().lambda()
                    .eq(ClcArrivalDetailedFilterConfig::getPropertyId, propertyId)
                    .eq(ClcArrivalDetailedFilterConfig::getCompanyName, query.getCompanyName()));
        }

        /**
         * 删除配置
         *
         * @param id 配置 id
         * @author yyt
         * @date 2024/11/19
         **/
        @Override
        public void delete(int id) {
            removeById(id);
        }

        /**
         * 获取导入 arrival 报告配置规则
         *
         * @param propertyId 资产 id
         * @param query      查询条件
         * @return 配置规则
         * @author yyt
         * @date 2026/1/19
         **/
        @Override
        public ListPagedDTO<ImportArrivalConfigDTO> listImportArrivalConfigDTO(Integer propertyId, ImportArrivalConfigQuery query) {
            long total;
            List<ClcArrivalDetailedFilterConfig> list;
            if (Objects.nonNull(query.getPage()) && Objects.nonNull(query.getSize())) {
                Page<ClcArrivalDetailedFilterConfig> page = listPage(propertyId, query);
                total = page.getTotal();
                list = page.getRecords();
            } else {
                list = list(propertyId, query);
                total = list.size();
            }

            return new ListPagedDTO<>(total, beanMapper.mapList(list, ImportArrivalConfigDTO.class));
        }

        /**
         * 保存导入 arrival 报告配置规则
         *
         * @param propertyId 资产 id
         * @param configDTO  配置规则
         * @param userId     操作人
         * @author yyt
         * @date 2026/1/19
         **/
        @Override
        public void save(Integer propertyId, ImportArrivalConfigDTO configDTO, int userId) {
            save(new ClcArrivalDetailedFilterConfig().setPropertyId(propertyId)
                    .setCompanyName(configDTO.getCompanyName())
                    .setUpdatedBy(userId)
                    .setUpdatedTime(DateTimeUtils.nowTimestamp()));
        }

        /**
         * 更新导入 arrival 报告配置规则
         *
         * @param id         配置 id
         * @param configDTO  配置规则
         * @param userId     操作人
         * @author yyt
         * @date 2026/1/19
         **/
        @Override
        public void update(int id, ImportArrivalConfigDTO configDTO, int userId) {
            updateById(new ClcArrivalDetailedFilterConfig().setId(id)
                    .setCompanyName(configDTO.getCompanyName())
                    .setUpdatedBy(userId)
                    .setUpdatedTime(DateTimeUtils.nowTimestamp()));
        }

        /**
         * 获取公司名称列表
         *
         * @param propertyId 资产 id
         * @return 公司名称列表
         * @author yyt
         * @date 2026/1/29
         **/
        @Override
        public List<String> listForCompanyName(Integer propertyId) {
            return list(new QueryWrapper<ClcArrivalDetailedFilterConfig>().lambda()
                    .eq(ClcArrivalDetailedFilterConfig::getPropertyId, propertyId)
                    .select(ClcArrivalDetailedFilterConfig::getCompanyName))
                    .stream().map(ClcArrivalDetailedFilterConfig::getCompanyName).collect(Collectors.toList());
        }

    }