package com.yeechart.dotMatrix.signage;

import com.yeechart.dotMatrix.canvas.DotMatrixCanvasUtil;
import com.yeechart.dotMatrix.canvas.DotMatrixDrawShapeUtil;
import com.yeechart.dotMatrix.canvas.DotMatrixImageUtil;
import com.yeechart.dotMatrix.oss.LatticeOSSUpload;
import com.yeechart.dotMatrix.signage.bean.Ter020201InfoVo;
import com.yeechart.dotMatrix.signage.bean.Ter020201Vo;
import com.yeechart.dotMatrix.signage.bean.mizilzo.BitmapDataVo;
import com.yeechart.dotMatrix.util.OtherUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
import java.util.stream.Stream;



/**
 * 绘制标识牌
 */

@Component
public class SignageDrawUtil {


    @javax.annotation.Resource
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;
    @Autowired
    private LatticeOSSUpload latticeOSSUpload;
    
    @Autowired
    private SignageFileUtil signageFileUtil;
    
    @Autowired
    private SignageDrawLeftUtil signageDrawLeftUtil;

    @Autowired
    private SignageDrawRightUtil signingDrawRightUtil;

    
    /**
     * 屏幕宽度
     **/
    public static int width = 480;
    /**
     * 屏幕高度
     **/
    public static int height = 648;
    /**
     * 左图边距
     **/
    public static int leftMargins = width / 20;


    /************************************** 将屏幕上面的功能点分成独立的个体 最后在做组合 以提高性能  **************************************/
    /**
     * 右图边距
     **/
    public static int rightMargins = 16;/// width/24 = 20
    /**
     * 普通文字 大小
     **/
    public static int textFontSize = 24;//宽
    public static int textFontSizeHeight = 24;//高
    /**
     * 非中文的文字大小 一般指数字英文之类的
     **/
    public static int noChineseFontSize = 8;//宽
    public static int noChineseFontSizeHeight = 16;//高
    /**
     * 普通文字 大小 第二种
     **/
    public static int textFontSize2 = 16;//宽
    public static int textFontSize2Height = 16;//高
    /**
     * 非中文的文字大小 一般指数字英文之类的 第二种
     **/
    public static int noChineseFontSize2 = 16;//宽 8 *2
    public static int noChineseFontSize2Height = 32;//高  16*2
    /**
     * 非中文的文字大小 一般指数字英文之类的 第三种
     **/
    public static int noChineseFontSize3 = 12;//宽 12
    public static int noChineseFontSize3Height = 24;//高  24
    /**
     * 普通文字 被选中的 大小
     **/
    public static int textFontSizeCheck = 32;
    public static int textFontSizeCheckHeight = 32;//高
    /**
     * 非中文的文字大小  被选中的 大小 一般指数字英文之类的
     **/
    public static int noChineseFontSizeCheck = 16;//6*3
    public static int noChineseFontSizeCheckHeight = 32;//12*3
    /**
     * 普通文字 被选中的 居中显示的 大小
     **/
    public static int textFontSizeCenterCheck = 64;//32*2
    public static int textFontSizeCenterCheckHeight = 64;//32*2 //高
    /**
     * 非中文的文字大小  被选中的 居中显示的 大小  一般指数字英文之类的
     **/
    public static int noChineseFontCenterCheck = 32;//8*4
    public static int noChineseFontCenterCheckHeight = 64;//16*4
    /**
     * 非中文的文字大小  被选中的 居中显示的 大小  一般指数字英文之类的
     **/
    public static int noChineseFontCenterCheck2 = 24;//6*4
    public static int noChineseFontCenterCheck2Height = 48;//12*4
    /**
     * 左屏 状态支持的文字数量 用于清除旧数据痕迹等
     **/
    public static int leftStateMax = 6;
    /**
     * 左屏 预留给终端上锁使用的位置
     **/
    public static int LockSize = 24;
    /**
     * 柱状图的宽度
     **/
    public static int barChartWidth = 25;
    /**
     * 柱状图间隔
     **/
    public static int barChartInterval = 15;
    /**
     * 柱状图高度
     **/
    public static int barChartHigh = 140;
    /**
     * 柱状图上 数字大小
     **/
    public static int numberSize = 5;
    /**
     * 柱状图上 数字高度
     **/
    public static int numberHigh = 7;

    /**
     * 获取点阵图
     * @param ter020201Vo
     * @return
     */
    public List<BitmapDataVo> getSignageDotMatrix(Ter020201Vo ter020201Vo) {
        if (ter020201Vo == null || ter020201Vo.getTer020201InfoVo() == null ||
                ter020201Vo.getType() == null || ter020201Vo.getType().length == 0) {
            return null;
        }
        List<Integer> type = Stream.of(ter020201Vo.getType()).collect(Collectors.toList());
        ;

        Set<String> typeCodeList = TypeCodeUtil.changTypeToTypeCode(type);

//        Ter020201InfoVo ter020201InfoVo = ter020201Vo.getTer020201InfoVo();

//        List<BitmapDataVo> bitmapDataVoList  = new ArrayList<>();

        //获取锁
//        String dId = ter020201InfoVo.getDeId();
//
//        RLock lock = redissonClient.getLock("getBatchBitmapArraysWithTypeCode:" + dId);
//
//        try {
//            while (!lock.tryLock(0, 5, TimeUnit.SECONDS)) {
////                Thread.sleep(2000);//此处1S 是因为点阵图更新后上传OSS 虽然正确的上传了 但是由于OSS的机制问题，再去获取的时候会得到旧的图片，导致基于旧图进行了修改，引起bug
//                LockSupport.parkNanos(2_000_000_000);
//                log.debug(" [未获取到] 锁" + "getBatchBitmapArraysWithTypeCode:" + dId );
//                //未获取到锁 则进入合并任务
//            }
//
//            log.debug(" [获取到了] 锁" + "getBatchBitmapArraysWithTypeCode:" + dId );

        //点阵图
        CompletableFuture<List<BitmapDataVo>> completableFuture = getBatchBitmapArraysWithTypeCode(ter020201Vo, typeCodeList);
        //List<BitmapDataVo> bitmapDataVoList  = getBatchBitmapArraysWithTypeCode(ter020201Vo,typeCodeList);

        return completableFuture.join();

//        }catch (Exception e){
//            e.printStackTrace();
//            log.error("addDeviceType error:{}",e.getMessage());
//        }finally {
//            try {
//                if (lock.isLocked() && lock.isHeldByCurrentThread()) {
//                    log.info("释放 点阵图生成锁 ");
//                    lock.unlock();
//                }
//            }catch (Exception e){
//                log.error("释放锁 "+ e);
//            }
//
//        }


    }



    /**
     * 生成点阵图
     *
     * @param ter020201Vo
     * @param typeCodeList
     * @return
     */
    private CompletableFuture<List<BitmapDataVo>> getBatchBitmapArraysWithTypeCode(Ter020201Vo ter020201Vo, Set<String> typeCodeList) {

        CompletableFuture<Void> uploadFuture = CompletableFuture.completedFuture(null);

        List<BitmapDataVo> bitmapDataVoList = new ArrayList<>();

        if (typeCodeList == null || typeCodeList.isEmpty()) {
            return CompletableFuture.completedFuture(null);
        }


        Ter020201InfoVo ter020201InfoVo = ter020201Vo.getTer020201InfoVo();

        List<Integer> typeList = Arrays.asList(ter020201Vo.getType());


        int[][] canvasBitmapArrays = new int[height][width];

        byte[] twoBytes = null;
        String productId = ter020201InfoVo.getProductId();
        String dId = ter020201InfoVo.getDeId();
        if (ter020201Vo.getTer020201InfoVo().getIsDefault()) {
            dId = "default";
        }


//        String path = SignageFileUtil.fileName + dId + "/";//路径;
        String sessionId = System.currentTimeMillis() + "";
        String path = SignageFileUtil.fileName + dId + "/" + sessionId + "/";//路径;


        List<String> leftGuideList = new ArrayList<>();
        List<String> rightGuideList = new ArrayList<>();

        List<String> leftBusinessList = new ArrayList<>();
        List<String> rightBusinessList = new ArrayList<>();

        if (ter020201Vo.getTer020201InfoVo().getIsDefault()) {

            leftGuideList.add(TypeCodeUtil.CODE_OFF + TypeCodeUtil.CODE_LEFT + TypeCodeUtil.CODE_GUIDE);

            //图表0的指针图
            leftBusinessList.add(TypeCodeUtil.CODE_ON + TypeCodeUtil.CODE_LEFT + TypeCodeUtil.CODE_BUSINESS + "0000");
            leftBusinessList.add(TypeCodeUtil.CODE_ON + TypeCodeUtil.CODE_LEFT + TypeCodeUtil.CODE_BUSINESS + "0001");
            leftBusinessList.add(TypeCodeUtil.CODE_ON + TypeCodeUtil.CODE_LEFT + TypeCodeUtil.CODE_BUSINESS + "0002");
            leftBusinessList.add(TypeCodeUtil.CODE_ON + TypeCodeUtil.CODE_LEFT + TypeCodeUtil.CODE_BUSINESS + "0003");
            leftBusinessList.add(TypeCodeUtil.CODE_ON + TypeCodeUtil.CODE_LEFT + TypeCodeUtil.CODE_BUSINESS + "0004");
            leftBusinessList.add(TypeCodeUtil.CODE_ON + TypeCodeUtil.CODE_LEFT + TypeCodeUtil.CODE_BUSINESS + "0005");

            //仅状态图
            leftBusinessList.add(TypeCodeUtil.CODE_ON + TypeCodeUtil.CODE_LEFT + TypeCodeUtil.CODE_BUSINESS + TypeCodeUtil.CODE_FF + "00");
            leftBusinessList.add(TypeCodeUtil.CODE_ON + TypeCodeUtil.CODE_LEFT + TypeCodeUtil.CODE_BUSINESS + TypeCodeUtil.CODE_FF + "01");
            leftBusinessList.add(TypeCodeUtil.CODE_ON + TypeCodeUtil.CODE_LEFT + TypeCodeUtil.CODE_BUSINESS + TypeCodeUtil.CODE_FF + "02");
            leftBusinessList.add(TypeCodeUtil.CODE_ON + TypeCodeUtil.CODE_LEFT + TypeCodeUtil.CODE_BUSINESS + TypeCodeUtil.CODE_FF + "03");
            leftBusinessList.add(TypeCodeUtil.CODE_ON + TypeCodeUtil.CODE_LEFT + TypeCodeUtil.CODE_BUSINESS + TypeCodeUtil.CODE_FF + "04");
            leftBusinessList.add(TypeCodeUtil.CODE_ON + TypeCodeUtil.CODE_LEFT + TypeCodeUtil.CODE_BUSINESS + TypeCodeUtil.CODE_FF + "05");


            rightGuideList.add(TypeCodeUtil.CODE_OFF + TypeCodeUtil.CODE_RIGHT + TypeCodeUtil.CODE_GUIDE + "00");
            rightBusinessList.add(TypeCodeUtil.CODE_ON + TypeCodeUtil.CODE_RIGHT + TypeCodeUtil.CODE_BUSINESS + "00");

        } else {
            for (String type : typeCodeList) {

                if (type.equals(TypeCodeUtil.CODE_OFF + TypeCodeUtil.CODE_LEFT + TypeCodeUtil.CODE_GUIDE)) {
                    leftGuideList.add(type);
                } else if (type.startsWith(TypeCodeUtil.CODE_ON + TypeCodeUtil.CODE_LEFT + TypeCodeUtil.CODE_BUSINESS)) {
                    leftBusinessList.add(type);
                } else if (type.startsWith(TypeCodeUtil.CODE_OFF + TypeCodeUtil.CODE_RIGHT + TypeCodeUtil.CODE_GUIDE)) {
                    rightGuideList.add(type);
                } else if (type.startsWith(TypeCodeUtil.CODE_ON + TypeCodeUtil.CODE_RIGHT + TypeCodeUtil.CODE_BUSINESS)) {
                    rightBusinessList.add(type);
                }

            }
        }

        //获取锁
//        RLock lock = redissonClient.getLock("getBatchBitmapArraysWithTypeCode:" + dId);

//        try {
//            while (!lock.tryLock(0, 10, TimeUnit.SECONDS)) {
//                //未获取到锁 则进入合并任务
//                Thread.sleep(1000);//此处1S 是因为点阵图更新后上传OSS 虽然正确的上传了 但是由于OSS的机制问题，再去获取的时候会得到旧的图片，导致基于旧图进行了修改，引起bug
//                log.debug(" [未获取到] 锁" + "getBatchBitmapArraysWithTypeCode:" + dId );
//            }
//
//            log.debug(" [获取到了] 锁" + "getBatchBitmapArraysWithTypeCode:" + dId );

        //处理图

//        关机_左屏_引导图
        if (!leftGuideList.isEmpty()) {

            BitmapDataVo bitmapDataVo = null;
            if (!isContainsType(typeList, 0)) {
                bitmapDataVo = signageFileUtil.getLastBitmapDataVo(dId, TypeCodeUtil.CODE_OFF + TypeCodeUtil.CODE_LEFT + TypeCodeUtil.CODE_GUIDE, sessionId, ter020201InfoVo.getIsDefault());
            }
            if (bitmapDataVo == null) {

                int[][] canvas = signageDrawLeftUtil.canvasLiftOFFBitmapArrays();
                //绘制NFC图标
                if (ter020201InfoVo.getNfcOpen() != null && ter020201InfoVo.getNfcOpen() == 1) {
                    signageDrawLeftUtil.cleanCanvasBitmapLeftNFCIMG(canvas, -2, -18);
                } else {
                    signageDrawLeftUtil.canvasBitmapLeftNFCIMG(canvas, -2, -18);
                }

                // 由于 硬件屏幕原因 需要将 将左屏需要倒装
                twoBytes = DotMatrixImageUtil.canvasBitmapArraysToTwo(
                        DotMatrixImageUtil.screenFlip(
                                canvas
                        )
                );

                bitmapDataVo = signageFileUtil.dealWithBitmapDataVo(twoBytes, dId, path, TypeCodeUtil.CODE_OFF + TypeCodeUtil.CODE_LEFT + TypeCodeUtil.CODE_GUIDE, ter020201Vo.getTer020201InfoVo().getIsDefault());

            }

            bitmapDataVoList.add(bitmapDataVo);

        }

        //关机_右屏_引导图
        if (!rightGuideList.isEmpty()) {

            canvasBitmapArrays = new int[height][width];

            boolean isContainsNetwork = false;
            boolean isContainsNotNetwork = false;

            BitmapDataVo bitmapDataVo = null;

            byte[] notNetworkData = null;
            byte[] canvasData = null;

            //获取未配网的图
            if (rightGuideList.contains(TypeCodeUtil.CODE_OFF + TypeCodeUtil.CODE_RIGHT + TypeCodeUtil.CODE_GUIDE + "00")) {
                bitmapDataVo = null;
                //未配网
                //从缓存中获取之前的点阵数据
                if (!isContainsType(typeList, 0)) {
                    bitmapDataVo = signageFileUtil.getLastBitmapDataVo(dId, TypeCodeUtil.CODE_OFF + TypeCodeUtil.CODE_RIGHT + TypeCodeUtil.CODE_GUIDE + "00",  sessionId, ter020201InfoVo.getIsDefault());
                }

                if (bitmapDataVo != null) {

                    bitmapDataVoList.add(bitmapDataVo);

                    isContainsNotNetwork = true;
                }
            }

            //获取 已配网的图
            if (rightGuideList.contains(TypeCodeUtil.CODE_OFF + TypeCodeUtil.CODE_RIGHT + TypeCodeUtil.CODE_GUIDE + "01")) {
                bitmapDataVo = null;
                //已配网
                //从缓存中获取之前的点阵数据
                if (!isContainsType(typeList, 0)) {
                    bitmapDataVo = signageFileUtil.getLastBitmapDataVo(dId, TypeCodeUtil.CODE_OFF + TypeCodeUtil.CODE_RIGHT + TypeCodeUtil.CODE_GUIDE + "01", sessionId, ter020201InfoVo.getIsDefault());
                }

                if (bitmapDataVo != null) {

                    //可能是修改
                    if (isContainsType(typeList, 0, 9, 10)) {
                        canvasBitmapArrays = DotMatrixImageUtil.screenHorizontalToVertical(
                                DotMatrixImageUtil.twoToCanvasBitmapArrays(bitmapDataVo.getTwoData(), height, width)
                        );
                        if (isContainsType(typeList, 0, 10)) {
                            signingDrawRightUtil.canvasBitmapRightWsName(canvasBitmapArrays, ter020201InfoVo);
                        }

                        if (isContainsType(typeList, 0, 9)) {
                            signingDrawRightUtil.canvasBitmapRightWsWorkCode(canvasBitmapArrays, ter020201InfoVo);
                        }
                        // 由于 硬件屏幕原因 需要将 将右屏的画布旋转
                        canvasData = DotMatrixImageUtil.canvasBitmapArraysToTwo(
                                DotMatrixImageUtil.screenHorizontalToVertical(
                                        canvasBitmapArrays
                                )
                        );
                        bitmapDataVo = null;
                        bitmapDataVo = signageFileUtil.dealWithBitmapDataVo(canvasData, dId, path, TypeCodeUtil.CODE_OFF + TypeCodeUtil.CODE_RIGHT + TypeCodeUtil.CODE_GUIDE + "01", ter020201Vo.getTer020201InfoVo().getIsDefault());

                    }

                    bitmapDataVoList.add(bitmapDataVo);

                    isContainsNetwork = true;

                }
            }


            //检查是否要重新生成
            if (!isContainsNetwork || !isContainsNotNetwork) {
                //获取初始图
                int[][] canvasDefaultBitmapArrays = signingDrawRightUtil.canvasRightOFFNotNetworkBitmapArrays(ter020201InfoVo.getDeId());

                //绘制未配网的图
                if (!isContainsNotNetwork) {
                    int[][] otherCanvasBitmap = Arrays.stream(canvasDefaultBitmapArrays)
                            .map(int[]::clone)
                            .toArray(int[][]::new);

                    signingDrawRightUtil.canvasRightOFFNetworkBitmapArraysText(otherCanvasBitmap);

                    // 由于 硬件屏幕原因 需要将 将右屏的画布旋转
                    notNetworkData = DotMatrixImageUtil.canvasBitmapArraysToTwo(
                            DotMatrixImageUtil.screenHorizontalToVertical(
                                    otherCanvasBitmap
                            )
                    );
                    bitmapDataVo = null;
                    bitmapDataVo = signageFileUtil.dealWithBitmapDataVo(notNetworkData, dId, path, TypeCodeUtil.CODE_OFF + TypeCodeUtil.CODE_RIGHT + TypeCodeUtil.CODE_GUIDE + "00", ter020201Vo.getTer020201InfoVo().getIsDefault());

                    bitmapDataVoList.add(bitmapDataVo);


                }

                //绘制已配网的图
                if (!isContainsNetwork) {

                    int[][] otherCanvasBitmap = Arrays.stream(canvasDefaultBitmapArrays)
                            .map(int[]::clone)
                            .toArray(int[][]::new);

                    signingDrawRightUtil.canvasBitmapRightWsName(otherCanvasBitmap, ter020201InfoVo);

                    signingDrawRightUtil.canvasBitmapRightWsWorkCode(otherCanvasBitmap, ter020201InfoVo);

                    // 由于 硬件屏幕原因 需要将 将右屏的画布旋转
                    canvasData = DotMatrixImageUtil.canvasBitmapArraysToTwo(
                            DotMatrixImageUtil.screenHorizontalToVertical(
                                    otherCanvasBitmap
                            )
                    );
                    bitmapDataVo = null;
                    bitmapDataVo = signageFileUtil.dealWithBitmapDataVo(canvasData, dId, path, TypeCodeUtil.CODE_OFF + TypeCodeUtil.CODE_RIGHT + TypeCodeUtil.CODE_GUIDE + "01", ter020201Vo.getTer020201InfoVo().getIsDefault());

                    bitmapDataVoList.add(bitmapDataVo);


                }

            }


        }


//         开机_左屏_业务图
        if (!leftBusinessList.isEmpty()) {

            Set<Integer> stateList = new HashSet<>();//状态列表
            Set<Integer> chartList = new HashSet<>();//图表列表

            for (String type : leftBusinessList) {
                String typeCode2 = type.substring(6);
//                logger.info(typeCode + "   "+typeCode2);

                String chartStr = typeCode2.substring(0, 2);
                String stateStr = typeCode2.substring(2);
                if (!TypeCodeUtil.CODE_FF.equals(chartStr)) {
                    int chart = Integer.parseInt(typeCode2.substring(0, 2), 16);
                    chartList.add(chart);
                }

                if (!TypeCodeUtil.CODE_FF.equals(stateStr)) {
                    int state = Integer.parseInt(typeCode2.substring(2), 16);
                    stateList.add(state);
                }


            }

            //先绘制出公共区域
            int[][] canvas = null;
            int[][] canvasSnap = null;//共用临时对象
            byte[] canvasSnap2 = null;//共用临时对象
            int[][] canvasSnap3 = null;//共用临时对象
            int[][] canvasSnap4 = null;//共用临时对象
            BitmapDataVo bitmapDataVo2 = null;

            int[][] rectangleCheckBitmapArrays = null;


            //图表0 指向的状态      20240613 画布缩小了  长宽都 - width/40
            int[][] runStateChartCheckCanvasBitmap = null;
            int[][] upkeepStateChartCheckCanvasBitmap = null;
            int[][] maintainStateChartCheckCanvasBitmap = null;
            int[][] faultStateChartCheckCanvasBitmap = null;
            int[][] waitStateChartCheckCanvasBitmap = null;
            int[][] haltStateChartCheckCanvasBitmap = null;
            int[][] nullStateChartCheckCanvasBitmap = null;//无状态

            //状态选中画布
            int[][] runStateCheckCanvasBitmap = new int[width][height];
            int[][] upkeepStateCheckCanvasBitmap = new int[width][height];
            int[][] maintainStateCheckCanvasBitmap = new int[width][height];
            int[][] faultStateCheckCanvasBitmap = new int[width][height];
            int[][] waitStateCheckCanvasBitmap = new int[width][height];
            int[][] haltStateCheckCanvasBitmap = new int[width][height];
            int[][] nullStateCheckCanvasBitmap = new int[width][height];//无状态 无选中

            //图表 1 20240613 画布缩小了  长宽都 - width/40
            int[][] chart1canvasBitmap = null;
            int[][] chart1canvasTextBitmap = null;

            //图表 2 20240613 画布缩小了  长宽都 - width/40
            int[][] chart2canvasBitmap = null;
            int[][] chart2canvasTextBitmap = null;

            //图表 3 图表三由图表1和图表2 合并而成  20240613 画布缩小了  长宽都 - width/40
            int[][] chart1canvasBitmap3 = null;
            int[][] chart1canvasTextBitmap3 = null;
            int[][] chart2canvasBitmap3 = null;
            int[][] chart2canvasTextBitmap3 = null;


            //开始合并 不同状态 不同选中 不同图表的整图
            //先绘制出状态 所以直接从缓存中随意获取一张点阵图即可
            if (!isContainsType(typeList, 0)) {
                outer:
                for (int state : stateList) {
                    for (int chart : chartList) {
                        bitmapDataVo2 = signageFileUtil.getLastBitmapDataVo(dId, TypeCodeUtil.CODE_ON + TypeCodeUtil.CODE_LEFT + TypeCodeUtil.CODE_BUSINESS + "0" + chart + "0" + state, sessionId, ter020201InfoVo.getIsDefault());
                        if (bitmapDataVo2 != null) {
                            break outer;
                        }
                    }
                }
            }


            //生成所有状态未选中的画布  由于状态对象是一个完整的类 改了一个则所有状态全部要重新绘制 此处后续可优化

            //修改了状态设置
            if (bitmapDataVo2 == null || isContainsType(typeList, 0, 19)) {


                int[][] rectangleBitmapArrays = signageDrawLeftUtil.getRoundedRectangularArray(false);//横向选中框
                int[][] rectangleBitmapArrays2 = signageDrawLeftUtil.getRoundedRectangularArray(true);//竖向选中框

                //无论是重新生成状态还是更新状态 都是一个新的画布所以此处直接创建一个新的画布
                canvas = new int[width][height];


//                saveToFile("D:/WorkSpace/Web/image/Device/测试测试","左屏运行","1",XZPImageUtil.canvasBitmapArraysToTwo(canvas));

                signageDrawLeftUtil.canvasBitmapLeftRunState(canvas, false, rectangleBitmapArrays, ter020201InfoVo, false); //运行状态
                signageDrawLeftUtil.canvasBitmapLeftWaitState(canvas, false, rectangleBitmapArrays2, ter020201InfoVo, false);//待料状态
                signageDrawLeftUtil.canvasBitmapLeftUpkeepState(canvas, false, rectangleBitmapArrays2, ter020201InfoVo, false);//保养状态
                signageDrawLeftUtil.canvasBitmapLeftMaintainState(canvas, false, rectangleBitmapArrays, ter020201InfoVo, false);//封存/维修状态
                signageDrawLeftUtil.canvasBitmapLeftFaultState(canvas, false, rectangleBitmapArrays2, ter020201InfoVo, false); //故障状态
                signageDrawLeftUtil.canvasBitmapLeftHaltState(canvas, false, rectangleBitmapArrays2, ter020201InfoVo, false);//停机状态

//                saveToFile("D:/WorkSpace/Web/image/Device/测试测试","左屏运行","2",XZPImageUtil.canvasBitmapArraysToTwo(canvas));

                //绘制NFC图标
                if (ter020201InfoVo.getNfcOpen() != null && ter020201InfoVo.getNfcOpen() == 1) {
                    signageDrawLeftUtil.cleanCanvasBitmapLeftNFCIMG(canvas, 0, 0);
                } else {
                    signageDrawLeftUtil.canvasBitmapLeftNFCIMG(canvas, 0, 0);
                }

//                saveToFile("D:/WorkSpace/Web/image/Device/测试测试","左屏运行","3",XZPImageUtil.canvasBitmapArraysToTwo(canvas));

                //绘制出选中矩形


                runStateCheckCanvasBitmap = Arrays.stream(canvas)
                        .map(int[]::clone)
                        .toArray(int[][]::new);
                upkeepStateCheckCanvasBitmap = Arrays.stream(canvas)
                        .map(int[]::clone)
                        .toArray(int[][]::new);
                maintainStateCheckCanvasBitmap = Arrays.stream(canvas)
                        .map(int[]::clone)
                        .toArray(int[][]::new);
                faultStateCheckCanvasBitmap = Arrays.stream(canvas)
                        .map(int[]::clone)
                        .toArray(int[][]::new);
                waitStateCheckCanvasBitmap = Arrays.stream(canvas)
                        .map(int[]::clone)
                        .toArray(int[][]::new);
                haltStateCheckCanvasBitmap = Arrays.stream(canvas)
                        .map(int[]::clone)
                        .toArray(int[][]::new);
                nullStateCheckCanvasBitmap = Arrays.stream(canvas)
                        .map(int[]::clone)
                        .toArray(int[][]::new);


//                saveToFile("D:/WorkSpace/Web/image/Device/测试测试","左屏运行","4",XZPImageUtil.canvasBitmapArraysToTwo(runStateCheckCanvasBitmap));

                //生成各个状态选中的画布
                signageDrawLeftUtil.canvasBitmapLeftRunState(runStateCheckCanvasBitmap, true, rectangleBitmapArrays, ter020201InfoVo, true); //运行状态
                signageDrawLeftUtil.canvasBitmapLeftWaitState(waitStateCheckCanvasBitmap, true, rectangleBitmapArrays2, ter020201InfoVo, true);//待料状态
                signageDrawLeftUtil.canvasBitmapLeftUpkeepState(upkeepStateCheckCanvasBitmap, true, rectangleBitmapArrays2, ter020201InfoVo, true);//保养状态
                signageDrawLeftUtil.canvasBitmapLeftMaintainState(maintainStateCheckCanvasBitmap, true, rectangleBitmapArrays, ter020201InfoVo, true);//封存/维修状态
                signageDrawLeftUtil.canvasBitmapLeftFaultState(faultStateCheckCanvasBitmap, true, rectangleBitmapArrays2, ter020201InfoVo, true); //故障状态
                signageDrawLeftUtil.canvasBitmapLeftHaltState(haltStateCheckCanvasBitmap, true, rectangleBitmapArrays2, ter020201InfoVo, true);//停机状态

//                saveToFile("D:/WorkSpace/Web/image/Device/测试测试","左屏运行","5",XZPImageUtil.canvasBitmapArraysToTwo(runStateCheckCanvasBitmap));


                //绘制选中框
                signageDrawLeftUtil.canvasBitmapLeftCheckState(runStateCheckCanvasBitmap, 0, rectangleBitmapArrays);
                signageDrawLeftUtil.canvasBitmapLeftCheckState(waitStateCheckCanvasBitmap, 1, rectangleBitmapArrays2);
                signageDrawLeftUtil.canvasBitmapLeftCheckState(upkeepStateCheckCanvasBitmap, 2, rectangleBitmapArrays2);
                signageDrawLeftUtil.canvasBitmapLeftCheckState(maintainStateCheckCanvasBitmap, 3, rectangleBitmapArrays);
                signageDrawLeftUtil.canvasBitmapLeftCheckState(faultStateCheckCanvasBitmap, 4, rectangleBitmapArrays2);
                signageDrawLeftUtil.canvasBitmapLeftCheckState(haltStateCheckCanvasBitmap, 5, rectangleBitmapArrays2);


//                saveToFile("D:/WorkSpace/Web/image/Device/测试测试","左屏运行","6",XZPImageUtil.canvasBitmapArraysToTwo(runStateCheckCanvasBitmap));

                //图表0 指向的状态 在此处生成是因为 状态修改时一定会更改指针图形 所有也不存在单独改指针的情况
//                if(chartList.contains(0)){
                runStateChartCheckCanvasBitmap = signageDrawLeftUtil.canvasBitmapLeftChart0(0, ter020201InfoVo);
                waitStateChartCheckCanvasBitmap = signageDrawLeftUtil.canvasBitmapLeftChart0(1, ter020201InfoVo);
                upkeepStateChartCheckCanvasBitmap = signageDrawLeftUtil.canvasBitmapLeftChart0(2, ter020201InfoVo);
                maintainStateChartCheckCanvasBitmap = signageDrawLeftUtil.canvasBitmapLeftChart0(3, ter020201InfoVo);
                faultStateChartCheckCanvasBitmap = signageDrawLeftUtil.canvasBitmapLeftChart0(4, ter020201InfoVo);
                haltStateChartCheckCanvasBitmap = signageDrawLeftUtil.canvasBitmapLeftChart0(5, ter020201InfoVo);
                nullStateChartCheckCanvasBitmap = signageDrawLeftUtil.canvasBitmapLeftChart0(254, ter020201InfoVo);

                //      saveToFile("D:/WorkSpace/Web/image/Device/测试测试","左屏运行","7",XZPImageUtil.canvasBitmapArraysToTwo(runStateChartCheckCanvasBitmap));


//                }


                //图表2 状态设置

                // height/3,  width/7,
//                chart2canvasBitmap = signageDrawLeftUtil.canvasBitmapLeftChart2(ter020201InfoVo,true);
                chart2canvasTextBitmap = signageDrawLeftUtil.canvasBitmapLeftChart2Text(ter020201InfoVo, true);

                //图表 3 状态设置
                //  height/3, width/7*4,
                chart2canvasTextBitmap3 = signageDrawLeftUtil.canvasBitmapLeftChart2Text(ter020201InfoVo, false);

            }

            //修改了 状态历史
            if (bitmapDataVo2 == null || isContainsType(typeList, 0, 29)) {
                //图表 2
                //height/3, width/4,
//                if(chartList.contains(1)){
                chart2canvasBitmap = signageDrawLeftUtil.canvasBitmapLeftChart2(ter020201InfoVo, true);
//                }
                //图表 3
//                if(chartList.contains(3)){
                //  height/3, width/7*4,
                chart2canvasBitmap3 = signageDrawLeftUtil.canvasBitmapLeftChart2(ter020201InfoVo, false);
//                }
            }


            //修改了打卡设置相关
            if (bitmapDataVo2 == null || isContainsType(typeList, 0, 20)) {
                //图表 1
                //height/3, width/4,
//                if(chartList.contains(1)){
                chart1canvasTextBitmap = signageDrawLeftUtil.canvasBitmapLeftChart1Text(ter020201InfoVo, true);
//                }

                //图表 3
//                if(chartList.contains(3)){
                //  height/3, width/6,
                chart1canvasTextBitmap3 = signageDrawLeftUtil.canvasBitmapLeftChart1Text(ter020201InfoVo, false);
//                }
            }


            //修改了打卡历史数据相关
            if (bitmapDataVo2 == null || isContainsType(typeList, 0, 30)) {
                //图表 1
                //height/3, width/4,
//                if(chartList.contains(1)){
                chart1canvasBitmap = signageDrawLeftUtil.canvasBitmapLeftChart1(ter020201InfoVo, true);
//                }

                //图表 3
//                if(chartList.contains(3)){
                //  height/3, width/6,
                chart1canvasBitmap3 = signageDrawLeftUtil.canvasBitmapLeftChart1(ter020201InfoVo, false);
//                }
            }


            //2024/06/13 将图表的图缩小
            int zoomOut = width / 10 * 2 + width / 40 * 2;

//            int[][] bitmapChart0Data;//图表0原数据
            int[][] bitmapChart1Data = null;//图表1原数据
            int[][] bitmapChart2Data = null;//图表2原数据
            int[][] bitmapChart3Data = null;//图表3原数据

            int[][] bitmapChartDataSnap = null;

            //2024/06/13 图表文件周边将不再有 状态文字
//                for(int state : stateList){
            //图表0 特殊处理 直接无视掉
            //图表1
            if (bitmapChart1Data == null) {
                BitmapDataVo bitmapDataVo = null;
                if (!isContainsType(typeList, 0)) {
                    bitmapDataVo = signageFileUtil.getLastBitmapDataVo(dId, TypeCodeUtil.CODE_ON + TypeCodeUtil.CODE_LEFT + TypeCodeUtil.CODE_BUSINESS + "01" + TypeCodeUtil.CODE_FF, sessionId, ter020201InfoVo.getIsDefault());//+ "0" + state

                }
                if (bitmapDataVo != null) {

                    bitmapChart1Data = DotMatrixImageUtil.screenFlip(
                            DotMatrixImageUtil.twoToCanvasBitmapArrays(bitmapDataVo.getTwoData(), height - zoomOut, width - zoomOut)
                    );
                    //2024/06/13 图表文件周边将不再有 状态文字
//                            //将图表去除掉周边状态则得到无状态的 图表图
//                            //清除残留文字
//                            cleanCanvasBitmapLeftState(bitmapChart1Data,-1);

                }
            }
            //图表2
            if (bitmapChart2Data == null) {
                BitmapDataVo bitmapDataVo = null;
                if (!isContainsType(typeList, 0)) {
                    bitmapDataVo = signageFileUtil.getLastBitmapDataVo(dId, TypeCodeUtil.CODE_ON + TypeCodeUtil.CODE_LEFT + TypeCodeUtil.CODE_BUSINESS + "02" + TypeCodeUtil.CODE_FF, sessionId, ter020201InfoVo.getIsDefault());//+ "0" + state
                }
                if (bitmapDataVo != null) {

                    bitmapChart2Data = DotMatrixImageUtil.screenFlip(
                            DotMatrixImageUtil.twoToCanvasBitmapArrays(bitmapDataVo.getTwoData(), height - zoomOut, width - zoomOut)
                    );
//                            //2024/06/13 图表文件周边将不再有 状态文字
//                            //将图表去除掉周边状态则得到无状态的 图表图
//                            //清除残留文字
//                            cleanCanvasBitmapLeftState(bitmapChart2Data,-1);

                }
            }
            //图表3
            if (bitmapChart3Data == null) {
                BitmapDataVo bitmapDataVo = null;
                if (!isContainsType(typeList, 0)) {
                    bitmapDataVo = signageFileUtil.getLastBitmapDataVo(dId, TypeCodeUtil.CODE_ON + TypeCodeUtil.CODE_LEFT + TypeCodeUtil.CODE_BUSINESS + "03" + TypeCodeUtil.CODE_FF, sessionId, ter020201InfoVo.getIsDefault());//+ "0" + state
                }
                if (bitmapDataVo != null) {

                    bitmapChart3Data = DotMatrixImageUtil.screenFlip(
                            DotMatrixImageUtil.twoToCanvasBitmapArrays(bitmapDataVo.getTwoData(), height - zoomOut, width - zoomOut)
                    );
//                            //2024/06/13 图表文件周边将不再有 状态文字
//                            //将图表去除掉周边状态则得到无状态的 图表图
//                            //清除残留文字
//                            cleanCanvasBitmapLeftState(bitmapChart3Data,-1);

                }
            }

//                    if(bitmapChart1Data != null && bitmapChart2Data != null && bitmapChart3Data != null ){
//                        break;
//                    }
//                }

            //绘制图表


            for (int chart : chartList) {
                //图表0 特殊处理 直接无视掉
                if (chart == 0) {
//                    if(state == 0){
//                        canvasSnap3 = runStateChartCheckCanvasBitmap;
//                    }
//                    else if(state == 1){
//                        canvasSnap3 = upkeepStateChartCheckCanvasBitmap;
//                    }
//                    else if(state == 2){
//                        canvasSnap3 = maintainStateChartCheckCanvasBitmap;
//                    }
//                    else if(state == 3){
//                        canvasSnap3 = faultStateChartCheckCanvasBitmap;
//                    }
//                    else if(state == 4){
//                        canvasSnap3 = waitStateChartCheckCanvasBitmap;
//                    }
//                    else if(state == 5){
//                        canvasSnap3 = haltStateChartCheckCanvasBitmap;
//                    }
//
//                    //将图表0进行绘制
//
//                    XZPCanvasUtil.mergeDotMatrices(canvasSnap,
//                            canvasSnap3,
//                            height/2-canvasSnap3.length/2,
//                            width/2  - canvasSnap3[0].length/2);
                } else if (chart == 1) {

                    if (bitmapChart1Data == null) {
                        bitmapChartDataSnap = new int[width - zoomOut][height - zoomOut];
                        if (chart1canvasTextBitmap == null) {
                            chart1canvasTextBitmap = signageDrawLeftUtil.canvasBitmapLeftChart1Text(ter020201InfoVo, true);
                        }

                        if (chart1canvasBitmap == null) {
                            chart1canvasBitmap = signageDrawLeftUtil.canvasBitmapLeftChart1(ter020201InfoVo, true);
                        }

                    } else {
                        bitmapChartDataSnap = bitmapChart1Data;
                    }


                    if (bitmapChart1Data == null || isContainsType(typeList, 0, 30)) {

                        DotMatrixCanvasUtil.mergeDotMatrices(bitmapChartDataSnap,
                                chart1canvasBitmap,
                                bitmapChartDataSnap.length / 2 - chart1canvasBitmap.length,
                                height / 2 - chart1canvasBitmap[0].length / 2 - zoomOut / 2);

                    }


                    if (bitmapChart1Data == null || isContainsType(typeList, 0, 20)) {

                        //由于残留数据的影响 先清除掉之前的 暂时将整个下面进行清除
                        int[][] cleanCanvasBitmap = new int[bitmapChartDataSnap.length / 2]
                                [bitmapChartDataSnap[0].length];

                        DotMatrixCanvasUtil.mergeDotMatricesConfuse(bitmapChartDataSnap,
                                cleanCanvasBitmap,
                                bitmapChartDataSnap.length / 2,
                                height / 2 - chart1canvasTextBitmap[0].length / 2 - zoomOut / 2,
                                0);

                        DotMatrixCanvasUtil.mergeDotMatrices(bitmapChartDataSnap,
                                chart1canvasTextBitmap,
                                bitmapChartDataSnap.length / 2,
                                height / 2 - chart1canvasTextBitmap[0].length / 2 - zoomOut / 2);

                    }

                    bitmapChart1Data = bitmapChartDataSnap;

                } else if (chart == 2) {

                    if (bitmapChart2Data == null) {
                        bitmapChartDataSnap = new int[width - zoomOut][height - zoomOut];
                        if (chart2canvasBitmap == null) {
                            chart2canvasBitmap = signageDrawLeftUtil.canvasBitmapLeftChart2(ter020201InfoVo, true);
                        }

                        if (chart2canvasTextBitmap == null) {
                            chart2canvasTextBitmap = signageDrawLeftUtil.canvasBitmapLeftChart2Text(ter020201InfoVo, true);
                        }

                    } else {
                        bitmapChartDataSnap = bitmapChart2Data;
                    }

                    if (bitmapChart2Data == null || isContainsType(typeList, 0, 29)) {
                        DotMatrixCanvasUtil.mergeDotMatrices(bitmapChartDataSnap,
                                chart2canvasBitmap,
                                bitmapChartDataSnap.length / 2 - chart2canvasBitmap.length,
                                height / 2 - chart2canvasBitmap[0].length / 2 - zoomOut / 2);//  /4
                    }

                    if (bitmapChart2Data == null || isContainsType(typeList, 0, 19)) {
                        //由于残留数据的影响 先清除掉之前的
                        int[][] cleanCanvasBitmap = new int[bitmapChartDataSnap.length / 2]
                                [bitmapChartDataSnap[0].length];

                        DotMatrixCanvasUtil.mergeDotMatricesConfuse(bitmapChartDataSnap,
                                cleanCanvasBitmap,
                                bitmapChartDataSnap.length / 2,
                                height / 2 - chart2canvasTextBitmap[0].length / 2 - zoomOut / 2,
                                0);


                        DotMatrixCanvasUtil.mergeDotMatrices(bitmapChartDataSnap,
                                chart2canvasTextBitmap,
                                bitmapChartDataSnap.length / 2,
                                height / 2 - chart2canvasTextBitmap[0].length / 2 - zoomOut / 2);//  /4
                    }
                    bitmapChart2Data = bitmapChartDataSnap;


                } else if (chart == 3) {

                    if (bitmapChart3Data == null) {
                        bitmapChartDataSnap = new int[width - zoomOut][height - zoomOut];
                        if (chart1canvasTextBitmap3 == null) {
                            chart1canvasTextBitmap3 = signageDrawLeftUtil.canvasBitmapLeftChart1Text(ter020201InfoVo, false);
                        }
                        if (chart1canvasBitmap3 == null) {
                            chart1canvasBitmap3 = signageDrawLeftUtil.canvasBitmapLeftChart1(ter020201InfoVo, false);
                        }

                        if (chart2canvasBitmap3 == null) {
                            chart2canvasBitmap3 = signageDrawLeftUtil.canvasBitmapLeftChart2(ter020201InfoVo, false);
                        }

                        if (chart2canvasTextBitmap3 == null) {
                            chart2canvasTextBitmap3 = signageDrawLeftUtil.canvasBitmapLeftChart2Text(ter020201InfoVo, false);
                        }

                    } else {
                        bitmapChartDataSnap = bitmapChart3Data;
                    }


                    if (bitmapChart3Data == null || isContainsType(typeList, 0, 30)) {
                        DotMatrixCanvasUtil.mergeDotMatrices(bitmapChartDataSnap,
                                chart1canvasBitmap3,
                                bitmapChartDataSnap.length / 2 - chart1canvasBitmap3.length,
                                height / 7 * 4 - zoomOut / 2);
                    }


                    if (bitmapChart3Data == null || isContainsType(typeList, 0, 20)) {
                        //由于残留数据的影响 先清除掉之前的
                        int[][] cleanCanvasBitmap = new int[bitmapChartDataSnap.length / 2]
                                [chart1canvasTextBitmap3[0].length];

                        DotMatrixCanvasUtil.mergeDotMatricesConfuse(bitmapChartDataSnap,
                                cleanCanvasBitmap,
                                bitmapChartDataSnap.length / 2,
                                height / 7 * 4 - zoomOut / 2,
                                0);

                        DotMatrixCanvasUtil.mergeDotMatrices(bitmapChartDataSnap,
                                chart1canvasTextBitmap3,
                                bitmapChartDataSnap.length / 2,
                                height / 7 * 4 - zoomOut / 2);
                    }


                    if (bitmapChart3Data == null || isContainsType(typeList, 0, 29)) {
                        DotMatrixCanvasUtil.mergeDotMatrices(bitmapChartDataSnap,
                                chart2canvasBitmap3,
                                bitmapChartDataSnap.length / 2 - chart2canvasBitmap3.length,
                                height / 6 - zoomOut / 2);
                    }


                    if (bitmapChart3Data == null || isContainsType(typeList, 0, 19)) {
                        //由于残留数据的影响 先清除掉之前的
                        int[][] cleanCanvasBitmap = new int[bitmapChartDataSnap.length / 2]
                                [chart2canvasTextBitmap3[0].length];

                        DotMatrixCanvasUtil.mergeDotMatricesConfuse(bitmapChartDataSnap,
                                cleanCanvasBitmap,
                                bitmapChartDataSnap.length / 2,
                                height / 6 - zoomOut / 2,
                                0);

                        DotMatrixCanvasUtil.mergeDotMatrices(bitmapChartDataSnap,
                                chart2canvasTextBitmap3,
                                bitmapChartDataSnap.length / 2,
                                height / 6 - zoomOut / 2);
                    }

                    bitmapChart3Data = bitmapChartDataSnap;

//                    saveToFile("D:/WorkSpace/Web/image/Device/测试测试","左屏运行","10",XZPImageUtil.canvasBitmapArraysToTwo(bitmapChart3Data));


                }

            }


            for (int state : stateList) {
                //绘制 选中状态
                if (bitmapDataVo2 == null || isContainsType(typeList, 0, 19)) {
                    if (state == 0) {
                        canvasSnap = runStateCheckCanvasBitmap;
                    } else if (state == 1) {
                        canvasSnap = waitStateCheckCanvasBitmap;
                    } else if (state == 2) {
                        canvasSnap = upkeepStateCheckCanvasBitmap;
                    } else if (state == 3) {
                        canvasSnap = maintainStateCheckCanvasBitmap;
                    } else if (state == 4) {
                        canvasSnap = faultStateCheckCanvasBitmap;
                    } else if (state == 5) {
                        canvasSnap = haltStateCheckCanvasBitmap;
                    } else if (state == 254) {
                        canvasSnap = nullStateCheckCanvasBitmap;
                    }

                    // 由于 硬件屏幕原因 需要将 将左屏需要倒装
                    twoBytes = DotMatrixImageUtil.canvasBitmapArraysToTwo(
                            DotMatrixImageUtil.screenFlip(
                                    canvasSnap
                            )
                    );

                    BitmapDataVo bitmapDataVo = signageFileUtil.dealWithBitmapDataVo(twoBytes, dId, path, TypeCodeUtil.CODE_ON + TypeCodeUtil.CODE_LEFT + TypeCodeUtil.CODE_BUSINESS + TypeCodeUtil.CODE_FF +
                                    OtherUtil.strToHighZeroPadding(Integer.toString(state, 16), 2).toUpperCase(),
                            ter020201Vo.getTer020201InfoVo().getIsDefault());

                    bitmapDataVoList.add(bitmapDataVo);


                }
            }
            //图表
            for (int chart : chartList) {

                //图表0 特殊处理
                if (chart == 0) {
                    for (int state : stateList) {
                        if (state == 0) {
                            canvasSnap3 = runStateChartCheckCanvasBitmap;
                        } else if (state == 1) {
                            canvasSnap3 = waitStateChartCheckCanvasBitmap;

                        } else if (state == 2) {
                            canvasSnap3 = upkeepStateChartCheckCanvasBitmap;

                        } else if (state == 3) {
                            canvasSnap3 = maintainStateChartCheckCanvasBitmap;

                        } else if (state == 4) {
                            canvasSnap3 = faultStateChartCheckCanvasBitmap;
                        } else if (state == 5) {
                            canvasSnap3 = haltStateChartCheckCanvasBitmap;
                        } else if (state == 254) {
                            canvasSnap3 = nullStateChartCheckCanvasBitmap;
                        }

                        // 由于 硬件屏幕原因 需要将 将左屏需要倒装
                        twoBytes = DotMatrixImageUtil.canvasBitmapArraysToTwo(
                                DotMatrixImageUtil.screenFlip(
                                        canvasSnap3
                                )
                        );
                        BitmapDataVo bitmapDataVo = signageFileUtil.dealWithBitmapDataVo(twoBytes, dId, path, TypeCodeUtil.CODE_ON + TypeCodeUtil.CODE_LEFT + TypeCodeUtil.CODE_BUSINESS + "0" + chart +
                                        OtherUtil.strToHighZeroPadding(Integer.toString(state, 16), 2).toUpperCase(),
                                ter020201Vo.getTer020201InfoVo().getIsDefault());
                        bitmapDataVoList.add(bitmapDataVo);

                    }
                    continue;
                }

                //图表1
                else if (chart == 1) {
                    //将图表1进行绘制
                    canvasSnap3 = bitmapChart1Data;

                }

                //图表2
                else if (chart == 2) {
                    //将图表2进行绘制
                    canvasSnap3 = bitmapChart2Data;
                }

                //图表3
                else if (chart == 3) {
                    //将图表3进行绘制
                    canvasSnap3 = bitmapChart3Data;
                }

                // 由于 硬件屏幕原因 需要将 将左屏需要倒装
                twoBytes = DotMatrixImageUtil.canvasBitmapArraysToTwo(
                        DotMatrixImageUtil.screenFlip(
                                canvasSnap3
                        )
                );


                BitmapDataVo bitmapDataVo = signageFileUtil.dealWithBitmapDataVo(twoBytes, dId, path, TypeCodeUtil.CODE_ON + TypeCodeUtil.CODE_LEFT + TypeCodeUtil.CODE_BUSINESS + "0" + chart + TypeCodeUtil.CODE_FF, ter020201Vo.getTer020201InfoVo().getIsDefault());

                bitmapDataVoList.add(bitmapDataVo);


            }


        }

//         开机_右屏_业务图
        if (!rightBusinessList.isEmpty()) {

            Set<Integer> chartList = new HashSet<>();//图表列表

            for (String type : rightBusinessList) {

                String typeCode2 = type.substring(6);
                int chart = Integer.parseInt(typeCode2.substring(0, 2));

                chartList.add(chart);
            }


            int xNumberSize = 5;//X轴的数字大小
            int xNumberHigh = 7;//X轴的数字高度


            int barChartWidth = 20;//柱状图的宽度
            int barChartInterval = 10;//柱状图间隔

            int yAxisDataPart = 5;//Y坐标轴数据段
            //绘制左侧Y轴
            int yLeftNumberSize = 5;//左侧数据每个数字的字体大小
            int yLeftNumberHigh = 7;//左侧数据每个数字的字体高度
            int yLeftNumberInterval = 25;//左侧数据每个数字的间隔 23

            int chartHeight = 135;//纯的柱状图的大小

            //图表的高度
            int startRowB = height / 5 + noChineseFontSize * 7 + textFontSize2 * 8;

            //清除画布数组
            int[][] cleanChartBitmapArrays = new int[chartHeight + yLeftNumberHigh + xNumberHigh + yLeftNumberSize + barChartInterval]
                    [width];

            BitmapDataVo bitmapDataVo2 = null;

            //从缓存中读取原点阵数据
            if (!isContainsType(typeList, 0)) {
                bitmapDataVo2 = signageFileUtil.getLastBitmapDataVo(dId, TypeCodeUtil.CODE_ON + TypeCodeUtil.CODE_RIGHT + TypeCodeUtil.CODE_BUSINESS + "00", sessionId, ter020201InfoVo.getIsDefault());//右屏 报表之外的数据是相同的 则随便获取其中一个即可
            }
            if (bitmapDataVo2 == null) {
                if (!isContainsType(typeList, 0)) {
                    bitmapDataVo2 = signageFileUtil.getLastBitmapDataVo(dId, TypeCodeUtil.CODE_ON + TypeCodeUtil.CODE_RIGHT + TypeCodeUtil.CODE_BUSINESS + "01", sessionId, ter020201InfoVo.getIsDefault());//右屏 报表之外的数据是相同的 则随便获取其中一个即可
                }
            }
            if (bitmapDataVo2 == null) {
                canvasBitmapArrays = new int[height][width];
            } else {
                canvasBitmapArrays = DotMatrixImageUtil.screenHorizontalToVertical(
                        DotMatrixImageUtil.twoToCanvasBitmapArrays(bitmapDataVo2.getTwoData(), height, width)
                );

                //清除图表部分
                DotMatrixCanvasUtil.mergeDotMatrices(canvasBitmapArrays, cleanChartBitmapArrays,
                        startRowB - yLeftNumberHigh / 2,
                        0);

            }
            //绘制公共区域

//            if(bitmapDataVo2 == null || isContainsType(typeList,0,2)){
//                canvasBitmapRightTerName(canvasBitmapArrays,ter020201InfoVo);
//            }
//
//            if(bitmapDataVo2 == null || isContainsType(typeList,0,1)){
//                canvasBitmapRightTerWorkCode(canvasBitmapArrays,ter020201InfoVo);
//            }

            //            if(bitmapDataVo2 == null || isContainsType(typeList,0,2)){
//                canvasBitmapRightTerName(canvasBitmapArrays,ter020201InfoVo);
//            }
//
//            if(bitmapDataVo2 == null || isContainsType(typeList,0,1)){
//                canvasBitmapRightTerWorkCode(canvasBitmapArrays,ter020201InfoVo);
//            }

            if (bitmapDataVo2 == null || isContainsType(typeList, 0, 10)) {


//                    saveToFile("D:/WorkSpace/Web/image/Device/测试测试","左屏运行","工站名称修改1", XZPImageUtil.canvasBitmapArraysToTwo(
////                            XZPImageUtil.screenHorizontalToVertical(
//                                    canvasBitmapArrays
////                            )
//                    ));
                signingDrawRightUtil.canvasBitmapRightWsName(canvasBitmapArrays, ter020201InfoVo);

//                    saveToFile("D:/WorkSpace/Web/image/Device/测试测试","左屏运行","工站名称修改2", XZPImageUtil.canvasBitmapArraysToTwo(
////                            XZPImageUtil.screenHorizontalToVertical(
//                                    canvasBitmapArrays
////                            )
//                    ));

            }

            //2024/07/19 默认右图 不展示编号
            if (!ter020201Vo.getTer020201InfoVo().getIsDefault()) {
                if (bitmapDataVo2 == null || isContainsType(typeList, 0, 9)) {
                    signingDrawRightUtil.canvasBitmapRightWsWorkCode(canvasBitmapArrays, ter020201InfoVo);
                }
            }


            if (bitmapDataVo2 == null || isContainsType(typeList, 0)) {
                //绘制间隔实线
                DotMatrixDrawShapeUtil.drawLine(canvasBitmapArrays, 1, 0, 0, height / 20 + height / 40 + noChineseFontSize * 4, rightMargins, width - rightMargins);
            }


            if (bitmapDataVo2 == null || isContainsType(typeList, 0, 12, 13)) {
                signingDrawRightUtil.canvasBitmapRightProduce(canvasBitmapArrays, ter020201InfoVo);
            }
            if (bitmapDataVo2 == null || isContainsType(typeList, 0, 12, 14)) {
                signingDrawRightUtil.canvasBitmapRightMaintain(canvasBitmapArrays, ter020201InfoVo);
            }
            if (bitmapDataVo2 == null || isContainsType(typeList, 0, 12, 15)) {
                signingDrawRightUtil.canvasBitmapRightInspection(canvasBitmapArrays, ter020201InfoVo);
            }
            if (bitmapDataVo2 == null || isContainsType(typeList, 0, 12, 16)) {
                signingDrawRightUtil.canvasBitmapRightUpkeep(canvasBitmapArrays, ter020201InfoVo);
            }
            if (bitmapDataVo2 == null || isContainsType(typeList, 0, 12, 17)) {
                signingDrawRightUtil.canvasBitmapRightOperator(canvasBitmapArrays, ter020201InfoVo);
            }

            if (bitmapDataVo2 == null || isContainsType(typeList, 0)) {
                //绘制间隔 虚线
                DotMatrixDrawShapeUtil.drawLine(canvasBitmapArrays, 1, 1, 1, height / 10 + noChineseFontSize * 7 + textFontSize2 * 7 + height / 20, rightMargins, width - rightMargins);

            }


            //2024/07/05 右屏去掉了标题
//                if(bitmapDataVo2 == null|| isContainsType(typeList,0) ){
//                    canvasBitmapRightCheckTitle(canvasBitmapArrays);
//                }

            if (isContainsType(typeList, 0)) {
                //去除旧版点阵图的残留

                int[][] cleanCanvasBitmap = new int[Math.max(textFontSize2Height, noChineseFontSizeHeight)]
                        [width];

                DotMatrixCanvasUtil.mergeDotMatricesConfuse(canvasBitmapArrays,
                        cleanCanvasBitmap,
                        height / 10 + noChineseFontSize * 7 + textFontSize2 * 8 + height / 20,
                        0,
                        0);
            }

            if (bitmapDataVo2 == null || isContainsType(typeList, 0, 19, 20, 23)) {
                signingDrawRightUtil.canvasBitmapRightCheckProduce(canvasBitmapArrays, ter020201InfoVo);
            }
            if (bitmapDataVo2 == null || isContainsType(typeList, 0, 19, 20, 24)) {
                signingDrawRightUtil.canvasBitmapRightCheckInspect(canvasBitmapArrays, ter020201InfoVo);
            }
            if (bitmapDataVo2 == null || isContainsType(typeList, 0, 19, 20, 26)) {
                signingDrawRightUtil.canvasBitmapRightCheckUpkeep(canvasBitmapArrays, ter020201InfoVo);
            }
            if (bitmapDataVo2 == null || isContainsType(typeList, 0, 19, 20, 25)) {
                signingDrawRightUtil.canvasBitmapRightCheckMaintain(canvasBitmapArrays, ter020201InfoVo);
            }


            if (bitmapDataVo2 == null || isContainsType(typeList, 0)) {
                //绘制间隔 虚线
                DotMatrixDrawShapeUtil.drawLine(canvasBitmapArrays, 1, 1, 1, height / 5 * 2 + noChineseFontSize * 10 + textFontSize2 * 8 + height / 40, rightMargins, width - rightMargins);

            }


            if (bitmapDataVo2 == null || isContainsType(typeList, 0, 11)) {
                signingDrawRightUtil.canvasBitmapRightWsNotice(canvasBitmapArrays, ter020201InfoVo);
            }

            if (bitmapDataVo2 == null || isContainsType(typeList, 0, 21, 22)) {
                signingDrawRightUtil.canvasBitmapRightSysNotice(canvasBitmapArrays, ter020201InfoVo);
            }

            //2024/06/15 绘制底部的按钮操作图
            if (bitmapDataVo2 == null || isContainsType(typeList, 0)) {
                signingDrawRightUtil.canvasBitmapRightBottomGuide(canvasBitmapArrays);
            }


            if (chartList.contains(0)) {
                bitmapDataVo2 = null;
                int[][] canvasBitmapArrays2 = new int[height][width];

                if (!isContainsType(typeList, 0)) {
                    bitmapDataVo2 = signageFileUtil.getLastBitmapDataVo(dId, TypeCodeUtil.CODE_ON + TypeCodeUtil.CODE_RIGHT + TypeCodeUtil.CODE_BUSINESS + "00", sessionId, ter020201InfoVo.getIsDefault());
                }


                if (bitmapDataVo2 == null || isContainsType(typeList, 0, 32, 18)) {

                    signingDrawRightUtil.canvasBitmapRightCapacityChart(canvasBitmapArrays, ter020201InfoVo);
                    canvasBitmapArrays2 = canvasBitmapArrays;
                } else {

                    canvasBitmapArrays2 = DotMatrixImageUtil.screenHorizontalToVertical(
                            DotMatrixImageUtil.twoToCanvasBitmapArrays(bitmapDataVo2.getTwoData(), height, width)
                    );

                    //清除非图表部分

                    //清除画布 上部分
                    int[][] cleanChartTopBitmapArrays = new int[startRowB - yLeftNumberHigh / 2]
                            [width];

                    DotMatrixCanvasUtil.mergeDotMatrices(canvasBitmapArrays2, cleanChartTopBitmapArrays,
                            0,
                            0);

                    //清除画布 下部分
                    int[][] cleanChartBottomBitmapArrays = new int[height - cleanChartTopBitmapArrays.length - cleanChartBitmapArrays.length]
                            [width];
                    DotMatrixCanvasUtil.mergeDotMatrices(canvasBitmapArrays2, cleanChartBottomBitmapArrays,
                            height - cleanChartBottomBitmapArrays.length,
                            0);

                    canvasBitmapArrays2 = DotMatrixCanvasUtil.mergeCanvasBitmap(width, height, 0, canvasBitmapArrays2, canvasBitmapArrays);

                }


                // 由于 硬件屏幕原因 需要将 将右屏的画布旋转
                twoBytes = DotMatrixImageUtil.canvasBitmapArraysToTwo(
                        DotMatrixImageUtil.screenHorizontalToVertical(
                                canvasBitmapArrays2
                        )
                );

                BitmapDataVo bitmapDataVo = signageFileUtil.dealWithBitmapDataVo(twoBytes, dId, path, TypeCodeUtil.CODE_ON + TypeCodeUtil.CODE_RIGHT + TypeCodeUtil.CODE_BUSINESS + "00", ter020201Vo.getTer020201InfoVo().getIsDefault());

                bitmapDataVoList.add(bitmapDataVo);

            }

            if (chartList.contains(1)) {
                bitmapDataVo2 = null;
                int[][] canvasBitmapArrays2 = new int[height][width];
                if (!isContainsType(typeList, 0)) {
                    bitmapDataVo2 = signageFileUtil.getLastBitmapDataVo(dId, TypeCodeUtil.CODE_ON + TypeCodeUtil.CODE_RIGHT + TypeCodeUtil.CODE_BUSINESS + "01", sessionId, ter020201InfoVo.getIsDefault());
                }

                if (bitmapDataVo2 == null || isContainsType(typeList, 0, 31)) {

                    signingDrawRightUtil.canvasBitmapRightWorkingTimeChart(canvasBitmapArrays, ter020201InfoVo);
                    canvasBitmapArrays2 = canvasBitmapArrays;
                } else {

                    canvasBitmapArrays2 = DotMatrixImageUtil.screenHorizontalToVertical(
                            DotMatrixImageUtil.twoToCanvasBitmapArrays(bitmapDataVo2.getTwoData(), height, width)
                    );

                    //清除非图表部分

                    //清除画布 上部分
                    int[][] cleanChartTopBitmapArrays = new int[startRowB - yLeftNumberHigh / 2]
                            [width];

                    DotMatrixCanvasUtil.mergeDotMatrices(canvasBitmapArrays2, cleanChartTopBitmapArrays,
                            0,
                            0);

                    //清除画布 下部分
                    int[][] cleanChartBottomBitmapArrays = new int[height - cleanChartTopBitmapArrays.length - cleanChartBitmapArrays.length]
                            [width];
                    DotMatrixCanvasUtil.mergeDotMatrices(canvasBitmapArrays2, cleanChartBottomBitmapArrays,
                            height - cleanChartBottomBitmapArrays.length,
                            0);

                    canvasBitmapArrays2 = DotMatrixCanvasUtil.mergeCanvasBitmap(width, height, 0, canvasBitmapArrays2, canvasBitmapArrays);

                }


                // 由于 硬件屏幕原因 需要将 将右屏的画布旋转
                twoBytes = DotMatrixImageUtil.canvasBitmapArraysToTwo(
                        DotMatrixImageUtil.screenHorizontalToVertical(
                                canvasBitmapArrays2
                        )
                );

                BitmapDataVo bitmapDataVo = signageFileUtil.dealWithBitmapDataVo(twoBytes, dId, path, TypeCodeUtil.CODE_ON + TypeCodeUtil.CODE_RIGHT + TypeCodeUtil.CODE_BUSINESS + "01", ter020201Vo.getTer020201InfoVo().getIsDefault());

                bitmapDataVoList.add(bitmapDataVo);
            }

        }


        //上传图片
        uploadFuture = latticeOSSUpload.uploadImages(dId, bitmapDataVoList);

        // 在上传操作完成后，才执行返回操作
        return uploadFuture.thenApplyAsync(v -> {
//                // 这里可以添加更多操作，如果需要
//                if (lock.isLocked() && lock.isHeldByCurrentThread()) {
//                    log.info("释放 点阵图生成锁 ");
//                    lock.unlock();
//                }
            return bitmapDataVoList; // 最终返回 bitmapDataVoList
        }, threadPoolTaskExecutor);


//    }catch (Exception e){
//        e.printStackTrace();
//        log.error("addDeviceType error:{}",e.getMessage());
//    }finally {
//        try {
//            if (lock.isLocked() && lock.isHeldByCurrentThread()) {
//                log.info("释放 点阵图生成锁 ");
//                lock.unlock();
//            }
//        }catch (Exception e){
//            log.error("释放锁 "+ e);
//        }
//
//    }

//    return CompletableFuture.completedFuture(null);


        //return bitmapDataVoList;

    }

    /**
     * 判断是否包含某项
     *
     * @param typeList
     * @param types
     * @return
     */
    private boolean isContainsType(List<Integer> typeList, Integer... types) {
//        if(typeList.contains(0)){
//            return true;
//        }
        for (Integer type : types) {
            if (typeList.contains(type)) {
                return true;
            }
        }
        return false;
    }


}
