fix
zhangqian
2023-12-01 8324f872ef3a4d0c978a9b1d062800c6a1701c12
service/progress.go
@@ -3,7 +3,7 @@
import (
   "apsClient/model"
   "errors"
   "gorm.io/gorm"
   "github.com/mitchellh/mapstructure"
)
type ProgressService struct {
@@ -13,61 +13,77 @@
   return &ProgressService{}
}
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{
         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
      }
      ProgressCacheSet(procedure.Position, 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 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(position, 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
}
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)
      }
// GetCurrentProgress 仅限plc数据采集定时任务用(缺少工序id参数)
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 int) (err error) {
   progress, err := slf.GetProcedureByProceduresId(proceduresId)
   if err != nil {
      return err
   }
   if progress == nil {
      return errors.New("progress not exists")
   }
   if finishedQuantity > progress.FinishedQuantity { //当有变化时才更新
      return model.NewProceduresSearch(nil).SetId(progress.ID).UpdateByMap(map[string]interface{}{"finished_quantity": finishedQuantity})
   }
   return nil
}
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 []*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, &progressList)
   return
}