Przeglądaj źródła

涌溢咨询指标添加到es搜索

xyxie 1 rok temu
rodzic
commit
9b0d20b986

+ 21 - 0
models/base_from_yongyi.go

@@ -211,6 +211,20 @@ type BaseFromYongyiIndex struct {
 	ModifyTime    time.Time
 }
 
+type BaseFromYongyiIndexList struct {
+	YongyiIndexId int64 `orm:"column(yongyi_index_id);pk"`
+	IndexCode     string
+	IndexName     string
+	Frequency     string
+	Unit          string
+	ClassifyId    int64
+	StartDate     string
+	EndDate       string
+	TerminalCode  string
+	CreateTime    string
+	ModifyTime    string
+}
+
 func (y *BaseFromYongyiData) GetByIndexCode(indexCode string) (list []*BaseFromYongyiData, err error) {
 	o := orm.NewOrm()
 	sql := ` SELECT * FROM base_from_yongyi_data WHERE index_code=? `
@@ -263,6 +277,13 @@ func (y *BaseFromYongyiIndex) GetByIndexCode(indexCode string) (item *BaseFromYo
 	return
 }
 
+func (y *BaseFromYongyiIndex) GetByIndexId(id int) (item *BaseFromYongyiIndexList, err error) {
+	o := orm.NewOrm()
+	sql := ` SELECT * FROM base_from_yongyi_index WHERE yongyi_index_id=? `
+	err = o.Raw(sql, id).QueryRow(&item)
+	return
+}
+
 func (y *BaseFromYongyiIndex) Add() (lastId int64, err error) {
 	o := orm.NewOrm()
 	lastId, err = o.Insert(y)

+ 17 - 2
services/base_from_yongyi.go

@@ -4,8 +4,10 @@ import (
 	"eta/eta_index_lib/logic"
 	"eta/eta_index_lib/models"
 	"eta/eta_index_lib/services/alarm_msg"
+	"eta/eta_index_lib/services/elastic"
 	"eta/eta_index_lib/utils"
 	"fmt"
+	"strconv"
 	"strings"
 	"time"
 )
@@ -202,7 +204,10 @@ func handleYongyiIndex(req *models.HandleYongyiExcelData) (err error) {
 			return
 		}
 
-		go indexObj.ModifyIndexMaxAndMinDate(indexCode, dateItem)
+		go func() {
+			indexObj.ModifyIndexMaxAndMinDate(indexCode, dateItem)
+			UpdateEsBaseFromYongyi(int(indexObj.YongyiIndexId))
+		}()
 	}
 
 	// 同步刷新ETA指标库的指标
@@ -224,8 +229,18 @@ func handleYongyiIndex(req *models.HandleYongyiExcelData) (err error) {
 		if edbInfo != nil {
 			go logic.RefreshBaseEdbInfo(edbInfo, ``)
 		}
+	}
+	return
+}
 
-		// todo 更新到es库里
+// UpdateEsBaseFromYongyi 更新数据源的es
+func UpdateEsBaseFromYongyi(yongyiIndexId int) (err error) {
+	indexObj := new(models.BaseFromYongyiIndex)
+	//添加es
+	itemInfo, err := indexObj.GetByIndexId(yongyiIndexId)
+	if err == nil {
+		err = elastic.EsAddOrEditBaseFromYongyiIndex(utils.ES_INDEX_BASE_FROM_YONGYI_INDEX, strconv.Itoa(yongyiIndexId), itemInfo)
+		return
 	}
 	return
 }

+ 150 - 0
services/elastic/elastic.go

@@ -5,8 +5,31 @@ import (
 	"eta/eta_index_lib/models"
 	"eta/eta_index_lib/utils"
 	"fmt"
+	"strings"
 )
 
+func EsCreateIndex(indexName, mappingJson string) (err error) {
+	client := utils.EsClient
+
+	//定义表结构
+	exists, err := client.IndexExists(indexName).Do(context.Background()) //<5>
+	if err != nil {
+		return
+	}
+	if !exists {
+		resp, err := client.CreateIndex(indexName).BodyJson(mappingJson).Do(context.Background())
+		//BodyJson(bodyJson).Do(context.Background())
+		if err != nil {
+			fmt.Println("CreateIndex Err:" + err.Error())
+			return err
+		}
+		fmt.Println(resp.Index, resp.ShardsAcknowledged, resp.Acknowledged)
+	} else {
+		fmt.Println(indexName + " 已存在")
+	}
+	return
+}
+
 // EsAddOrEditEdbInfoData 新增/修改es中的指标数据
 func EsAddOrEditEdbInfoData(indexName, docId string, item *models.EdbInfoList) (err error) {
 	defer func() {
@@ -33,3 +56,130 @@ func EsAddOrEditEdbInfoData(indexName, docId string, item *models.EdbInfoList) (
 	}
 	return
 }
+
+// EsAddOrEditBaseFromYongyiIndex 新增/修改es中的原始涌溢指标
+func EsAddOrEditBaseFromYongyiIndex(indexName, docId string, item *models.BaseFromYongyiIndexList) (err error) {
+	defer func() {
+		if err != nil {
+			fmt.Println("EsAddOrEditData Err:", err.Error())
+		}
+	}()
+	client := utils.EsClient
+	if err != nil {
+		return
+	}
+
+	resp, err := client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background())
+	if err != nil {
+		if strings.Contains(err.Error(), "no such index") {
+			//新增index
+			err = CreateEsEditBaseFromYongyiIndex()
+			if err != nil {
+				return
+			}
+			resp, err = client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background())
+			if err != nil {
+				return
+			}
+		} else {
+			fmt.Println("新增失败:", err.Error())
+			return err
+		}
+	}
+	fmt.Println(resp)
+	if resp.Status == 0 {
+		fmt.Println("新增成功", resp.Result)
+		err = nil
+	} else {
+		fmt.Println("AddData", resp.Status, resp.Result)
+	}
+	return
+}
+
+func CreateEsEditBaseFromYongyiIndex() (err error) {
+	indexName := utils.ES_INDEX_BASE_FROM_YONGYI_INDEX
+	mappingJson := `{
+  "mappings": {
+    "dynamic": true,
+    "properties": {
+      "YongyiIndexId": {
+         "type" : "long"
+      },
+      "IndexName": {
+        "type": "text",
+          "fields" : {
+            "keyword" : {
+              "type" : "keyword",
+              "ignore_above" : 256
+            }
+          }
+      },
+      "ClassifyId": {
+         "type" : "long"
+      },
+      "Frequency": {
+        "type": "text",
+          "fields" : {
+            "keyword" : {
+              "type" : "keyword",
+              "ignore_above" : 256
+            }
+          }
+      },
+      "IndexCode": {
+        "type": "text"
+      },
+      "Unit": {
+        "type": "text",
+          "fields" : {
+            "keyword" : {
+              "type" : "keyword",
+              "ignore_above" : 256
+            }
+          }
+      },
+      "CreateTime": {
+          "type" : "text",
+          "fields" : {
+            "keyword" : {
+              "type" : "keyword",
+              "ignore_above" : 256
+            }
+          }
+        },
+      "ModifyTime": {
+          "type" : "text",
+          "fields" : {
+            "keyword" : {
+              "type" : "keyword",
+              "ignore_above" : 256
+            }
+          }
+        },
+      "StartDate": {
+          "type" : "text",
+          "fields" : {
+            "keyword" : {
+              "type" : "keyword",
+              "ignore_above" : 256
+            }
+          }
+        },
+      "EndDate": {
+          "type" : "text",
+          "fields" : {
+            "keyword" : {
+              "type" : "keyword",
+              "ignore_above" : 256
+            }
+          }
+        },
+      "TerminalCode": {
+        "type": "text"
+      }
+    }
+  }
+}`
+	err = EsCreateIndex(indexName, mappingJson)
+	return
+}

+ 2 - 0
utils/config.go

@@ -28,6 +28,7 @@ var (
 
 // DATA_INDEX_NAME 数据指标库ES索引名称
 var DATA_INDEX_NAME string
+var ES_INDEX_BASE_FROM_YONGYI_INDEX string
 
 // 基础配置
 var (
@@ -214,6 +215,7 @@ func init() {
 		ES_USERNAME = config["es_username"]
 		ES_PASSWORD = config["es_password"]
 		DATA_INDEX_NAME = config["data_index_name"]
+		ES_INDEX_BASE_FROM_YONGYI_INDEX = config["es_index_base_from_yongyi_index"]
 	}
 
 	//日志配置