|
@@ -0,0 +1,190 @@
|
|
|
+package com.qhtx.eta.domain.service.impl;
|
|
|
+
|
|
|
+import com.alibaba.druid.pool.DruidDataSource;
|
|
|
+import com.alibaba.druid.util.StringUtils;
|
|
|
+import com.qhtx.eta.common.constant.ETAConstants;
|
|
|
+import com.qhtx.eta.common.eunms.ErrorEnum;
|
|
|
+import com.qhtx.eta.common.exception.ETAException;
|
|
|
+import com.qhtx.eta.domain.convert.DWIndexFrameDTOConverter;
|
|
|
+import com.qhtx.eta.domain.entity.DWIndexFrameDTO;
|
|
|
+import com.qhtx.eta.domain.service.DWIndexFrameService;
|
|
|
+import com.qhtx.eta.domain.utils.RedisUtils;
|
|
|
+import com.qhtx.eta.infra.annotation.UseDataSource;
|
|
|
+import com.qhtx.eta.infra.datasource.DataSourceContextHolder;
|
|
|
+import com.qhtx.eta.infra.entity.dw.DWIndexFrame;
|
|
|
+import com.qhtx.eta.infra.entity.dw.ETAClassifyIndexFrameMapping;
|
|
|
+import com.qhtx.eta.infra.service.EtaApiClassifyService;
|
|
|
+import com.qhtx.eta.infra.service.IndexFrameService;
|
|
|
+import lombok.Data;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.jetbrains.annotations.NotNull;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.TransactionStatus;
|
|
|
+import org.springframework.transaction.support.TransactionCallbackWithoutResult;
|
|
|
+import org.springframework.transaction.support.TransactionTemplate;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
+import java.util.UUID;
|
|
|
+
|
|
|
+import com.qhtx.eta.common.eunms.DataSourceType;
|
|
|
+
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+@UseDataSource(dataSourceType = DataSourceType.DW)
|
|
|
+public class DWIndexFrameServiceImpl implements DWIndexFrameService {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private RedisUtils redisUtils;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private IndexFrameService indexFrameService;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private TransactionTemplate transactionTemplate;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private EtaApiClassifyService etaApiClassifyService;
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void syncIndexFrame() {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String generateTableCode() throws ETAException {
|
|
|
+ if (!redisUtils.exist(ETAConstants.REDIS_INDEX_FRAME_TABLE_CODE_KEY)) {
|
|
|
+ throw new ETAException(ErrorEnum.DW_INDEX_TABLE_CODE_GET_ERROR);
|
|
|
+// synchronized (this) {
|
|
|
+// if (!redisUtils.exist(ETAConstants.REDIS_INDEX_FRAME_TABLE_CODE_KEY)) {
|
|
|
+// long currentTableCode = 100;
|
|
|
+// redisUtils.setNx(ETAConstants.REDIS_INDEX_FRAME_TABLE_CODE_KEY, currentTableCode, -1L, TimeUnit.SECONDS);
|
|
|
+// }
|
|
|
+// }
|
|
|
+ }
|
|
|
+ return ETAConstants.INDEX_FRAME_TABLE_CODE_PREFIX + String.format("%07d", redisUtils.add(ETAConstants.REDIS_INDEX_FRAME_TABLE_CODE_KEY, 1L));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void processing(List<DWIndexFrameDTO> processList) {
|
|
|
+ DWIndexFrame hzIndexFrame = indexFrameService.getByCnName(ETAConstants.DW_INDEX_FRAME_PARENT_NAME);
|
|
|
+ if (hzIndexFrame == null) {
|
|
|
+ log.info("未找到根节点,不同步eta指标目录");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ hzIndexFrame.setHasChild(1);
|
|
|
+ FrameNode root = new FrameNode();
|
|
|
+ root.setId(hzIndexFrame.getId());
|
|
|
+ root.setName(hzIndexFrame.getCnName());
|
|
|
+ root.setIdPath(hzIndexFrame.getFrameIdPath());
|
|
|
+ root.setPath(hzIndexFrame.getFramePath());
|
|
|
+ root.setPId(hzIndexFrame.getPId());
|
|
|
+ root.setFrameLevel(hzIndexFrame.getFrameLevel());
|
|
|
+ root.setClassifyId(0);
|
|
|
+ root.setParentClassifyId(0);
|
|
|
+ root.setChildren(new ArrayList<>());
|
|
|
+ buildTree(processList, root, 1, 10);
|
|
|
+ //建完树开始插入数据库 ,树转List
|
|
|
+ List<DWIndexFrameDTO> dtoList = parseList(root);
|
|
|
+ log.info("开始插入数据库{}", dtoList.size());
|
|
|
+ for (DWIndexFrameDTO dto : dtoList) {
|
|
|
+ ETAClassifyIndexFrameMapping mapping = etaApiClassifyService.getByClassifyId(dto.getClassifyId());
|
|
|
+ if (mapping == null) {
|
|
|
+ log.info("未找到对应eta分类,{}:{}", dto.getCnName(), dto.getClassifyId());
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ //无法代理方法直接使用手动方法设置数据源
|
|
|
+ DataSourceContextHolder.setDataSourceType(DataSourceType.DW);
|
|
|
+ transactionTemplate.execute(new TransactionCallbackWithoutResult() {
|
|
|
+ @Override
|
|
|
+ protected void doInTransactionWithoutResult(@NotNull TransactionStatus transactionStatus) {
|
|
|
+ try {
|
|
|
+ //更新mapping已处理
|
|
|
+ mapping.setIndexFrameId(dto.getId());
|
|
|
+ etaApiClassifyService.update(mapping);
|
|
|
+ indexFrameService.insertOrUpdate(DWIndexFrameDTOConverter.INSTANCE.convertDTOToEntity(dto));
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn(String.format(ErrorEnum.DATASOURCE_EXECUTE_ERROR.getMsg(), e.getMessage()));
|
|
|
+ throw e;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private List<DWIndexFrameDTO> parseList(FrameNode node) {
|
|
|
+ if (node == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ List<DWIndexFrameDTO> result = new ArrayList<>();
|
|
|
+ DWIndexFrameDTO dto = new DWIndexFrameDTO();
|
|
|
+ dto.setTableCode(generateTableCode());
|
|
|
+ dto.setId(node.getId());
|
|
|
+ dto.setCnName(node.getName());
|
|
|
+ dto.setFrameLevel(node.getFrameLevel());
|
|
|
+ dto.setPId(node.getPId());
|
|
|
+ dto.setSort(node.getSort());
|
|
|
+ dto.setFramePath(node.getPath());
|
|
|
+ dto.setFrameIdPath(node.getIdPath());
|
|
|
+ dto.setClassifyId(node.getClassifyId());
|
|
|
+ dto.setParentId(node.getParentClassifyId());
|
|
|
+ result.add(dto);
|
|
|
+ if (!CollectionUtils.isEmpty(node.children)) {
|
|
|
+ dto.setHasChild(1);
|
|
|
+ for (FrameNode subNode : node.children) {
|
|
|
+ result.addAll(parseList(subNode));
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ dto.setHasChild(0);
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void buildTree(List<DWIndexFrameDTO> list, FrameNode node, int current, int depth) {
|
|
|
+ if (current > depth) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ for (DWIndexFrameDTO dto : list) {
|
|
|
+ if (Objects.equals(dto.getParentId(), node.getClassifyId())) {
|
|
|
+ String path = StringUtils.isEmpty(node.getPath().trim()) ? dto.getCnName() : node.getPath() + "|" + dto.getCnName();
|
|
|
+ String id = (dto.getId() == null || dto.getId().trim().isEmpty()) ? UUID.randomUUID().toString().replaceAll("-", "") : dto.getId();
|
|
|
+ String idPath = StringUtils.isEmpty(node.getPath().trim()) ? id : node.getIdPath() + "|" + id;
|
|
|
+ FrameNode temp = new FrameNode();
|
|
|
+ temp.setId(id);
|
|
|
+ temp.setClassifyId(dto.getClassifyId());
|
|
|
+ temp.setParentClassifyId(dto.getParentId());
|
|
|
+ temp.setIdPath(idPath);
|
|
|
+ temp.setClassifyId(dto.getClassifyId());
|
|
|
+ temp.setFrameLevel(node.getFrameLevel() + 1);
|
|
|
+ temp.setParentClassifyId(dto.getParentId());
|
|
|
+ temp.setName(dto.getCnName());
|
|
|
+ temp.setPId(node.getId());
|
|
|
+ temp.setPath(path);
|
|
|
+ temp.setSort(dto.getSort());
|
|
|
+ temp.setChildren(new ArrayList<>());
|
|
|
+ node.getChildren().add(temp);
|
|
|
+ buildTree(list, temp, current + 1, depth);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Data
|
|
|
+ private static class FrameNode {
|
|
|
+ private int frameLevel;
|
|
|
+ private String pId;
|
|
|
+ private String id;
|
|
|
+ private String path;
|
|
|
+ private String idPath;
|
|
|
+ private String name;
|
|
|
+ private Integer classifyId;
|
|
|
+ private Integer parentClassifyId;
|
|
|
+ private Integer sort;
|
|
|
+ private List<FrameNode> children;
|
|
|
+ }
|
|
|
+}
|