From ba442a966e3a0a79182f32110e6849d20c79fb28 Mon Sep 17 00:00:00 2001 From: zhangqian <zhangqian@123.com> Date: 星期四, 30 十一月 2023 21:18:26 +0800 Subject: [PATCH] 重构生产进度存储和同步 --- service/progress.go | 97 +++---- model/procedures.go | 25 + model/production_progress.go | 567 +++++++++++++++++++++++----------------------- crontask/cron_task.go | 13 model/task_status_sync.go | 1 api/v1/task.go | 5 6 files changed, 346 insertions(+), 362 deletions(-) diff --git a/api/v1/task.go b/api/v1/task.go index f9741cb..6729282 100644 --- a/api/v1/task.go +++ b/api/v1/task.go @@ -234,6 +234,7 @@ ProductProcedureID: procedure.ProductProcedureID, IsProcessing: false, IsFinish: true, + FinishedQuantity: procedure.FinishedQuantity, } return service.NewTaskService().SaveTaskStatusSync(db, &record) }) @@ -324,10 +325,6 @@ return err } err = taskService.UpdateOrderStatus(db, order.ID, model.OrderStatusProcessing) - if err != nil { - return err - } - err = service.NewProgressService().Add(db, procedure, order) if err != nil { return err } diff --git a/crontask/cron_task.go b/crontask/cron_task.go index 24ffc22..dcc64c0 100644 --- a/crontask/cron_task.go +++ b/crontask/cron_task.go @@ -43,10 +43,10 @@ logx.Infof("plc read finish number err: %v", err) continue } - finishNumber := cast.ToInt64(value) + finishNumber := cast.ToInt(value) if finishNumber != 0 { service.PlcCacheSet(conf.Conf.CurrentDeviceID, addressItem.Channel, constvar.PlcCacheKeyFinishNumber, finishNumber) - _ = service.NewProgressService().UpdateProgress(conf.Conf.CurrentDeviceID, addressItem.Channel, cast.ToInt64(finishNumber)) + _ = service.NewProgressService().UpdateProgress(conf.Conf.CurrentDeviceID, addressItem.Channel, finishNumber) } logx.Infof("plc read finish number: %v", finishNumber) } @@ -115,20 +115,13 @@ } syncOkIds := make([]uint, 0, len(records)) for _, record := range records { - var finishAmount int - if record.IsFinish { - progress, err := model.NewProductionProgressSearch(nil).SetWorkOrderId(record.WorkOrderId).SetProductProcedureId(record.ProductProcedureID).First() - if err == nil { - finishAmount = int(progress.FinishedQuantity) - } - } msg := &common.MsgTaskStatusUpdate{ WorkOrderId: record.WorkOrderId, ProcedureID: record.ProcedureID, DeviceId: record.DeviceId, IsProcessing: record.IsProcessing, IsFinish: record.IsFinish, - FinishAmount: finishAmount, + FinishAmount: record.FinishedQuantity, ProductProcedureID: record.ProductProcedureID, StartTs: record.StartTs, FinishTs: record.FinishTs, diff --git a/model/procedures.go b/model/procedures.go index d30f5a9..79e3842 100644 --- a/model/procedures.go +++ b/model/procedures.go @@ -7,6 +7,7 @@ "encoding/json" "fmt" "github.com/jinzhu/gorm" + "strings" ) type ( @@ -23,6 +24,7 @@ EndTime int64 `json:"endTime"` RealStartTime int64 `json:"realStartTime"` RealEndTime int64 `json:"realEndTime"` + FinishedQuantity int `gorm:"type:int;" json:"finishedQuantity"` Status ProcedureStatus ProcedureData string `json:"-"` //common.ProductProcedure json涓� ProceduresInfo common.ProductProcedure `json:"procedure" gorm:"-"` //common.ProductProcedure 瀵硅薄 @@ -44,6 +46,7 @@ Channels []int32 Offset int Limit int + Fields []string } ) @@ -59,13 +62,15 @@ 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 + if slf.ProcedureData != "" { + 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 } - slf.ProceduresInfo = proceduresInfo return nil } @@ -161,8 +166,8 @@ return slf } -func (slf *ProceduresSearch) SetChannel(channel int32) *ProceduresSearch { - slf.Channel = channel +func (slf *ProceduresSearch) SetFields(fields []string) *ProceduresSearch { + slf.Fields = fields return slf } @@ -225,6 +230,10 @@ db = db.Where("channel IN (?)", slf.Channels) } + if len(slf.Fields) > 0 { + db = db.Select(strings.Join(slf.Fields, ",")) + } + return db } diff --git a/model/production_progress.go b/model/production_progress.go index 58bc036..c6deb29 100644 --- a/model/production_progress.go +++ b/model/production_progress.go @@ -1,285 +1,286 @@ package model -import ( - "apsClient/pkg/sqlitex" - "fmt" - "github.com/jinzhu/gorm" -) - -type ( - ProductionProgress struct { - gorm.Model - ProceduresID uint `gorm:"index;type:varchar(191)" json:"proceduresId"` //procedures琛ㄧ殑id - WorkOrderID string `gorm:"index;type:varchar(191);not null" json:"workOrderID"` - OrderID string `gorm:"index;type:varchar(191);not null" json:"orderID"` - ProcedureID string `gorm:"type:varchar(191)" json:"procedureId"` - ProductProcedureID string `gorm:"type:varchar(191);" json:"productProcedureID"` //浜у搧宸ュ簭id - DeviceID string `gorm:"type:varchar(191);not null" json:"deviceId"` - FinishedQuantity int64 `gorm:"type:int;not null" json:"finishedQuantity"` - Channel int32 `gorm:"type:int" json:"channel"` //閫氶亾 - TotalQuantity int64 `gorm:"type:int;not null" json:"totalQuantity"` - } - - ProductionProgressSearch struct { - ProductionProgress - Order string - PageNum int - PageSize int - Orm *gorm.DB - UnFinished bool - } -) - -func (slf *ProductionProgress) TableName() string { - return "production_progress" -} - -func NewProductionProgressSearch(db *gorm.DB) *ProductionProgressSearch { - if db == nil { - db = sqlitex.GetDB() - } - return &ProductionProgressSearch{Orm: db} -} - -func (slf *ProductionProgressSearch) SetOrm(tx *gorm.DB) *ProductionProgressSearch { - slf.Orm = tx - return slf -} - -func (slf *ProductionProgressSearch) SetPage(page, size int) *ProductionProgressSearch { - slf.PageNum, slf.PageSize = page, size - return slf -} - -func (slf *ProductionProgressSearch) SetOrder(order string) *ProductionProgressSearch { - slf.Order = order - return slf -} - -func (slf *ProductionProgressSearch) SetWorkOrderId(orderId string) *ProductionProgressSearch { - slf.WorkOrderID = orderId - return slf -} - -func (slf *ProductionProgressSearch) SetProcedureId(procedureId string) *ProductionProgressSearch { - slf.ProcedureID = procedureId - return slf -} - -func (slf *ProductionProgressSearch) SetProceduresId(proceduresId uint) *ProductionProgressSearch { - slf.ProceduresID = proceduresId - return slf -} - -func (slf *ProductionProgressSearch) SetDeviceId(id string) *ProductionProgressSearch { - slf.DeviceID = id - return slf -} - -func (slf *ProductionProgressSearch) SetProductProcedureId(productProcedureId string) *ProductionProgressSearch { - slf.ProductProcedureID = productProcedureId - return slf -} - -func (slf *ProductionProgressSearch) SetId(id uint) *ProductionProgressSearch { - slf.ID = id - return slf -} - -func (slf *ProductionProgressSearch) SetChannel(channel int32) *ProductionProgressSearch { - slf.Channel = channel - return slf -} - -func (slf *ProductionProgressSearch) SetUnFinished() *ProductionProgressSearch { - slf.UnFinished = true - return slf -} - -func (slf *ProductionProgressSearch) build() *gorm.DB { - var db = slf.Orm.Model(&ProductionProgress{}) - - 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.OrderID != "" { - db = db.Where("order_id = ?", slf.OrderID) - } - - if slf.ProcedureID != "" { - db = db.Where("procedure_id = ?", slf.ProcedureID) - } - - if slf.ProceduresID != 0 { - db = db.Where("procedures_id = ?", slf.ProceduresID) - } - - if slf.DeviceID != "" { - db = db.Where("device_id = ?", slf.DeviceID) - } - - if slf.Channel != 0 { - db = db.Where("channel = ?", slf.Channel) - } - - if slf.UnFinished { - db = db.Where("finished_quantity < total_quantity") - } - - if slf.ProductProcedureID != "" { - db = db.Where("product_procedure_id = ?", slf.ProductProcedureID) - } - - return db -} - -// Create 鍗曟潯鎻掑叆 -func (slf *ProductionProgressSearch) Create(record *ProductionProgress) 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 *ProductionProgressSearch) Save(record *ProductionProgress) error { - var db = slf.build() - - if err := db.Save(record).Error; err != nil { - return fmt.Errorf("save err: %v, record: %+v", err, record) - } - - return nil -} - -func (slf *ProductionProgressSearch) 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 *ProductionProgressSearch) 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 *ProductionProgressSearch) Delete() error { - var db = slf.build() - - if err := db.Unscoped().Delete(&ProductionProgress{}).Error; err != nil { - return err - } - - return nil -} - -func (slf *ProductionProgressSearch) First() (*ProductionProgress, error) { - var ( - record = new(ProductionProgress) - db = slf.build() - ) - - if err := db.First(record).Error; err != nil { - return record, err - } - - return record, nil -} - -func (slf *ProductionProgressSearch) Find() ([]*ProductionProgress, int64, error) { - var ( - records = make([]*ProductionProgress, 0) - total int64 - db = slf.build() - ) - - 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) - } - - return records, total, nil -} - -func (slf *ProductionProgressSearch) FindNotTotal() ([]*ProductionProgress, error) { - var ( - records = make([]*ProductionProgress, 0) - db = slf.build() - ) - - 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) - } - - return records, nil -} - -// FindByQuery 鎸囧畾鏉′欢鏌ヨ. -func (slf *ProductionProgressSearch) FindByQuery(query string, args []interface{}) ([]*ProductionProgress, int64, error) { - var ( - records = make([]*ProductionProgress, 0) - total int64 - db = slf.Orm.Table(slf.TableName()).Where(query, args...) - ) - - 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 records, total, nil -} - -// FindByQueryNotTotal 鎸囧畾鏉′欢鏌ヨ&涓嶆煡璇㈡�绘潯鏁�. -func (slf *ProductionProgressSearch) FindByQueryNotTotal(query string, args []interface{}) ([]*ProductionProgress, error) { - var ( - records = make([]*ProductionProgress, 0) - db = slf.Orm.Table(slf.TableName()).Where(query, args...) - ) - - 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 -} +// +//import ( +// "apsClient/pkg/sqlitex" +// "fmt" +// "github.com/jinzhu/gorm" +//) +// +//type ( +// ProductionProgress struct { +// gorm.Model +// ProceduresID uint `gorm:"index;type:varchar(191)" json:"proceduresId"` //procedures琛ㄧ殑id +// WorkOrderID string `gorm:"index;type:varchar(191);not null" json:"workOrderID"` +// OrderID string `gorm:"index;type:varchar(191);not null" json:"orderID"` +// ProcedureID string `gorm:"type:varchar(191)" json:"procedureId"` +// ProductProcedureID string `gorm:"type:varchar(191);" json:"productProcedureID"` //浜у搧宸ュ簭id +// DeviceID string `gorm:"type:varchar(191);not null" json:"deviceId"` +// FinishedQuantity int64 `gorm:"type:int;not null" json:"finishedQuantity"` +// Channel int32 `gorm:"type:int" json:"channel"` //閫氶亾 +// TotalQuantity int64 `gorm:"type:int;not null" json:"totalQuantity"` +// } +// +// ProductionProgressSearch struct { +// ProductionProgress +// Order string +// PageNum int +// PageSize int +// Orm *gorm.DB +// UnFinished bool +// } +//) +// +//func (slf *ProductionProgress) TableName() string { +// return "production_progress" +//} +// +//func NewProductionProgressSearch(db *gorm.DB) *ProductionProgressSearch { +// if db == nil { +// db = sqlitex.GetDB() +// } +// return &ProductionProgressSearch{Orm: db} +//} +// +//func (slf *ProductionProgressSearch) SetOrm(tx *gorm.DB) *ProductionProgressSearch { +// slf.Orm = tx +// return slf +//} +// +//func (slf *ProductionProgressSearch) SetPage(page, size int) *ProductionProgressSearch { +// slf.PageNum, slf.PageSize = page, size +// return slf +//} +// +//func (slf *ProductionProgressSearch) SetOrder(order string) *ProductionProgressSearch { +// slf.Order = order +// return slf +//} +// +//func (slf *ProductionProgressSearch) SetWorkOrderId(orderId string) *ProductionProgressSearch { +// slf.WorkOrderID = orderId +// return slf +//} +// +//func (slf *ProductionProgressSearch) SetProcedureId(procedureId string) *ProductionProgressSearch { +// slf.ProcedureID = procedureId +// return slf +//} +// +//func (slf *ProductionProgressSearch) SetProceduresId(proceduresId uint) *ProductionProgressSearch { +// slf.ProceduresID = proceduresId +// return slf +//} +// +//func (slf *ProductionProgressSearch) SetDeviceId(id string) *ProductionProgressSearch { +// slf.DeviceID = id +// return slf +//} +// +//func (slf *ProductionProgressSearch) SetProductProcedureId(productProcedureId string) *ProductionProgressSearch { +// slf.ProductProcedureID = productProcedureId +// return slf +//} +// +//func (slf *ProductionProgressSearch) SetId(id uint) *ProductionProgressSearch { +// slf.ID = id +// return slf +//} +// +//func (slf *ProductionProgressSearch) SetChannel(channel int32) *ProductionProgressSearch { +// slf.Channel = channel +// return slf +//} +// +//func (slf *ProductionProgressSearch) SetUnFinished() *ProductionProgressSearch { +// slf.UnFinished = true +// return slf +//} +// +//func (slf *ProductionProgressSearch) build() *gorm.DB { +// var db = slf.Orm.Model(&ProductionProgress{}) +// +// 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.OrderID != "" { +// db = db.Where("order_id = ?", slf.OrderID) +// } +// +// if slf.ProcedureID != "" { +// db = db.Where("procedure_id = ?", slf.ProcedureID) +// } +// +// if slf.ProceduresID != 0 { +// db = db.Where("procedures_id = ?", slf.ProceduresID) +// } +// +// if slf.DeviceID != "" { +// db = db.Where("device_id = ?", slf.DeviceID) +// } +// +// if slf.Channel != 0 { +// db = db.Where("channel = ?", slf.Channel) +// } +// +// if slf.UnFinished { +// db = db.Where("finished_quantity < total_quantity") +// } +// +// if slf.ProductProcedureID != "" { +// db = db.Where("product_procedure_id = ?", slf.ProductProcedureID) +// } +// +// return db +//} +// +//// Create 鍗曟潯鎻掑叆 +//func (slf *ProductionProgressSearch) Create(record *ProductionProgress) 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 *ProductionProgressSearch) Save(record *ProductionProgress) error { +// var db = slf.build() +// +// if err := db.Save(record).Error; err != nil { +// return fmt.Errorf("save err: %v, record: %+v", err, record) +// } +// +// return nil +//} +// +//func (slf *ProductionProgressSearch) 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 *ProductionProgressSearch) 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 *ProductionProgressSearch) Delete() error { +// var db = slf.build() +// +// if err := db.Unscoped().Delete(&ProductionProgress{}).Error; err != nil { +// return err +// } +// +// return nil +//} +// +//func (slf *ProductionProgressSearch) First() (*ProductionProgress, error) { +// var ( +// record = new(ProductionProgress) +// db = slf.build() +// ) +// +// if err := db.First(record).Error; err != nil { +// return record, err +// } +// +// return record, nil +//} +// +//func (slf *ProductionProgressSearch) Find() ([]*ProductionProgress, int64, error) { +// var ( +// records = make([]*ProductionProgress, 0) +// total int64 +// db = slf.build() +// ) +// +// 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) +// } +// +// return records, total, nil +//} +// +//func (slf *ProductionProgressSearch) FindNotTotal() ([]*ProductionProgress, error) { +// var ( +// records = make([]*ProductionProgress, 0) +// db = slf.build() +// ) +// +// 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) +// } +// +// return records, nil +//} +// +//// FindByQuery 鎸囧畾鏉′欢鏌ヨ. +//func (slf *ProductionProgressSearch) FindByQuery(query string, args []interface{}) ([]*ProductionProgress, int64, error) { +// var ( +// records = make([]*ProductionProgress, 0) +// total int64 +// db = slf.Orm.Table(slf.TableName()).Where(query, args...) +// ) +// +// 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 records, total, nil +//} +// +//// FindByQueryNotTotal 鎸囧畾鏉′欢鏌ヨ&涓嶆煡璇㈡�绘潯鏁�. +//func (slf *ProductionProgressSearch) FindByQueryNotTotal(query string, args []interface{}) ([]*ProductionProgress, error) { +// var ( +// records = make([]*ProductionProgress, 0) +// db = slf.Orm.Table(slf.TableName()).Where(query, args...) +// ) +// +// 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 +//} diff --git a/model/task_status_sync.go b/model/task_status_sync.go index d45ca91..0000777 100644 --- a/model/task_status_sync.go +++ b/model/task_status_sync.go @@ -18,6 +18,7 @@ IsFinish bool //鏄惁瀹屾垚 StartTs int64 `json:"start_ts"` //寮�濮嬫椂闂� FinishTs int64 `json:"finish_ts"` //缁撴潫鏃堕棿 + FinishedQuantity int `json:"finishedQuantity"` } TaskStatusSyncSearch struct { diff --git a/service/progress.go b/service/progress.go index 4205413..cc67368 100644 --- a/service/progress.go +++ b/service/progress.go @@ -3,7 +3,7 @@ import ( "apsClient/model" "errors" - "github.com/jinzhu/gorm" + "github.com/mitchellh/mapstructure" ) type ProgressService struct { @@ -13,71 +13,32 @@ return &ProgressService{} } -func (slf ProgressService) Add(db *gorm.DB, procedure *model.Procedures, order *model.Order) error { - _, err := model.NewProductionProgressSearch(db).SetProceduresId(procedure.ID).First() - if err == gorm.ErrRecordNotFound { - progress := &model.ProductionProgress{ - ProceduresID: procedure.ID, - WorkOrderID: procedure.WorkOrderID, - OrderID: procedure.OrderID, - ProcedureID: procedure.ProceduresInfo.ProcedureID, - ProductProcedureID: procedure.ProductProcedureID, - DeviceID: procedure.DeviceID, - TotalQuantity: order.Amount.IntPart(), - Channel: procedure.Channel, - } - err := model.NewProductionProgressSearch(db).Create(progress) - if err != nil { - return err - } - ProgressCacheSet(procedure.DeviceID, procedure.Channel, progress) - } - - return nil -} - // UpdateProgress 浠呴檺plc鏁版嵁閲囬泦瀹氭椂浠诲姟鐢�(缂哄皯宸ュ簭id鍙傛暟) -func (slf ProgressService) UpdateProgress(deviceID string, channel int32, finishedQuantity int64) (err error) { - progressCache, err := slf.GetCurrentProgress(deviceID, channel) +func (slf ProgressService) UpdateProgress(deviceID string, channel int32, finishedQuantity int) (err error) { + progress, err := slf.GetCurrentProgress(deviceID, channel) if err != nil { return err } - if progressCache == nil { + if progress == nil { return errors.New("progress cache not found") } - if finishedQuantity > progressCache.FinishedQuantity { //褰撴湁鍙樺寲鏃舵墠鏇存柊 - progressCache.FinishedQuantity = finishedQuantity - ProgressCacheSet(deviceID, channel, progressCache) - return model.NewProductionProgressSearch(nil).SetId(progressCache.ID).Save(progressCache) + if finishedQuantity > progress.FinishedQuantity { //褰撴湁鍙樺寲鏃舵墠鏇存柊 + return model.NewProceduresSearch(nil).SetId(progress.ID).UpdateByMap(map[string]interface{}{"finished_quantity": finishedQuantity}) } return nil } // GetCurrentProgress 浠呴檺plc鏁版嵁閲囬泦瀹氭椂浠诲姟鐢�(缂哄皯宸ュ簭id鍙傛暟) -func (slf ProgressService) GetCurrentProgress(deviceID string, channel int32) (progressCache *model.ProductionProgress, err error) { - var ok bool - progressCache, ok = ProgressCacheGet(deviceID, channel) - if !ok { - progressCache, err = model.NewProductionProgressSearch(nil).SetDeviceId(deviceID).SetChannel(channel).SetOrder("id asc").First() - if err == gorm.ErrRecordNotFound { - return nil, errors.New("progress not found") - } - if err != nil { - return nil, err - } - - if progressCache.FinishedQuantity >= progressCache.TotalQuantity { //濡傛灉瀹屾垚閲忓ぇ浜庣瓑浜庢�婚噺灏辫鏄庢槸涓婁竴涓凡瀹屾垚鐨勪换鍔★紝涓嶆槸褰撳墠杩涜涓殑浠诲姟銆� - progressCache = nil - } - if progressCache != nil { - ProgressCacheSet(deviceID, channel, progressCache) - } +func (slf ProgressService) GetCurrentProgress(deviceID string, channel int32) (progress *model.Procedures, err error) { + progress, err = model.NewProceduresSearch(nil).SetDeviceId(deviceID).SetChannels([]int32{channel}).SetStatus(model.ProcedureStatusProcessing).SetOrder("id desc").First() + if err != nil { + return nil, err } return } -func (slf ProgressService) UpdateProgressByProceduresId(proceduresId uint, finishedQuantity int64) (err error) { - progress, err := slf.GetCurrentProgressByProceduresId(proceduresId) +func (slf ProgressService) UpdateProgressByProceduresId(proceduresId uint, finishedQuantity int) (err error) { + progress, err := slf.GetProcedureByProceduresId(proceduresId) if err != nil { return err } @@ -85,22 +46,44 @@ return errors.New("progress not exists") } if finishedQuantity > progress.FinishedQuantity { //褰撴湁鍙樺寲鏃舵墠鏇存柊 - progress.FinishedQuantity = finishedQuantity - return model.NewProductionProgressSearch(nil).SetId(progress.ID).Save(progress) + return model.NewProceduresSearch(nil).SetId(progress.ID).UpdateByMap(map[string]interface{}{"finished_quantity": finishedQuantity}) } + return nil } -func (slf ProgressService) GetCurrentProgressByProceduresId(proceduresId uint) (progress *model.ProductionProgress, err error) { - progress, err = model.NewProductionProgressSearch(nil).SetProceduresId(proceduresId).First() +func (slf ProgressService) GetProcedureByProceduresId(proceduresId uint) (progress *model.Procedures, err error) { + progress, err = model.NewProceduresSearch(nil).SetId(proceduresId).First() if err != nil { return nil, err } return } +type ProductionProgress struct { + WorkOrderID string `json:"workOrderID"` + OrderID string `json:"orderID"` + ProcedureID string `json:"procedureId"` + ProductProcedureID string `json:"productProcedureID"` //浜у搧宸ュ簭id + DeviceID string `json:"deviceId"` + FinishedQuantity int64 `json:"finishedQuantity"` +} + // GetProgressList 鑾峰彇寰呭悓姝ヨ繘搴﹀伐搴� -func (slf ProgressService) GetProgressList() (progressList []*model.ProductionProgress, err error) { - progressList, err = model.NewProductionProgressSearch(nil).SetUnFinished().SetOrder("id desc").SetPage(1, 100).FindNotTotal() +func (slf ProgressService) GetProgressList() (progressList []*ProductionProgress, err error) { + var procedureList []*model.Procedures + procedureList, err = model.NewProceduresSearch(nil). + SetFields([]string{"id", + "finished_quantity", + "work_order_id", + "order_id", + "procedure_id", + "product_procedure_id", + "device_id", + }).SetStatus(model.ProcedureStatusProcessing).SetOrder("id desc").FindNotTotal() + if err != nil { + return + } + err = mapstructure.Decode(procedureList, procedureList) return } -- Gitblit v1.8.0