From 578b74f9de4b96e88e2fddb726c7c6f78162b033 Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期六, 21 十月 2023 14:34:23 +0800
Subject: [PATCH] 启停生产者
---
model/device.go | 232 +++++++++++++++++++++++++++++++++++++++++-----------------
1 files changed, 164 insertions(+), 68 deletions(-)
diff --git a/model/device.go b/model/device.go
index 9ac1006..59c7859 100644
--- a/model/device.go
+++ b/model/device.go
@@ -1,49 +1,149 @@
package model
import (
- "apsClient/pkg/mysqlx"
+ "apsClient/pkg/sqlitex"
"fmt"
- "gorm.io/gorm"
+ "github.com/jinzhu/gorm"
+ "strings"
)
type (
+ // Device 璁惧
Device struct {
- ID int `json:"id" gorm:"primaryKey;type:bigint(20);comment:璁惧ID"`
- Name string `json:"name" gorm:"index;type:varchar(255);comment:璁惧鍚嶇О"`
- IP string `json:"IP" gorm:"index;type:varchar(255);comment:璁惧IP"`
- Account string `json:"account" gorm:"index;type:varchar(255);comment:root璐﹀彿"`
- Password string `json:"password" gorm:"index;type:varchar(255);comment:root瀵嗙爜"`
- Port string `json:"port" gorm:"index;type:varchar(255);comment:绔彛鍙�"`
+ gorm.Model
+ DeviceID string `gorm:"unique;column:device_id;type:varchar(255);not null;default ''" json:"deviceID"` //璁惧缂栧彿
+ ExtChannelAmount int `gorm:"type:tinyint;default:0" json:"extChannelAmount"`
+ Procedures string `gorm:"column:procedure;type:varchar(255);not null;default ''" json:"procedures"` //璁惧鏀寔鐨勫伐搴忥紝鐢ㄩ�楀彿鍒嗛殧
+ ProceduresArr []string `gorm:"-" json:"procedureAdd"` //璁惧鏀寔鐨勫伐搴忓垏鐗�
}
DeviceSearch struct {
Device
- Order string
- PageNum int
- PageSize int
- Orm *gorm.DB
+ Order string
+ PageNum int
+ PageSize int
+ Orm *gorm.DB
+ DeviceIDs []string
}
)
-func (slf Device) TableName() string {
+func (slf *Device) TableName() string {
return "device"
}
-func NewDeviceSearch(db *gorm.DB) *DeviceSearch {
- if db == nil {
- db = mysqlx.GetDB()
+func (slf *Device) AfterFind(db *gorm.DB) error {
+ slf.ProceduresArr = strings.Split(slf.Procedures, ",")
+ return nil
+}
+
+func NewDeviceSearch() *DeviceSearch {
+ return &DeviceSearch{Orm: sqlitex.GetDB()}
+}
+
+func (slf *DeviceSearch) SetOrm(tx *gorm.DB) *DeviceSearch {
+ slf.Orm = tx
+ return slf
+}
+
+func (slf *DeviceSearch) SetPage(page, size int) *DeviceSearch {
+ slf.PageNum, slf.PageSize = page, size
+ return slf
+}
+
+func (slf *DeviceSearch) SetOrder(order string) *DeviceSearch {
+ slf.Order = order
+ return slf
+}
+
+func (slf *DeviceSearch) SetID(id uint) *DeviceSearch {
+ slf.ID = id
+ return slf
+}
+
+func (slf *DeviceSearch) SetDeviceId(deviceId string) *DeviceSearch {
+ slf.DeviceID = deviceId
+ return slf
+}
+
+func (slf *DeviceSearch) SetDeviceIds(deviceIds []string) *DeviceSearch {
+ slf.DeviceIDs = deviceIds
+ return slf
+}
+
+func (slf *DeviceSearch) build() *gorm.DB {
+ var db = slf.Orm.Table(slf.TableName())
+
+ if slf.ID != 0 {
+ db = db.Where("id = ?", slf.ID)
}
- return &DeviceSearch{Orm: db}
+
+ if len(slf.DeviceID) != 0 {
+ db = db.Where("device_id = ?", slf.DeviceID)
+ }
+
+ if len(slf.DeviceIDs) != 0 {
+ db = db.Where("device_id in (?)", slf.DeviceIDs)
+ }
+
+ if slf.Order != "" {
+ db = db.Order(slf.Order)
+ }
+
+ return db
}
-func (slf *DeviceSearch) SetDeviceName(name string) *DeviceSearch {
- slf.Name = name
- return slf
+// Create 鍗曟潯鎻掑叆
+func (slf *DeviceSearch) Create(record *Device) error {
+ var db = slf.build()
+
+ if err := db.Create(record).Error; err != nil {
+ return fmt.Errorf("create err: %v, record: %+v", err, record)
+ }
+
+ return nil
}
-func (slf *DeviceSearch) SetDeviceIp(ip string) *DeviceSearch {
- slf.IP = ip
- return slf
+func (slf *DeviceSearch) Save(record *Device) error {
+ var db = slf.build()
+
+ if err := db.Updates(record).Error; err != nil {
+ return fmt.Errorf("create err: %v, record: %+v", err, record)
+ }
+ return nil
+}
+
+func (slf *DeviceSearch) UpdateByMap(upMap map[string]interface{}) error {
+ var (
+ db = slf.build()
+ )
+
+ if err := db.Updates(upMap).Error; err != nil {
+ return fmt.Errorf("update by map err: %v, upMap: %+v", err, upMap)
+ }
+
+ return nil
+}
+
+func (slf *DeviceSearch) UpdateByQuery(query string, args []interface{}, upMap map[string]interface{}) error {
+ var (
+ db = slf.Orm.Table(slf.TableName()).Where(query, args...)
+ )
+
+ if err := db.Updates(upMap).Error; err != nil {
+ return fmt.Errorf("update by query err: %v, query: %s, args: %+v, upMap: %+v", err, query, args, upMap)
+ }
+
+ return nil
+}
+
+func (slf *DeviceSearch) Delete() error {
+ var db = slf.build()
+
+ if err := db.Unscoped().Delete(&Device{}).Error; err != nil {
+ return err
+ }
+
+ return nil
}
func (slf *DeviceSearch) First() (*Device, error) {
@@ -59,16 +159,6 @@
return record, nil
}
-func (slf *DeviceSearch) SetPage(page, size int) *DeviceSearch {
- slf.PageNum, slf.PageSize = page, size
- return slf
-}
-
-func (slf *DeviceSearch) SetOrder(order string) *DeviceSearch {
- slf.Order = order
- return slf
-}
-
func (slf *DeviceSearch) Find() ([]*Device, int64, error) {
var (
records = make([]*Device, 0)
@@ -79,11 +169,9 @@
if err := db.Count(&total).Error; err != nil {
return records, total, fmt.Errorf("find count err: %v", err)
}
-
if slf.PageNum*slf.PageSize > 0 {
db = db.Offset((slf.PageNum - 1) * slf.PageSize).Limit(slf.PageSize)
}
-
if err := db.Find(&records).Error; err != nil {
return records, total, fmt.Errorf("find records err: %v", err)
}
@@ -91,48 +179,56 @@
return records, total, nil
}
-func (slf *DeviceSearch) build() *gorm.DB {
- var db = slf.Orm.Model(&Device{})
+func (slf *DeviceSearch) FindNotTotal() ([]*Device, error) {
+ var (
+ records = make([]*Device, 0)
+ db = slf.build()
+ )
- if slf.ID > 0 {
- db = db.Where("id = ?", slf.ID)
+ if slf.PageNum*slf.PageSize > 0 {
+ db = db.Offset((slf.PageNum - 1) * slf.PageSize).Limit(slf.PageSize)
+ }
+ if err := db.Find(&records).Error; err != nil {
+ return records, fmt.Errorf("find records err: %v", err)
}
- if slf.IP != "" {
- db = db.Where("ip = ?", slf.IP)
- }
-
- if slf.Account != "" {
- db = db.Where("account = ?", slf.Account)
- }
-
- if slf.Password != "" {
- db = db.Where("password = ?", slf.Password)
- }
-
- if slf.Port != "" {
- db = db.Where("port = ?", slf.Port)
- }
-
- return db
+ return records, nil
}
-func (slf *DeviceSearch) Create(record *Device) error {
- var db = slf.build()
+// FindByQuery 鎸囧畾鏉′欢鏌ヨ.
+func (slf *DeviceSearch) FindByQuery(query string, args []interface{}) ([]*Device, int64, error) {
+ var (
+ records = make([]*Device, 0)
+ total int64
+ db = slf.Orm.Table(slf.TableName()).Where(query, args...)
+ )
- if err := db.Create(record).Error; err != nil {
- return fmt.Errorf("create err: %v, record: %+v", err, record)
+ if err := db.Count(&total).Error; err != nil {
+ return records, total, fmt.Errorf("find by query count err: %v", err)
+ }
+ if slf.PageNum*slf.PageSize > 0 {
+ db = db.Offset((slf.PageNum - 1) * slf.PageSize).Limit(slf.PageSize)
+ }
+ if err := db.Find(&records).Error; err != nil {
+ return records, total, fmt.Errorf("find by query records err: %v, query: %s, args: %+v", err, query, args)
}
- return nil
+ return records, total, nil
}
-func (slf *DeviceSearch) SetId(id int) *DeviceSearch {
- slf.ID = id
- return slf
-}
+// FindByQueryNotTotal 鎸囧畾鏉′欢鏌ヨ&涓嶆煡璇㈡�绘潯鏁�.
+func (slf *DeviceSearch) FindByQueryNotTotal(query string, args []interface{}) ([]*Device, error) {
+ var (
+ records = make([]*Device, 0)
+ db = slf.Orm.Table(slf.TableName()).Where(query, args...)
+ )
-func (slf *DeviceSearch) SetIds(ids []int) *DeviceSearch {
- slf.Orm = slf.Orm.Where("id in (?)", ids)
- return slf
+ if slf.PageNum*slf.PageSize > 0 {
+ db = db.Offset((slf.PageNum - 1) * slf.PageSize).Limit(slf.PageSize)
+ }
+ if err := db.Find(&records).Error; err != nil {
+ return records, fmt.Errorf("find by query records err: %v, query: %s, args: %+v", err, query, args)
+ }
+
+ return records, nil
}
--
Gitblit v1.8.0