From 96536e0052cc29e9fb9fab729fe2f50f53bcc084 Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期五, 10 十一月 2023 17:56:53 +0800
Subject: [PATCH] 切换设备时不需要重启任务

---
 nsq/msg_handler.go |   39 ++++++++++++++++++++++++++++++++++++++-
 1 files changed, 38 insertions(+), 1 deletions(-)

diff --git a/nsq/msg_handler.go b/nsq/msg_handler.go
index e791fff..82d2118 100644
--- a/nsq/msg_handler.go
+++ b/nsq/msg_handler.go
@@ -268,6 +268,7 @@
 		Procedures:       strings.Join(procedures, ","),
 		ExtChannelAmount: msg.ExtChannelAmount,
 		DeviceMac:        msg.DeviceMac,
+		DeviceName:       msg.DeviceName,
 	}
 
 	oldRecord, err := model.NewDeviceSearch().SetDeviceId(msg.ID).First()
@@ -275,7 +276,8 @@
 		err = model.NewDeviceSearch().Create(deviceRecord)
 	} else if oldRecord.ExtChannelAmount != deviceRecord.ExtChannelAmount ||
 		oldRecord.Procedures != deviceRecord.Procedures ||
-		oldRecord.DeviceMac != deviceRecord.DeviceMac {
+		oldRecord.DeviceMac != deviceRecord.DeviceMac ||
+		oldRecord.DeviceName != deviceRecord.DeviceName {
 		err = model.NewDeviceSearch().SetDeviceId(msg.ID).Save(deviceRecord)
 	}
 
@@ -345,6 +347,8 @@
 		err = slf.DealProcessModelData(pullDataResponse.Data)
 	case common.PullDataTypeDevice:
 		err = slf.DealDeviceData(pullDataResponse.Data)
+	case common.PullDataTypeProcessModelPlcAddress:
+		err = slf.DealProcessModelPlcAddressData(pullDataResponse.Data)
 	}
 	if err != nil {
 		logx.Infof("process pull data  err :%s", err)
@@ -393,5 +397,38 @@
 }
 
 func (slf *PullDataResponse) DealDeviceData(data interface{}) error {
+	//宸插湪鍒殑topic澶勭悊
+	return nil
+}
+
+func (slf *PullDataResponse) DealProcessModelPlcAddressData(data interface{}) error {
+	var addressList []*model.ProcessModelPlcAddress
+	err := mapstructure.Decode(data, &addressList)
+	if err != nil {
+		return err
+	}
+	deviceIDs := make([]string, 0, len(addressList))
+	for _, item := range addressList {
+		deviceIDs = append(deviceIDs, item.DeviceID)
+	}
+	existsRecords, err := model.NewProcessModelPlcAddressSearch().SetDeviceIDs(deviceIDs).FindNotTotal()
+	if err != nil {
+		return err
+	}
+
+	existsRecordsMap := make(map[string]*model.ProcessModelPlcAddress, len(existsRecords))
+	for _, item := range existsRecords {
+		existsRecordsMap[item.DeviceID] = item
+	}
+
+	for _, item := range addressList {
+		if v, exists := existsRecordsMap[item.DeviceID]; !exists {
+			item.ID = 0
+			err = model.NewProcessModelPlcAddressSearch().Create(item)
+		} else if v.Address != item.Address {
+			v.Address = item.Address
+			err = model.NewProcessModelPlcAddressSearch().Save(v)
+		}
+	}
 	return nil
 }

--
Gitblit v1.8.0