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

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.db.mybatisplus.handler.DynamicTableNameHandler;
import com.njcn.gather.storage.mapper.ContrastNonHarmonicMappper;
import com.njcn.gather.storage.pojo.po.ContrastNonHarmonicResult;
import com.njcn.gather.storage.service.ContrastNonHarmonicService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author caozehui
 * @data 2025-07-28
 */
@Service
@RequiredArgsConstructor
public class ContrastNonHarmonicServiceImpl extends ServiceImpl<ContrastNonHarmonicMappper, ContrastNonHarmonicResult> implements ContrastNonHarmonicService {


    @Override
    public List<ContrastNonHarmonicResult> listAllRawData(String code, Integer num, Integer waveNum, Boolean isWave, String dataType, Integer flag, String devId, List<String> adTypeList) {
        String prefix = "ad_non_harmonic_" + code;
        DynamicTableNameHandler.setTableName(prefix);
        LambdaQueryChainWrapper<ContrastNonHarmonicResult> wrapper = this.lambdaQuery().likeRight(ContrastNonHarmonicResult::getDevMonitorId, devId)
                .eq(ObjectUtil.isNotNull(num), ContrastNonHarmonicResult::getNum, num)
                .eq(ContrastNonHarmonicResult::getFlag, flag)
                .eq(StrUtil.isNotBlank(dataType), ContrastNonHarmonicResult::getDataType, dataType)
                .in(CollUtil.isNotEmpty(adTypeList), ContrastNonHarmonicResult::getAdType, adTypeList)
                .orderByAsc(ContrastNonHarmonicResult::getTimeId);
        if (isWave) {
            if (ObjectUtil.isNotNull(waveNum)) {
                wrapper.eq(ContrastNonHarmonicResult::getWaveNum, waveNum);
            } else {
                wrapper.isNotNull(ContrastNonHarmonicResult::getWaveNum);
            }
        } else {
            wrapper.isNull(ContrastNonHarmonicResult::getWaveNum);
        }
        List<ContrastNonHarmonicResult> result = wrapper.list();
        DynamicTableNameHandler.remove();
        return result;
    }

    @Override
    public List<ContrastNonHarmonicResult> listAllResultData(String code, Integer num, Integer waveNum, Boolean isWave, String dataType, String devId, List<String> adTypeList) {
        String prefix = "ad_non_harmonic_result_" + code;
        DynamicTableNameHandler.setTableName(prefix);
        LambdaQueryChainWrapper<ContrastNonHarmonicResult> wrapper = this.lambdaQuery().likeRight(ContrastNonHarmonicResult::getDevMonitorId, devId)
                .eq(ObjectUtil.isNotNull(num), ContrastNonHarmonicResult::getNum, num)
                .eq(StrUtil.isNotBlank(dataType), ContrastNonHarmonicResult::getDataType, dataType)
                .in(CollUtil.isNotEmpty(adTypeList), ContrastNonHarmonicResult::getAdType, adTypeList);
        if (isWave) {
            if (ObjectUtil.isNotNull(waveNum)) {
                wrapper.eq(ContrastNonHarmonicResult::getWaveNum, waveNum);
            } else {
                wrapper.isNotNull(ContrastNonHarmonicResult::getWaveNum);
            }
        } else {
            wrapper.isNull(ContrastNonHarmonicResult::getWaveNum);
        }
        List<ContrastNonHarmonicResult> result = wrapper.list();
        DynamicTableNameHandler.remove();
        return result;
    }

    @Override
    public ContrastNonHarmonicResult getContrastResultHarm(Integer planCode, String monitorId, List<String> scriptId, String resultType, int time) {
        boolean isWave = resultType.contains("wave_data");
        int waveTime = 1;
        if (isWave) {
            // 说明是要进一步拆解type，查询录波的数据
            // 从 wave_data_1 格式中提取录波次数
            String[] parts = resultType.split("_");
            if (parts.length > 2) {
                waveTime = Integer.parseInt(parts[parts.length - 1]);
            }
        }
        List<ContrastNonHarmonicResult> result = this.listAllResultData(String.valueOf(planCode), time, waveTime, isWave, null, monitorId, scriptId);
        if (CollectionUtil.isNotEmpty(result)) {
            return result.get(0);
        }
        return null;
    }

    @Override
    public int getNumOfData(Integer planCode, String monitorId, List<String> scriptId, String resultType, int time) {
        String prefix = "ad_non_harmonic_" + planCode;
        DynamicTableNameHandler.setTableName(prefix);
        boolean isWave = resultType.contains("wave_data");
        int waveTime = 1;
        if (isWave) {
            // 说明是要进一步拆解type，查询录波的数据
            // 从 wave_data_1 格式中提取录波次数
            String[] parts = resultType.split("_");
            if (parts.length > 2) {
                waveTime = Integer.parseInt(parts[parts.length - 1]);
            }
        }
        LambdaQueryChainWrapper<ContrastNonHarmonicResult> wrapper = this.lambdaQuery()
                .eq(ContrastNonHarmonicResult::getDevMonitorId, monitorId)
                .in(ContrastNonHarmonicResult::getAdType, scriptId)
                .eq(ContrastNonHarmonicResult::getFlag, 0)
                .eq(ContrastNonHarmonicResult::getNum, time);
        if (isWave) {
            wrapper.eq(ContrastNonHarmonicResult::getDataType, "wave_data")
                    .eq(ContrastNonHarmonicResult::getWaveNum, waveTime);
        } else {
            wrapper.eq(ContrastNonHarmonicResult::getDataType, resultType);
        }
        // 执行查询并统计满足条件的记录数
        int count = wrapper.count();
        DynamicTableNameHandler.remove();
        return count;
    }
}
