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

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.gather.detection.mapper.AdPairMapper;
import com.njcn.gather.detection.pojo.po.AdPair;
import com.njcn.gather.detection.service.IAdPariService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * @author caozehui
 * @data 2025-08-18
 */
@Service
@RequiredArgsConstructor
public class AdPairServiceImpl extends ServiceImpl<AdPairMapper, AdPair> implements IAdPariService {

    @Override
    public Integer getMaxNum(String devMonitorId) {
        List<AdPair> adPairList = this.lambdaQuery().select(AdPair::getNum)
                .eq(AdPair::getDevMonitorId, devMonitorId)
                .orderByDesc(AdPair::getNum)
                .last("LIMIT 1").list();
        if (CollUtil.isNotEmpty(adPairList)) {
            return adPairList.get(0).getNum();
        }
        return 1;
    }

    @Override
    public List<AdPair> listByDevIds(List<String> devIds) {
        if (CollUtil.isNotEmpty(devIds)) {
            QueryWrapper<AdPair> wrapper = new QueryWrapper<>();
            devIds.forEach(devId -> wrapper.likeRight("ad_pair.Dev_Monitor_Id", devId));
            return this.list(wrapper);
        } else {
            return Collections.emptyList();
        }
    }
}
