package com.njcn.gather.device.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.gather.device.mapper.PqDevSubMapper;
import com.njcn.gather.device.pojo.po.PqDevSub;
import com.njcn.gather.device.service.IPqDevSubService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;

/**
 * @author caozehui
 * @date 2025-07-04
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class PqDevSubServiceImpl extends ServiceImpl<PqDevSubMapper, PqDevSub> implements IPqDevSubService {

    @Override
    public LocalDateTime resolveBatchMaxCheckEndTime(List<String> devIds) {
        if (CollUtil.isEmpty(devIds)) {
            return null;
        }
        return this.list(new LambdaQueryWrapper<PqDevSub>()
                        .in(PqDevSub::getDevId, devIds)
                        .isNotNull(PqDevSub::getCheckEndTime))
                .stream()
                .map(PqDevSub::getCheckEndTime)
                .filter(Objects::nonNull)
                .max(Comparator.naturalOrder())
                .orElse(null);
    }
}
