zhangqian
2023-09-27 b331c9990a0396301e934daffe095f99d62d1c89
model/procedures.go
@@ -7,6 +7,7 @@
   "encoding/json"
   "fmt"
   "gorm.io/gorm"
   "gorm.io/gorm/clause"
)
type (
@@ -17,7 +18,7 @@
      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"`
      Position       int    `gorm:"type:int;comment:工作位置" json:"position"` //每个设备可能有多个机位同时生产,用position表示位置
      Channel        int32  `gorm:"index;comment:通道" json:"channel"` //通道
      StartTime      int64  `gorm:"comment:计划开始时间" json:"startTime"`
      EndTime        int64  `gorm:"comment:计划结束时间" json:"endTime"`
      Status         ProcedureStatus
@@ -36,6 +37,7 @@
      EndTimeMin   int64
      StatusNot    ProcedureStatus
      ProcedureIds []string
      Channels     []int32
   }
)
@@ -133,6 +135,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 +187,10 @@
      db = db.Where("procedure_id = ?", slf.ProcedureID)
   }
   if len(slf.Channels) > 0 {
      db = db.Where("channel in ?", slf.Channels)
   }
   return db
}
@@ -205,6 +216,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()