# ScheduleHistoryAndForecastImportLogServiceImpl.java

    package cloud.wconnect.workperformance.service.schedule.impl;

    import cloud.wconnect.commonlib.component.BeanMapper;
    import cloud.wconnect.commonlib.pojo.dto.ListPagedDTO;
    import cloud.wconnect.commonlib.redis.PropertyI18nRedisService;
    import cloud.wconnect.commonlib.service.UserInternalService;
    import cloud.wconnect.commonlib.util.DateTimeUtils;
    import cloud.wconnect.workperformance.entity.schedule.ScheduleHistoryAndForecastImportLog;
    import cloud.wconnect.workperformance.mapper.schedule.ScheduleHistoryAndForecastImportLogMapper;
    import cloud.wconnect.workperformance.pojo.dto.schedule.HistoryAndForecastImportLogDTO;
    import cloud.wconnect.workperformance.pojo.query.schedule.ImportLogQuery;
    import cloud.wconnect.workperformance.service.schedule.IScheduleHistoryAndForecastImportLogService;
    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 com.google.common.collect.Sets;
    import org.apache.commons.collections.CollectionUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;

    import java.util.*;

    /**
     * <p>
     * history and forcast 导入记录 服务实现类
     * </p>
     *
     * @author yyt
     * @since 2023-06-28
     */
    @Service
    @DS("work_performance")
    public class ScheduleHistoryAndForecastImportLogServiceImpl extends ServiceImpl<ScheduleHistoryAndForecastImportLogMapper, ScheduleHistoryAndForecastImportLog> implements IScheduleHistoryAndForecastImportLogService {

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

        /**
         * 插入导入记录
         *
         * @param propertyId 资产 id
         * @param theDate    指定日期
         * @param startDate  开始日期
         * @param toDate     结束日期
         * @param status     状态
         * @param userId     操作人
         * @return 导入记录
         * @author yyt
         * @date 2023/10/31
         */
        @Override
        public ScheduleHistoryAndForecastImportLog saveRecord(Integer propertyId, Long theDate, Long startDate, Long toDate,
                                                              int status, int userId) {
            ScheduleHistoryAndForecastImportLog importLog = new ScheduleHistoryAndForecastImportLog()
                    .setPropertyId(propertyId)
                    .setTheDate(theDate)
                    .setFromDate(startDate)
                    .setToDate(toDate)
                    .setImportResult(status)
                    .setCreatedBy(userId)
                    .setCreatedTime(DateTimeUtils.nowTimestamp());
            save(importLog);
            return importLog;
        }

        /**
         * 分页查询数据
         *
         * @param propertyId 资产 id
         * @param query      查询条件
         * @return 导入记录
         * @author yyt
         * @date 2024/11/22
         **/
        @Override
        public Page<ScheduleHistoryAndForecastImportLog> listPage(Integer propertyId, ImportLogQuery query) {
            return page(new Page<>(query.getPage(), query.getSize()),
                            packageSql(propertyId, query).orderByDesc(ScheduleHistoryAndForecastImportLog::getCreatedTime));
        }

        /**
         * 查询数据
         *
         * @param propertyId 资产 id
         * @param query      查询条件
         * @return 导入记录
         * @author yyt
         * @date 2024/11/22
         **/
        @Override
        public List<ScheduleHistoryAndForecastImportLog> list(Integer propertyId, ImportLogQuery query) {
            return list(packageSql(propertyId, query));
        }

        /**
         * 封装查询 sql
         *
         * @param propertyId 资产 id
         * @param query      查询条件
         * @return sql
         * @author yyt
         * @date 2024/11/22
         **/
        private LambdaQueryWrapper<ScheduleHistoryAndForecastImportLog> packageSql(Integer propertyId, ImportLogQuery query) {
            return new QueryWrapper<ScheduleHistoryAndForecastImportLog>().lambda()
                    .eq(ScheduleHistoryAndForecastImportLog::getPropertyId, propertyId)
                    .in(CollectionUtils.isNotEmpty(query.getImportResult()),
                            ScheduleHistoryAndForecastImportLog::getImportResult, query.getImportResult())
                    .between(Objects.nonNull(query.getStartFromDataDate()) && Objects.nonNull(query.getStartToDataDate()),
                            ScheduleHistoryAndForecastImportLog::getFromDate, query.getStartFromDataDate(), query.getStartToDataDate())
                    .between(Objects.nonNull(query.getEndFromDataDate()) && Objects.nonNull(query.getEndToDataDate()),
                            ScheduleHistoryAndForecastImportLog::getToDate, query.getEndFromDataDate(), query.getEndToDataDate())
                    .nested(Objects.nonNull(query.getStartOperationDate()) && Objects.nonNull(query.getEndOperationDate()),
                            sql -> {
                                sql.between(ScheduleHistoryAndForecastImportLog::getCreatedTime, query.getStartOperationDate(), query.getEndOperationDate())
                                        .or()
                                        .between(ScheduleHistoryAndForecastImportLog::getResolvedTime, query.getStartOperationDate(), query.getEndOperationDate());
                            });
        }

        /**
         * 获取导入记录
         *
         * @param propertyId 资产 id
         * @param importLogQuery 查询条件
         * @return 导入记录
         * @author yyt
         * @date 2025/12/31
         **/
        @Override
        public ListPagedDTO<HistoryAndForecastImportLogDTO> listHistoryAndForecastImportLog(Integer propertyId, ImportLogQuery importLogQuery) {
            List<ScheduleHistoryAndForecastImportLog> importLogs;
            long total;

            if (importLogQuery.getPage() != null && importLogQuery.getSize() != null) {
                Page<ScheduleHistoryAndForecastImportLog> page = listPage(propertyId, importLogQuery);
                importLogs = page.getRecords();
                total = page.getTotal();
            } else {
                importLogs = list(propertyId, importLogQuery);
                total = importLogs.size();
            }

            Map<Integer, String> userNameById = Collections.emptyMap();
            if (CollectionUtils.isNotEmpty(importLogs)) {
                Set<Integer> userIds = Sets.newHashSetWithExpectedSize(importLogs.size());
                for (ScheduleHistoryAndForecastImportLog item : importLogs) {
                    if (item.getCreatedBy() > 0) {
                        userIds.add(item.getCreatedBy());
                    }
                    if (item.getResolvedBy() > 0) {
                        userIds.add(item.getResolvedBy());
                    }
                }
                if (CollectionUtils.isNotEmpty(userIds)) {
                    userNameById = userInternalService.getNameMapByIds(propertyI18nRedisService.get(propertyId).getTenantId(), userIds);
                }
            }

            Map<Integer, String> finalUserNameById = userNameById;
            return new ListPagedDTO<>(total, beanMapper.mapList(importLogs, HistoryAndForecastImportLogDTO.class,
                    item -> {
                        item.setCreatedName(finalUserNameById.get(item.getCreatedBy()));
                        item.setResolvedName(finalUserNameById.get(item.getResolvedBy()));
                    }));
        }

        /**
         * 更新导入记录
         *
         * @param id     导入记录 id
         * @param status 状态
         * @param userId 操作人
         * @author yyt
         * @date 2025/12/31
         **/
        @Override
        public void update(int id, int status, int userId) {
            updateById(new ScheduleHistoryAndForecastImportLog()
                    .setId(id)
                    .setImportResult(status)
                    .setResolvedBy(userId)
                    .setResolvedTime(DateTimeUtils.nowTimestamp()));
        }

    }