zhangqian
2023-09-05 49e90e5de2e7166e74e26102dff9064b933fc5fd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package service
 
import (
    "apsClient/model"
    "errors"
    "gorm.io/gorm"
)
 
type ProgressService struct {
}
 
func NewProgressService() *ProgressService {
    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")
        }
        if err != nil {
            return err
        }
        if progressCache.FinishedQuantity < progressCache.TotalQuantity {
            ProgressCacheSet(progressCache)
        }
    }
    if progressCache == nil {
        return errors.New("progress cache not found")
    }
    if finishedQuantity > progressCache.FinishedQuantity { //当有变化时才更新
        progressCache.FinishedQuantity = finishedQuantity
        ProgressCacheSet(progressCache)
        return model.NewProductionProgressSearch(nil).SetId(progressCache.ID).Save(progressCache)
    }
    return nil
}