|
@@ -0,0 +1,179 @@
|
|
|
+package com.gyee.clientside.service;
|
|
|
+
|
|
|
+import cn.hutool.core.collection.ListUtil;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.gyee.clientside.entity.Point;
|
|
|
+import com.gyee.clientside.entity.TaosEntity;
|
|
|
+import com.gyee.clientside.mapper.master.PointMapper;
|
|
|
+import com.gyee.clientside.mapper.vacant.PointDBMapper;
|
|
|
+import com.gyee.feign.api.GetKeysApi;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.poi.util.StringUtil;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class TaosService {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private PointDBMapper pointDBMapper;
|
|
|
+ @Resource
|
|
|
+ private PointMapper pointMapper;
|
|
|
+ @Resource
|
|
|
+ private GetKeysApi getKeysApi;
|
|
|
+
|
|
|
+ @Value("${taoscz.db_name:zgxny}")
|
|
|
+ private String dbname;
|
|
|
+ @Value("${taoscz.ai_stable_name:pointai}")
|
|
|
+ private String aistablename;
|
|
|
+ @Value("${taoscz.di_stable_name:pointdi}")
|
|
|
+ private String distablename;
|
|
|
+
|
|
|
+ public void createDataBase() {
|
|
|
+ log.info("创建taos库!");
|
|
|
+ pointDBMapper.createDataBase("mydbinfo", Wrappers.emptyWrapper());
|
|
|
+ QueryWrapper wrapper = new QueryWrapper<>();
|
|
|
+ wrapper.last("cachemodel 'last_row' wal_retention_period 1800");
|
|
|
+ pointDBMapper.createDataBase(dbname, wrapper);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public boolean createStables(String aiSTablename, String diSTablename) {
|
|
|
+
|
|
|
+ if (StringUtil.isBlank(aiSTablename) || StringUtil.isBlank(diSTablename)) {
|
|
|
+ aiSTablename = aistablename;
|
|
|
+ diSTablename = distablename;
|
|
|
+ }
|
|
|
+
|
|
|
+ log.info("创建taos超级表!");
|
|
|
+ pointMapper.createSuperTable(aiSTablename, "double");
|
|
|
+ pointMapper.createSuperTable(diSTablename, "bool");
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ public int createTables() {
|
|
|
+ log.info("创建taos表!");
|
|
|
+ List<String> aiKeys = getKeysApi.getAIKeys();
|
|
|
+ List<String> diKeys = getKeysApi.getDIKeys();
|
|
|
+
|
|
|
+ //按数量分组,di点
|
|
|
+ List<List<String>> diStrll = ListUtil.split(diKeys, 2900);
|
|
|
+ //按数量分组,ai点
|
|
|
+ List<List<String>> aiStrll = ListUtil.split(aiKeys, 2900);
|
|
|
+
|
|
|
+ int k = 0;
|
|
|
+ for (List<String> s : diStrll) {
|
|
|
+ pointMapper.runSql(getDiSql(s, distablename));
|
|
|
+ k += s.size();
|
|
|
+ log.info(String.valueOf(k));
|
|
|
+ }
|
|
|
+ for (List<String> s : aiStrll) {
|
|
|
+ pointMapper.runSql(getAiSql(s, aistablename));
|
|
|
+ k += s.size();
|
|
|
+ log.info(String.valueOf(k));
|
|
|
+ }
|
|
|
+ log.info("taos表创建完成!");
|
|
|
+ return k;
|
|
|
+ }
|
|
|
+
|
|
|
+ public int createTablesByExcel(List<TaosEntity> entities) {
|
|
|
+ log.info("创建taos表!");
|
|
|
+ List<StringBuilder> excelTaos = taosEntity2CreateStr(entities);
|
|
|
+ for (StringBuilder excelTao : excelTaos) {
|
|
|
+ try {
|
|
|
+ //pointMapper.runSql(excelTao.toString());
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ log.info(excelTao.toString());
|
|
|
+ }
|
|
|
+ log.info(String.valueOf(entities.size()));
|
|
|
+ }
|
|
|
+ log.info("taos表创建完成!");
|
|
|
+
|
|
|
+ log.info("更新taos的Tag!");
|
|
|
+ int i = 0;
|
|
|
+ for (TaosEntity entity : entities) {
|
|
|
+ pointMapper.alterTag(entity.getPoint(),"description",entity.getDescription());
|
|
|
+ pointMapper.alterTag(entity.getPoint(),"station",entity.getStation());
|
|
|
+ pointMapper.alterTag(entity.getPoint(),"category",entity.getCategory());
|
|
|
+ pointMapper.alterTag(entity.getPoint(),"facility",entity.getFacility());
|
|
|
+ pointMapper.alterTag(entity.getPoint(),"uniformcode",entity.getUniformcode());
|
|
|
+ pointMapper.alterTagNum(entity.getPoint(),"rate",entity.getRate());
|
|
|
+ pointMapper.alterTag(entity.getPoint(),"remark",entity.getRemark());
|
|
|
+ i++;
|
|
|
+ log.info(String.valueOf(i));
|
|
|
+ }
|
|
|
+ log.info("taos的Tag更新完成!");
|
|
|
+ return i;
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<StringBuilder> taosEntity2CreateStr(List<TaosEntity> entities) {
|
|
|
+ List<StringBuilder> sbList = new ArrayList<>();
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ Map<String, List<TaosEntity>> collect = entities.stream().collect(Collectors.groupingBy(TaosEntity::getDatatype));
|
|
|
+ List<TaosEntity> teDouble = collect.get("double");
|
|
|
+ if (teDouble == null) teDouble = new ArrayList<>();
|
|
|
+ List<TaosEntity> teBool = collect.get("bool");
|
|
|
+ if (teBool == null) teBool = new ArrayList<>();
|
|
|
+
|
|
|
+ TaosEntity te;
|
|
|
+ for (int i = 0; i < teDouble.size(); i++) {
|
|
|
+ te = teDouble.get(i);
|
|
|
+ if (i % 2999 == 0) {
|
|
|
+ sb = new StringBuilder();
|
|
|
+ sbList.add(sb);
|
|
|
+ sb.append("CREATE TABLE");
|
|
|
+ }
|
|
|
+ sb.append(" IF NOT EXISTS ").append(te.getPoint()).append(" USING ").append(aistablename).append(" TAGS('")
|
|
|
+ .append(te.getDescription()).append("','").append(te.getStation()).append("','")
|
|
|
+ .append(te.getCategory()).append("','").append(te.getFacility()).append("','")
|
|
|
+ .append(te.getUniformcode()).append("',").append(te.getRate()).append(",'")
|
|
|
+ .append(te.getRemark()).append("')");
|
|
|
+ }
|
|
|
+ for (int i = 0; i < teBool.size(); i++) {
|
|
|
+ te = teBool.get(i);
|
|
|
+ if (i % 2999 == 0) {
|
|
|
+ sb = new StringBuilder();
|
|
|
+ sbList.add(sb);
|
|
|
+ sb.append("CREATE TABLE");
|
|
|
+ }
|
|
|
+ sb.append(" IF NOT EXISTS ").append(te.getPoint()).append(" USING ").append(distablename).append(" TAGS('")
|
|
|
+ .append(te.getDescription()).append("','").append(te.getStation()).append("','")
|
|
|
+ .append(te.getCategory()).append("','").append(te.getFacility()).append("','")
|
|
|
+ .append(te.getUniformcode()).append("',").append(te.getRate()).append(",'")
|
|
|
+ .append(te.getRemark()).append("')");
|
|
|
+ }
|
|
|
+ return sbList;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getAiSql(List<String> ls, String astn) {
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ sb.append("CREATE TABLE");
|
|
|
+ for (String s : ls) {
|
|
|
+ sb.append(" IF NOT EXISTS ").append(s).append(" USING ").append(astn).append(" TAGS('','','','','',1,'')");
|
|
|
+ }
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getDiSql(List<String> ls, String dastn) {
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ sb.append("CREATE TABLE");
|
|
|
+ for (String s : ls) {
|
|
|
+ sb.append(" IF NOT EXISTS ").append(s).append(" USING ").append(dastn).append(" TAGS('','','','','',1,'')");
|
|
|
+ }
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<Point> selectPage(Page<Point> page, String stablename, QueryWrapper wrapper) {
|
|
|
+ return pointMapper.selectTableNamesPage(page, stablename, wrapper);
|
|
|
+ }
|
|
|
+}
|