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

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.gather.device.mapper.PqStandardDevGainRecordMapper;
import com.njcn.gather.device.pojo.po.PqStandardDevGainRecord;
import com.njcn.gather.device.service.IPqStandardDevGainRecordService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author caozehui
 * @date 2026-01-15
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class PqStandardDevGainRecordServiceImpl extends ServiceImpl<PqStandardDevGainRecordMapper, PqStandardDevGainRecord> implements IPqStandardDevGainRecordService {

    @Override
    public int getMaxNum(String stdDevMonitorId, String devMonitorId) {
        return this.lambdaQuery().eq(PqStandardDevGainRecord::getStdDevMonitorId, stdDevMonitorId)
                .eq(PqStandardDevGainRecord::getDevMonitorId, devMonitorId)
                .list().stream().mapToInt(PqStandardDevGainRecord::getNum).max().orElse(0);
    }

    @Override
    public boolean addOrUpdate(List<PqStandardDevGainRecord> recordList) {
        recordList.forEach(record -> {
            List<PqStandardDevGainRecord> list = this.lambdaQuery().eq(PqStandardDevGainRecord::getStdDevMonitorId, record.getStdDevMonitorId())
                    .eq(PqStandardDevGainRecord::getDevMonitorId, record.getDevMonitorId()).list();
            if (CollectionUtil.isNotEmpty(list)) {
                this.lambdaUpdate().eq(PqStandardDevGainRecord::getStdDevMonitorId, record.getStdDevMonitorId())
                        .eq(PqStandardDevGainRecord::getDevMonitorId, record.getDevMonitorId())
                        .set(PqStandardDevGainRecord::getNum, record.getNum()).update();
            } else {
                this.save(record);
            }
        });
        return true;
    }
}
