From 8324f872ef3a4d0c978a9b1d062800c6a1701c12 Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期五, 01 十二月 2023 09:58:17 +0800
Subject: [PATCH] fix

---
 service/device.go |   67 +++++++++++++++++++++++++++++++--
 1 files changed, 63 insertions(+), 4 deletions(-)

diff --git a/service/device.go b/service/device.go
index 29f2784..1906d99 100644
--- a/service/device.go
+++ b/service/device.go
@@ -2,14 +2,21 @@
 
 import (
 	"apsClient/conf"
+	"apsClient/constvar"
 	"apsClient/model"
+	"apsClient/model/response"
 	"apsClient/pkg/logx"
+	"errors"
+	"github.com/jinzhu/gorm"
 	"os"
 	"strings"
 )
 
 func GetDeviceIDList() (deviceIds []string, err error) {
 	devices, err := model.NewDeviceSearch().SetDeviceMac(conf.Conf.System.DeviceId).FindNotTotal()
+	if err == gorm.ErrRecordNotFound {
+		return nil, nil
+	}
 	if err != nil {
 		return nil, err
 	}
@@ -20,7 +27,26 @@
 	return deviceIds, nil
 }
 
-func InitCurrentDeviceID() (err error) {
+func GetDeviceList() (deviceList []*response.Device, err error) {
+	devices, err := model.NewDeviceSearch().SetDeviceMac(conf.Conf.System.DeviceId).FindNotTotal()
+	if err == gorm.ErrRecordNotFound {
+		return nil, nil
+	}
+	if err != nil {
+		return nil, err
+	}
+	deviceList = make([]*response.Device, 0, len(devices))
+	for _, device := range devices {
+		deviceList = append(deviceList, &response.Device{
+			DeviceID:             device.DeviceID,
+			DeviceName:           device.DeviceName,
+			NeedSetProcessParams: device.NeedSetProcessParams,
+		})
+	}
+	return deviceList, nil
+}
+
+func InitCurrentDeviceID(ServerID string) (err error) {
 	currentDeviceID := ReadDeviceIDFromFile()
 	if currentDeviceID != "" {
 		conf.Conf.CurrentDeviceID = currentDeviceID
@@ -30,11 +56,13 @@
 	if err != nil {
 		return err
 	}
-	if len(deviceList) == 0 {
+	if len(deviceList) > 0 {
+		conf.Conf.CurrentDeviceID = deviceList[0]
+	} else if conf.Conf.System.DeviceId != "" {
 		conf.Conf.CurrentDeviceID = conf.Conf.System.DeviceId
-		return nil
+	} else {
+		conf.Conf.CurrentDeviceID = ServerID
 	}
-	conf.Conf.CurrentDeviceID = deviceList[0]
 	SetDeviceIDToFile(conf.Conf.CurrentDeviceID)
 	return nil
 }
@@ -61,3 +89,34 @@
 	deviceId = strings.Trim(deviceId, "\n")
 	return deviceId
 }
+
+func UpdateDevice(deviceId string, needSetProcessParams bool) (err error) {
+	device, err := model.NewDeviceSearch().SetDeviceId(deviceId).First()
+	if err == gorm.ErrRecordNotFound {
+		return errors.New("璁惧涓嶅瓨鍦�")
+	}
+	device.NeedSetProcessParams = needSetProcessParams
+	return model.NewDeviceSearch().SetID(device.ID).UpdateByMap(map[string]interface{}{"need_set_process_params": needSetProcessParams})
+}
+
+func GetCurrentDevice() (device *model.Device, err error) {
+	if conf.Conf.CurrentDeviceID == "" {
+		return nil, errors.New("褰撳墠璁惧ID涓嶅瓨鍦紝璇锋鏌�")
+	}
+	device, err = model.NewDeviceSearch().SetDeviceId(conf.Conf.CurrentDeviceID).First()
+	if err == gorm.ErrRecordNotFound {
+		return nil, errors.New("璁惧涓嶅瓨鍦�")
+	}
+	return device, nil
+}
+
+// ReportsSystemDeviceToCloud 鍒涘缓鍚屾璁惧id璁板綍
+func ReportsSystemDeviceToCloud(systemDeviceID string) {
+	err := model.NewReportsToCloudSearch(nil).Create(&model.ReportsToCloud{
+		ReportType: constvar.ReportTypeSystemDeviceID,
+		Content:    systemDeviceID,
+	})
+	if err != nil {
+		logx.Errorf("ReportsSystemDeviceToCloud create record error:%v", err)
+	}
+}

--
Gitblit v1.8.0