zhangqian
2023-11-28 778f7fbabb2ad5cdb8e203e1695ae2a8c7327edb
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package service
 
import (
    "apsClient/model"
    "errors"
    "github.com/jinzhu/gorm"
)
 
type ProgressService struct {
}
 
func NewProgressService() *ProgressService {
    return &ProgressService{}
}
 
func (slf ProgressService) Add(db *gorm.DB, procedure *model.Procedures, order *model.Order) error {
    _, err := model.NewProductionProgressSearch(db).SetProceduresId(procedure.ID).First()
    if err == gorm.ErrRecordNotFound {
        progress := &model.ProductionProgress{
            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.DeviceID, procedure.Channel, progress)
    }
 
    return nil
}
 
// 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
    }
    if progressCache == nil {
        return errors.New("progress cache not found")
    }
    if finishedQuantity > progressCache.FinishedQuantity { //当有变化时才更新
        progressCache.FinishedQuantity = finishedQuantity
        ProgressCacheSet(deviceID, channel, progressCache)
        return model.NewProductionProgressSearch(nil).SetId(progressCache.ID).Save(progressCache)
    }
    return nil
}
 
// GetCurrentProgress 仅限plc数据采集定时任务用(缺少工序id参数)
func (slf ProgressService) GetCurrentProgress(deviceID string, channel int32) (progressCache *model.ProductionProgress, err error) {
    var ok bool
    progressCache, ok = ProgressCacheGet(deviceID, channel)
    if !ok {
        progressCache, err = model.NewProductionProgressSearch(nil).SetDeviceId(deviceID).SetChannel(channel).SetOrder("id asc").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(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
}