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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.db.mybatisplus.handler.DynamicTableNameHandler;
import com.njcn.gather.freqConverter.config.FreqConverterConfig;
import com.njcn.gather.freqConverter.mapper.PqFreqConverterTestResMapper;
import com.njcn.gather.freqConverter.pojo.po.PqFreqConverterTestRes;
import com.njcn.gather.freqConverter.service.IPqFreqConverterTestResService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author caozehui
 * @data 2026-04-14
 */
@Service
@AllArgsConstructor
public class PqFreqConverterTestResServiceImpl extends ServiceImpl<PqFreqConverterTestResMapper, PqFreqConverterTestRes> implements IPqFreqConverterTestResService {
    private final FreqConverterConfig freqConverterConfig;

    @Override
    public void clearAllData(Integer suffix) {
        DynamicTableNameHandler.setTableName(PqFreqConverterConfigServiceImpl.PQ_FREQ_CONVERTER_TEST_RES_TB_PREFIX + suffix);
        this.remove(null);
        DynamicTableNameHandler.remove();
    }


    @Override
    public boolean saveTestRes(Integer suffix, List<PqFreqConverterTestRes> testResList) {
        DynamicTableNameHandler.setTableName(PqFreqConverterConfigServiceImpl.PQ_FREQ_CONVERTER_TEST_RES_TB_PREFIX + suffix);
        this.saveBatch(testResList);
        DynamicTableNameHandler.remove();
        return true;
    }

    @Override
    public boolean updateTestRes(Integer suffix, List<PqFreqConverterTestRes> testResList) {
        DynamicTableNameHandler.setTableName(PqFreqConverterConfigServiceImpl.PQ_FREQ_CONVERTER_TEST_RES_TB_PREFIX + suffix);
        this.updateBatchById(testResList);
        DynamicTableNameHandler.remove();
        return true;
    }

    @Override
    public List<PqFreqConverterTestRes> listTestRes(Integer suffix) {
        DynamicTableNameHandler.setTableName(PqFreqConverterConfigServiceImpl.PQ_FREQ_CONVERTER_TEST_RES_TB_PREFIX + suffix);
        List<PqFreqConverterTestRes> result = this.list();
        DynamicTableNameHandler.remove();
        return result;
    }

    @Override
    public PqFreqConverterTestRes getLastByDuration(Integer suffix, String id, Integer durationMs) {
        DynamicTableNameHandler.setTableName(PqFreqConverterConfigServiceImpl.PQ_FREQ_CONVERTER_TEST_RES_TB_PREFIX + suffix);
        LambdaQueryWrapper<PqFreqConverterTestRes> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.between(PqFreqConverterTestRes::getDurationMs, durationMs - freqConverterConfig.getAllowErrorDuration(), durationMs + freqConverterConfig.getAllowErrorDuration())
                .ne(PqFreqConverterTestRes::getId, id)
                .orderByAsc(PqFreqConverterTestRes::getResidualVoltage)
                .last("limit 1");
        PqFreqConverterTestRes result = this.getOne(queryWrapper);
        DynamicTableNameHandler.remove();
        return result;
    }

    @Override
    public PqFreqConverterTestRes getLastByResidualVoltage(Integer suffix, String id, Double residualVoltage) {
        DynamicTableNameHandler.setTableName(PqFreqConverterConfigServiceImpl.PQ_FREQ_CONVERTER_TEST_RES_TB_PREFIX + suffix);
        LambdaQueryWrapper<PqFreqConverterTestRes> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.between(PqFreqConverterTestRes::getResidualVoltage, residualVoltage - freqConverterConfig.getAllowErrorResidualVoltage(), residualVoltage + freqConverterConfig.getAllowErrorResidualVoltage())
                .ne(PqFreqConverterTestRes::getId, id)
                .orderByDesc(PqFreqConverterTestRes::getDurationMs)
                .last("limit 1");
        PqFreqConverterTestRes result = this.getOne(queryWrapper);
        DynamicTableNameHandler.remove();
        return result;
    }
}
