/*
* Copyright (c) 京北方信息技术股份有限公司 Corporation 2022 . All rights reserved.
*
*/
package net.northking.cctp.scriptcase.api.component.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import net.northking.cctp.common.cache.EntityNameCache;
import net.northking.cctp.common.enums.FileBusinessTypeEnum;
import net.northking.cctp.common.exception.PlatformRuntimeException;
import net.northking.cctp.common.security.authentication.NKSecurityContext;
import net.northking.cctp.common.util.UUIDUtil;

import net.northking.cctp.scriptcase.api.atuScript.service.AtuScriptInfoApiService;
import net.northking.cctp.scriptcase.constants.ScriptConstant;
import net.northking.cctp.scriptcase.db.entity.*;
import net.northking.cctp.scriptcase.db.service.*;
import net.northking.cctp.scriptcase.dto.comLib.LibraryBusComInfoDetailDto;
import net.northking.cctp.scriptcase.dto.component.*;
import net.northking.cctp.scriptcase.enums.ScriptCaseAppError;
import net.northking.cctp.scriptcase.feign.AttachmentFeignClient;
import net.northking.cctp.scriptcase.feign.PlatformFeignClient;
import net.northking.cctp.scriptcase.tools.RuleDefinitionTool;
import net.northking.cctp.scriptcase.tools.ScriptCaseUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 业务组件表_发布服务实现类
 *
 * @author maven-cctp-plugin
 * @since 1.0
 */

@Service
@ConditionalOnMissingBean(name = "ReBusComponentInfoApiServiceImpl")
public class ReBusComponentInfoApiServiceImpl implements ReBusComponentInfoApiService
{
  /**
   * 日志
   */
  private final static Logger logger = LoggerFactory.getLogger(ReBusComponentInfoApiServiceImpl.class);

  @Autowired
  private MdBusComponentInfoService mdBusComponentInfoService;

  @Autowired
  private MdComponentParamService mdComponentParamService;

  @Autowired
  private ScriptCaseUtils scriptCaseUtils;
  @Autowired
  private BusComponentTreeService busComponentTreeService;
  @Autowired
  private LibraryBusComTreeService libraryBusComTreeService;
  @Autowired
  private LibraryBusComInfoService libraryBusComInfoService;
  @Autowired
  private PlatformFeignClient platformFeignClient;
  @Autowired
  private HisBusComponentInfoApiService hisApiService;
  @Autowired
  private EntityNameCache entityNameCache;
  @Autowired
  private AttachmentFeignClient attachmentFeignClient;
  @Autowired
  private AtuScriptInfoApiService atuScriptInfoApiService;


  /**
   * 脚本录制生产业务组件
   * @param inputDto
   * @return
   */
  @Override
  @Transactional
  public ReBusComponentInfoDetailDto genBusComponent(ReBusComponentInfoAddDto inputDto) {
    ReBusComponentInfoDetailDto result = new ReBusComponentInfoDetailDto();
    //先校验名称是否存在
    MdBusComponentInfo check = new MdBusComponentInfo();
    check.setTreeId(inputDto.getTreeId());
    check.setIsDeleted(0);
    check.setTenantId(NKSecurityContext.getTenantId());
    check.setProjectId(inputDto.getProjectId());
    check.setComName(inputDto.getComName());
    if(mdBusComponentInfoService.count(check)>0){
      throw new PlatformRuntimeException(ScriptCaseAppError.BUS_COM_NAME_EXIT);
    }
    String id = UUIDUtil.create32UUID();
    String scriptContent = inputDto.getScriptContent();
    JSONObject jsonObject = JSON.parseObject(scriptContent, JSONObject.class);
    Integer scriptType = inputDto.getComType();
    FileBusinessTypeEnum businessCode = null;
    if (scriptType.equals(2) || scriptType.equals(3)) {
      businessCode = FileBusinessTypeEnum.PC_RECORD_SCREENSHOT;
    } else if (scriptType.equals(4) || scriptType.equals(5)) {
      businessCode = FileBusinessTypeEnum.MOBILE_RECORD_SCREENSHOT;
    }
    logger.info("生成组件类型：{}, 文件businessCode：{}", scriptType, businessCode);
    atuScriptInfoApiService.getIamgeUrlByJsonArray(id, jsonObject.get("commands"),  new HashMap<>(), true, businessCode);
    scriptContent = jsonObject.toJSONString();
    String path = scriptCaseUtils.generateScriptFileAndUpload(scriptContent, id, FileBusinessTypeEnum.BUSINESS_COMPONENT_SCRIPT);
    if(path==null){
      throw new PlatformRuntimeException(ScriptCaseAppError.JSON_FILE_UPLOAD_FAILED);
    }
    inputDto.setScriptContent(path);//用脚本文件地址替换json
    //同时保存编辑表，发布表以及例是表
    MdBusComponentInfo comMd = new MdBusComponentInfo();
    BeanUtils.copyProperties(inputDto,comMd);
    comMd.setIsDeleted(0);
    comMd.setComVersion(ScriptConstant.COM_DEFAULT_VERSION);//默认开始版本号
    comMd.setId(id);
    comMd.setIsDeprecated(0);
    comMd.setPrincipalId(NKSecurityContext.getUserId());//默认负责人为创建人
    comMd.setSharedFlag(0);//默认为未共享
    mdBusComponentInfoService.insert(comMd);
    MdComponentParam paramMd = new MdComponentParam();
    paramMd.setId(id);
    paramMd.setParamJson(inputDto.getParamJson());
    paramMd.setComJson(inputDto.getComJson());
    mdComponentParamService.insert(paramMd);
    result.setId(id);
    result.setComVersion(comMd.getComVersion());
    result.setScriptContent(path);
    return result;
  }
  @Override
  public List<RecordComTreeDto> getComTreeByType(RecordComTreeQueryDto query) {
    BusComponentTree node = busComponentTreeService.findByPrimaryKey(query.getTreeId());
    LibraryBusComTree busComTree = libraryBusComTreeService.findByPrimaryKey(query.getTreeId());
    int type;
    if(ScriptConstant.TREE_TOP.equals(query.getTreeId())){
      type = 0;
    }else if(ScriptConstant.TREE_TOP_ONE.equals(query.getTreeId())){
      type = 1;
    }else if(ScriptConstant.TREE_TOP_TWO.equals(query.getTreeId())){
      type = 3;
    }else if(busComTree !=null){
      type = 2;
    }else if(node !=null){
      type = 4;
    }else {
      throw new PlatformRuntimeException(ScriptCaseAppError.TREE_NOT_EXIT);
    }
    List<RecordComTreeDto> result = new ArrayList<>();
    List<Integer> types=new ArrayList<>();
    types.add(0);
    types.add(1);
    if(query.getRecordType()>1){
      types.add(query.getRecordType());
    }
    if(type==0){ //顶层节点
      getTopTree(result);
    }else if(type<3){//公共组件库
        getTop01Tree(result, query, types, busComTree);
    }else{//私有组件库
      getTop02Tree(result,query,types,node);
    }
    return result;
  }
  /**
   * 组装公共组件库目录
   * @param result
   */
  private void getTop01Tree(List<RecordComTreeDto> result,RecordComTreeQueryDto query,List<Integer> types,LibraryBusComTree node){
    if(ScriptConstant.TREE_TOP_ONE.equals(query.getTreeId())){
      node = new LibraryBusComTree();
      node.setIsLeaf(0);
      query.setTreeId(ScriptConstant.TREE_TOP);
    }
    if (node != null && node.getIsLeaf() == 0) {
      LibraryBusComInfo comQuery = new LibraryBusComInfo();
      comQuery.setComType(query.getRecordType());

      List<String> list = libraryBusComTreeService.getComTreePathByLib(comQuery,types);
      if(list.size()>0){
        List<Object> trees = new ArrayList<>();
        for (String path : list) {
          if(path.contains(query.getTreeId())){
            trees.add(path.split(query.getTreeId()+"/")[1].substring(0,32));
          }
        }
        List<LibraryBusComTree> treeList = libraryBusComTreeService.findByPrimaryKeys(trees);
        for (LibraryBusComTree one : treeList) {
          RecordComTreeDto dto = new RecordComTreeDto();
          dto.setTreeId(one.getId());
          dto.setComFlag(ScriptConstant.STR_ONE);//业务组件
          dto.setIsCom(ScriptConstant.STR_ZERO);//目录
          dto.setIsLeaf(ScriptConstant.STR_ZERO);//目录永远是非叶子节点
          dto.setMemo(one.getMemo());
          dto.setTreeName(one.getTreeName());
          dto.setTreeAlias(one.getTreeName());
          result.add(dto);
        }
      }
    }else{
      //查业务组件
      LibraryBusComInfo comQuery = new LibraryBusComInfo();
      comQuery.setComType(query.getRecordType());
      comQuery.setTreeId(query.getTreeId());
      List<LibraryBusComInfoDetailDto> infoList = libraryBusComInfoService.queryComListByType(comQuery,types);
      List<Object> ids = new ArrayList<>();
      infoList.forEach(one->{ids.add(one.getId());});
      List<MdBusComponentInfo> infos = mdBusComponentInfoService.findByPrimaryKeys(ids);
      Map<String,MdBusComponentInfo> infoMap = new HashMap<>();
      infos.forEach(one->{infoMap.put(one.getId(),one);});
      for (LibraryBusComInfoDetailDto one : infoList) {
        MdBusComponentInfo oneInfo = infoMap.get(one.getId());
        RecordComTreeDto dto = new RecordComTreeDto();
        dto.setTreeId(one.getId());
        dto.setComFlag(ScriptConstant.STR_ONE);//业务组件
        dto.setIsCom(ScriptConstant.STR_ONE);//组件
        dto.setIsLeaf(ScriptConstant.STR_ONE);//组件永远是叶子节节点
        dto.setMemo(oneInfo.getMemo());
        dto.setTreeName(oneInfo.getComName());
        dto.setTreeAlias(oneInfo.getComName());
        dto.setParamJson(one.getParamJson());
        dto.setVersion(oneInfo.getComVersion());
        dto.setScriptContent(oneInfo.getScriptContent());
        result.add(dto);
      }
    }
  }
  /**
   * 组装系统组件库目录
   * @param result
   */
  private void getTop02Tree(List<RecordComTreeDto> result,RecordComTreeQueryDto query,List<Integer> types,BusComponentTree node){
    if(ScriptConstant.TREE_TOP_TWO.equals(query.getTreeId())){
      node = new BusComponentTree();
      node.setIsLeaf(0);
      query.setTreeId(ScriptConstant.TREE_TOP);
    }
    if (node.getIsLeaf() == 0) {
      MdBusComponentInfo comQuery = new MdBusComponentInfo();
      comQuery.setComType(query.getRecordType());
      comQuery.setIsDeleted(0);
      comQuery.setProjectId(query.getProjectId());

      List<String> list = busComponentTreeService.getComTreePathByMd(comQuery,types);
      if(list.size()>0){
        List<Object> trees = new ArrayList<>();
        for (String path : list) {
          if(path.contains(query.getTreeId())){
            trees.add(path.split(query.getTreeId()+"/")[1].substring(0,32));
          }
        }
        List<BusComponentTree> treeList = busComponentTreeService.findByPrimaryKeys(trees);
        for (BusComponentTree one : treeList) {
          RecordComTreeDto dto = new RecordComTreeDto();
          dto.setTreeId(one.getId());
          dto.setComFlag(ScriptConstant.STR_ONE);//业务组件
          dto.setIsCom(ScriptConstant.STR_ZERO);//目录
          dto.setIsLeaf(ScriptConstant.STR_ZERO);//目录永远是非叶子节点
          dto.setMemo(one.getMemo());
          dto.setTreeName(one.getTreeName());
          dto.setTreeAlias(one.getTreeName());
          result.add(dto);
        }
      }
    }else{
      //查业务组件
      MdBusComponentInfo comQuery = new MdBusComponentInfo();
      comQuery.setComType(query.getRecordType());
      comQuery.setProjectId(query.getProjectId());
      comQuery.setTreeId(query.getTreeId());
      List<MdBusComponentInfoDetailDto> infoList = mdBusComponentInfoService.queryComListByType(comQuery,types);
      for (MdBusComponentInfoDetailDto one : infoList) {
        RecordComTreeDto dto = new RecordComTreeDto();
        dto.setTreeId(one.getId());
        dto.setComFlag(ScriptConstant.STR_ONE);//业务组件
        dto.setIsCom(ScriptConstant.STR_ONE);//组件
        dto.setIsLeaf(ScriptConstant.STR_ONE);//组件永远是叶子节节点
        dto.setMemo(one.getMemo());
        dto.setTreeName(one.getComName());
        dto.setTreeAlias(one.getComName());
        dto.setParamJson(one.getParamJson());
        dto.setVersion(one.getComVersion());
        dto.setScriptContent(one.getScriptContent());
        result.add(dto);
      }
    }
  }
  /**
   * 组装第二级默认目录
   * @param result
   */
  private void getTopTree(List<RecordComTreeDto> result){
    RecordComTreeDto dto = new RecordComTreeDto();
    dto.setTreeId(ScriptConstant.TREE_TOP_ONE);
    dto.setComFlag(ScriptConstant.STR_ONE);//业务组件
    dto.setIsCom(ScriptConstant.STR_ZERO);//目录
    dto.setIsLeaf(ScriptConstant.STR_ZERO);
    dto.setTreeName(ScriptConstant.PLATFORM_COM);
    dto.setTreeAlias(ScriptConstant.PLATFORM_COM);
    result.add(dto);
    RecordComTreeDto dto1 = new RecordComTreeDto();
    dto1.setTreeId(ScriptConstant.TREE_TOP_TWO);
    dto1.setComFlag(ScriptConstant.STR_ONE);//业务组件
    dto1.setIsCom(ScriptConstant.STR_ZERO);//目录
    dto1.setIsLeaf(ScriptConstant.STR_ZERO);
    dto1.setTreeName(ScriptConstant.PRO_COM);
    dto1.setTreeAlias(ScriptConstant.PRO_COM);
    result.add(dto1);
  }

  @Override
  public RecordComTreeLikeDto getComTreeForLike(RecordComTreeQueryDto query) {
    //最近使用
    List<RecordComTreeDto> hisList = new ArrayList<>();
    //业务组件
    List<RecordComTreeDto> busTreeList = new ArrayList<>();

    List<Integer> types=new ArrayList<>();
    types.add(0);
    types.add(1);
    if(query.getRecordType()>1){
      types.add(query.getRecordType());
    }

    //业务组件查询-私有
    MdBusComponentInfo comQuery = new MdBusComponentInfo();
    comQuery.setComType(query.getRecordType());
    comQuery.setIsDeleted(0);
    comQuery.setProjectId(query.getProjectId());
    comQuery.setComName(query.getSelectName());

    List<MdBusComponentInfoDetailDto> allList = mdBusComponentInfoService.queryComListByType(comQuery,types);
    Map<String,String> treeMap = new HashMap<>();
    Map<String,List<RecordComTreeDto>> comTreeMap = new HashMap<>();
    for (MdBusComponentInfoDetailDto one : allList) {
      treeMap.put(one.getTreeId(),"");
      if (comTreeMap.containsKey(one.getTreeId())) {
        comTreeMap.get(one.getTreeId()).add(dataTransCom(one));
      }else{
        List<RecordComTreeDto> comList = new ArrayList<>();
        comList.add(dataTransCom(one));
        comTreeMap.put(one.getTreeId(),comList);
      }
    }
    //业务组件树-私有
    comQuery.setComName(null);
    List<BusComponentTree> leafs =busComponentTreeService.findByPrimaryKeys(findKeys(treeMap));
    treeMap.clear();
    List<String> allPath = busComponentTreeService.getComTreePath(comQuery,types);
    for (String one : allPath) {
      String[] paths = one.split(RuleDefinitionTool.getTreeSplit());
      for (String path : paths) {
        treeMap.put(path,"");
      }
    }
    comQuery.setComName(query.getSelectName());
    leafs.addAll(busComponentTreeService.findComTreeListLike(comQuery,findKeys(treeMap)));
    treeMap.clear();
    for (BusComponentTree leaf : leafs) {
      String[] paths = leaf.getIdPath().split(RuleDefinitionTool.getTreeSplit());
      for (String path : paths) {
        treeMap.put(path,"");
      }
    }
    List<BusComponentTree> allTree =busComponentTreeService.findByPrimaryKeys(findKeys(treeMap));
    RecordComTreeDto busTree = genTreeList(allTree,comTreeMap);

    //业务组件查询-公有
    LibraryBusComInfo comQueryBus = new LibraryBusComInfo();
    comQueryBus.setComType(query.getRecordType());
    comQueryBus.setComName(query.getSelectName());

    List<LibraryBusComInfoDetailDto> libAllList = libraryBusComInfoService.queryComListByType(comQueryBus,types);
    Map<String,String> libTreeMap = new HashMap<>();
    Map<String,List<RecordComTreeDto>> comTreeMapLib = new HashMap<>();
    for (LibraryBusComInfoDetailDto one : libAllList) {
      libTreeMap.put(one.getTreeId(),"");
      if (comTreeMapLib.containsKey(one.getTreeId())) {
        comTreeMapLib.get(one.getTreeId()).add(dataTransCom(one));
      }else{
        List<RecordComTreeDto> comList = new ArrayList<>();
        comList.add(dataTransCom(one));
        comTreeMapLib.put(one.getTreeId(),comList);
      }
    }
    //业务组件树-公有
    List<LibraryBusComTree> libLeafs =libraryBusComTreeService.findByPrimaryKeys(findKeys(libTreeMap));
    libTreeMap.clear();
    List<String> libAllPath = libraryBusComTreeService.getComTreePathByLib(comQueryBus,types);
    for (String one : libAllPath) {
      String[] paths = one.split(RuleDefinitionTool.getTreeSplit());
      for (String path : paths) {
        libTreeMap.put(path,"");
      }
    }
    comQueryBus.setComName(query.getSelectName());
    libLeafs.addAll(libraryBusComTreeService.findComTreeListLike(comQueryBus,findKeys(libTreeMap)));
    libTreeMap.clear();
    for (LibraryBusComTree leaf : libLeafs) {
      String[] paths = leaf.getIdPath().split(RuleDefinitionTool.getTreeSplit());
      for (String path : paths) {
        libTreeMap.put(path,"");
      }
    }
    List<LibraryBusComTree> libAllTree =libraryBusComTreeService.findByPrimaryKeys(findKeys(libTreeMap));
    RecordComTreeDto libBusTree = genTreeListLib(libAllTree,comTreeMapLib);

    busTreeList.add(libBusTree);
    busTreeList.add(busTree);


    //基础组件
    List<RecordComTreeDto> basicList = platformFeignClient.getComTreeForLike(query).getData();

    RecordComTreeLikeDto result = new RecordComTreeLikeDto();
    result.setBusTree(busTreeList);
    result.setBasicTree(basicList);
    result.setHisTree(hisList);
    return result;
  }


  @Override
  public ReBusComponentInfoQueryDto getComListByIds(ReBusComponentInfoQueryDto query) {
    ReBusComponentInfoQueryDto returnDto = new ReBusComponentInfoQueryDto();
    //基础组件
    Map<String, List<String>> totalMap = new HashMap<>();
    if (query.getLibs()!=null && query.getLibs().size()>0){
      List<Map<String,List<String>>> libList =  platformFeignClient.getComBasicInfoByIdList(query.getLibs()).getData();
      if(libList!=null){
        for (Map<String, List<String>> oneMap : libList) {
          totalMap.putAll(oneMap);
        }
      }
    }
    returnDto.setLibMap(totalMap);

    //处理业务组件
    List<String> allList = query.getIds();
     //此查询包含删除组件
    List<ReBusComponentInfoVersionDto> result = new ArrayList<>();
    if(allList!=null && allList.size()>0){
      ReBusComponentInfoVersionDto one;
      Map<String,String> delMap = new HashMap<>();
      List<String> ids = new ArrayList<>();
      for (MdBusComponentInfo info : mdBusComponentInfoService.findByPrimaryKeys(new ArrayList<>(allList))) {
        if(info.getIsDeleted()==1){
          //删除的业务组件
          one =  new ReBusComponentInfoVersionDto();
          one.setId(info.getId());
          one.setIsDeleted(1);
          one.setComName(info.getComName());
          one.setComVersion(info.getComVersion());
          one.setScriptContent(info.getScriptContent());
          result.add(one);
          delMap.put(one.getId(),"");
        }else{
          //其他业务组件
          one =  new ReBusComponentInfoVersionDto();
          one.setId(info.getId());
          one.setIsDeleted(0);
          one.setComName(info.getComName());
          one.setComVersion(info.getComVersion());
          one.setScriptContent(info.getScriptContent());
          result.add(one);
        }
      }
    }
    returnDto.setComList(result);
    return returnDto;
  }


  private RecordComTreeDto genTreeListLib(List<LibraryBusComTree> dtoList, Map<String, List<RecordComTreeDto>> comTreeMapLib) {
    List<RecordComTreeDto> treeList = new ArrayList<>();
    for (LibraryBusComTree tree : dtoList) {
      RecordComTreeDto rec = dataTrans(tree);
      if(ScriptConstant.TREE_TOP.equals(rec.getParentId())){
        if(comTreeMapLib.containsKey(rec.getTreeId())){
          rec.setChildList(comTreeMapLib.get(rec.getTreeId()));
        }else{
          getSonTreeLib(rec,dtoList,comTreeMapLib);
        }
        treeList.add(rec);
      }
    }
    RecordComTreeDto top = new RecordComTreeDto();
    top.setTreeId(ScriptConstant.TREE_TOP_ONE);
    top.setComFlag(ScriptConstant.STR_ONE);//业务组件
    top.setIsCom(ScriptConstant.STR_ZERO);//目录
    top.setIsLeaf(ScriptConstant.STR_ZERO);
    top.setTreeName(ScriptConstant.PLATFORM_COM);
    top.setTreeAlias(ScriptConstant.PLATFORM_COM);
    top.setChildList(treeList);
    return top;
  }
  private void getSonTreeLib(RecordComTreeDto fa, List<LibraryBusComTree> dtoList,Map<String,List<RecordComTreeDto>> comTreeMap) {
    List<RecordComTreeDto> sonList = new ArrayList<>();
    for (LibraryBusComTree dto : dtoList) {
      RecordComTreeDto rec = dataTrans(dto);
      if(fa.getTreeId().equals(rec.getParentId())){
        if(comTreeMap.containsKey(rec.getTreeId())){
          rec.setChildList(comTreeMap.get(rec.getTreeId()));
        }else{
          getSonTreeLib(rec,dtoList,comTreeMap);
        }
        sonList.add(rec);
      }
    }
    List<RecordComTreeDto> reSonList = sonList.stream().sorted(Comparator.comparing(RecordComTreeDto::getOrderSeq)).collect(Collectors.toList());
    fa.setChildList(reSonList);
  }

  private RecordComTreeDto genTreeList(List<BusComponentTree> dtoList,Map<String,List<RecordComTreeDto>> comTreeMap) {
    List<RecordComTreeDto> treeList = new ArrayList<>();
    for (BusComponentTree tree : dtoList) {
      RecordComTreeDto rec = dataTrans(tree);
      if(ScriptConstant.TREE_TOP.equals(rec.getParentId())){
        if(comTreeMap.containsKey(rec.getTreeId())){
          rec.setChildList(comTreeMap.get(rec.getTreeId()));
        }else{
          getSonTree(rec,dtoList,comTreeMap);
        }
        treeList.add(rec);
      }
    }
    RecordComTreeDto top = new RecordComTreeDto();
    top.setTreeId(ScriptConstant.TREE_TOP_TWO);
    top.setComFlag(ScriptConstant.STR_ONE);//业务组件
    top.setIsCom(ScriptConstant.STR_ZERO);//目录
    top.setIsLeaf(ScriptConstant.STR_ZERO);
    top.setTreeName(ScriptConstant.PRO_COM);
    top.setTreeAlias(ScriptConstant.PRO_COM);
    top.setChildList(treeList);
    return top;
  }

  private void getSonTree(RecordComTreeDto fa, List<BusComponentTree> dtoList,Map<String,List<RecordComTreeDto>> comTreeMap) {
    List<RecordComTreeDto> sonList = new ArrayList<>();
    for (BusComponentTree dto : dtoList) {
      RecordComTreeDto rec = dataTrans(dto);
      if(fa.getTreeId().equals(rec.getParentId())){
        if(comTreeMap.containsKey(rec.getTreeId())){
          rec.setChildList(comTreeMap.get(rec.getTreeId()));
        }else{
          getSonTree(rec,dtoList,comTreeMap);
        }
        sonList.add(rec);
      }
    }
    List<RecordComTreeDto> reSonList = sonList.stream().sorted(Comparator.comparing(RecordComTreeDto::getOrderSeq)).collect(Collectors.toList());
    fa.setChildList(reSonList);
  }

  private RecordComTreeDto dataTrans(BusComponentTree tree){
    RecordComTreeDto rec = new RecordComTreeDto();
    rec.setTreeId(tree.getId());
    rec.setTreeName(tree.getTreeName());
    rec.setTreeAlias(tree.getTreeName());
    rec.setIsLeaf(ScriptConstant.STR_ZERO);
    rec.setIsCom(ScriptConstant.STR_ZERO);
    rec.setComFlag(ScriptConstant.STR_ONE);
    rec.setOrderSeq(tree.getOrderSeq());
    rec.setParentId(tree.getParentId());
    return rec;
  }

  private RecordComTreeDto dataTrans(LibraryBusComTree tree){
    RecordComTreeDto rec = new RecordComTreeDto();
    rec.setTreeId(tree.getId());
    rec.setTreeName(tree.getTreeName());
    rec.setTreeAlias(tree.getTreeName());
    rec.setIsLeaf(ScriptConstant.STR_ZERO);
    rec.setIsCom(ScriptConstant.STR_ZERO);
    rec.setComFlag(ScriptConstant.STR_ONE);
    rec.setOrderSeq(tree.getOrderSeq());
    rec.setParentId(tree.getParentId());
    return rec;
  }

  private RecordComTreeDto dataTransCom(MdBusComponentInfoDetailDto one){
    RecordComTreeDto dto = new RecordComTreeDto();
    dto.setTreeId(one.getId());
    dto.setTreeName(one.getComName());
    dto.setTreeAlias(one.getComName());
    dto.setComFlag(ScriptConstant.STR_ONE);//业务组件
    dto.setIsCom(ScriptConstant.STR_ONE);//组件
    dto.setIsLeaf(ScriptConstant.STR_ONE);//组件永远是叶子节节点
    dto.setMemo(one.getMemo());
    dto.setParamJson(one.getParamJson());
    dto.setVersion(one.getComVersion());
    dto.setScriptContent(one.getScriptContent());
    return dto;
  }

  private RecordComTreeDto dataTransCom(LibraryBusComInfoDetailDto one){
    RecordComTreeDto dto = new RecordComTreeDto();
    dto.setTreeId(one.getId());
    dto.setTreeName(one.getComName());
    dto.setTreeAlias(one.getComName());
    dto.setComFlag(ScriptConstant.STR_ONE);//业务组件
    dto.setIsCom(ScriptConstant.STR_ONE);//组件
    dto.setIsLeaf(ScriptConstant.STR_ONE);//组件永远是叶子节节点
    dto.setMemo(one.getMemo());
    dto.setParamJson(one.getParamJson());
    dto.setVersion(one.getComVersion());
    dto.setScriptContent(one.getScriptContent());
    return dto;
  }

  private List<Object> findKeys(Map<String,String> treeMap){
    List<Object> result = new ArrayList<>();
    result.add(ScriptConstant.TREE_TOP);
    treeMap.keySet().forEach(one->result.add(one+""));
    return result;
  }


}
