| | |
| | | package service |
| | | |
| | | import ( |
| | | "apsClient/conf" |
| | | "apsClient/model" |
| | | "errors" |
| | | "gorm.io/gorm" |
| | | "github.com/jinzhu/gorm" |
| | | ) |
| | | |
| | | type ProgressService struct { |
| | |
| | | return &ProgressService{} |
| | | } |
| | | |
| | | func (slf ProgressService) Upsert(db *gorm.DB, procedure *model.Procedures, order *model.Order) error { |
| | | func (slf ProgressService) Add(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{ |
| | |
| | | ProcedureID: procedure.ProceduresInfo.ProcedureID, |
| | | DeviceID: procedure.DeviceID, |
| | | TotalQuantity: order.Amount.IntPart(), |
| | | Position: procedure.Position, |
| | | Channel: procedure.Channel, |
| | | } |
| | | err := model.NewProductionProgressSearch(db).Create(progress) |
| | | if err != nil { |
| | | return err |
| | | } |
| | | ProgressCacheSet(procedure.Position, progress) |
| | | ProgressCacheSet(procedure.Channel, progress) |
| | | } |
| | | |
| | | return nil |
| | | } |
| | | |
| | | func (slf ProgressService) UpdateProgress(position int, finishedQuantity int64) (err error) { |
| | | progressCache, err := slf.GetCurrentProgress(position) |
| | | func (slf ProgressService) UpdateProgress(channel int32, finishedQuantity int64) (err error) { |
| | | progressCache, err := slf.GetCurrentProgress(channel) |
| | | if err != nil { |
| | | return err |
| | | } |
| | |
| | | } |
| | | if finishedQuantity > progressCache.FinishedQuantity { //当有变化时才更新 |
| | | progressCache.FinishedQuantity = finishedQuantity |
| | | ProgressCacheSet(position, progressCache) |
| | | ProgressCacheSet(channel, progressCache) |
| | | return model.NewProductionProgressSearch(nil).SetId(progressCache.ID).Save(progressCache) |
| | | } |
| | | return nil |
| | | } |
| | | |
| | | func (slf ProgressService) GetCurrentProgress(position int) (progressCache *model.ProductionProgress, err error) { |
| | | func (slf ProgressService) GetCurrentProgress(channel int32) (progressCache *model.ProductionProgress, err error) { |
| | | var ok bool |
| | | progressCache, ok = ProgressCacheGet(position) |
| | | progressCache, ok = ProgressCacheGet(channel) |
| | | if !ok { |
| | | progressCache, err = model.NewProductionProgressSearch(nil).SetPosition(position).SetOrder("id desc").First() |
| | | progressCache, err = model.NewProductionProgressSearch(nil).SetDeviceId(conf.Conf.CurrentDeviceID).SetChannel(channel).SetOrder("id desc").First() |
| | | if err == gorm.ErrRecordNotFound { |
| | | return nil, errors.New("progress not found") |
| | | } |
| | |
| | | progressCache = nil |
| | | } |
| | | if progressCache != nil { |
| | | ProgressCacheSet(position, progressCache) |
| | | ProgressCacheSet(channel, progressCache) |
| | | } |
| | | } |
| | | 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 |
| | | } |