| | |
| | | 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) Upsert(db *gorm.DB, procedure *model.Procedures, order *model.Order) error { |
| | | _, err := model.NewProductionProgressSearch(db).SetProcedureId(procedure.ProcedureID).SetWorkOrderId(procedure.WorkOrderID).First() |
| | | if err == gorm.ErrRecordNotFound { |
| | | progress := &model.ProductionProgress{ |
| | | WorkOrderID: procedure.WorkOrderID, |
| | | OrderID: procedure.OrderID, |
| | | ProcedureID: procedure.ProceduresInfo.ProcedureID, |
| | | DeviceID: procedure.DeviceID, |
| | | TotalQuantity: order.Amount.IntPart(), |
| | | Position: procedure.Position, |
| | | } |
| | | err := model.NewProductionProgressSearch(db).Create(progress) |
| | | if err != nil { |
| | | return err |
| | | } |
| | | if progressCache.FinishedQuantity < progressCache.TotalQuantity { |
| | | ProgressCacheSet(progressCache) |
| | | } |
| | | ProgressCacheSet(procedure.Position, progress) |
| | | } |
| | | |
| | | return nil |
| | | } |
| | | |
| | | func (slf ProgressService) UpdateProgress(position int, finishedQuantity int64) (err error) { |
| | | progressCache, err := slf.GetCurrentProgress(position) |
| | | 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(position, progressCache) |
| | | return model.NewProductionProgressSearch(nil).SetId(progressCache.ID).Save(progressCache) |
| | | } |
| | | return nil |
| | | } |
| | | |
| | | func (slf ProgressService) GetCurrentProgress(position int) (progressCache *model.ProductionProgress, err error) { |
| | | var ok bool |
| | | progressCache, ok = ProgressCacheGet(position) |
| | | if !ok { |
| | | progressCache, err = model.NewProductionProgressSearch(nil).SetPosition(position).SetOrder("id desc").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(position, progressCache) |
| | | } |
| | | } |
| | | return |
| | | } |