# AdJournalLogServiceImpl.java

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

    import cloud.wconnect.commonlib.component.BeanMapper;
    import cloud.wconnect.commonlib.cons.*;
    import cloud.wconnect.commonlib.pojo.dto.ListPagedDTO;
    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.pojo.dto.scheduler.JobParameterDTO;
    import cloud.wconnect.commonlib.service.UserInternalService;
    import cloud.wconnect.commonlib.util.DateTimeUtils;
    import cloud.wconnect.frontoffice.entity.adjustment.AdJournalLog;
    import cloud.wconnect.frontoffice.mapper.adjustment.AdJournalLogMapper;
    import cloud.wconnect.frontoffice.pojo.dto.adjustment.AdjImportReportDTO;
    import cloud.wconnect.frontoffice.pojo.query.adjustment.ImportLogQuery;
    import cloud.wconnect.frontoffice.pojo.vo.adjustment.AdjImportLogVO;
    import cloud.wconnect.frontoffice.service.adjustment.IAdJournalLogService;
    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.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.Set;
    import java.util.stream.Collectors;
    import java.util.stream.Stream;

    /**
     * <p>
     * 服务实现类
     * </p>
     *
     * @author lichen
     * @since 2023-04-03
     */
    @Service
    @DS("logbook")
    public class AdJournalLogServiceImpl extends ServiceImpl<AdJournalLogMapper, AdJournalLog> implements IAdJournalLogService {

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

        /**
         * 根据日志id更新处理结果和解决人、解决时间
         *
         * @param id           日志id
         * @param userId       用户id
         * @param url          文件路径
         * @param importResult 处理结果 true:成功 false:失败
         * @return void
         * @author lichen
         * @date 2023/9/29
         */
        @Override
        public void updateLog(Long id, Integer userId, String url, boolean importResult) {
            AdJournalLog oriLog = getById(id);
            if (oriLog.getStatus() == ReportImportResultCons.IMPORT_RESULT_REPAIRING && importResult) {
                oriLog.setResolvedBy(userId);
                oriLog.setResolvedTime(DateTimeUtils.nowTimestamp());
            }
            oriLog.setStatus(importResult ? ReportImportResultCons.IMPORT_RESULT_SUCCESS : ReportImportResultCons.IMPORT_RESULT_FAIL);
            oriLog.setUrl(url);
            updateById(oriLog);
        }

        /**
         * 根据状态查询异常数量
         *
         * @param propertyId   资产ID
         * @param importStatus 导入状态
         * @return java.lang.Long
         * @author lichen
         * @date 2023/12/11
         */
        @Override
        public Long countByStatus(Integer propertyId, int importStatus) {
            return count(new LambdaQueryWrapper<AdJournalLog>()
                    .eq(AdJournalLog::getPropertyId, propertyId)
                    .eq(AdJournalLog::getStatus, importStatus));
        }

        /**
         * 保存导入日志
         *
         * @param propertyDTO  资产DTO
         * @param dateRange    数据范围
         * @param path         s3路径
         * @param importResult 处理结果
         * @param uid          操作人
         * @return void
         * @author lichen
         * @date 2024/5/31
         */
        @Override
        public void save(PropertyDTO propertyDTO, Map<String, Long> dateRange, String path, boolean importResult, Integer uid) {
            AdJournalLog adJournalLog = new AdJournalLog();
            adJournalLog.setPropertyId(propertyDTO.getId());
            adJournalLog.setStartDate(dateRange.get(JobParameterDTO.START_DATE));
            adJournalLog.setEndDate(dateRange.get(JobParameterDTO.END_DATE));
            adJournalLog.setCreatedBy(uid);
            adJournalLog.setCreatedTime(DateTimeUtils.nowTimestamp());
            adJournalLog.setResolvedBy(0);
            adJournalLog.setResolvedTime(0L);
            adJournalLog.setStatus(importResult ? ReportImportResultCons.IMPORT_RESULT_SUCCESS : ReportImportResultCons.IMPORT_RESULT_FAIL);
            adJournalLog.setUrl(path);
            save(adJournalLog);
        }

        /**
         * 新增处理中的导入日志
         *
         * @param propertyDTO        资产DTO
         * @param adjImportReportDTO 导入DTO
         * @param loginUserId        登录用户id
         * @return long 主键id
         * @author lichen
         * @date 2025/7/26
         */
        @Override
        public long insertInProgress(PropertyDTO propertyDTO, AdjImportReportDTO adjImportReportDTO, int loginUserId) {
            AdJournalLog adJournalLog = new AdJournalLog();
            adJournalLog.setPropertyId(propertyDTO.getId());
            adJournalLog.setStartDate(adjImportReportDTO.getStartDate());
            adJournalLog.setEndDate(adjImportReportDTO.getEndDate());
            adJournalLog.setCreatedBy(loginUserId);
            adJournalLog.setCreatedTime(DateTimeUtils.nowTimestamp());
            adJournalLog.setResolvedBy(0);
            adJournalLog.setResolvedTime(0L);
            adJournalLog.setStatus(ReportImportResultCons.IMPORT_IN_PROGRESS);

            save(adJournalLog);

            return adJournalLog.getId();
        }

        /**
         * 获取导入日志列表
         *
         * @param tenantId   租户id
         * @param propertyId 资产id
         * @param query      查询参数
         * @return cloud.wconnect.commonlib.pojo.dto.ListPagedDTO<cloud.wconnect.frontoffice.pojo.vo.adjustment.AdjImportLogVO>
         * @author lichen
         * @date 2025/7/26
         */
        @Override
        public ListPagedDTO<AdjImportLogVO> pageList(Integer tenantId, int propertyId, ImportLogQuery query) {
            long total;
            List<AdJournalLog> importLogs;
            LambdaQueryWrapper<AdJournalLog> condition = new QueryWrapper<AdJournalLog>().lambda()
                    .eq(AdJournalLog::getPropertyId, propertyId)
                    .in(CollectionUtils.isNotEmpty(query.getStatus()), AdJournalLog::getStatus, query.getStatus())
                    .and(Objects.nonNull(query.getStartOperationDate()) && Objects.nonNull(query.getEndOperationDate()),
                            wrapper -> wrapper.between(AdJournalLog::getCreatedTime,
                                            query.getStartOperationDate(), query.getEndOperationDate())
                                    .or().between(AdJournalLog::getResolvedTime,
                                            query.getStartOperationDate(), query.getEndOperationDate()))
                    .between(Objects.nonNull(query.getStartFromDataDate()) && Objects.nonNull(query.getStartToDataDate()),
                            AdJournalLog::getStartDate, query.getStartFromDataDate(), query.getStartToDataDate())
                    .between(Objects.nonNull(query.getEndFromDataDate()) && Objects.nonNull(query.getEndToDataDate()),
                            AdJournalLog::getEndDate, query.getEndFromDataDate(), query.getEndToDataDate())
                    .orderByDesc(AdJournalLog::getCreatedTime);
            if (Objects.nonNull(query.getPage()) && Objects.nonNull(query.getSize())) {
                Page<AdJournalLog> page = page(new Page<>(query.getPage(), query.getSize()), condition);
                total = page.getTotal();
                importLogs = page.getRecords();
            } else {
                importLogs = list(condition);
                total = importLogs.size();
            }
            if (total == BaseConstant.ZERO) return new ListPagedDTO<>();

            //查询用户信息
            Set<Integer> userIds = importLogs.stream()
                    .flatMap(item -> Stream.of(item.getCreatedBy(), item.getResolvedBy())).collect(Collectors.toSet());
            Map<Integer, String> userId2Name = userInternalService.getNameMapByIds(tenantId, userIds);

            return new ListPagedDTO<>(total, beanMapper.mapList(importLogs, AdjImportLogVO.class, (entity, vo) -> {
                vo.setCreatedName(userId2Name.get(vo.getCreatedBy()));
                vo.setResolvedName(userId2Name.get(vo.getResolvedBy()));
            }));
        }
    }