From a455a9d0ca01baa985acb2b69a8d72bee982ed66 Mon Sep 17 00:00:00 2001 From: zhangqian <zhangqian@123.com> Date: 星期六, 07 十月 2023 20:52:12 +0800 Subject: [PATCH] gorm版本v2改成v1 fix --- model/procedures.go | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 files changed, 52 insertions(+), 5 deletions(-) diff --git a/model/procedures.go b/model/procedures.go index 6a4762e..b3d9e73 100644 --- a/model/procedures.go +++ b/model/procedures.go @@ -6,17 +6,17 @@ "apsClient/pkg/sqlitex" "encoding/json" "fmt" - "gorm.io/gorm" + "github.com/jinzhu/gorm" ) type ( Procedures struct { - gorm.Model `json:"-"` - ID int `gorm:"primarykey"` + gorm.Model WorkOrderID string `gorm:"index;type:varchar(191);not null;comment:宸ュ崟ID" json:"-"` OrderID string `gorm:"index;type:varchar(191);not null;comment:璁㈠崟ID" json:"-"` DeviceID string `gorm:"index;type:varchar(191);comment:璁惧ID" json:"deviceId"` ProcedureID string `gorm:"index;type:varchar(191);comment:宸ュ簭ID" json:"procedureId"` + Channel int32 `gorm:"index;comment:閫氶亾" json:"channel"` //閫氶亾 StartTime int64 `gorm:"comment:璁″垝寮�濮嬫椂闂�" json:"startTime"` EndTime int64 `gorm:"comment:璁″垝缁撴潫鏃堕棿" json:"endTime"` Status ProcedureStatus @@ -32,8 +32,10 @@ Orm *gorm.DB Preload bool StartTimeMax int64 + EndTimeMin int64 StatusNot ProcedureStatus ProcedureIds []string + Channels []int32 } ) @@ -96,7 +98,12 @@ return slf } -func (slf *ProceduresSearch) SetId(id int) *ProceduresSearch { +func (slf *ProceduresSearch) SetEndTimeMin(ts int64) *ProceduresSearch { + slf.EndTimeMin = ts + return slf +} + +func (slf *ProceduresSearch) SetId(id uint) *ProceduresSearch { slf.ID = id return slf } @@ -123,6 +130,11 @@ func (slf *ProceduresSearch) SetPreload(preload bool) *ProceduresSearch { slf.Preload = preload + return slf +} + +func (slf *ProceduresSearch) SetChannels(channels []int32) *ProceduresSearch { + slf.Channels = channels return slf } @@ -153,6 +165,10 @@ db = db.Where("start_time <= ?", slf.StartTimeMax) } + if slf.EndTimeMin != 0 { + db = db.Where("end_time > ?", slf.EndTimeMin) + } + if slf.Status != 0 { db = db.Where("status = ?", slf.Status) } @@ -162,11 +178,15 @@ } if len(slf.ProcedureIds) > 0 { - db = db.Where("procedure_id in ?", slf.ProcedureIds) + db = db.Where("procedure_id IN (?)", slf.ProcedureIds) } if slf.ProcedureID != "" { db = db.Where("procedure_id = ?", slf.ProcedureID) + } + + if len(slf.Channels) > 0 { + db = db.Where("channel IN (?)", slf.Channels) } return db @@ -189,6 +209,22 @@ if err := db.Create(&records).Error; err != nil { return fmt.Errorf("create batch err: %v, records: %+v", err, records) + } + + return nil +} + +func (slf *ProceduresSearch) Upsert(record *Procedures) error { + var db = slf.build() + old, err := slf.First() + if err != gorm.ErrRecordNotFound && old.ID != 0 { + record.ID = old.ID + err = db.Save(&record).Error + } else { + err = db.Create(&record).Error + } + if err != nil { + return fmt.Errorf("save err: %v, record: %+v", err, record) } return nil @@ -271,6 +307,17 @@ return records, total, nil } +func (slf *ProceduresSearch) Count() (int64, error) { + var ( + total int64 + db = slf.build() + ) + if err := db.Count(&total).Error; err != nil { + return total, fmt.Errorf("find count err: %v", err) + } + return total, nil +} + func (slf *ProceduresSearch) FindNotTotal() ([]*Procedures, error) { var ( records = make([]*Procedures, 0) -- Gitblit v1.8.0