fix
zhangqian
2023-10-19 cab6eea89a0d82710369604ecb51f1cdb122e433
service/device_plc.go
@@ -7,7 +7,8 @@
   "apsClient/pkg/logx"
   "apsClient/pkg/structx"
   "encoding/json"
   "gorm.io/gorm"
   "errors"
   "github.com/jinzhu/gorm"
)
type DevicePlcService struct{}
@@ -16,19 +17,19 @@
   return DevicePlcService{}
}
func (DevicePlcService) GetDevicePlc() (*model.DevicePlc, int) {
func (slf DevicePlcService) GetDevicePlc() (*model.DevicePlc, int) {
   DevicePlc, err := model.NewDevicePlcSearch().SetOrder("id desc").First()
   if err == gorm.ErrRecordNotFound {
      return &model.DevicePlc{
         Brand:     "",
         Method:    "",
         PortName:  "",
         Frequency: 0,
         Address:   "",
         Port:      0,
         IsOpen:    false,
         Detail:    "",
         Details:   make([]*model.DevicePlcAddress, 0),
         Brand:      "",
         Method:     "",
         Address:    "",
         Port:       0,
         BaudRate:   0,
         SerialName: "",
         IsOpen:     false,
         Detail:     "",
         Details:    make([]*model.DevicePlcAddress, 0),
      }, ecode.OK
   }
   if err != nil {
@@ -38,6 +39,17 @@
   return DevicePlc, ecode.OK
}
func (slf DevicePlcService) GetDeviceChannelAmount() (int, error) {
   plcConfig, code := slf.GetDevicePlc()
   if code != ecode.OK || plcConfig.ID == 0 {
      return 0, errors.New("未获取到PLC地址,请在工艺模型库中上传!")
   }
   if len(plcConfig.Details) == 0 {
      return 0, errors.New("未获取到PLC地址,请在工艺模型库中上传!")
   }
   return len(plcConfig.Details) / 2, nil
}
func (DevicePlcService) UpdateDevicePlc(plc *request.UpdatePlc) int {
   var record model.DevicePlc
   err := structx.AssignTo(plc, &record)