From 28addaa46cb97c20ad37e13eb10535de7b75e71c Mon Sep 17 00:00:00 2001 From: zhangqian <zhangqian@123.com> Date: 星期三, 29 十一月 2023 19:29:43 +0800 Subject: [PATCH] 首次报工开始时间为工序开始时间 --- service/progress.go | 106 +++++++++++++++++++++++++++++++++++++++-------------- 1 files changed, 78 insertions(+), 28 deletions(-) diff --git a/service/progress.go b/service/progress.go index bb0d859..4205413 100644 --- a/service/progress.go +++ b/service/progress.go @@ -3,7 +3,7 @@ import ( "apsClient/model" "errors" - "gorm.io/gorm" + "github.com/jinzhu/gorm" ) type ProgressService struct { @@ -13,44 +13,94 @@ return &ProgressService{} } -func (slf ProgressService) AddProgress(db *gorm.DB, procedure *model.Procedures, order *model.Order) error { - progress := &model.ProductionProgress{ - WorkOrderID: procedure.WorkOrderID, - OrderID: procedure.OrderID, - ProcedureID: procedure.ProceduresInfo.ProcedureID, - DeviceID: procedure.DeviceID, - TotalQuantity: order.Amount.IntPart(), - } - err := model.NewProductionProgressSearch(db).Create(progress) - if err != nil { - return err - } - ProgressCacheSet(progress) - return nil -} - -func (slf ProgressService) UpdateProgress(finishedQuantity int64) (err error) { - var progressCache *model.ProductionProgress - progressCache, ok := ProgressCacheGet() - if !ok { - progressCache, err = model.NewProductionProgressSearch(nil).SetOrder("id desc").First() - if err == gorm.ErrRecordNotFound { - return errors.New("progress cache not found") +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 } - if progressCache.FinishedQuantity < progressCache.TotalQuantity { - ProgressCacheSet(progressCache) - } + 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) + if err != nil { + return err } if progressCache == nil { return errors.New("progress cache not found") } if finishedQuantity > progressCache.FinishedQuantity { //褰撴湁鍙樺寲鏃舵墠鏇存柊 progressCache.FinishedQuantity = finishedQuantity - ProgressCacheSet(progressCache) + ProgressCacheSet(deviceID, channel, progressCache) return model.NewProductionProgressSearch(nil).SetId(progressCache.ID).Save(progressCache) } 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) + } + } + return +} + +func (slf ProgressService) UpdateProgressByProceduresId(proceduresId uint, finishedQuantity int64) (err error) { + progress, err := slf.GetCurrentProgressByProceduresId(proceduresId) + if err != nil { + return err + } + if progress == nil { + return errors.New("progress not exists") + } + if finishedQuantity > progress.FinishedQuantity { //褰撴湁鍙樺寲鏃舵墠鏇存柊 + progress.FinishedQuantity = finishedQuantity + return model.NewProductionProgressSearch(nil).SetId(progress.ID).Save(progress) + } + return nil +} + +func (slf ProgressService) GetCurrentProgressByProceduresId(proceduresId uint) (progress *model.ProductionProgress, err error) { + progress, err = model.NewProductionProgressSearch(nil).SetProceduresId(proceduresId).First() + if err != nil { + return nil, err + } + return +} + +// GetProgressList 鑾峰彇寰呭悓姝ヨ繘搴﹀伐搴� +func (slf ProgressService) GetProgressList() (progressList []*model.ProductionProgress, err error) { + progressList, err = model.NewProductionProgressSearch(nil).SetUnFinished().SetOrder("id desc").SetPage(1, 100).FindNotTotal() + return +} -- Gitblit v1.8.0