# PastReservationImportLogServiceImpl.java

    package cloud.wconnect.frontoffice.service.pastreservation.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.frontoffice.entity.pastreservation.PastReservationImportLog;
    import cloud.wconnect.frontoffice.mapper.pastreservation.PastReservationImportLogMapper;
    import cloud.wconnect.frontoffice.pojo.dto.clc.ImportGuestInHouseLogDTO;
    import cloud.wconnect.frontoffice.pojo.query.clc.GuestInHouseImportLogQuery;
    import cloud.wconnect.frontoffice.service.pastreservation.IPastReservationImportLogService;
    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.collections.CollectionUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;

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

    /**
     * <p>
     *  服务实现类
     * </p>
     *
     * @author yyt
     * @since 2024-10-01
     */
    @Service
    @DS("logbook")
    public class PastReservationImportLogServiceImpl extends ServiceImpl<PastReservationImportLogMapper, PastReservationImportLog> implements IPastReservationImportLogService {

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

        /**
         * 添加记录
         *
         * @param propertyId 资产 id
         * @param type       类型
         * @param status     状态
         * @param path       文件路径
         * @param uid        操作人
         * @return 记录
         * @author yyt
         * @date 2024/10/3
         **/
        @Override
        public PastReservationImportLog add(Integer propertyId, int type, int status, String path, Integer uid) {
            PastReservationImportLog importLog = new PastReservationImportLog().setPropertyId(propertyId).setType(type).setStatus(status).setUrl(path)
                    .setCreatedBy(uid).setCreatedTime(DateTimeUtils.nowTimestamp());
            save(importLog);
            return importLog;
        }

        /**
         * 分页查询数据
         *
         * @param propertyId 资产 id
         * @param query      查询条件
         * @return 导入记录
         * @author yyt
         * @date 2024/11/12
         **/
        @Override
        public Page<PastReservationImportLog> listPage(Integer propertyId, GuestInHouseImportLogQuery query) {
            LambdaQueryWrapper<PastReservationImportLog> queryWrapper = packageQuerySql(propertyId, query);
            return page(new Page<>(query.getPage(), query.getSize()), queryWrapper.orderByDesc(PastReservationImportLog::getId));
        }

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

        /**
         * 封装 sql
         *
         * @param propertyId 资产 id
         * @param query      查询条件
         * @return sql
         * @author yyt
         * @date 2024/11/12
         **/
        private LambdaQueryWrapper<PastReservationImportLog> packageQuerySql(Integer propertyId, GuestInHouseImportLogQuery query) {
            return new QueryWrapper<PastReservationImportLog>()
                    .lambda().eq(PastReservationImportLog::getPropertyId, propertyId)
                    .eq(Objects.nonNull(query.getType()), PastReservationImportLog::getType, query.getType())
                    .in(CollectionUtils.isNotEmpty(query.getStatus()), PastReservationImportLog::getStatus, query.getStatus())
                    .between(Objects.nonNull(query.getStartOperationDate()) && Objects.nonNull(query.getEndOperationDate()),
                            PastReservationImportLog::getCreatedTime, query.getStartOperationDate(), query.getEndOperationDate());
        }

        /**
         * 获取导入记录
         *
         * @param tenantId   租户 id
         * @param propertyId 资产 id
         * @param query      查询条件
         * @return 导入记录
         * @author yyt
         * @date 2026/1/19
         **/
        @Override
        public ListPagedDTO<ImportGuestInHouseLogDTO> listImportGuestInHouseLogDTO(int tenantId, int propertyId, GuestInHouseImportLogQuery query) {
            List<PastReservationImportLog> records;
            long total;

            if (Objects.nonNull(query.getPage()) && Objects.nonNull(query.getSize())) {
                Page<PastReservationImportLog> importLogPage = listPage(propertyId, query);
                records = importLogPage.getRecords();
                total = importLogPage.getTotal();
            } else {
                List<PastReservationImportLog> list = list(propertyId, query);
                records = list;
                total = list.size();
            }

            Map<Integer, String> userNameById = summaryUserName(tenantId, records);

            return new ListPagedDTO<>(total, beanMapper.mapList(records, ImportGuestInHouseLogDTO.class,
                    (item) -> item.setCreatedName(userNameById.get(item.getCreatedBy()))));
        }

        /**
         * 汇总操作用户名称
         *
         * @param tenantId 租户 id
         * @param records  记录
         * @return 用户名称
         * @author yyt
         * @date 2026/1/19
         **/
        private Map<Integer, String> summaryUserName(int tenantId, List<PastReservationImportLog> records) {
            if (CollectionUtils.isNotEmpty(records)) {
                List<Integer> userIds = records.stream().map(PastReservationImportLog::getCreatedBy)
                        .filter(createdBy -> createdBy > 0).collect(Collectors.toList());
                if (CollectionUtils.isNotEmpty(userIds)) {
                    return userInternalService.getNameMapByIds(tenantId, userIds);
                }
            }
            return Collections.emptyMap();
        }

    }