From 8d2a95fc0eeabe1b13d0a914c9ec2845d42c0be3 Mon Sep 17 00:00:00 2001
From: zhangzengfei <zhangzengfei@smartai.com>
Date: 星期四, 19 十月 2023 11:32:57 +0800
Subject: [PATCH] 添加主从serf切换事件
---
model/procedures.go | 184 +++++++++++++++++++++++++++++++++++++++++-----
1 files changed, 164 insertions(+), 20 deletions(-)
diff --git a/model/procedures.go b/model/procedures.go
index 1dfc611..64d3cbb 100644
--- a/model/procedures.go
+++ b/model/procedures.go
@@ -1,39 +1,65 @@
package model
import (
+ "apsClient/model/common"
+ "apsClient/pkg/logx"
"apsClient/pkg/sqlitex"
+ "encoding/json"
"fmt"
- "gorm.io/gorm"
+ "github.com/jinzhu/gorm"
)
type (
Procedures struct {
- gorm.Model `json:"-"`
- Id int `json:"id"`
- ProcedureId string `json:"orderId"`
- ProcedureName string `json:"product"`
- DeviceId string `json:"deviceId"`
- StartTime string `json:"startTime"`
- EndTime string `json:"endTime"`
- WorkHours string `json:"workHours"`
- InputMaterialsId int `json:"-"`
- InputMaterials *Materials `json:"inputMaterials" gorm:"foreignKey:InputMaterialsId"`
- OutputMaterialsId int `json:"-"`
- OutputMaterials *Materials `json:"outputMaterials" gorm:"foreignKey:InputMaterialsId"`
+ gorm.Model `json:"-"`
+ WorkOrderID string `gorm:"index;type:varchar(191);not null" json:"-"`
+ OrderID string `gorm:"index;type:varchar(191);not null" json:"-"`
+ DeviceID string `gorm:"index;type:varchar(191)" json:"deviceId"`
+ ProcedureID string `gorm:"index;type:varchar(191)" json:"procedureId"`
+ Channel int32 `gorm:"index;" json:"channel"` //閫氶亾
+ ProcessModelNumber string `gorm:"index;" json:"processModelNumber"` //宸ヨ壓妯″瀷缂栧彿
+ StartTime int64 `json:"startTime"`
+ EndTime int64 `json:"endTime"`
+ Status ProcedureStatus
+ ProcedureData string `json:"-"` //common.ProductProcedure json涓�
+ ProceduresInfo common.ProductProcedure `json:"procedure" gorm:"-"` //common.ProductProcedure 瀵硅薄
}
ProceduresSearch struct {
Procedures
- Order string
- PageNum int
- PageSize int
- Orm *gorm.DB
- Preload bool
+ Order string
+ PageNum int
+ PageSize int
+ Orm *gorm.DB
+ Preload bool
+ StartTimeMax int64
+ EndTimeMin int64
+ StatusNot ProcedureStatus
+ ProcedureIds []string
+ Channels []int32
}
+)
+
+type ProcedureStatus int
+
+const (
+ ProcedureStatusWaitProcess ProcedureStatus = 1
+ ProcedureStatusProcessing ProcedureStatus = 2
+ ProcedureStatusFinished ProcedureStatus = 3
)
func (slf *Procedures) TableName() string {
return "procedures"
+}
+func (slf *Procedures) AfterFind(db *gorm.DB) error {
+ var proceduresInfo common.ProductProcedure
+ err := json.Unmarshal([]byte(slf.ProcedureData), &proceduresInfo)
+ if err != nil {
+ logx.Errorf("AfterFind Unmarshal err: %v", err.Error())
+ return err
+ }
+ slf.ProceduresInfo = proceduresInfo
+ return nil
}
func NewProceduresSearch(db *gorm.DB) *ProceduresSearch {
@@ -58,8 +84,58 @@
return slf
}
+func (slf *ProceduresSearch) SetWorkOrderId(orderId string) *ProceduresSearch {
+ slf.WorkOrderID = orderId
+ return slf
+}
+
+func (slf *ProceduresSearch) SetProcedureId(id string) *ProceduresSearch {
+ slf.ProcedureID = id
+ return slf
+}
+
+func (slf *ProceduresSearch) SetStartTimeMax(ts int64) *ProceduresSearch {
+ slf.StartTimeMax = ts
+ return slf
+}
+
+func (slf *ProceduresSearch) SetEndTimeMin(ts int64) *ProceduresSearch {
+ slf.EndTimeMin = ts
+ return slf
+}
+
+func (slf *ProceduresSearch) SetId(id uint) *ProceduresSearch {
+ slf.ID = id
+ return slf
+}
+
+func (slf *ProceduresSearch) SetProcedureIds(procedureIds []string) *ProceduresSearch {
+ slf.ProcedureIds = procedureIds
+ return slf
+}
+
+func (slf *ProceduresSearch) SetDeviceId(id string) *ProceduresSearch {
+ slf.DeviceID = id
+ return slf
+}
+
+func (slf *ProceduresSearch) SetStatus(status ProcedureStatus) *ProceduresSearch {
+ slf.Status = status
+ return slf
+}
+
+func (slf *ProceduresSearch) SetStatusNot(status ProcedureStatus) *ProceduresSearch {
+ slf.StatusNot = status
+ return slf
+}
+
func (slf *ProceduresSearch) SetPreload(preload bool) *ProceduresSearch {
slf.Preload = preload
+ return slf
+}
+
+func (slf *ProceduresSearch) SetChannels(channels []int32) *ProceduresSearch {
+ slf.Channels = channels
return slf
}
@@ -69,8 +145,49 @@
if slf.Order != "" {
db = db.Order(slf.Order)
}
+
+ if slf.ID != 0 {
+ db = db.Where("id = ?", slf.ID)
+ }
+
+ if slf.WorkOrderID != "" {
+ db = db.Where("work_order_id = ?", slf.WorkOrderID)
+ }
+
+ if slf.DeviceID != "" {
+ db = db.Where("device_id = ?", slf.DeviceID)
+ }
+
if slf.Preload {
db = db.Preload("InputMaterials").Preload("OutputMaterials")
+ }
+
+ if slf.StartTimeMax != 0 {
+ 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)
+ }
+
+ if slf.StatusNot != 0 {
+ db = db.Where("status <> ?", slf.StatusNot)
+ }
+
+ if len(slf.ProcedureIds) > 0 {
+ 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
@@ -98,10 +215,26 @@
return nil
}
-func (slf *ProceduresSearch) Save(record *Procedures) error {
+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
+}
+
+func (slf *ProceduresSearch) Updates(record *Procedures) error {
var db = slf.build()
- if err := db.Save(record).Error; err != nil {
+ if err := db.Updates(record).Error; err != nil {
return fmt.Errorf("save err: %v, record: %+v", err, record)
}
@@ -175,6 +308,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