# OooImportLogServiceImpl.java

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

    import cloud.wconnect.commonlib.component.BeanMapper;
    import cloud.wconnect.commonlib.cons.BaseConstant;
    import cloud.wconnect.commonlib.cons.BaseErrorCodeEnum;
    import cloud.wconnect.commonlib.cons.CommonErrorI18nConstant;
    import cloud.wconnect.commonlib.exception.BadRequestException;
    import cloud.wconnect.commonlib.pojo.dto.ListPagedDTO;
    import cloud.wconnect.commonlib.service.UserInternalService;
    import cloud.wconnect.commonlib.util.DateTimeUtils;
    import cloud.wconnect.frontoffice.entity.oooroom.OooImportLog;
    import cloud.wconnect.frontoffice.mapper.oooroom.OooImportLogMapper;
    import cloud.wconnect.frontoffice.pojo.dto.oooroom.OooImportLogDTO;
    import cloud.wconnect.frontoffice.pojo.query.oooroom.OooImportLogQuery;
    import cloud.wconnect.frontoffice.service.oooroom.IOooImportLogService;
    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.lang.Nullable;
    import org.springframework.stereotype.Service;

    import java.util.*;

    /**
     * <p>
     * ooo导入记录 服务实现类
     * </p>
     *
     * @author yyt
     * @since 2023-08-05
     */
    @DS("ratelog_manage")
    @Service
    public class OooImportLogServiceImpl extends ServiceImpl<OooImportLogMapper, OooImportLog> implements IOooImportLogService {

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

        /**
         * 校验存在
         *
         * @param id 异常 id
         * @return 异常
         * @author yyt
         * @date 2023/8/7
         */
        @Override
        public OooImportLog checExist(int id) {
            OooImportLog importLog = getById(id);
            if (Objects.isNull(importLog)) {
                throw new BadRequestException(CommonErrorI18nConstant.DATA_NOT_FOUND,
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(), "the oooImportLog is not found;id:" + id);
            }
            return importLog;
        }

        /**
         * 插入导入记录
         *
         * @param propertyId 资产 id
         * @param theDate    指定日期
         * @param status     状态
         * @param path       文件路径
         * @param userId     操作人
         * @return 导入记录
         * @author yyt
         * @date 2023/10/31
         */
        @Override
        public OooImportLog saveRecord(Integer propertyId, Long theDate, int status, @Nullable String path, int userId) {
            OooImportLog importLog = new OooImportLog()
                    .setPropertyId(propertyId)
                    .setTheDate(theDate)
                    .setStatus(status)
                    .setUrl(path)
                    .setCreatedBy(userId)
                    .setCreatedTime(DateTimeUtils.nowTimestamp())
                    .setResolvedBy(BaseConstant.ZERO)
                    .setResolvedTime(BaseConstant.LONG_ZERO);
            save(importLog);
            return importLog;
        }

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

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

        /**
         * 封装查询 sql
         *
         * @param propertyId 资产 id
         * @param query      查询条件
         * @return sql
         * @author yyt
         * @date 2024/11/20
         **/
        private LambdaQueryWrapper<OooImportLog> packageSql(Integer propertyId, OooImportLogQuery query) {
            return new QueryWrapper<OooImportLog>().lambda()
                    .eq(OooImportLog::getPropertyId, propertyId)
                    .in(CollectionUtils.isNotEmpty(query.getStatus()), OooImportLog::getStatus, query.getStatus())
                    .between(Objects.nonNull(query.getStartDataDate()) && Objects.nonNull(query.getEndDataDate()),
                            OooImportLog::getTheDate, query.getStartDataDate(), query.getEndDataDate())
                    .nested(Objects.nonNull(query.getStartOperationDate()) && Objects.nonNull(query.getEndOperationDate()),
                            sql -> {
                                sql.between(OooImportLog::getCreatedTime, query.getStartOperationDate(), query.getEndOperationDate())
                                        .or()
                                        .between(OooImportLog::getResolvedTime, query.getStartOperationDate(), query.getEndOperationDate());
                            });
        }

        /**
         * 获取导入记录
         *
         * @param tenantId   租户 id
         * @param propertyId 资产 id
         * @param query      查询条件
         * @return 导入记录
         * @author yyt
         * @date 2026/1/19
         **/
        @Override
        public ListPagedDTO<OooImportLogDTO> listImportLogDTO(Integer tenantId, Integer propertyId, OooImportLogQuery query) {
            long total;
            List<OooImportLog> importLogs;
            if (Objects.nonNull(query.getPage()) && Objects.nonNull(query.getSize())) {
                Page<OooImportLog> page = listPage(propertyId, query);
                total = page.getTotal();
                importLogs = page.getRecords();
            } else {
                importLogs = list(propertyId, query);
                total = importLogs.size();
            }

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

            return new ListPagedDTO<>(total, beanMapper.mapList(importLogs, OooImportLogDTO.class,
                    item -> {
                        item.setCreatedName(userNameById.get(item.getCreatedBy()));
                        item.setResolvedName(userNameById.get(item.getResolvedBy()));
                    }));
        }

        /**
         * 获取操作用户名称
         *
         * @param tenantId   租户 id
         * @param importLogs 导入记录
         * @return 用户名称
         * @author yyt
         * @date 2026/1/19
         **/
        private Map<Integer, String> summaryUserName(Integer tenantId, List<OooImportLog> importLogs) {
            if (CollectionUtils.isNotEmpty(importLogs)) {
                Set<Integer> userIds = Sets.newHashSetWithExpectedSize(importLogs.size());
                for (OooImportLog item : importLogs) {
                    if (item.getCreatedBy() > 0) {
                        userIds.add(item.getCreatedBy());
                    }
                    if (item.getResolvedBy() > 0) {
                        userIds.add(item.getResolvedBy());
                    }
                }
                if (CollectionUtils.isNotEmpty(userIds)) {
                    return userInternalService.getNameMapByIds(tenantId, userIds);
                }
            }
            return Collections.emptyMap();
        }

        /**
         * 更新导入记录状态
         *
         * @param id     导入记录 id
         * @param status 状态
         * @param userId 操作人
         * @author yyt
         * @date 2026/1/19
         **/
        @Override
        public void updateStatus(int id, int status, int userId) {
            OooImportLog updateContent = new OooImportLog();
            updateContent.setId(id);
            updateContent.setResolvedBy(userId);
            updateContent.setResolvedTime(DateTimeUtils.nowTimestamp());
            updateContent.setStatus(status);
            updateById(updateContent);
        }

    }