From 8324f872ef3a4d0c978a9b1d062800c6a1701c12 Mon Sep 17 00:00:00 2001 From: zhangqian <zhangqian@123.com> Date: 星期五, 01 十二月 2023 09:58:17 +0800 Subject: [PATCH] fix --- model/device.go | 244 +++++++++++++++++++++++++++++++++++------------- 1 files changed, 176 insertions(+), 68 deletions(-) diff --git a/model/device.go b/model/device.go index 9ac1006..1e490d3 100644 --- a/model/device.go +++ b/model/device.go @@ -1,49 +1,161 @@ 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:"column:device_id;type:varchar(255);not null;unique" json:"deviceID"` //璁惧缂栧彿 + DeviceName string `gorm:"column:device_name;type:varchar(255);" json:"deviceName"` //璁惧鍚嶇О + ExtChannelAmount int `gorm:"type:tinyint;default:0" json:"extChannelAmount"` + Procedures string `gorm:"column:procedure;type:varchar(255);not null;default ''" json:"procedures"` //璁惧鏀寔鐨勫伐搴忥紝鐢ㄩ�楀彿鍒嗛殧 + DeviceMac string `gorm:"type:varchar(255);" json:"deviceMac"` //缁戝畾鐨勫伐鎺ф満璁惧ID + ProceduresArr []string `gorm:"-" json:"ProceduresArr"` //璁惧鏀寔鐨勫伐搴忓垏鐗� + NeedSetProcessParams bool `gorm:"column:need_set_process_params" json:"needSetProcessParams"` //鏄惁闇�瑕佽缃伐鑹哄弬鏁� //璁惧鏀寔鐨勫伐搴忓垏鐗� } 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) SetDeviceMac(deviceMac string) *DeviceSearch { + slf.DeviceMac = deviceMac + 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 slf.DeviceMac != "" { + db = db.Where("device_mac = ?", slf.DeviceMac) + } + + 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 +171,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 +181,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 +191,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