From 978757c6cd702684d736ddf733f1d718a8c67060 Mon Sep 17 00:00:00 2001
From: liuxiaolong <736321739@qq.com>
Date: 星期四, 01 八月 2019 21:08:44 +0800
Subject: [PATCH] add es cache change

---
 controllers/dbtablesCon.go            |   19 +++++++++
 discovery/server.go                   |   13 +++---
 service/EnableStatusControlService.go |   19 +++++++++
 controllers/dbtableperson.go          |   59 +++++++++++++++++++++++++++++
 4 files changed, 103 insertions(+), 7 deletions(-)

diff --git a/controllers/dbtableperson.go b/controllers/dbtableperson.go
index 6d7d5c7..3a987c0 100644
--- a/controllers/dbtableperson.go
+++ b/controllers/dbtableperson.go
@@ -1,10 +1,12 @@
 package controllers
 
 import (
+	"basic.com/pubsub/protomsg.git"
 	"encoding/json"
 	"log"
 	"strconv"
 	"time"
+	"webserver/discovery"
 	"webserver/extend/logger"
 
 	"github.com/gin-gonic/gin"
@@ -14,6 +16,7 @@
 	"webserver/extend/esutil"
 	"webserver/extend/util"
 	"webserver/models"
+	esApi "basic.com/pubsub/esutil.git"
 )
 
 type DbPersonController struct {
@@ -60,6 +63,17 @@
 	params := string(personbytes)
 	logger.Debug("璇锋眰url:%s;\n 璇锋眰鍙傛暟params:%s", url, params)
 	data, _ := esutil.PutEsDataReq(url, params)
+	if data["_id"] !=""{
+		//閫氱煡姣斿杩涚▼缂撳瓨鏇存柊
+		discovery.AddDbMessage(&protomsg.EsPersonCacheChange{
+			Type: protomsg.EsCacheChanged_T_DbTablePerson,
+			PersonId: personId,
+			TableId: []string{ dbperson.TableId },
+			Feature: dbperson.FaceFeature,
+			Action: protomsg.DbAction_Insert,
+		})
+	}
+
 	//c.JSON(200, changeEsRespData(data, "娣诲姞浜哄憳鎴愬姛"))
 	result = changeEsRespData(data, "娣诲姞鎴愬姛")
 	return result
@@ -97,6 +111,23 @@
 	result := changeEsRespData(data, "淇敼鎴愬姛")
 	if result["success"].(bool) {
 		//code.Success.Message = "淇敼搴曞簱浜哄憳鎴愬姛"
+		if dbperson.Enable == 1 {
+			discovery.AddDbMessage(&protomsg.EsPersonCacheChange{
+				Type: protomsg.EsCacheChanged_T_DbTablePerson,
+				PersonId: dbperson.Id,
+				TableId: []string{ dbperson.TableId },
+				Feature: "",
+				Action: protomsg.DbAction_Insert,
+			})
+		} else {
+			discovery.AddDbMessage(&protomsg.EsPersonCacheChange{
+				Type: protomsg.EsCacheChanged_T_DbTablePerson,
+				PersonId: dbperson.Id,
+				TableId: []string{ dbperson.TableId },
+				Feature: "",
+				Action: protomsg.DbAction_Delete,
+			})
+		}
 		util.ResponseFormat(c, code.Success, result["data"])
 	} else {
 		//code.ServiceInsideError.Message += result["msg"].(string)
@@ -127,6 +158,20 @@
 	result := changeEsRespData(data, "鍒犻櫎鎴愬姛")
 	if result["success"].(bool) {
 		//code.Success.Message = "鍒犻櫎搴曞簱浜哄憳鎴愬姛"
+		//閫氱煡姣斿杩涚▼锛屾浜哄凡鍒犻櫎
+		dbperArr, e := esApi.Dbpersoninfosbyid([]string{uuid}, config.EsInfo.EsIndex.Dbtablepersons.IndexName, config.EsInfo.Masterip, config.EsInfo.Httpport)
+		if e ==nil && len(dbperArr) > 0{
+			if dbperArr[0].TableId !=""{
+				discovery.AddDbMessage(&protomsg.EsPersonCacheChange{
+					Type: protomsg.EsCacheChanged_T_DbTablePerson,
+					PersonId: uuid,
+					TableId: []string{ dbperArr[0].TableId },
+					Feature: "",
+					Action: protomsg.DbAction_Delete,
+				})
+			}
+		}
+
 		util.ResponseFormat(c, code.Success, result["data"])
 	} else {
 		//code.ServiceInsideError.Message += result["msg"].(string)
@@ -158,6 +203,20 @@
 	//result := changeEsRespData(data, "鍒犻櫎鎴愬姛")
 	if data["error"] == nil {
 		//code.Success.Message = "鍒犻櫎搴曞簱浜哄憳鎴愬姛"
+		dbperArr, e := esApi.Dbpersoninfosbyid(uuids, config.EsInfo.EsIndex.Dbtablepersons.IndexName, config.EsInfo.Masterip, config.EsInfo.Httpport)
+		if e ==nil && len(dbperArr) > 0{
+			for _,esPer :=range dbperArr {
+				if esPer.TableId !=""{
+					discovery.AddDbMessage(&protomsg.EsPersonCacheChange{
+						Type: protomsg.EsCacheChanged_T_DbTablePerson,
+						PersonId: esPer.Id,
+						TableId: []string{ esPer.TableId },
+						Feature: "",
+						Action: protomsg.DbAction_Delete,
+					})
+				}
+			}
+		}
 		util.ResponseFormat(c, code.Success, "鍒犻櫎搴曞簱浜哄憳鎴愬姛")
 	} else {
 		//code.ServiceInsideError.Message += result["msg"].(string)
diff --git a/controllers/dbtablesCon.go b/controllers/dbtablesCon.go
index e942fc8..d3f9f89 100644
--- a/controllers/dbtablesCon.go
+++ b/controllers/dbtablesCon.go
@@ -2,12 +2,14 @@
 
 import (
 	"basic.com/dbapi.git"
+	"basic.com/pubsub/protomsg.git"
 	"encoding/json"
 	"fmt"
 	"github.com/gin-gonic/gin"
 	"github.com/satori/go.uuid"
 	"log"
 	"time"
+	"webserver/discovery"
 	"webserver/extend/code"
 	"webserver/extend/config"
 	"webserver/extend/esutil"
@@ -100,6 +102,23 @@
 	result := changeEsRespData(data, "淇敼鎴愬姛")
 	if result["success"].(bool) {
 		//code.Success.Message = "淇敼搴曞簱鎴愬姛"
+		if dbtable.Enable ==1 {
+			discovery.AddDbMessage(&protomsg.EsPersonCacheChange{
+				Type: protomsg.EsCacheChanged_T_DbTable,
+				PersonId: "",
+				TableId: []string{ dbtable.Id },
+				Feature: "",
+				Action: protomsg.DbAction_Insert,
+			})
+		} else {
+			discovery.AddDbMessage(&protomsg.EsPersonCacheChange{
+				Type: protomsg.EsCacheChanged_T_DbTable,
+				PersonId: "",
+				TableId: []string{ dbtable.Id },
+				Feature: "",
+				Action: protomsg.DbAction_Delete,
+			})
+		}
 		util.ResponseFormat(c, code.Success, result["data"])
 	} else {
 		//code.ServiceInsideError.Message += result["msg"].(string)
diff --git a/discovery/server.go b/discovery/server.go
index cedfc10..52ac5d3 100644
--- a/discovery/server.go
+++ b/discovery/server.go
@@ -3,10 +3,9 @@
 import (
 	"basic.com/pubsub/protomsg.git"
 	"basic.com/valib/gopherdiscovery.git"
-	"fmt"
+	"github.com/gogo/protobuf/proto"
 	"time"
 	"webserver/extend/logger"
-	"github.com/gogo/protobuf/proto"
 )
 
 const (
@@ -16,7 +15,7 @@
 
 //鍚姩discovery鐨剆erver
 var discoveryServer *gopherdiscovery.DiscoveryServer
-var dbChangeChan chan *protomsg.DbChangeMessage
+var dbChangeChan chan *protomsg.EsPersonCacheChange
 func StartServer() {
 	var clients []string
 	var err error
@@ -34,7 +33,7 @@
 	logger.Debug("err:",err)
 	logger.Debug("clients: ",clients)
 
-	dbChangeChan = make(chan *protomsg.DbChangeMessage)
+	dbChangeChan = make(chan *protomsg.EsPersonCacheChange)
 
 	for {
 		select {
@@ -45,14 +44,14 @@
 }
 
 //骞挎挱鏁版嵁搴撴敼鍙樼殑msg
-func publishMessage(msg *protomsg.DbChangeMessage) {
+func publishMessage(msg *protomsg.EsPersonCacheChange) {
 	sendBytes,err := proto.Marshal(msg)
 	if err ==nil{
 		discoveryServer.PublishMsg(string(sendBytes))
 	}
 }
 
-func AddDbMessage(msg *protomsg.DbChangeMessage) {
-	fmt.Println("MSG In")
+func AddDbMessage(msg *protomsg.EsPersonCacheChange) {
+	logger.Debug("MSG EsPersonCacheChange In")
 	dbChangeChan<-msg
 }
\ No newline at end of file
diff --git a/service/EnableStatusControlService.go b/service/EnableStatusControlService.go
index a7dae12..7206b0b 100644
--- a/service/EnableStatusControlService.go
+++ b/service/EnableStatusControlService.go
@@ -2,11 +2,13 @@
 
 import (
 	"basic.com/dbapi.git"
+	"basic.com/pubsub/protomsg.git"
 	"encoding/json"
 	"fmt"
 	"strconv"
 	"strings"
 	"time"
+	"webserver/discovery"
 	"webserver/extend/config"
 	"webserver/extend/esutil"
 	"webserver/extend/logger"
@@ -117,6 +119,23 @@
 	}
 	if middle > 0 {
 		logger.Debug("淇敼鎴愬姛")
+		if flag {//鐢熸晥鐨�
+			discovery.AddDbMessage(&protomsg.EsPersonCacheChange{
+				Type: protomsg.EsCacheChanged_T_DbTable,
+				PersonId: "",
+				TableId: id,
+				Feature: "",
+				Action: protomsg.DbAction_Insert,
+			})
+		} else {//澶辨晥鐨�
+			discovery.AddDbMessage(&protomsg.EsPersonCacheChange{
+				Type: protomsg.EsCacheChanged_T_DbTable,
+				PersonId: "",
+				TableId: id,
+				Feature: "",
+				Action: protomsg.DbAction_Insert,
+			})
+		}
 		message = "淇敼鎴愬姛"
 	}
 	return message

--
Gitblit v1.8.0