Generator.java 30.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
package com.viontech.storage.entity;

import cn.hutool.cache.CacheUtil;
import cn.hutool.cache.impl.TimedCache;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Pair;
import cn.hutool.core.util.XmlUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.viontech.storage.config.Dict;
import com.viontech.storage.model.Caption;
import com.viontech.storage.model.PicConfig;
import com.viontech.storage.model.StorageConfig;
import com.viontech.storage.service.CaptionService;
import com.viontech.storage.service.PicConfigService;
import com.viontech.storage.service.StorageConfigService;
import com.viontech.storage.vo.CaptionVo;
import com.viontech.storage.vo.PicConfigVo;
import com.viontech.storage.vo.StorageConfigVo;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import java.time.Duration;
import java.util.*;
import java.util.stream.Collectors;

/**
 * .
 *
 * @author 谢明辉
 * @date 2021/11/9
 */


public class Generator {
    private static final String ROOT_ELEMENT_NAME = "StorageConfig";
    private static final String FILE_VERSION = "001-012-000-000";
    private static final TimedCache<Long, Generator> CACHE = CacheUtil.newTimedCache(Duration.ofSeconds(5).toMillis());
    private final Document document = XmlUtil.createXml(ROOT_ELEMENT_NAME);
    /** 来自 StorageConfig, 对应 Solution */
    private final List<Context> contexts;
    /** key: picConfigVoId */
    private final Map<Long, PicConfigVo> picConfigMap;
    /** key: captionSetId */
    private final Map<Long, List<CaptionVo>> captionSetMap;
    private String config;

    public Generator(Long storageConfigId) {
        Generator generator = CACHE.get(storageConfigId);
        if (generator == null) {
            StorageConfigService storageConfigService = SpringUtil.getBean(StorageConfigService.class);
            PicConfigService picConfigService = SpringUtil.getBean(PicConfigService.class);
            CaptionService captionService = SpringUtil.getBean(CaptionService.class);

            StorageConfig storageConfig0 = storageConfigService.getById(storageConfigId);
            this.contexts = StorageConfigVo.copy(storageConfig0).getContexts();
            if (contexts == null || contexts.size() == 0) {
                throw new RuntimeException("无法构建改配置");
            }

            List<Long> picConfigIds = contexts.stream().map(Context::getPicConfigId).collect(Collectors.toList());

            List<PicConfig> picConfigs = picConfigService.listByIds(picConfigIds);
            this.picConfigMap = new HashMap<>(picConfigs.size());
            ArrayList<Long> captionSetIds = new ArrayList<>();
            for (PicConfig item : picConfigs) {
                PicConfigVo copy = PicConfigVo.copy(item);
                picConfigMap.put(copy.getId(), copy);
                captionSetIds.add(copy.getCaptionSetId());
            }

            List<Caption> captions = captionService.list(captionService.query().in("caption_set_id", captionSetIds).getWrapper());

            this.captionSetMap = captions.stream().map(CaptionVo::copy).collect(Collectors.groupingBy(CaptionVo::getCaptionSetId, Collectors.toList()));
            CACHE.put(storageConfigId, this);
        } else {
            this.contexts = generator.contexts;
            this.picConfigMap = generator.picConfigMap;
            this.captionSetMap = generator.captionSetMap;
            this.config = generator.config;
        }
    }

    public String build() {
        if (this.config == null) {
            // 基本上算是固定写法
            Node rootNode = document.getFirstChild();
            rootNode.appendChild(createTextNode("FileVersion", FILE_VERSION));
            Element standard = document.createElement("Standard");
            rootNode.appendChild(standard);
            standard.appendChild(createTextNode("SolutionNum", String.valueOf(contexts.size())));
            Element switchPathConfig = document.createElement("SwitchPathConfig");
            switchPathConfig.appendChild(createTextNode("Flag", "1"));
            switchPathConfig.appendChild(createTextNode("RootPaths", "/VionData/RecordData/viondata0;/VionData/RecordData/viondata1;/VionData/RecordData/viondata2;/VionData/RecordData/viondata3;"));
            switchPathConfig.appendChild(createTextNode("VolumeThreshold", "2048"));
            switchPathConfig.appendChild(createTextNode("CountThreshold", "64"));
            standard.appendChild(switchPathConfig);

            // 开始组装 Solution
            for (Context context : contexts) {
                Node solution = buildSolution(context);
                if (solution != null) {
                    standard.appendChild(solution);
                }
            }
            this.config = XmlUtil.toStr(document, "GBK", false);
        }
        return this.config;
    }


    private Node buildSolution(Context context) {
        String conditions = combineConditions(context.getDataTypes());
        Integer order = context.getOrder();
        Long picConfigId = context.getPicConfigId();
        PicConfigVo picConfig = picConfigMap.get(picConfigId);
        if (picConfig == null || !picConfig.getEnabled()) {
            return null;
        }

        // 基本上算是固定写法
        Element solution = document.createElement("Solution" + order);
        solution.appendChild(createTextNode("SolutionName", picConfig.getName()));
        solution.appendChild(createTextNode("Conditions", conditions));
        Element iniInfo = document.createElement("IniInfo");
        iniInfo.appendChild(createTextNode("infContent", "(开始时间:YYYY-MM-DD HH:mm:SS_xxx)+(换行分隔符)+EndTime=+(结束时间:YYYY-MM-DD HH:mm:SS xxx)+(换行分隔符)+"));
        solution.appendChild(iniInfo);
        Element pathConfig = document.createElement("PathConfig");
        pathConfig.appendChild(createTextNode("PicRelativePath", "/+(设备编号)+/+(开始时间:YYYYMM/DD/HH)+/+(开始时间:YYYYMMDDHHmmSSxxx)+_+(车牌号码)+_+车道+(车道号)+.jpg;/+(设备编号)+/+(开始时间:YYYYMM/DD/HH)+/+(开始时间:YYYYMMDDHHmmSSxxx)+_+(车牌号码)+_+车道+(车道号)+_2+.jpg;/+(设备编号)+/+(开始时间:YYYYMM/DD/HH)+/+(开始时间:YYYYMMDDHHmmSSxxx)+_+(车牌号码)+_+车道+(车道号)+_3+.jpg;/+(设备编号)+/+(开始时间:YYYYMM/DD/HH)+/+(开始时间:YYYYMMDDHHmmSSxxx)+_+(车牌号码)+_+车道+(车道号)+_4+.jpg;/+(设备编号)+/+(开始时间:YYYYMM/DD/HH)+/+(开始时间:YYYYMMDDHHmmSSxxx)+_+(车牌号码)+_+车道+(车道号)+_5+.jpg;;"));
        pathConfig.appendChild(createTextNode("PicRootPath", "/kk/shuju;;;;;"));
        pathConfig.appendChild(createTextNode("INIRelativePath", "/+(设备编号)+/+(开始时间:YYYYMM/DD/HH)+/+(开始时间:YYYYMMDDHHmmSSxxx)+_+(车牌号码)+_+车道+(车道号)+.ini"));
        pathConfig.appendChild(createTextNode("INIRootPath", "/kk/ini;;;;;"));
        pathConfig.appendChild(document.createTextNode("VideoRelativePath"));
        pathConfig.appendChild(createTextNode("VideoRootPath", "/kk/video;;;;;"));
        solution.appendChild(pathConfig);

        // 组装图片属性和字幕
        Node picConfigNode = buildPicConfig(picConfig);
        solution.appendChild(picConfigNode);
        return solution;
    }

    private Node buildPicConfig(PicConfigVo picConfig) {
        // 没有自定义的地方,先固定写死
        Element configNode = document.createElement("PicConfig");
        configNode.appendChild(createTextNode("PicCount", "1"));
        configNode.appendChild(createTextNode("DigPicFlag", "1"));
        configNode.appendChild(createTextNode("scImagePrptyCnt", "-858993460"));
        configNode.appendChild(createTextNode("CoverCapFlag", "0"));
        configNode.appendChild(createTextNode("ImageQualityGlobalFlag", "0"));
        Element gamm = XmlUtil.appendChild(configNode, "Gamm");
        Element denoiser = XmlUtil.appendChild(configNode, "Denoiser");
        Element picQuality = XmlUtil.appendChild(configNode, "PicQuality");
        gamm.appendChild(createTextNode("GammFlag", "0"));
        gamm.appendChild(createTextNode("GammValue", "0.600000"));
        denoiser.appendChild(createTextNode("EnableFlag", "0"));
        denoiser.appendChild(createTextNode("Type", "3"));
        denoiser.appendChild(createTextNode("Param1", "3"));
        denoiser.appendChild(createTextNode("Param2", "3"));
        denoiser.appendChild(createTextNode("Param3", "0.000000"));
        denoiser.appendChild(createTextNode("Param4", "0.000000"));
        Element fixedSizeQuality = XmlUtil.appendChild(picQuality, "FixedSizeQuality");
        Element st200wImageQuality = XmlUtil.appendChild(picQuality, "st200WImageQuality");
        Element st500wImageQuality = XmlUtil.appendChild(picQuality, "st500WImageQuality");
        fixedSizeQuality.appendChild(createTextNode("DayQuality", "500"));
        fixedSizeQuality.appendChild(createTextNode("NightQuality", "500"));
        st200wImageQuality.appendChild(createTextNode("DayQuality", "80"));
        st200wImageQuality.appendChild(createTextNode("NightQuality", "80"));
        st500wImageQuality.appendChild(createTextNode("DayQuality", "60"));
        st500wImageQuality.appendChild(createTextNode("NightQuality", "60"));
        picQuality.appendChild(createTextNode("LightThreshold", "40"));
        picQuality.appendChild(createTextNode("UseFixedSize", "0"));

        Element pic1 = XmlUtil.appendChild(configNode, "Pic1");
        pic1.appendChild(createTextNode("OriPicCount", String.valueOf(picConfig.calPicCount())));
        pic1.appendChild(createTextNode("DirectSaveFlag", "0"));

        PicConfigGenerator picConfigGenerator = new PicConfigGenerator(picConfig, captionSetMap.get(picConfig.getCaptionSetId()));
        PicNode[] picNodes = picConfigGenerator.getPicNodes();
        Element captionsElement = XmlUtil.appendChild(pic1, "Captions");
        int captionCount = 0;
        for (int i = 0; i < picNodes.length; i++) {
            PicNode item = picNodes[i];
            Element oriPic = XmlUtil.appendChild(pic1, "OriPic" + (i + 1));
            XmlUtil.appendChild(oriPic, "Content").setTextContent(item.getPicTypeName());
            XmlUtil.appendChild(oriPic, "Type").setTextContent("0");
            Element srcCoordinate = XmlUtil.appendChild(oriPic, "SrcCoordinate");
            Element desCoordinate = XmlUtil.appendChild(oriPic, "DesCoordinate");
            XmlUtil.appendChild(srcCoordinate, "left").setTextContent("0");
            XmlUtil.appendChild(srcCoordinate, "top").setTextContent("0");
            XmlUtil.appendChild(srcCoordinate, "width").setTextContent(item.getPicTypeName() + "宽");
            XmlUtil.appendChild(srcCoordinate, "height").setTextContent(item.getPicTypeName() + "高");
            XmlUtil.appendChild(desCoordinate, "left").setTextContent(item.getX());
            XmlUtil.appendChild(desCoordinate, "top").setTextContent(item.getY());
            XmlUtil.appendChild(desCoordinate, "width").setTextContent("全景1宽");
            XmlUtil.appendChild(desCoordinate, "height").setTextContent("全景1高");

            // 字幕
            CaptionNode captionNode = item.getCaptionNode();
            for (int j = 0; j < 4; j++) {
                CaptionVo caption;
                String content;
                String height;
                if (j == 0 && captionNode.getTop() != null) {
                    caption = captionNode.getTop();
                    content = captionNode.getTopContent();
                    height = item.getMaxTopCaptionHeight();
                } else if (j == 1 && captionNode.getBottom() != null) {
                    caption = captionNode.getBottom();
                    content = captionNode.getBottomContent();
                    height = item.getMaxBottomCaptionHeight();
                } else if (j == 2 && captionNode.getSecond() != null) {
                    caption = captionNode.getSecond();
                    content = captionNode.getSecondContent();
                    height = String.valueOf(captionNode.getSecondHeight());
                } else if (j == 3 && captionNode.getThird() != null) {
                    caption = captionNode.getThird();
                    content = captionNode.getThirdContent();
                    height = String.valueOf(captionNode.getThirdHeight());
                } else {
                    continue;
                }

                captionCount++;
                Element ele = XmlUtil.appendChild(captionsElement, "Caption" + captionCount);
                XmlUtil.appendChild(ele, "Content").setTextContent(content);
                XmlUtil.appendChild(ele, "bgColor").setTextContent(caption.getBgColor().toString());
                XmlUtil.appendChild(ele, "Font").setTextContent(caption.getFont().toString());
                XmlUtil.appendChild(ele, "FontSize").setTextContent(caption.getFontSize().toString());
                XmlUtil.appendChild(ele, "fgColor").setTextContent(caption.getFgColor().toString());
                XmlUtil.appendChild(ele, "ShadowColor").setTextContent("4");
                XmlUtil.appendChild(ele, "ShadowSize").setTextContent("0");
                XmlUtil.appendChild(ele, "bgTransparency").setTextContent(caption.getBgTransparency().toString());
                Element coordinateElement = XmlUtil.appendChild(ele, "Coordinate");
                // 坐标需要根据 positionType 来算
                Integer positionType = caption.getPositionType();
                String y = item.getY();
                if (positionType == 1) {
                    y = y + "+全景1高";
                } else if (positionType == 0) {
                    y = y + "-" + height;
                } else if (positionType == 3) {
                    y = y + "+全景1高-" + height;
                }
                XmlUtil.appendChild(coordinateElement, "left").setTextContent(item.getX());
                XmlUtil.appendChild(coordinateElement, "top").setTextContent(y);
                XmlUtil.appendChild(coordinateElement, "width").setTextContent("全景1宽");
                XmlUtil.appendChild(coordinateElement, "height").setTextContent(height);

            }

        }
        // 整张合成图的字幕处理
        CaptionNode mainCaptionNode = picConfigGenerator.getMainCaptionNode();
        for (int j = 0; j < 4; j++) {
            CaptionVo caption;
            String content;
            String height;
            if (j == 0 && mainCaptionNode.getTop() != null) {
                caption = mainCaptionNode.getTop();
                content = mainCaptionNode.getTopContent();
                height = String.valueOf(mainCaptionNode.getTopHeight());
            } else if (j == 1 && mainCaptionNode.getBottom() != null) {
                caption = mainCaptionNode.getBottom();
                content = mainCaptionNode.getBottomContent();
                height = String.valueOf(mainCaptionNode.getBottomHeight());
            } else if (j == 2 && mainCaptionNode.getSecond() != null) {
                caption = mainCaptionNode.getSecond();
                content = mainCaptionNode.getSecondContent();
                height = String.valueOf(mainCaptionNode.getSecondHeight());
            } else if (j == 3 && mainCaptionNode.getThird() != null) {
                caption = mainCaptionNode.getThird();
                content = mainCaptionNode.getThirdContent();
                height = String.valueOf(mainCaptionNode.getThirdHeight());
            } else {
                continue;
            }

            captionCount++;
            Element ele = XmlUtil.appendChild(captionsElement, "Caption" + captionCount);
            XmlUtil.appendChild(ele, "Content").setTextContent(content);
            XmlUtil.appendChild(ele, "bgColor").setTextContent(caption.getBgColor().toString());
            XmlUtil.appendChild(ele, "Font").setTextContent(caption.getFont().toString());
            XmlUtil.appendChild(ele, "FontSize").setTextContent(caption.getFontSize().toString());
            XmlUtil.appendChild(ele, "fgColor").setTextContent(caption.getFgColor().toString());
            XmlUtil.appendChild(ele, "ShadowColor").setTextContent("4");
            XmlUtil.appendChild(ele, "ShadowSize").setTextContent("0");
            XmlUtil.appendChild(ele, "bgTransparency").setTextContent(caption.getBgTransparency().toString());
            Element coordinateElement = XmlUtil.appendChild(ele, "Coordinate");
            // 坐标需要根据 positionType 来算
            Integer positionType = caption.getPositionType();
            String y;
            if (positionType == 0) {
                y = "0";
            } else if (positionType == 1) {
                PicNode lastNode = picNodes[picNodes.length - 1];
                y = lastNode.getY() + "+全景1高+" + lastNode.getMaxBottomCaptionHeight();
            } else if (positionType == 2) {
                y = picNodes[0].getY();
            } else if (positionType == 3) {
                PicNode lastNode = picNodes[picNodes.length - 1];
                y = lastNode.getY() + "+全景1高-" + height;
            } else {
                throw new IllegalArgumentException(positionType + "");
            }
            XmlUtil.appendChild(coordinateElement, "left").setTextContent("0");
            XmlUtil.appendChild(coordinateElement, "top").setTextContent(y);
            XmlUtil.appendChild(coordinateElement, "width").setTextContent(picConfigGenerator.getLayoutType() / 10 + "*全景1宽");
            XmlUtil.appendChild(coordinateElement, "height").setTextContent(height);

        }
        XmlUtil.appendChild(captionsElement, "Count").setTextContent(String.valueOf(captionCount));
        return configNode;
    }

    private Element createTextNode(String name, String text) {
        Element node = document.createElement(name);
        node.setTextContent(text);
        return node;
    }

    private String combineConditions(List<String> dataTypes) {
        return dataTypes.stream().map(x -> "数据类型=" + x).collect(Collectors.joining("+"));
    }

    /**
     * 一个图片属性会有多个图片节点
     * 按照类型不同排列
     */
    public @Getter
    @Setter
    @Accessors(chain = true)
    static class PicNode {
        private final PicConfigGenerator generator;
        /** 图片类型代码 */
        private final int picType;
        /** 图片类型名称 */
        private final String picTypeName;
        private final int index;
        private CaptionNode captionNode = new CaptionNode();
        private String x;
        private String y;

        public PicNode(PicConfigGenerator generator, int picType, int index) {
            this.generator = generator;
            this.picTypeName = Dict.INSTANCE.picType.get(picType);
            this.picType = picType;
            this.index = index;
        }

        /**
         * 计算该图片在合成图上的 X 坐标
         */
        public String getX() {
            if (this.x != null) {
                return this.x;
            }
            PicNode left = getLeft();
            int colIndex = index % (generator.getLayoutType() / 10);
            switch (colIndex) {
                case 0:
                    this.x = "0";
                    break;
                case 1:
                case 2:
                case 3:
                case 4:
                case 5:
                    this.x = "0".equals(left.x) ? "全景1宽" : left.x + "+" + "全景1宽";
                    break;
                default:
                    throw new RuntimeException("no such type");
            }
            return this.x;
        }

        /**
         * 计算该图片在合成图上的 Y 坐标
         */
        public String getY() {
            if (this.y != null) {
                return this.y;
            }
            PicNode upside = this.getUpside();
            String mainPaddingTop = generator.paddingTop();
            String paddingTop = paddingTop();

            int rowIndex = index / (generator.getLayoutType() / 10);
            switch (rowIndex) {
                case 0:
                    this.y = paddingTop + "+" + mainPaddingTop;
                    break;
                case 1:
                case 2:
                case 3:
                case 4:
                case 5:
                    this.y = upside.y + "+" + "全景1高" + "+" + paddingTop;
                    break;
                default:
                    throw new RuntimeException("no such type");
            }

            return this.y;
        }

        /**
         * 计算 Y 坐标时需要的填充值
         * 一行内每个图片节点的外上边缘字幕的高度加上该节点上层节点的外下边缘字母的高度的最大值作为结果
         * 例如
         * <ul>
         *     <li>A B C</li>
         *     <li>D E F</li>
         * </ul>
         * D,E,F 的 paddingTop = {@code max(A外下字幕高度 + D外上字幕高度, B外下字幕高度 + E外上字幕高度, C外下字幕高度 + F外上字幕高度)}
         */
        private String paddingTop() {
            PicNode upside = getUpside();
            String a = Optional.ofNullable(upside).map(PicNode::getMaxBottomCaptionHeight).orElse("0");
            String b = getMaxTopCaptionHeight();
            return a + "+" + b;
        }

        private String getMaxTopCaptionHeight() {
            PicNode node = this;
            int maxTop = Integer.MIN_VALUE;
            while (node.getLeft() != null) {
                node = node.getLeft();
            }
            while (node != null) {
                int topHeight = node.getCaptionNode().getTopHeight() == null ? 0 : node.getCaptionNode().getTopHeight();
                maxTop = Math.max(maxTop, topHeight);
                node = node.getRight();
            }
            return String.valueOf(maxTop);
        }

        private String getMaxBottomCaptionHeight() {
            PicNode node = this;
            int maxBottom = Integer.MIN_VALUE;
            while (node.getLeft() != null) {
                node = node.getLeft();
            }
            while (node != null) {
                int bottomHeight = node.getCaptionNode().getBottomHeight() == null ? 0 : node.getCaptionNode().getBottomHeight();
                maxBottom = Math.max(maxBottom, bottomHeight);
                node = node.getRight();
            }
            return String.valueOf(maxBottom);
        }

        /**
         * 获取一个图片节点的左侧图片节点
         */
        public PicNode getLeft() {
            int colIndex = index % (generator.getLayoutType() / 10);
            if (colIndex == 0) {
                return null;
            } else {
                return generator.picNodes[index - 1];
            }
        }

        /**
         * 获取一个图片节点的右侧图片节点
         */
        public PicNode getRight() {
            int colIndex = index % (generator.getLayoutType() / 10);
            int maxColIndex = generator.getLayoutType() / 10 - 1;
            if (colIndex == maxColIndex) {
                return null;
            } else {
                return generator.picNodes[index + 1];
            }
        }

        /**
         * 获取一个图片节点的上层图片节点
         */
        public PicNode getUpside() {
            int colNum = generator.getLayoutType() / 10;
            int rowIndex = index / (colNum);
            if (rowIndex == 0) {
                return null;
            } else {
                return generator.picNodes[index - colNum];
            }
        }
    }

    /**
     * 字幕属性和图片属性的生成器
     * 用来生成图片的坐标,字幕的高度,字幕的内容
     */
    public @Getter
    @Setter
    @Accessors(chain = true)
    static class PicConfigGenerator {

        private final CaptionNode mainCaptionNode = new CaptionNode();

        private final PicNode[] picNodes;
        /** 图片排列方式类型 */
        private final int layoutType;

        /**
         * @param picConfigVo 图片属性
         * @param captionVos  图片属性对应的所有字幕
         */
        public PicConfigGenerator(PicConfigVo picConfigVo, List<CaptionVo> captionVos) {
            List<Context> contexts = picConfigVo.getContexts();
            layoutType = picConfigVo.getType();
            picNodes = new PicNode[contexts.size()];
            for (int i = 0; i < picNodes.length; i++) {
                Context context = contexts.get(i);
                picNodes[i] = new PicNode(this, context.getType(), i);
            }

            for (CaptionVo captionVo : captionVos) {
                // 9 代表合成图
                if (captionVo.getPicType() == 9) {
                    mainCaptionNode.add(captionVo);
                } else {
                    for (PicNode picNode : picNodes) {
                        if (picNode.getPicType() == captionVo.getPicType()) {
                            picNode.getCaptionNode().add(captionVo);
                        }
                    }
                }
            }
        }

        public String paddingTop() {
            if (mainCaptionNode.getTopHeight() == null) {
                return "0";
            } else {
                return String.valueOf(mainCaptionNode.getTopHeight());
            }
        }
    }

    /**
     * 字幕节点
     * 每个图片节点对应一个字幕节点
     */
    public @Getter
    @Setter
    static final class CaptionNode {
        /** 图片外上边缘字幕的配置 */
        private CaptionVo top;
        /** 图片内上边缘字幕的配置 */
        private CaptionVo second;
        /** 图片内下边缘字幕的配置 */
        private CaptionVo third;
        /** 图片外下边缘字幕的配置 */
        private CaptionVo bottom;

        /** 图片外上边缘字幕的内容 */
        private String topContent;
        /** 图片内上边缘字幕的内容 */
        private String secondContent;
        /** 图片内下边缘字幕的内容 */
        private String thirdContent;
        /** 图片外下边缘字幕的内容 */
        private String bottomContent;

        /** 图片外上边缘字幕的高度 */
        private Integer topHeight;
        /** 图片内上边缘字幕的高度 */
        private Integer secondHeight;
        /** 图片内下边缘字幕的高度 */
        private Integer thirdHeight;
        /** 图片外下边缘字幕的高度 */
        private Integer bottomHeight;

        /**
         * 根据坐标位置类型复制不同的字段,如果是同一类就会覆盖
         * 目前没写坐标类型为 自定义 的逻辑,相信自定义坐标也不会用上,如果有的话实现也很简单
         */
        public void add(CaptionVo vo) {
            Integer positionType = vo.getPositionType();
            Integer fontSize = vo.getFontSize();
            String dateFormat = (vo.getMillisecond() != null && vo.getMillisecond()) ? vo.getDateFormat() + ".xxx" : vo.getDateFormat();
            Pair<String, Integer> pair = combineCaptionContent(vo.getPicType(), vo.getContexts(), dateFormat);
            Integer wrapCount = pair.getValue();
            int height = (wrapCount + 1) * fontSize + 10;
            switch (positionType) {
                case 0:
                    this.top = vo;
                    this.topContent = pair.getKey();
                    this.topHeight = height;
                    break;
                case 1:
                    this.bottom = vo;
                    this.bottomContent = pair.getKey();
                    this.bottomHeight = height;
                    break;
                case 2:
                    this.second = vo;
                    this.secondContent = pair.getKey();
                    this.secondHeight = height;
                    break;
                case 3:
                    this.third = vo;
                    this.thirdContent = pair.getKey();
                    this.secondHeight = height;
                    break;
                default:
                    break;
            }
        }

        /**
         * 组合字幕内容
         *
         * @param picType    图片类型
         * @param contexts   内容列表
         * @param dateFormat 时间格式
         *
         * @return key: 字幕内容, value: 换行了多少次,用来计算字幕所占的高度
         */
        private Pair<String, Integer> combineCaptionContent(Integer picType, List<Context> contexts, String dateFormat) {
            if (CollectionUtil.isEmpty(contexts)) {
                return Pair.of(null, null);
            }
            String picTypeName = Dict.INSTANCE.picType.get(picType);
            StringBuilder sb = new StringBuilder();
            int wrapCount = 0;
            for (Context context : contexts) {
                Integer type = context.getType();
                if (type != 16) {
                    sb.append(Dict.INSTANCE.captionType.get(type)).append(":+");
                }
                switch (type) {
                    case 1:
                        if (picTypeName.contains("特写")) {
                            picTypeName = "特写图";
                        }
                        sb.append("(").append(picTypeName).append("时间:").append(dateFormat).append(")");
                        break;
                    case 9:
                        sb.append("(用户车辆类型1)");
                        break;
                    case 10:
                        sb.append("(用户车身颜色1)");
                        break;
                    case 11:
                        sb.append("(车辆子品牌)");
                        break;
                    case 15:
                        sb.append("(合成图1防伪码)");
                        break;
                    case 16:
                        sb.append(context.getContent());
                        break;
                    case 17:
                        sb.append("(违法类型代码)");
                        break;
                    default:
                        sb.append("(").append(Dict.INSTANCE.captionType.get(type)).append(")");
                }

                if (context.getWrap()) {
                    wrapCount++;
                    sb.append("+(换行分隔符)");
                }
                sb.append("+");
            }
            sb.setLength(sb.length() - 1);
            return Pair.of(sb.toString(), wrapCount);
        }
    }
}