# ClcGuestInhImportLogServiceImpl.java

    package cloud.wconnect.frontoffice.service.clc.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.clc.ClcGuestInhImportLog;
    import cloud.wconnect.frontoffice.mapper.clc.ClcGuestInhImportLogMapper;
    import cloud.wconnect.frontoffice.pojo.dto.clc.ImportGuestInHouseLogDTO;
    import cloud.wconnect.frontoffice.pojo.query.clc.GuestInHouseImportLogQuery;
    import cloud.wconnect.frontoffice.service.clc.IClcGuestInhImportLogService;
    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 2023-07-24
     */
    @Service
    @DS("logbook")
    public class ClcGuestInhImportLogServiceImpl extends ServiceImpl<ClcGuestInhImportLogMapper, ClcGuestInhImportLog> implements IClcGuestInhImportLogService {

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

        /**
         * 添加记录
         *
         * @param propertyId 资产 id
         * @param status     状态
         * @param path       文件路径
         * @param uid        操作人
         * @author yyt
         * @date 2024/6/5
         **/
        @Override
        public void add(Integer propertyId, int status, String path, Integer uid) {
            save(new ClcGuestInhImportLog().setPropertyId(propertyId).setStatus(status).setUrl(path)
                    .setCreatedBy(uid).setCreatedTime(DateTimeUtils.nowTimestamp()));
        }

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

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

        /**
         * 封装查询 sql
         *
         * @param propertyId 资产 id
         * @param query 查询条件
         * @return sql
         * @author yyt
         * @date 2024/11/19
         **/
        private LambdaQueryWrapper<ClcGuestInhImportLog> packageSQL(Integer propertyId, GuestInHouseImportLogQuery query) {
            return new QueryWrapper<ClcGuestInhImportLog>().lambda()
                    .eq(ClcGuestInhImportLog::getPropertyId, propertyId)
                    .in(CollectionUtils.isNotEmpty(query.getStatus()), ClcGuestInhImportLog::getStatus, query.getStatus())
                    .between(Objects.nonNull(query.getStartOperationDate()) && Objects.nonNull(query.getEndOperationDate()),
                            ClcGuestInhImportLog::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(Integer tenantId, Integer propertyId, GuestInHouseImportLogQuery query) {
            List<ClcGuestInhImportLog> records;
            long total;
            if (Objects.nonNull(query.getPage()) && Objects.nonNull(query.getSize())) {
                Page<ClcGuestInhImportLog> importLogPage = listPage(propertyId, query);
                records = importLogPage.getRecords();
                total = importLogPage.getTotal();
            } else {
                List<ClcGuestInhImportLog> 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 logs     导入记录
         * @return 操作人名称
         * @author yyt
         * @date 2026/1/19
         **/
        private Map<Integer, String> summaryUserName(Integer tenantId, List<ClcGuestInhImportLog> logs) {
            if (CollectionUtils.isNotEmpty(logs)) {
                List<Integer> userIds = logs.stream().map(ClcGuestInhImportLog::getCreatedBy)
                        .filter(createdBy -> createdBy > 0).collect(Collectors.toList());
                if (CollectionUtils.isNotEmpty(userIds)) {
                    return userInternalService.getNameMapByIds(tenantId, userIds);
                }
            }
            return Collections.emptyMap();
        }

    }