/
transport_finish.go
208 lines (205 loc) · 7.01 KB
/
transport_finish.go
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package TMSTransport
import (
"errors"
"fmt"
CoreFilter "github.com/fotomxq/weeekj_core/v5/core/filter"
CoreSQL "github.com/fotomxq/weeekj_core/v5/core/sql"
CoreSQLConfig "github.com/fotomxq/weeekj_core/v5/core/sql/config"
MapMathArgs "github.com/fotomxq/weeekj_core/v5/map/math/args"
MapMathPoint "github.com/fotomxq/weeekj_core/v5/map/math/point"
Router2SystemConfig "github.com/fotomxq/weeekj_core/v5/router2/system_config"
ServiceOrderMod "github.com/fotomxq/weeekj_core/v5/service/order/mod"
)
// ArgsUpdateTransportFinish 更新配送单状态为完成参数
type ArgsUpdateTransportFinish struct {
//ID
ID int64 `db:"id" json:"id" check:"id"`
//组织ID
// -1 跳过
OrgID int64 `db:"org_id" json:"orgID" check:"id" empty:"true"`
//操作组织人员ID
BindID int64 `db:"bind_id" json:"bindID" check:"id" empty:"true"`
//协助操作人员ID
OperateBindID int64 `json:"operateBindID"`
//是否为订单退款完成
IsOrderRefund bool `json:"isOrderRefund"`
}
// UpdateTransportFinish 更新配送单状态为完成
func UpdateTransportFinish(args *ArgsUpdateTransportFinish) (err error) {
var data FieldsTransport
data, err = GetTransport(&ArgsGetTransport{
ID: args.ID,
OrgID: args.OrgID,
})
if err != nil {
return
}
//检查是否为退单
isRefund, b := data.Params.GetValBool("isRefund")
if !b {
isRefund = false
}
//必须已经付款,否则拒绝完成配送
if data.Price > 0 && data.PayFinishAt.Unix() < 1000000 {
err = errors.New("no pay")
return
}
if args.IsOrderRefund {
data.Params = CoreSQLConfig.Set(data.Params, "orderRefund", "true")
_, err = CoreSQL.UpdateOneSoft(Router2SystemConfig.MainDB.DB, "UPDATE tms_transport SET update_at = NOW(), finish_at = NOW(), status = 3, params = :params WHERE id = :id AND (org_id = :org_id OR :org_id < 1) AND status = 2", map[string]interface{}{
"id": args.ID,
"org_id": args.OrgID,
"params": data.Params,
})
} else {
_, err = CoreSQL.UpdateOneSoft(Router2SystemConfig.MainDB.DB, "UPDATE tms_transport SET update_at = NOW(), finish_at = NOW(), status = 3 WHERE id = :id AND (org_id = :org_id OR :org_id < 1) AND status = 2", map[string]interface{}{
"id": args.ID,
"org_id": args.OrgID,
})
}
if err != nil {
return
}
//获取支付方式
payFromSystem, b := data.Params.GetVal("paySystem")
if !b || payFromSystem == "" {
payFromSystem = "unkonw"
}
if args.OperateBindID > 0 {
_ = appendLog(&argsAppendLog{
OrgID: args.OrgID,
BindID: args.OperateBindID,
TransportID: args.ID,
TransportBindID: data.BindID,
Mark: "finish",
Des: fmt.Sprint("平台工作人员,协助完成更新状态为完成配送"),
})
} else {
_ = appendLog(&argsAppendLog{
OrgID: args.OrgID,
BindID: args.BindID,
TransportID: args.ID,
TransportBindID: data.BindID,
Mark: "finish",
Des: fmt.Sprint("更新状态为完成配送"),
})
}
if data.BindID > 0 {
var km int64
km, err = MapMathPoint.GetDistance(&MapMathPoint.ArgsGetDistance{
StartPoint: MapMathArgs.ParamsPoint{
PointType: data.FromAddress.GetMapType(),
Longitude: data.FromAddress.Longitude,
Latitude: data.FromAddress.Latitude,
},
EndPoint: MapMathArgs.ParamsPoint{
PointType: data.ToAddress.GetMapType(),
Longitude: data.ToAddress.Longitude,
Latitude: data.ToAddress.Latitude,
},
})
if err != nil {
km = 0
}
err = appendAnalysis(&argsAppendAnalysis{
OrgID: data.OrgID,
BindID: data.BindID,
InfoID: data.InfoID,
UserID: data.UserID,
TransportID: data.ID,
KM: km,
OverTime: CoreFilter.GetNowTimeCarbon().Time.Unix() - data.CreateAt.Unix(),
Level: 0,
})
if err != nil {
return
}
_, err = CoreSQL.UpdateOneSoft(Router2SystemConfig.MainDB.DB, "UPDATE tms_transport_bind SET time_1_day = time_1_day + :time_1_day, count_finish_1_day = count_finish_1_day + 1 WHERE bind_id = :bind_id", map[string]interface{}{
"bind_id": data.BindID,
"time_1_day": CoreFilter.GetNowTimeCarbon().Time.Unix() - data.CreateAt.Unix(),
})
if err != nil {
return
}
//货物统计
var dataBindAnalysisGoods FieldsBindAnalysisGoods
if err = Router2SystemConfig.MainDB.Get(&dataBindAnalysisGoods, "SELECT id, create_at, org_id, bind_id, goods FROM tms_transport_bind_analysis_goods WHERE org_id = $1 AND bind_id = $2 AND create_at >= $3 AND create_at <= $4 AND is_refund = $5 LIMIT 1", data.OrgID, data.BindID, CoreFilter.GetNowTimeCarbon().StartOfDay().Time, CoreFilter.GetNowTimeCarbon().EndOfDay().Time, isRefund); err != nil || dataBindAnalysisGoods.ID < 1 {
//创建新的数据
err = CoreSQL.CreateOneAndData(Router2SystemConfig.MainDB.DB, "tms_transport_bind_analysis_goods", "INSERT INTO tms_transport_bind_analysis_goods (org_id, bind_id, is_refund, goods) VALUES (:org_id,:bind_id,:is_refund,:goods)", map[string]interface{}{
"org_id": data.OrgID,
"bind_id": data.BindID,
"is_refund": isRefund,
"goods": FieldsBindAnalysisGoodsGoods{},
}, &dataBindAnalysisGoods)
if err != nil {
err = errors.New(fmt.Sprint("create analysis bind goods, ", err))
return
}
}
for _, v := range data.Goods {
isFind := false
var orderData ServiceOrderMod.FieldsOrder
orderData, err = ServiceOrderMod.GetByID(&ServiceOrderMod.ArgsGetByID{
ID: data.OrderID,
OrgID: data.OrgID,
UserID: -1,
})
if err != nil {
err = nil
continue
}
var goodPrice int64 = 0
for _, v2 := range orderData.Goods {
if v.System == v2.From.System && v.ID == v2.From.ID {
goodPrice = v2.Count * v2.Price
for _, v3 := range v2.Exemptions {
goodPrice -= v3.Price
}
if goodPrice < 1 {
goodPrice = 0
}
}
}
for k2, v2 := range dataBindAnalysisGoods.Goods {
if v.System == v2.System && v.ID == v2.ID && data.OrderID > 0 && v2.PaySystem == payFromSystem {
if orderData.CreateFrom == v2.FromSystem {
isFind = true
dataBindAnalysisGoods.Goods[k2].Count += int64(v.Count)
dataBindAnalysisGoods.Goods[k2].Price += goodPrice
break
}
}
}
if !isFind {
dataBindAnalysisGoods.Goods = append(dataBindAnalysisGoods.Goods, FieldsBindAnalysisGoodsGood{
System: v.System,
ID: v.ID,
FromSystem: orderData.CreateFrom,
PaySystem: payFromSystem,
Count: int64(v.Count),
Price: goodPrice,
})
}
}
if _, err = CoreSQL.UpdateOne(Router2SystemConfig.MainDB.DB, "UPDATE tms_transport_bind_analysis_goods SET goods = :goods WHERE id = :id", map[string]interface{}{
"goods": dataBindAnalysisGoods.Goods,
"id": dataBindAnalysisGoods.ID,
}); err != nil {
err = errors.New(fmt.Sprint("update analysis bind goods, ", err))
return
}
}
if err != nil {
return
}
//推送MQTT更新
pushMQTTTransportUpdate(data.OrgID, data.BindID, data.ID, 1)
//推送nats更新
pushNatsStatusUpdate("finish", data.ID, "配送单配送完成")
//统计
if data.BindID > 0 {
pushNatsAnalysisBind(data.BindID)
}
//反馈
return
}