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.ContrastHarmonicMappper;
import com.njcn.gather.storage.pojo.po.ContrastHarmonicResult;
import com.njcn.gather.storage.service.ContrastHarmonicService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author caozehui
 * @data 2025-07-28
 */
@Service
@RequiredArgsConstructor
public class ContrastHarmonicServiceImpl extends ServiceImpl<ContrastHarmonicMappper, ContrastHarmonicResult> implements ContrastHarmonicService {


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


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

    /**
     * @param planCode   计划code
     * @param monitorId  监测点ID
     * @param scriptId   指标id
     * @param resultType 结果类型
     * @param time       第几次检测
     * @return 检测结果
     */
    @Override
    public ContrastHarmonicResult 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<ContrastHarmonicResult> result = this.listAllResultData(String.valueOf(planCode), time, waveTime, isWave, null, monitorId, scriptId);
        if (CollectionUtil.isNotEmpty(result)) {
            return result.get(0);
        }
        return null;
    }

    /**
     * 去原始表获取总次数
     *
     * @param planCode   计划code
     * @param monitorId  监测点ID
     * @param scriptId   指标id
     * @param resultType 结果类型
     * @param time       第几次检测
     * @return 数据组数
     */
    @Override
    public int getNumOfData(Integer planCode, String monitorId, List<String> scriptId, String resultType, int time) {
        String prefix = "ad_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<ContrastHarmonicResult> wrapper = this.lambdaQuery()
                .eq(ContrastHarmonicResult::getDevMonitorId, monitorId)
                .in(ContrastHarmonicResult::getAdType, scriptId)
                .eq(ContrastHarmonicResult::getFlag, 0)
                .eq(ContrastHarmonicResult::getNum, time);
        if (isWave) {
            wrapper.eq(ContrastHarmonicResult::getDataType, "wave_data")
                    .eq(ContrastHarmonicResult::getWaveNum, waveTime);
        } else {
            wrapper.eq(ContrastHarmonicResult::getDataType, resultType);
        }
        // 执行查询并统计满足条件的记录数
        int count = wrapper.count();
        DynamicTableNameHandler.remove();
        return count;
    }
}
