From cacf363f222e354cad6db18c4a00fb1097d35c2c Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期四, 09 十一月 2023 14:30:58 +0800
Subject: [PATCH] fix

---
 service/device_plc.go |   17 ++++++++++++-----
 1 files changed, 12 insertions(+), 5 deletions(-)

diff --git a/service/device_plc.go b/service/device_plc.go
index 90c3e51..d76e781 100644
--- a/service/device_plc.go
+++ b/service/device_plc.go
@@ -1,6 +1,7 @@
 package service
 
 import (
+	"apsClient/conf"
 	"apsClient/model"
 	"apsClient/model/request"
 	"apsClient/pkg/ecode"
@@ -18,21 +19,26 @@
 }
 
 func (slf DevicePlcService) GetDevicePlc() (*model.DevicePlc, int) {
-	DevicePlc, err := model.NewDevicePlcSearch().SetOrder("id desc").First()
+	DevicePlc, err := model.NewDevicePlcSearch().SetDeviceId(conf.Conf.CurrentDeviceID).First()
 	if err == gorm.ErrRecordNotFound {
+		logx.Errorf("GetDevicePlc failed:%v", err)
 		return &model.DevicePlc{
+			DeviceID:   "",
 			Brand:      "",
 			Method:     "",
 			Address:    "",
 			Port:       0,
 			BaudRate:   0,
 			SerialName: "",
+			DataBit:    0,
+			StopBit:    0,
+			Parity:     0,
 			IsOpen:     false,
-			Detail:     "",
 			Details:    make([]*model.DevicePlcAddress, 0),
 		}, ecode.OK
 	}
 	if err != nil {
+		logx.Errorf("GetDevicePlc failed:%v", err)
 		return nil, ecode.DBErr
 	}
 
@@ -42,9 +48,11 @@
 func (slf DevicePlcService) GetDeviceChannelAmount() (int, error) {
 	plcConfig, code := slf.GetDevicePlc()
 	if code != ecode.OK || plcConfig.ID == 0 {
+		logx.Errorf("GetDeviceChannelAmount failed:%v, code:%v", plcConfig, code)
 		return 0, errors.New("鏈幏鍙栧埌PLC鍦板潃锛岃鍦ㄥ伐鑹烘ā鍨嬪簱涓笂浼狅紒")
 	}
 	if len(plcConfig.Details) == 0 {
+		logx.Errorf("GetDeviceChannelAmount failed, addressList == 0, %+v", plcConfig)
 		return 0, errors.New("鏈幏鍙栧埌PLC鍦板潃锛岃鍦ㄥ伐鑹烘ā鍨嬪簱涓笂浼狅紒")
 	}
 	return len(plcConfig.Details) / 2, nil
@@ -63,11 +71,10 @@
 		return ecode.ParamsErr
 	}
 	record.Detail = string(detail)
-
-	if plc.Id == 0 {
+	if record.ID == 0 {
 		err = model.NewDevicePlcSearch().Create(&record)
 	} else {
-		err = model.NewDevicePlcSearch().SetId(plc.Id).Save(&record)
+		err = model.NewDevicePlcSearch().Save(&record)
 	}
 
 	if err != nil {

--
Gitblit v1.8.0