package com.njcn.event.file.component;

import cn.hutool.core.img.ImgUtil;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.common.utils.FileUtil;
import com.njcn.echarts.json.LineGenerator;
import com.njcn.echarts.pojo.constant.PicCommonData;
import com.njcn.echarts.util.DrawPicUtil;
import com.njcn.event.file.pojo.bo.WaveDataDetail;
import com.njcn.event.file.pojo.dto.WaveDataDTO;
import com.njcn.event.file.pojo.enums.WaveFileResponseEnum;
import com.njcn.oss.constant.OssPath;
import com.njcn.oss.utils.FileStorageUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import sun.awt.image.BufferedImageGraphicsConfig;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author hongawen
 * @version 1.0.0
 * @date 2023年09月21日 09:18
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class WavePicComponent {

    private final DrawPicUtil drawPicUtil;

    private final FileStorageUtil fileStorageUtil;


    /***
     * 绘制瞬时波形图治理
     * @author hongawen
     * @date 2023/9/21 15:32
     * @return String 文件地址
     */
    public String generateInstantImageZl(Integer nPush, List<WaveDataDetail> waveDataDetails, Double lastTime) {
        String firstPic = null, secondPic = null, thirdPic = null, forthPic = null;
        WaveDataDetail waveDataDetail0 = waveDataDetails.get(0);
        // 从instantData提取null边界x值，作为共享参考（与Shun图保持一致）
        List<Float> sharedNullBoundaries = LineGenerator.extractNullBoundaryXValues(waveDataDetail0.getInstantData().getAValue());
        for (WaveDataDetail waveDataDetail : waveDataDetails) {
            if (waveDataDetail.getChannelName().toUpperCase().startsWith("SU")) {
                firstPic = drawPicUtil.drawWavePic("电压-电网侧", waveDataDetail.getInstantData().getAValue(),
                        waveDataDetail.getInstantData().getBValue(), waveDataDetail.getInstantData().getCValue(),
                        waveDataDetail.getUnit(), waveDataDetail.getInstantData().getMax(), waveDataDetail.getInstantData().getMin(),
                        waveDataDetail.getA(), waveDataDetail.getB(), waveDataDetail.getC(),
                        waveDataDetail.getColors(), waveDataDetail.getIsOpen(),lastTime,sharedNullBoundaries,nPush
                );
            } else if (waveDataDetail.getChannelName().toUpperCase().startsWith("SI")) {
                thirdPic = drawPicUtil.drawWavePic("电流-电网侧", waveDataDetail.getInstantData().getAValue(),
                        waveDataDetail.getInstantData().getBValue(), waveDataDetail.getInstantData().getCValue(),
                        waveDataDetail.getUnit(), waveDataDetail.getInstantData().getMax(), waveDataDetail.getInstantData().getMin(),
                        waveDataDetail.getA(), waveDataDetail.getB(), waveDataDetail.getC(),
                        waveDataDetail.getColors(), waveDataDetail.getIsOpen(),lastTime,sharedNullBoundaries,nPush
                );
            } else if (waveDataDetail.getChannelName().toUpperCase().startsWith("LU")) {
                secondPic = drawPicUtil.drawWavePic("电压-负载侧", waveDataDetail.getInstantData().getAValue(),
                        waveDataDetail.getInstantData().getBValue(), waveDataDetail.getInstantData().getCValue(),
                        waveDataDetail.getUnit(), waveDataDetail.getInstantData().getMax(), waveDataDetail.getInstantData().getMin(),
                        waveDataDetail.getA(), waveDataDetail.getB(), waveDataDetail.getC(),
                        waveDataDetail.getColors(), waveDataDetail.getIsOpen(),lastTime,sharedNullBoundaries,nPush
                );
            } else {
                forthPic = drawPicUtil.drawWavePic("电流-负载侧", waveDataDetail.getInstantData().getAValue(),
                        waveDataDetail.getInstantData().getBValue(), waveDataDetail.getInstantData().getCValue(),
                        waveDataDetail.getUnit(), waveDataDetail.getInstantData().getMax(), waveDataDetail.getInstantData().getMin(),
                        waveDataDetail.getA(), waveDataDetail.getB(), waveDataDetail.getC(),
                        waveDataDetail.getColors(), waveDataDetail.getIsOpen(),lastTime,sharedNullBoundaries,nPush
                );
            }
        }
        return composeImageZl(firstPic, secondPic, thirdPic, forthPic);
    }

    /***
     * 绘制RMS波形图治理
     * @author hongawen
     * @date 2023/9/21 15:32
     * @return String 文件地址
     */
    public String generateRmsImageZl(Integer nPush, List<WaveDataDetail> waveDataDetails, Double lastTime) {
        String firstPic = null, secondPic = null, thirdPic = null, forthPic = null;
        WaveDataDetail waveDataDetail0 = waveDataDetails.get(0);
        // 从instantData提取null边界x值，作为共享参考（与Shun图保持一致）
        List<Float> sharedNullBoundaries = LineGenerator.extractNullBoundaryXValues(waveDataDetail0.getInstantData().getAValue());
        for (WaveDataDetail waveDataDetail : waveDataDetails) {
            if (waveDataDetail.getChannelName().toUpperCase().startsWith("SU")) {
                firstPic = drawPicUtil.drawWavePic("电压-电网侧", waveDataDetail.getRmsData().getAValue(),
                        waveDataDetail.getRmsData().getBValue(), waveDataDetail.getRmsData().getCValue(),
                        waveDataDetail.getUnit(), waveDataDetail.getRmsData().getMax(), waveDataDetail.getRmsData().getMin(),
                        waveDataDetail.getA(), waveDataDetail.getB(), waveDataDetail.getC(),
                        waveDataDetail.getColors(), waveDataDetail.getIsOpen(),lastTime,sharedNullBoundaries,nPush
                );
            } else if (waveDataDetail.getChannelName().toUpperCase().startsWith("SI")) {
                thirdPic = drawPicUtil.drawWavePic("电流-电网侧", waveDataDetail.getRmsData().getAValue(),
                        waveDataDetail.getRmsData().getBValue(), waveDataDetail.getRmsData().getCValue(),
                        waveDataDetail.getUnit(), waveDataDetail.getRmsData().getMax(), waveDataDetail.getRmsData().getMin(),
                        waveDataDetail.getA(), waveDataDetail.getB(), waveDataDetail.getC(),
                        waveDataDetail.getColors(), waveDataDetail.getIsOpen(),lastTime,sharedNullBoundaries,nPush
                );
            } else if (waveDataDetail.getChannelName().toUpperCase().startsWith("LU")) {
                secondPic = drawPicUtil.drawWavePic("电压-负载侧", waveDataDetail.getRmsData().getAValue(),
                        waveDataDetail.getRmsData().getBValue(), waveDataDetail.getRmsData().getCValue(),
                        waveDataDetail.getUnit(), waveDataDetail.getRmsData().getMax(), waveDataDetail.getRmsData().getMin(),
                        waveDataDetail.getA(), waveDataDetail.getB(), waveDataDetail.getC(),
                        waveDataDetail.getColors(), waveDataDetail.getIsOpen(),lastTime,sharedNullBoundaries,nPush
                );
            } else {
                forthPic = drawPicUtil.drawWavePic("电流-负载侧", waveDataDetail.getRmsData().getAValue(),
                        waveDataDetail.getRmsData().getBValue(), waveDataDetail.getRmsData().getCValue(),
                        waveDataDetail.getUnit(), waveDataDetail.getRmsData().getMax(), waveDataDetail.getRmsData().getMin(),
                        waveDataDetail.getA(), waveDataDetail.getB(), waveDataDetail.getC(),
                        waveDataDetail.getColors(), waveDataDetail.getIsOpen(),lastTime,sharedNullBoundaries,nPush
                );
            }
        }
        return composeImageZl(firstPic, secondPic, thirdPic, forthPic);
    }

    /***
     * 合并4个原图
     * @author hongawen
     * @date 2023/9/28 14:45
     * @return String
     */
    private String composeImageZl(String firstPic, String secondPic, String thirdPic, String forthPic) {
        try {
            //第一张图片 base64截取删除前缀data:image/png;base64,
            BufferedImage imageOne = ImgUtil.toBufferedImage(ImgUtil.toImage(firstPic.substring(22)), ImgUtil.IMAGE_TYPE_PNG);
            int width = imageOne.getWidth();
            int height = imageOne.getHeight();
            int[] imageArrayOne = new int[width * height];
            imageArrayOne = imageOne.getRGB(0, 0, width, height, imageArrayOne, 0, width);
            //第二张图片 base64截取删除前缀data:image/png;base64,
            BufferedImage imageTwo = ImgUtil.toBufferedImage(ImgUtil.toImage(secondPic.substring(22)), ImgUtil.IMAGE_TYPE_PNG);
            int[] ImageArrayTwo = new int[width * height];
            ImageArrayTwo = imageTwo.getRGB(0, 0, width, height, ImageArrayTwo, 0, width);
            //第三张图片 base64截取删除前缀data:image/png;base64,
            BufferedImage imageThree = ImgUtil.toBufferedImage(ImgUtil.toImage(thirdPic.substring(22)), ImgUtil.IMAGE_TYPE_PNG);
            int[] ImageArrayThree = new int[width * height];
            ImageArrayThree = imageThree.getRGB(0, 0, width, height, ImageArrayThree, 0, width);
            //第四张图片 base64截取删除前缀data:image/png;base64,
            BufferedImage imageForth = ImgUtil.toBufferedImage(ImgUtil.toImage(forthPic.substring(22)), ImgUtil.IMAGE_TYPE_PNG);
            int[] ImageArrayForth = new int[width * height];
            ImageArrayForth = imageForth.getRGB(0, 0, width, height, ImageArrayForth, 0, width);
            //新图片
            BufferedImage imageNew = new BufferedImage(width, height * 4, BufferedImage.TYPE_INT_RGB);
            BufferedImageGraphicsConfig config = BufferedImageGraphicsConfig.getConfig(imageNew);
            imageNew = config.createCompatibleImage(width, height * 4, Transparency.TRANSLUCENT);
            imageNew.setRGB(0, 0, width, height, imageArrayOne, 0, width);
            imageNew.setRGB(0, height, width, height, ImageArrayTwo, 0, width);
            imageNew.setRGB(0, height * 2, width, height, ImageArrayThree, 0, width);
            imageNew.setRGB(0, height * 3, width, height, ImageArrayForth, 0, width);
            String resultImg = ImgUtil.toBase64(imageNew, ImgUtil.IMAGE_TYPE_PNG);
            byte[] bytes = Base64.getDecoder().decode(resultImg);
            return fileStorageUtil.uploadStream(new ByteArrayInputStream(bytes), OssPath.EVENT_WAVE_PIC, FileUtil.generateFileName("png"));
        } catch (Exception e) {
            throw new BusinessException(WaveFileResponseEnum.COMPOSE_PIC_ERROR);
        }
    }


    /***
     * 绘制瞬时波形图
     * @author hongawen
     * @date 2023/9/21 15:32
     * @return String 文件地址
     */
    public String generateImageShun(WaveDataDTO waveDataDTO, List<WaveDataDetail> waveDataDetails) {
        String picPath = null;
        String time = waveDataDTO.getTime();
        String title = "监测点名称：" + waveDataDTO.getMonitorName() + " 发生时刻：" + time;
        WaveDataDetail waveDataDetail = waveDataDetails.get(0);
        String firstPic = drawPicUtil.drawWavePic(title, waveDataDetail.getInstantData().getAValue(),
                waveDataDetail.getInstantData().getBValue(), waveDataDetail.getInstantData().getCValue(),
                waveDataDetail.getUnit(), waveDataDetail.getInstantData().getMax(), waveDataDetail.getInstantData().getMin(),
                waveDataDetail.getA(), waveDataDetail.getB(), waveDataDetail.getC(),
                waveDataDetail.getColors(), waveDataDetail.getIsOpen()
        );
        String secondPic;
        if (waveDataDetails.size() == 1) {
            //将图片上传到minioss
            if (firstPic.contains(PicCommonData.PNG_PREFIX)) {
                firstPic = firstPic.replace(PicCommonData.PNG_PREFIX, "");
            }
            byte[] bytes = Base64.getDecoder().decode(firstPic);
            picPath = fileStorageUtil.uploadStream(new ByteArrayInputStream(bytes), OssPath.EVENT_WAVE_PIC, FileUtil.generateFileName("png"));
        } else if (waveDataDetails.size() == 2) {
            //绘制第二个电压瞬时波形图
            waveDataDetail = waveDataDetails.get(1);
            secondPic = drawPicUtil.drawWavePic("", waveDataDetail.getInstantData().getAValue(),
                    waveDataDetail.getInstantData().getBValue(), waveDataDetail.getInstantData().getCValue(),
                    waveDataDetail.getUnit(), waveDataDetail.getInstantData().getMax(), waveDataDetail.getInstantData().getMin(),
                    waveDataDetail.getA(), waveDataDetail.getB(), waveDataDetail.getC(),
                    waveDataDetail.getColors(), waveDataDetail.getIsOpen()
            );
            picPath = composeImage(firstPic, secondPic);
        } else if (waveDataDetails.size() == 4) {
            WaveDataDetail waveDataDetail1 = waveDataDetails.get(0);
            String pic1 = drawPicUtil.drawWavePic(getTitle2(waveDataDetail1.getChannelName(),time), waveDataDetail1.getInstantData().getAValue(),
                    waveDataDetail1.getInstantData().getBValue(), waveDataDetail1.getInstantData().getCValue(),
                    waveDataDetail1.getUnit(), waveDataDetail1.getInstantData().getMax(), waveDataDetail1.getInstantData().getMin(),
                    waveDataDetail1.getA(), waveDataDetail1.getB(), waveDataDetail1.getC(),
                    waveDataDetail1.getColors(), waveDataDetail1.getIsOpen()
            );

            WaveDataDetail waveDataDetail2 = waveDataDetails.get(1);
            String pic2 = drawPicUtil.drawWavePic(getTitle2(waveDataDetail2.getChannelName(),time), waveDataDetail2.getInstantData().getAValue(),
                    waveDataDetail2.getInstantData().getBValue(), waveDataDetail2.getInstantData().getCValue(),
                    waveDataDetail2.getUnit(), waveDataDetail2.getInstantData().getMax(), waveDataDetail2.getInstantData().getMin(),
                    waveDataDetail2.getA(), waveDataDetail2.getB(), waveDataDetail2.getC(),
                    waveDataDetail2.getColors(), waveDataDetail2.getIsOpen());

            WaveDataDetail waveDataDetail3 = waveDataDetails.get(2);
            String pic3 = drawPicUtil.drawWavePic(getTitle2(waveDataDetail3.getChannelName(),time), waveDataDetail3.getInstantData().getAValue(),
                    waveDataDetail3.getInstantData().getBValue(), waveDataDetail3.getInstantData().getCValue(),
                    waveDataDetail3.getUnit(), waveDataDetail3.getInstantData().getMax(), waveDataDetail3.getInstantData().getMin(),
                    waveDataDetail3.getA(), waveDataDetail3.getB(), waveDataDetail3.getC(),
                    waveDataDetail3.getColors(), waveDataDetail3.getIsOpen());

            WaveDataDetail waveDataDetail4 = waveDataDetails.get(3);
            String pic4 = drawPicUtil.drawWavePic(getTitle2(waveDataDetail4.getChannelName(),time), waveDataDetail4.getInstantData().getAValue(),
                    waveDataDetail4.getInstantData().getBValue(), waveDataDetail4.getInstantData().getCValue(),
                    waveDataDetail4.getUnit(), waveDataDetail4.getInstantData().getMax(), waveDataDetail4.getInstantData().getMin(),
                    waveDataDetail4.getA(), waveDataDetail4.getB(), waveDataDetail4.getC(),
                    waveDataDetail4.getColors(), waveDataDetail4.getIsOpen()
            );
            picPath = composeImageZl(pic1, pic2, pic3, pic4);
        }
        return picPath;
    }

    /***
     * 绘制瞬时波形图 App端用来绘制图片，会去掉部分数据，只保留开始数据 结束数据
     * @author xy
     * @return String 文件地址
     */
    public String generateImageShun(Integer nPush, List<WaveDataDetail> waveDataDetails, Double lastTime) {
        String picPath = null;
        WaveDataDetail waveDataDetail = waveDataDetails.get(0);
        // 从instantData提取null边界x值，作为共享参考
        List<Float> sharedNullBoundaries = LineGenerator.extractNullBoundaryXValues(waveDataDetail.getInstantData().getAValue());

        String firstPic = drawPicUtil.drawWavePic(getTitle(waveDataDetail.getChannelName()), waveDataDetail.getInstantData().getAValue(),
                waveDataDetail.getInstantData().getBValue(), waveDataDetail.getInstantData().getCValue(),
                waveDataDetail.getUnit(), waveDataDetail.getInstantData().getMax(), waveDataDetail.getInstantData().getMin(),
                waveDataDetail.getA(), waveDataDetail.getB(), waveDataDetail.getC(),
                waveDataDetail.getColors(), waveDataDetail.getIsOpen(), lastTime, sharedNullBoundaries,nPush
        );
        String secondPic;
        if (waveDataDetails.size() == 1) {
            if (firstPic.contains(PicCommonData.PNG_PREFIX)) {
                firstPic = firstPic.replace(PicCommonData.PNG_PREFIX, "");
            }
            byte[] bytes = Base64.getDecoder().decode(firstPic);
            picPath = fileStorageUtil.uploadStream(new ByteArrayInputStream(bytes), OssPath.EVENT_WAVE_PIC, FileUtil.generateFileName("png"));
        } else if (waveDataDetails.size() == 2) {
            waveDataDetail = waveDataDetails.get(1);
            secondPic = drawPicUtil.drawWavePic(getTitle(waveDataDetail.getChannelName()), waveDataDetail.getInstantData().getAValue(),
                    waveDataDetail.getInstantData().getBValue(), waveDataDetail.getInstantData().getCValue(),
                    waveDataDetail.getUnit(), waveDataDetail.getInstantData().getMax(), waveDataDetail.getInstantData().getMin(),
                    waveDataDetail.getA(), waveDataDetail.getB(), waveDataDetail.getC(),
                    waveDataDetail.getColors(), waveDataDetail.getIsOpen(), lastTime, sharedNullBoundaries,nPush
            );
            picPath = composeImage(firstPic, secondPic);
        }
        return picPath;
    }

    public String getTitle(String channelName) {
        return channelName.toUpperCase().startsWith("U1") ? "电压" : "电流";
    }

    /***
     * 绘制RMS波形图
     * @author hongawen
     * @date 2023/9/21 15:32
     * @return String 文件地址
     */
    public String generateImageRms(WaveDataDTO waveDataDTO, List<WaveDataDetail> waveDataDetails) {
        String picPath = null;
        String time = waveDataDTO.getTime();
        String title = "监测点名称：" + waveDataDTO.getMonitorName() + " 发生时刻：" + time;
        WaveDataDetail waveDataDetail = waveDataDetails.get(0);
        String firstPic = drawPicUtil.drawWavePic(title, waveDataDetail.getRmsData().getAValue(),
                waveDataDetail.getRmsData().getBValue(), waveDataDetail.getRmsData().getCValue(),
                waveDataDetail.getUnit(), waveDataDetail.getRmsData().getMax(), waveDataDetail.getRmsData().getMin(),
                waveDataDetail.getA(), waveDataDetail.getB(), waveDataDetail.getC(),
                waveDataDetail.getColors(), waveDataDetail.getIsOpen()
        );
        String secondPic;
        if (waveDataDetails.size() == 1) {
            //将图片上传到minioss
            if (firstPic.contains(PicCommonData.PNG_PREFIX)) {
                firstPic = firstPic.replace(PicCommonData.PNG_PREFIX, "");
            }
            byte[] bytes = Base64.getDecoder().decode(firstPic);
            picPath = fileStorageUtil.uploadStream(new ByteArrayInputStream(bytes), OssPath.EVENT_WAVE_PIC, FileUtil.generateFileName("png"));
        } else if (waveDataDetails.size() == 2) {
            //绘制第二个电压瞬时波形图
            waveDataDetail = waveDataDetails.get(1);
            secondPic = drawPicUtil.drawWavePic("", waveDataDetail.getRmsData().getAValue(),
                    waveDataDetail.getRmsData().getBValue(), waveDataDetail.getRmsData().getCValue(),
                    waveDataDetail.getUnit(), waveDataDetail.getRmsData().getMax(), waveDataDetail.getRmsData().getMin(),
                    waveDataDetail.getA(), waveDataDetail.getB(), waveDataDetail.getC(),
                    waveDataDetail.getColors(), waveDataDetail.getIsOpen()
            );
            picPath = composeImage(firstPic, secondPic);
        } else if (waveDataDetails.size() == 4) {
            WaveDataDetail waveDataDetail1 = waveDataDetails.get(0);
            String pic1 = drawPicUtil.drawWavePic(getTitle2(waveDataDetail1.getChannelName(),time), waveDataDetail1.getRmsData().getAValue(),
                    waveDataDetail1.getRmsData().getBValue(), waveDataDetail1.getRmsData().getCValue(),
                    waveDataDetail1.getUnit(), waveDataDetail1.getRmsData().getMax(), waveDataDetail1.getRmsData().getMin(),
                    waveDataDetail1.getA(), waveDataDetail1.getB(), waveDataDetail1.getC(),
                    waveDataDetail1.getColors(), waveDataDetail1.getIsOpen()
            );

            WaveDataDetail waveDataDetail2 = waveDataDetails.get(1);
            String pic2 = drawPicUtil.drawWavePic(getTitle2(waveDataDetail2.getChannelName(),time), waveDataDetail2.getRmsData().getAValue(),
                    waveDataDetail2.getRmsData().getBValue(), waveDataDetail2.getRmsData().getCValue(),
                    waveDataDetail2.getUnit(), waveDataDetail2.getRmsData().getMax(), waveDataDetail2.getRmsData().getMin(),
                    waveDataDetail2.getA(), waveDataDetail2.getB(), waveDataDetail2.getC(),
                    waveDataDetail2.getColors(), waveDataDetail2.getIsOpen());

            WaveDataDetail waveDataDetail3 = waveDataDetails.get(2);
            String pic3 = drawPicUtil.drawWavePic(getTitle2(waveDataDetail3.getChannelName(),time), waveDataDetail3.getRmsData().getAValue(),
                    waveDataDetail3.getRmsData().getBValue(), waveDataDetail3.getRmsData().getCValue(),
                    waveDataDetail3.getUnit(), waveDataDetail3.getRmsData().getMax(), waveDataDetail3.getRmsData().getMin(),
                    waveDataDetail3.getA(), waveDataDetail3.getB(), waveDataDetail3.getC(),
                    waveDataDetail3.getColors(), waveDataDetail3.getIsOpen());

            WaveDataDetail waveDataDetail4 = waveDataDetails.get(3);
            String pic4 = drawPicUtil.drawWavePic(getTitle2(waveDataDetail4.getChannelName(),time), waveDataDetail4.getRmsData().getAValue(),
                    waveDataDetail4.getRmsData().getBValue(), waveDataDetail4.getRmsData().getCValue(),
                    waveDataDetail4.getUnit(), waveDataDetail4.getRmsData().getMax(), waveDataDetail4.getRmsData().getMin(),
                    waveDataDetail4.getA(), waveDataDetail4.getB(), waveDataDetail4.getC(),
                    waveDataDetail4.getColors(), waveDataDetail4.getIsOpen()
            );
            picPath = composeImageZl(pic1, pic2, pic3, pic4);
        }
        return picPath;
    }

    public String getTitle2(String channelName, String time) {
        String title;
        if (channelName.toUpperCase().startsWith("SU")) {
            title ="电压-电网侧" + " 发生时刻：" + time;
        } else if (channelName.toUpperCase().startsWith("SI")) {
            title ="电流-电网侧" + " 发生时刻：" + time;
        } else if (channelName.toUpperCase().startsWith("LU")) {
            title ="电压-负载侧" + " 发生时刻：" + time;
        } else {
            title ="电流-负载侧" + " 发生时刻：" + time;
        }
        return title;
    }

    /***
     * 绘制RMS波形图 App端用来绘制图片，会去掉部分数据，只保留开始数据 结束数据
     * @author xy
     * @return String 文件地址
     */
    public String generateImageRms(Integer nPush, List<WaveDataDetail> waveDataDetails, Double lastTime) {
        String picPath = null;
        WaveDataDetail waveDataDetail = waveDataDetails.get(0);

        // 从instantData提取null边界x值，作为共享参考（与Shun图保持一致）
        List<Float> sharedNullBoundaries = LineGenerator.extractNullBoundaryXValues(waveDataDetail.getInstantData().getAValue());

        String firstPic = drawPicUtil.drawWavePic(getTitle(waveDataDetail.getChannelName()), waveDataDetail.getRmsData().getAValue(),
                waveDataDetail.getRmsData().getBValue(), waveDataDetail.getRmsData().getCValue(),
                waveDataDetail.getUnit(), waveDataDetail.getRmsData().getMax(), waveDataDetail.getRmsData().getMin(),
                waveDataDetail.getA(), waveDataDetail.getB(), waveDataDetail.getC(),
                waveDataDetail.getColors(), waveDataDetail.getIsOpen(), lastTime, sharedNullBoundaries,nPush
        );
        String secondPic;
        if (waveDataDetails.size() == 1) {
            if (firstPic.contains(PicCommonData.PNG_PREFIX)) {
                firstPic = firstPic.replace(PicCommonData.PNG_PREFIX, "");
            }
            byte[] bytes = Base64.getDecoder().decode(firstPic);
            picPath = fileStorageUtil.uploadStream(new ByteArrayInputStream(bytes), OssPath.EVENT_WAVE_PIC, FileUtil.generateFileName("png"));
        } else if (waveDataDetails.size() == 2) {
            waveDataDetail = waveDataDetails.get(1);
            secondPic = drawPicUtil.drawWavePic(getTitle(waveDataDetail.getChannelName()), waveDataDetail.getRmsData().getAValue(),
                    waveDataDetail.getRmsData().getBValue(), waveDataDetail.getRmsData().getCValue(),
                    waveDataDetail.getUnit(), waveDataDetail.getRmsData().getMax(), waveDataDetail.getRmsData().getMin(),
                    waveDataDetail.getA(), waveDataDetail.getB(), waveDataDetail.getC(),
                    waveDataDetail.getColors(), waveDataDetail.getIsOpen(), lastTime, sharedNullBoundaries,nPush
            );
            picPath = composeImage(firstPic, secondPic);
        }
        return picPath;
    }

    /***
     * 合成图片
     * @author hongawen
     * @date 2023/9/21 15:33
     * @param firstPic 第一张图
     * @param secondPic 第二张图
     * @return String
     */
    private String composeImage(String firstPic, String secondPic) {
        try {
            //第一张图片 base64截取删除前缀data:image/png;base64,
            BufferedImage imageOne = ImgUtil.toBufferedImage(ImgUtil.toImage(firstPic.substring(22)), ImgUtil.IMAGE_TYPE_PNG);
            int width = imageOne.getWidth();
            int height = imageOne.getHeight();
            int[] imageArrayOne = new int[width * height];
            imageArrayOne = imageOne.getRGB(0, 0, width, height, imageArrayOne, 0, width);
            //第二张图片 base64截取删除前缀data:image/png;base64,
            BufferedImage imageTwo = ImgUtil.toBufferedImage(ImgUtil.toImage(secondPic.substring(22)), ImgUtil.IMAGE_TYPE_PNG);
            int width2 = imageTwo.getWidth();
            int height2 = imageTwo.getHeight();
            int[] ImageArrayTwo = new int[width2 * height2];
            ImageArrayTwo = imageTwo.getRGB(0, 0, width, height, ImageArrayTwo, 0, width);
            //新图片
            BufferedImage imageNew = new BufferedImage(width, height * 2, BufferedImage.TYPE_INT_RGB);
            BufferedImageGraphicsConfig config = BufferedImageGraphicsConfig.getConfig(imageNew);
            imageNew = config.createCompatibleImage(width, height * 2, Transparency.TRANSLUCENT);
            imageNew.setRGB(0, 0, width, height, imageArrayOne, 0, width);
            imageNew.setRGB(0, height, width, height, ImageArrayTwo, 0, width);
            String resultImg = ImgUtil.toBase64(imageNew, ImgUtil.IMAGE_TYPE_PNG);
            byte[] bytes = Base64.getDecoder().decode(resultImg);
            return fileStorageUtil.uploadStream(new ByteArrayInputStream(bytes), OssPath.EVENT_WAVE_PIC, FileUtil.generateFileName("png"));
        } catch (Exception e) {
            throw new BusinessException(WaveFileResponseEnum.COMPOSE_PIC_ERROR);
        }
    }

    private String composeImage(String firstPic, String secondPic, String thirdPic, String forthPic) {
        try {
            //第一张图片 base64截取删除前缀data:image/png;base64,
            BufferedImage imageOne = ImgUtil.toBufferedImage(ImgUtil.toImage(firstPic.substring(22)), ImgUtil.IMAGE_TYPE_PNG);
            int width = imageOne.getWidth();
            int height = imageOne.getHeight();
            int[] imageArrayOne = new int[width * height];
            imageArrayOne = imageOne.getRGB(0, 0, width, height, imageArrayOne, 0, width);
            //第二张图片 base64截取删除前缀data:image/png;base64,
            BufferedImage imageTwo = ImgUtil.toBufferedImage(ImgUtil.toImage(secondPic.substring(22)), ImgUtil.IMAGE_TYPE_PNG);
            int width2 = imageTwo.getWidth();
            int height2 = imageTwo.getHeight();
            int[] ImageArrayTwo = new int[width2 * height2];
            ImageArrayTwo = imageTwo.getRGB(0, 0, width, height, ImageArrayTwo, 0, width);

            int[] ImageArrayThree = null;
            if (Objects.equals(thirdPic, "")) {
                BufferedImage imageThree = ImgUtil.toBufferedImage(ImgUtil.toImage(thirdPic.substring(22)), ImgUtil.IMAGE_TYPE_PNG);
                int width3 = imageThree.getWidth();
                int height3 = imageThree.getHeight();
                ImageArrayThree = new int[width3 * height3];
                ImageArrayThree = imageThree.getRGB(0, 0, width, height, ImageArrayThree, 0, width);
            }

            int[] ImageArrayForth = null;
            if (Objects.equals(forthPic, "")) {
                BufferedImage imageForth = ImgUtil.toBufferedImage(ImgUtil.toImage(forthPic.substring(22)), ImgUtil.IMAGE_TYPE_PNG);
                int width4 = imageForth.getWidth();
                int height4 = imageForth.getHeight();
                ImageArrayForth = new int[width4 * height4];
                ImageArrayForth = imageForth.getRGB(0, 0, width, height, ImageArrayForth, 0, width);
            }

            //新图片
            BufferedImage imageNew = new BufferedImage(width, height * 2, BufferedImage.TYPE_INT_RGB);
            BufferedImageGraphicsConfig config = BufferedImageGraphicsConfig.getConfig(imageNew);
            imageNew = config.createCompatibleImage(width, height * 2, Transparency.TRANSLUCENT);
            imageNew.setRGB(0, 0, width, height, imageArrayOne, 0, width);
            imageNew.setRGB(0, height, width, height, ImageArrayTwo, 0, width);
            if (!Objects.isNull(ImageArrayThree)) {
                imageNew.setRGB(0, height, width, height, ImageArrayThree, 0, width);
            }
            if (!Objects.isNull(ImageArrayForth)) {
                imageNew.setRGB(0, height, width, height, ImageArrayForth, 0, width);
            }
            String resultImg = ImgUtil.toBase64(imageNew, ImgUtil.IMAGE_TYPE_PNG);
            byte[] bytes = Base64.getDecoder().decode(resultImg);
            return fileStorageUtil.uploadStream(new ByteArrayInputStream(bytes), OssPath.EVENT_WAVE_PIC, FileUtil.generateFileName("png"));
        } catch (Exception e) {
            throw new BusinessException(WaveFileResponseEnum.COMPOSE_PIC_ERROR);
        }
    }

    /**
     * app越限指标图
     * @param title
     * @param values
     * @param limitName
     * @param limit
     * @return
     */
    public String generateSteadyTargetImage(String title, Map<String ,List<List<Object>>> values, String limitName, List<List<Object>> limit) {
        String firstPic = drawPicUtil.drawSteadyTargetPic(title,values,limitName,limit);
        if (firstPic.contains(PicCommonData.PNG_PREFIX)) {
            firstPic = firstPic.replace(PicCommonData.PNG_PREFIX, "");
        }
        byte[] bytes = Base64.getDecoder().decode(firstPic);
        return fileStorageUtil.uploadStream(new ByteArrayInputStream(bytes), OssPath.EVENT_WAVE_PIC, FileUtil.generateFileName("png"));
    }
}
