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

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.gather.device.mapper.PqStandardDevGainMapper;
import com.njcn.gather.device.pojo.po.PqStandardDevGain;
import com.njcn.gather.device.service.IPqStandardDevGainService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author caozehui
 * @date 2026-01-12
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class PqStandardDevGainServiceImpl extends ServiceImpl<PqStandardDevGainMapper, PqStandardDevGain> implements IPqStandardDevGainService {

    @Override
    public boolean add(List<PqStandardDevGain> pqStandardDevGainList) {
        log.info("add PqStandardDevGainList: {}", pqStandardDevGainList.size());
        List<String> stdMonitorIdList = pqStandardDevGainList.stream().map(PqStandardDevGain::getStdDevMonitorId).distinct().collect(Collectors.toList());
        List<String> existingStdMonitorIdList = this.listByIds(stdMonitorIdList).stream().map(PqStandardDevGain::getStdDevMonitorId).collect(Collectors.toList());

        pqStandardDevGainList = pqStandardDevGainList.stream().filter(pqStandardDevGain -> !existingStdMonitorIdList.contains(pqStandardDevGain.getStdDevMonitorId())).collect(Collectors.toList());
        return this.saveBatch(pqStandardDevGainList);
    }

    @Override
    public PqStandardDevGain get(String stdDevMonitorId) {
        return this.lambdaQuery().eq(PqStandardDevGain::getStdDevMonitorId, stdDevMonitorId).one();
    }

    @Override
    public List<PqStandardDevGain> list(String stdDevId) {
        return this.lambdaQuery().likeRight(PqStandardDevGain::getStdDevMonitorId, stdDevId).orderByAsc(PqStandardDevGain::getStdDevMonitorId).list();
    }
}
