From 81cda4f09d4c0fff6edd087d1c49d05cb6eaab38 Mon Sep 17 00:00:00 2001 From: zhangqian <zhangqian@123.com> Date: 星期一, 09 十月 2023 20:47:27 +0800 Subject: [PATCH] 当前使用的工艺编号置顶 --- model/procedures.go | 51 +++++++++++++++++++++++++++++++++++++++------------ 1 files changed, 39 insertions(+), 12 deletions(-) diff --git a/model/procedures.go b/model/procedures.go index 8d893e6..8a7f145 100644 --- a/model/procedures.go +++ b/model/procedures.go @@ -7,22 +7,24 @@ "encoding/json" "fmt" "gorm.io/gorm" + "gorm.io/gorm/clause" ) type ( Procedures struct { - gorm.Model `json:"-"` - ID int `gorm:"primarykey"` - 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 - ProcedureData string `json:"-"` //common.ProductProcedure json涓� - ProceduresInfo common.ProductProcedure `json:"procedure" gorm:"-"` //common.ProductProcedure 瀵硅薄 + gorm.Model `json:"-"` + ID int `gorm:"primarykey"` + 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"` //閫氶亾 + ProcessModelNumber string `gorm:"index;comment:宸ヨ壓妯″瀷缂栧彿" json:"processModelNumber"` //宸ヨ壓妯″瀷缂栧彿 + StartTime int64 `gorm:"comment:璁″垝寮�濮嬫椂闂�" json:"startTime"` + EndTime int64 `gorm:"comment:璁″垝缁撴潫鏃堕棿" json:"endTime"` + Status ProcedureStatus + ProcedureData string `json:"-"` //common.ProductProcedure json涓� + ProceduresInfo common.ProductProcedure `json:"procedure" gorm:"-"` //common.ProductProcedure 瀵硅薄 } ProceduresSearch struct { @@ -36,6 +38,7 @@ EndTimeMin int64 StatusNot ProcedureStatus ProcedureIds []string + Channels []int32 } ) @@ -133,6 +136,11 @@ return slf } +func (slf *ProceduresSearch) SetChannels(channels []int32) *ProceduresSearch { + slf.Channels = channels + return slf +} + func (slf *ProceduresSearch) build() *gorm.DB { var db = slf.Orm.Model(&Procedures{}) @@ -180,6 +188,10 @@ db = db.Where("procedure_id = ?", slf.ProcedureID) } + if len(slf.Channels) > 0 { + db = db.Where("channel in ?", slf.Channels) + } + return db } @@ -205,6 +217,21 @@ 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 + } + if err := db.Clauses(clause.OnConflict{ + UpdateAll: true, + }).Create(&record).Error; err != nil { + return fmt.Errorf("save err: %v, record: %+v", err, record) + } + + return nil +} + func (slf *ProceduresSearch) Updates(record *Procedures) error { var db = slf.build() -- Gitblit v1.8.0