From f86d25d5143847692779dd4e81b7e47d9b25376e Mon Sep 17 00:00:00 2001
From: sunty <suntianyu0923@163.com>
Date: 星期二, 30 七月 2019 11:16:15 +0800
Subject: [PATCH] fix delete dbtable add GetTotalFromDb and EsReq

---
 controllers/dbtablesCon.go |    6 +-
 extend/esutil/EsClient.go  |   28 ++++++++++++++
 controllers/taglist.go     |   38 ++++++++++++++++++
 3 files changed, 68 insertions(+), 4 deletions(-)

diff --git a/controllers/dbtablesCon.go b/controllers/dbtablesCon.go
index f47c7d8..a0dd2eb 100644
--- a/controllers/dbtablesCon.go
+++ b/controllers/dbtablesCon.go
@@ -50,9 +50,9 @@
 	//c.JSON(200, data)
 	util.ResponseFormat(c, code.Success, data)
 }
-
+锟�
 // 渚濇嵁搴曞簱id  鏌ヨ鏁版嵁
-func QueryDbTableInfo(tableId string) map[string]interface{} {
+func QueryDbTableInfo(tableId string) map[string]interface{} {锟�
 	url := "http://" + config.EsInfo.Masterip + ":" + config.EsInfo.Httpport +
 		"/" + config.EsInfo.EsIndex.DbTables.IndexName + "/" + config.EsInfo.EsIndex.DbTables.IndexName + "/" + tableId
 	logger.Debug("璇锋眰url:%s;", url)
@@ -156,7 +156,7 @@
 	uuid := c.Params.ByName("id")
 	url := "http://" + config.EsInfo.Masterip + ":" + config.EsInfo.Httpport +
 		"/" + config.EsInfo.EsIndex.DbTables.IndexName + "/" + config.EsInfo.EsIndex.DbTables.IndexType + "/" + uuid + "/_update?refresh=wait_for"
-	params := "{\"doc\":{\"isDelete\":\"1\"}}"
+	params := "{\"doc\":{\"isDelete\":1}}"
 	logger.Debug("鍒犻櫎璇锋眰url:%s;\n 璇锋眰鍙傛暟params:%s", url, params)
 	data := esutil.GetEsDataReq(url, params, false)
 	//c.JSON(200, changeEsRespData(data, "鍒犻櫎鎴愬姛"))
diff --git a/controllers/taglist.go b/controllers/taglist.go
index 27e679d..774f21d 100644
--- a/controllers/taglist.go
+++ b/controllers/taglist.go
@@ -35,8 +35,44 @@
 		tokenRes["key"] = value.(map[string]interface{})["id"].(string)
 		tokenRes["title"] = value.(map[string]interface{})["tableName"].(string)
 		tokenRes["value"] = value.(map[string]interface{})["id"].(string)
-		tokenRes["status"] = value.(map[string]interface{})["isDelete"]
+		tokenRes["status"] = int(value.(map[string]interface{})["isDelete"])
+		if tokenRes["status"] == 1  {
+			if GetTotalFromDb(tokenRes["key"]) == false{
+				continue
+			}
+
+		}
 		sources = append(sources, tokenRes)
 	}
 	util.ResponseFormat(c, code.Success, sources)
+}
+//鍒ゆ柇搴曞簱鏄惁鏈夋暟鎹�
+func GetTotalFromDb(id string) (flag bool) {
+	flag = false
+	url := "http://" + config.EsInfo.Masterip + ":" + config.EsInfo.Httpport +
+		"/" + config.EsInfo.EsIndex.DbTables.IndexName + "/_search"
+	prama := "{\"query\":{\"bool\":{\"filter\":[{\"term\":{\"baseInfo.tableId\":\""+id+"\"}}]}},\"size\":0}"
+	buf, err := EsReq("POST", url, []byte(prama))
+            if err != nil {
+                fmt.Println("http request info is err!")
+                return
+            }
+            var info interface{}
+            json.Unmarshal(buf, &info)
+            out, ok := info.(map[string]interface{})
+            if !ok {
+                fmt.Println("http response interface can not change map[string]interface{}")
+                return
+            }
+            middle, ok := out["hits"].(map[string]interface{})
+            if !ok {
+                fmt.Println("first hits change error!")
+                return
+            }
+            total = int(middle["total"].(float64))
+			fmt.Println(total)
+			if total>0 {
+				flag=true
+			}
+            return flag
 }
\ No newline at end of file
diff --git a/extend/esutil/EsClient.go b/extend/esutil/EsClient.go
index 6bd550c..dd8bc3e 100644
--- a/extend/esutil/EsClient.go
+++ b/extend/esutil/EsClient.go
@@ -245,3 +245,31 @@
 		return dat
 	}
 }
+//瑙f瀽http
+func EsReq(method string, url string, parama []byte) (buf []byte, err error) {
+    timeout := time.Duration(10 * time.Second) 
+    client := http.Client{
+        Timeout: timeout,
+    }
+    request, err := http.NewRequest(method, url, bytes.NewBuffer(parama)) 
+    request.Header.Set("Content-type", "application/json")
+
+    if err != nil {
+        fmt.Println("build request fail !")
+        return nil, err 
+    }
+
+    resp, err := client.Do(request)
+    if err != nil{
+        fmt.Println("request error: ", err)
+        return nil, err 
+    }
+
+    defer resp.Body.Close()
+    body, err := ioutil.ReadAll(resp.Body)
+    if err != nil {
+        fmt.Println(err) 
+        return nil , err
+    }
+    return body, nil
+}
\ No newline at end of file

--
Gitblit v1.8.0