fix
zhangqian
2023-12-09 c0fa4c8f502607f739ce3c91099b7fb7573258ad
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
package service
 
import (
    "apsClient/model"
    "errors"
    "github.com/mitchellh/mapstructure"
)
 
type ProgressService struct {
}
 
func NewProgressService() *ProgressService {
    return &ProgressService{}
}
 
// 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 progress == nil {
        return errors.New("progress cache not found")
    }
    if finishedQuantity > progress.FinishedQuantity { //当有变化时才更新
        return model.NewProceduresSearch(nil).SetId(progress.ID).UpdateByMap(map[string]interface{}{"finished_quantity": finishedQuantity})
    }
    return nil
}
 
// 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
}