zhangqian
2023-11-29 28addaa46cb97c20ad37e13eb10535de7b75e71c
service/progress.go
@@ -3,7 +3,7 @@
import (
   "apsClient/model"
   "errors"
   "gorm.io/gorm"
   "github.com/jinzhu/gorm"
)
type ProgressService struct {
@@ -14,28 +14,31 @@
}
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()
   _, err := model.NewProductionProgressSearch(db).SetProceduresId(procedure.ID).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,
         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.Position, progress)
      ProgressCacheSet(procedure.DeviceID, procedure.Channel, progress)
   }
   return nil
}
func (slf ProgressService) UpdateProgress(position int, finishedQuantity int64) (err error) {
   progressCache, err := slf.GetCurrentProgress(position)
// 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
   }
@@ -44,17 +47,18 @@
   }
   if finishedQuantity > progressCache.FinishedQuantity { //当有变化时才更新
      progressCache.FinishedQuantity = finishedQuantity
      ProgressCacheSet(position, progressCache)
      ProgressCacheSet(deviceID, channel, progressCache)
      return model.NewProductionProgressSearch(nil).SetId(progressCache.ID).Save(progressCache)
   }
   return nil
}
func (slf ProgressService) GetCurrentProgress(position int) (progressCache *model.ProductionProgress, err error) {
// GetCurrentProgress 仅限plc数据采集定时任务用(缺少工序id参数)
func (slf ProgressService) GetCurrentProgress(deviceID string, channel int32) (progressCache *model.ProductionProgress, err error) {
   var ok bool
   progressCache, ok = ProgressCacheGet(position)
   progressCache, ok = ProgressCacheGet(deviceID, channel)
   if !ok {
      progressCache, err = model.NewProductionProgressSearch(nil).SetPosition(position).SetOrder("id desc").First()
      progressCache, err = model.NewProductionProgressSearch(nil).SetDeviceId(deviceID).SetChannel(channel).SetOrder("id asc").First()
      if err == gorm.ErrRecordNotFound {
         return nil, errors.New("progress not found")
      }
@@ -66,8 +70,37 @@
         progressCache = nil
      }
      if progressCache != nil {
         ProgressCacheSet(position, progressCache)
         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
}