# BookingImportLogServiceImpl.java

    package cloud.wconnect.rateshop.service.booking.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.rateshop.entity.booking.BookingImportLog;
    import cloud.wconnect.rateshop.mapper.booking.BookingImportLogMapper;
    import cloud.wconnect.rateshop.pojo.dto.booking.BookingImportLogDTO;
    import cloud.wconnect.rateshop.pojo.query.booking.BookingImportLogQuery;
    import cloud.wconnect.rateshop.service.booking.IBookingImportLogService;
    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 jakarta.annotation.Nullable;

    import java.util.*;

    /**
     * <p>
     * Booking 导入记录 服务实现类
     * </p>
     *
     * @author yyt
     * @since 2024-07-22
     */
    @Service
    @DS("rate_shop")
    public class BookingImportLogServiceImpl extends ServiceImpl<BookingImportLogMapper, BookingImportLog> implements IBookingImportLogService {

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

        /**
         * 保存记录
         *
         * @param propertyId 资产 id
         * @param startDate  开始日期
         * @param toDate     结束日期
         * @param status     状态
         * @param userId     操作人
         * @param key        s3 key
         * @return 记录
         * @author yyt
         * @date 2024/7/23
         **/
        @Override
        public BookingImportLog saveRecord(Integer propertyId, Long startDate, Long toDate, int status, int userId,
                                           @Nullable String key) {
            BookingImportLog importLog = new BookingImportLog()
                    .setPropertyId(propertyId)
                    .setFromDate(startDate)
                    .setToDate(toDate)
                    .setImportResult(status)
                    .setCreatedBy(userId)
                    .setCreatedTime(DateTimeUtils.nowTimestamp())
                    .setUrl(key);
            save(importLog);
            return importLog;
        }

        /**
         * 查询导入记录
         *
         * @param propertyId 资产 id
         * @param query      查询条件
         * @return 导入记录
         * @author yyt
         * @date 2025/11/4
         **/
        @Override
        public ListPagedDTO<BookingImportLogDTO> listImportLog(int propertyId, BookingImportLogQuery query) {
            List<BookingImportLog> importLogs;
            long total;
            LambdaQueryWrapper<BookingImportLog> queryWrapper = new QueryWrapper<BookingImportLog>().lambda()
                    .eq(BookingImportLog::getPropertyId, propertyId)
                    .in(CollectionUtils.isNotEmpty(query.getImportResult()),
                            BookingImportLog::getImportResult, query.getImportResult())
                    .between(Objects.nonNull(query.getStartFromDataDate()) && Objects.nonNull(query.getStartToDataDate()),
                            BookingImportLog::getFromDate, query.getStartFromDataDate(), query.getStartToDataDate())
                    .between(Objects.nonNull(query.getEndFromDataDate()) && Objects.nonNull(query.getEndToDataDate()),
                            BookingImportLog::getToDate, query.getEndFromDataDate(), query.getEndToDataDate())
                    .nested(Objects.nonNull(query.getStartOperationDate()) && Objects.nonNull(query.getEndOperationDate()),
                            sql -> {
                                sql.between(BookingImportLog::getCreatedTime, query.getStartOperationDate(), query.getEndOperationDate())
                                        .or()
                                        .between(BookingImportLog::getResolvedTime, query.getStartOperationDate(), query.getEndOperationDate());
                            });
            if (query.getPage() != null && query.getSize() != null) {
                Page<BookingImportLog> page = page(new Page<>(query.getPage(), query.getSize()),
                        queryWrapper.orderByDesc(BookingImportLog::getCreatedTime));
                importLogs = page.getRecords();
                total = page.getTotal();
            } else {
                importLogs = list(queryWrapper);
                total = importLogs.size();
            }

            Map<Integer, String> userNameById = Collections.emptyMap();
            if (CollectionUtils.isNotEmpty(importLogs)) {
                Set<Integer> userIds = Sets.newHashSetWithExpectedSize(importLogs.size());
                for (BookingImportLog 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, BookingImportLogDTO.class,
                    item -> {
                        item.setCreatedName(finalUserNameById.get(item.getCreatedBy()));
                        item.setResolvedName(finalUserNameById.get(item.getResolvedBy()));
                    }));
        }

    }