# CbCaseImportLogServiceImpl.java

    package cloud.wconnect.finance.service.chargeback.impl;

    import cloud.wconnect.commonlib.component.BeanMapper;
    import cloud.wconnect.commonlib.pojo.dto.ListPagedDTO;
    import cloud.wconnect.commonlib.service.UserInternalService;
    import cloud.wconnect.commonlib.util.DateTimeUtils;
    import cloud.wconnect.finance.cons.chargeback.ImportCaseStatusCons;
    import cloud.wconnect.finance.entity.chargeback.CbCaseImportLog;
    import cloud.wconnect.finance.mapper.chargeback.CbCaseImportLogMapper;
    import cloud.wconnect.finance.pojo.dto.chargeback.ImportCaseLogDTO;
    import cloud.wconnect.finance.pojo.query.chargeback.ImportCaseLogQuery;
    import cloud.wconnect.finance.service.chargeback.ICbCaseImportLogService;
    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.collections.CollectionUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;

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

    /**
     * <p>
     *  服务实现类
     * </p>
     *
     * @author yyt
     * @since 2023-08-22
     */
    @Service
    @DS("finance")
    public class CbCaseImportLogServiceImpl extends ServiceImpl<CbCaseImportLogMapper, CbCaseImportLog> implements ICbCaseImportLogService {

        @Autowired
        private UserInternalService userInternalService;
        @Autowired
        private BeanMapper beanMapper;

        /**
         * 检查指定用户是否有进行中的导入记录
         *
         * @param userId     指定用户
         * @param propertyId 资产 id
         * @return
         * @author yyt
         * @date 2023/8/28
         */
        @Override
        public boolean checkIsExistInProgressLog(int userId, Integer propertyId) {
            return count(new QueryWrapper<CbCaseImportLog>().lambda().eq(CbCaseImportLog::getPropertyId, propertyId)
                    .eq(CbCaseImportLog::getCreatedBy, userId).eq(CbCaseImportLog::getStatus, ImportCaseStatusCons.IMPORT_IN_PROGRESS)) > 0;
        }

        /**
         * 添加导入记录
         *
         * @param userId     指定用户
         * @param propertyId 资产 iD
         * @param fileName   文件名称
         * @param key        文件路径
         * @return 记录 id
         * @author yyt
         * @date 2023/8/28
         */
        @Override
        public CbCaseImportLog addLog(int userId, Integer propertyId, String fileName, String key) {
            CbCaseImportLog importLog = new CbCaseImportLog().setPropertyId(propertyId).setCreatedBy(userId)
                    .setCreatedTime(DateTimeUtils.nowTimestamp()).setFileName(fileName)
                    .setStatus(ImportCaseStatusCons.IMPORT_IN_PROGRESS).setUrl(key);
            save(importLog);
            return importLog;
        }

        /**
         * 分页查询数据
         *
         * @param propertyId 资产 id
         * @param query      查询条件
         * @return 导入记录
         * @author yyt
         * @date 2024/12/4
         **/
        @Override
        public Page<CbCaseImportLog> listPage(Integer propertyId, ImportCaseLogQuery query) {
            return page(new Page<>(query.getPage(), query.getSize()), new QueryWrapper<CbCaseImportLog>().lambda()
                    .eq(CbCaseImportLog::getPropertyId, propertyId)
                    .in(CollectionUtils.isNotEmpty(query.getStatus()), CbCaseImportLog::getStatus, query.getStatus())
                    .between(Objects.nonNull(query.getStartOperationDate()) && Objects.nonNull(query.getEndOperationDate()),
                            CbCaseImportLog::getCreatedTime, query.getStartOperationDate(), query.getEndOperationDate())
                    .orderByDesc(CbCaseImportLog::getId));
        }

        /**
         * 查询导入记录
         *
         * @param tenantId   租户 id
         * @param propertyId 资产 id
         * @param query      查询条件
         * @return 导入记录
         * @author yyt
         * @date 2026/1/19
         **/
        @Override
        public ListPagedDTO<ImportCaseLogDTO> listImportCaseLogDTO(int tenantId, int propertyId, ImportCaseLogQuery query) {
            Page<CbCaseImportLog> page = listPage(propertyId, query);
            if (CollectionUtils.isEmpty(page.getRecords())) {
                return new ListPagedDTO<>();
            }
            List<Integer> userIds = page.getRecords().stream().map(CbCaseImportLog::getCreatedBy).distinct().collect(Collectors.toList());
            Map<Integer, String> nameById = userInternalService.getNameMapByIds(tenantId, userIds);
            return new ListPagedDTO<>(page.getTotal(), beanMapper.mapList(page.getRecords(), ImportCaseLogDTO.class,
                    item -> item.setCreatedByName(nameById.get(item.getCreatedBy()))));
        }

    }