Generator.java 2.99 KB
package com.viontech.storage.entity;

import cn.hutool.cache.CacheUtil;
import cn.hutool.cache.impl.TimedCache;
import cn.hutool.core.util.XmlUtil;
import cn.hutool.extra.spring.SpringUtil;
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 org.w3c.dom.Document;
import org.w3c.dom.Element;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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, String> STORAGE_CONFIG_ID_RESULT_CACHE = CacheUtil.newTimedCache(20 * 1000);
    private StorageConfigVo storageConfigVo;
    /** key: picConfigVoId */
    private Map<Long, PicConfigVo> picConfigVoMap;
    /** key: captionSetId */
    private Map<Long, List<CaptionVo>> captionMap;

    public Generator create(Long storageConfigId) {
        StorageConfigService storageConfigService = SpringUtil.getBean(StorageConfigService.class);
        PicConfigService picConfigService = SpringUtil.getBean(PicConfigService.class);
        CaptionService captionService = SpringUtil.getBean(CaptionService.class);

        StorageConfig storageConfig0 = storageConfigService.getById(storageConfigId);
        this.storageConfigVo = StorageConfigVo.copy(storageConfig0);
        List<Long> picConfigIds = storageConfigVo.getContexts().stream().map(Context::getPicConfigId).collect(Collectors.toList());

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

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

        this.captionMap = captions.stream().map(CaptionVo::copy).collect(Collectors.groupingBy(CaptionVo::getCaptionSetId, Collectors.toList()));

        return this;
    }

    public String build() {
        Document root = XmlUtil.createXml("StorageConfig");
        Element fileVersion = root.createElement("FileVersion");
        fileVersion.setTextContent(FILE_VERSION);
        Element standard = root.createElement("Standard");


        return XmlUtil.toStr(root, "GBK", false);
    }

}