allied-creation-byte-bank.go
4.1 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package domainService
import (
"github.com/linmadan/egglib-go/core/application"
pG "github.com/linmadan/egglib-go/transaction/pg"
"github.com/tidwall/gjson"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/allied-lib/gateway/byte_bank"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/repository"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/log"
)
type ByteBankService struct {
internalService *byte_bank.HttpLibByteBankServiceGateway
}
func NewByteBankService() *ByteBankService {
return &ByteBankService{
internalService: byte_bank.NewHttpLibByteBankServiceGateway(constant.MMM_BYTE_BANK_HOST),
}
}
//GetFormulasId 获取公式id
func (svr *ByteBankService) GetFormulasId(formulaName string) string {
//获取公式id
formulaId := ""
listLoop:
for i := 0; ; i++ {
data, err := svr.internalService.ListFormulas(1000, int64(i))
if err != nil {
return ""
}
if len(data.Formulas) == 0 {
break listLoop
}
for _, item := range data.Formulas {
if item.FormulaDescription == formulaName {
formulaId = item.FormulaId
break listLoop
}
}
}
return formulaId
}
// DeviceOperationEfficiency 设备运行效率
func (svr *ByteBankService) DeviceOperationEfficiency() (interface{}, error) {
//获取公式id时段产能
formulaId := svr.GetFormulasId("设备运行效率")
//获取解析数据
byteResult, err := svr.internalService.AnalysisFormulaByte(formulaId)
if err != nil {
return struct{}{}, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
log.Logger.Debug("获取设备运行效率:" + string(byteResult))
code := gjson.GetBytes(byteResult, "code").String()
if code != "0" {
msg := gjson.GetBytes(byteResult, "msg").String()
return struct{}{}, application.ThrowError(application.INTERNAL_SERVER_ERROR, msg)
}
//待解析的
queryResult := gjson.GetBytes(byteResult, "data.queryResult").Array()
/*
1.生产设备
2.设备本月运行数据
*/
transactionContext := pG.NewPGTransactionContext(pg.DB)
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
deviceRepository, _ := repository.NewDeviceRepository(transactionContext)
_, devices, err := deviceRepository.Find(map[string]interface{}{
"companyId": constant.MANUFACTURE_DEFAULT_COMPANYID,
"orgId": constant.MANUFACTURE_DEFAULT_ORGID,
"workshopId": constant.MANUFACTURE_DEFAULT_WORKSHOPID,
"orderBy": "device_name asc"})
if err != nil {
return nil, err
}
var response = make([]interface{}, 0)
for _, d := range devices {
var deviceSn string
var productCount, runningTime int64
for _, item := range queryResult {
deviceSn = item.Get("device_sn").String()
if deviceSn == d.DeviceCode {
productCount = item.Get("product_count").Int()
runningTime = item.Get("timing").Int()
break
}
}
response = append(response, map[string]interface{}{
"device_name": d.DeviceName,
"device_type": d.DeviceType,
"product_count": productCount,
"timing": runningTime,
})
}
//var response = make([]interface{}, 0)
//var deviceSn string
//var productCount, runningTime int64
//for _, item := range queryResult {
// deviceSn = item.Get("device_sn").String()
// if deviceSn != "" {
// productCount = item.Get("product_count").Int()
// runningTime = item.Get("timing").Int()
// response = append(response, map[string]interface{}{
// "设备名称": deviceSn,
// "设备类型": item.Get("device_type").String(),
// "运行时长": productCount,
// "数量": runningTime,
// })
// }
//}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return map[string]interface{}{"devices": response}, nil
}