order_base.go
10.6 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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package domain
import (
"time"
"github.com/shopspring/decimal"
)
const (
OrderReal = iota + 1 //实发订单
OrderIntention //意向订单
OrderTypeBestShop //来自小程序香米的订单
)
func GetOrderBaseTypeName(orderType int) string {
var name string
switch orderType {
case OrderReal:
name = "自建订单"
case OrderTypeBestShop:
name = "小程序订单"
case OrderIntention:
name = "意向订单"
}
return name
}
const (
OrderDisableNot = iota //订单未关闭
OrderDisableYes //订单已关闭
)
//Buyer 买家
type Buyer struct {
//买家姓名
BuyerName string `json:"buyerName"`
//联系方式
ContactInfo string `json:"contactInfo"`
//收获地址
ShippingAddress string `json:"shippingAddress"`
//买家备注
Remark string `json:"remark"`
}
type OrderCompute struct {
//合伙人应收分红
PlanPartnerBonus float64 `json:"planPartnerBonus"`
//调整后的合伙人应收分红 (初始值=-1);
//业务判定时0是有效值,
//所以用空串表示无值,转换到数据库中为负值
UsePartnerBonus float64 `json:"usePartnerBonus"`
//合伙人已收分红
PartnerBonusHas float64 `json:"partnerBonusHas"`
//合伙人未收分红
PartnerBonusNot float64 `json:"partnerBonusNot"`
//合伙人分红支出
PartnerBonusExpense float64 `json:"partnerBonusExpense"`
//业务员抽成
SalesmanBonus float64 `json:"salesmanBonus"`
//预计的订单内货品总数
PlanOrderCount int `json:"planOrderCount"`
//预计的订单的总金额
PlanOrderAmount float64 `json:"planOrderAmount"`
//按需使用的订单内货品总数 (初始值=-1)
//业务判定时0是有效值,
//所以用空串表示无值,转换到数据库中为负值
UseOrderCount int `json:"useOrderCount"`
//按需使用的订单内货总金额 (初始值=-1)
//业务判定时0是有效值,
//所以用空串表示无值,转换到数据库中为负值
UseOrderAmount float64 `json:"useOrderAmount"`
}
type OrderBaseRemark struct {
RemarkBonus string `json:"remarkBonus"`
}
//OrderBase 订单基础
type OrderBase struct {
//表id
Id int64 `json:"id"`
//订单类型
OrderType int `json:"orderType"`
//订单编号
OrderCode string `json:"orderCode"`
//交货编号
DeliveryCode string `json:"deliveryCode"`
//买家
Buyer Buyer `json:"buyer"`
//订单区域信息
RegionInfo RegionInfo `json:"regionInfo"`
//订单对应的合伙人
PartnerId int64 `json:"partnerId"`
PartnerInfo Partner `json:"partnerInfo"`
//业务员抽成比例
SalesmanBonusPercent float64 `json:"salesmanBonusPercent"`
//订单的创建时间
CreateTime time.Time `json:"createTime"`
//发货时间
DeliveryTime time.Time `json:"deliveryTime"`
//更新时间
UpdateTime time.Time `json:"updateTime"`
//货品
Goods []OrderGood `json:"goods"`
//核算订单相关数据
OrderCompute OrderCompute `json:"orderCompute"`
//是否关闭订单
IsDisable int `json:"isDisable"`
//分红支付状态
BonusStatus int `json:"bonusStatus"`
//公司
CompanyId int64 `json:"companyId"`
//数据来源
DataFrom OrderDataFrom `json:"dataFrom"`
//备注
Remark OrderBaseRemark `json:"remark"`
//合伙人类型
PartnerCategory PartnerCategory `json:"partnerCategory"`
}
//GetCurrentPartnerBonus 获取当前合伙人应收分红
func (order *OrderBase) GetCurrentPartnerBonus() float64 {
if order.OrderCompute.UsePartnerBonus >= 0 {
return order.OrderCompute.UsePartnerBonus
}
return order.OrderCompute.PlanPartnerBonus
}
//GetCurrentOrderCount 获取当前订单商品总数
func (order *OrderBase) GetCurrentOrderCount() int {
if order.OrderCompute.UseOrderCount >= 0 {
return order.OrderCompute.UseOrderCount
}
return order.OrderCompute.PlanOrderCount
}
// GetCurrentOrderAmount 获取当前订单的总额
func (order *OrderBase) GetCurrentOrderAmount() float64 {
if order.OrderCompute.UseOrderAmount >= 0 {
return order.OrderCompute.UseOrderAmount
}
return order.OrderCompute.PlanOrderAmount
}
//Update 更新订单数据
//orderData 订单数据
//goodsMap 货品的数据,以货品的id做键,map[id]map[string]interface{}
func (order *OrderBase) Update(orderData map[string]interface{}, goodsMap map[int64]map[string]interface{}) error {
if v, ok := orderData["OrderCode"]; ok {
order.OrderCode = v.(string)
}
if v, ok := orderData["DeliveryCode"]; ok {
order.DeliveryCode = v.(string)
}
if v, ok := orderData["Buyer"]; ok {
order.Buyer = v.(Buyer)
}
if v, ok := orderData["SalesmanBonusPercent"]; ok {
order.SalesmanBonusPercent = v.(float64)
}
if v, ok := orderData["DeliveryTime"]; ok {
order.DeliveryTime = v.(time.Time)
}
if v, ok := orderData["IsDisable"]; ok {
order.IsDisable = v.(int)
}
for i := range order.Goods {
goodId := order.Goods[i].Id
if _, ok := goodsMap[goodId]; !ok {
continue
}
if err := order.Goods[i].Update(goodsMap[goodId]); err != nil {
return err
}
}
err := order.Compute()
return err
}
func (order *OrderBase) AddGoods(goods []OrderGood) {
order.Goods = append(order.Goods, goods...)
order.Compute()
}
// func (order *OrderBase) ModifyGoodNumber(goodid int64, number int64) {
// for i := range order.Goods {
// if order.Goods[i].Id != goodid {
// continue
// }
// // thisGood := order.Goods[i]
// }
// order.Compute()
// }
//Compute 数据汇总核算
func (order *OrderBase) Compute() error {
planPartnerBonus := decimal.NewFromFloat(0)
planOrderAmount := decimal.NewFromFloat(0)
var (
//预计的货品总数,不包含调整后的值
planOrderCount int = 0
//调整后的货品总数,混合相加计算预计值和调整值中的一个,优先取调整值计算
useOrderCount int = 0
//订单的货品列表中是否存在调整货品数量
HasUseOrderCount bool = false
)
//调整后的合伙人分红,混合相加计算预计值和调整值中的一个,优先取调整值计算
usePartnerBonus := decimal.NewFromFloat(0)
//订单的货品列表中是否存在调整的合伙人分红
var hasUsePartnerBonus bool = false
//调整后的订单总额,混合相加计算预计值和调整值中的一个,优先取调整值计算
useOrderAmount := decimal.NewFromFloat(0)
var hasUseOrderAmount bool = false
//已支付分红
partnerBonusHas := decimal.NewFromFloat(0)
//未支付分红
partnerBonusNot := decimal.NewFromFloat(0)
//分红支出
partnerBonusExpense := decimal.NewFromFloat(0)
//初始订单的支付状态
order.BonusStatus = OrderGoodWaitPay
//统计所以货品的值
for i := range order.Goods {
if order.Goods[i].BonusStatus == OrderGoodHasPay {
//确定订单的支付状态
//订单的货品列表中“存在”支付状态的为已支付,则整体订单设定为已支付状态
order.BonusStatus = OrderGoodHasPay
}
//计算预计的合伙人分红,不含调整的值
planPartnerBonus = planPartnerBonus.Add(decimal.NewFromFloat(order.Goods[i].GoodCompute.PlanPartnerBonus))
//计算预计的订单总金额,不含调整的值
planOrderAmount = planOrderAmount.Add(decimal.NewFromFloat(order.Goods[i].GoodCompute.PlanAmount))
//计算预计的订单中货品总数,不含调整的值
planOrderCount += order.Goods[i].PlanGoodNumber
//获取调整货品调整后(数量)的总金额,
goodUseAmount := decimal.NewFromFloat(order.Goods[i].GoodCompute.UseAmount)
if goodUseAmount.GreaterThanOrEqual(decimal.NewFromFloat(0)) {
//调整后货品总金额,非负值(大于等于0)的情况
hasUseOrderAmount = true
useOrderAmount = useOrderAmount.Add(goodUseAmount)
} else {
useOrderAmount = useOrderAmount.Add(decimal.NewFromFloat(order.Goods[i].GoodCompute.PlanAmount))
}
goodUsePartnerBonus := decimal.NewFromFloat(order.Goods[i].GoodCompute.UsePartnerBonus)
if goodUsePartnerBonus.GreaterThanOrEqual(decimal.NewFromFloat(0)) {
//货品中存在调整后的合伙人分红,即调整大于等于0
hasUsePartnerBonus = true
usePartnerBonus = usePartnerBonus.Add(goodUsePartnerBonus)
} else {
usePartnerBonus = usePartnerBonus.Add(decimal.NewFromFloat(order.Goods[i].GoodCompute.PlanPartnerBonus))
}
if order.Goods[i].UseGoodNumber >= 0 {
//货品中存在调整后的数量,调整值大于等于0
HasUseOrderCount = true
useOrderCount += order.Goods[i].UseGoodNumber
} else {
useOrderCount += order.Goods[i].PlanGoodNumber
}
partnerBonusHas = partnerBonusHas.Add(decimal.NewFromFloat(order.Goods[i].GoodCompute.PartnerBonusHas))
partnerBonusNot = partnerBonusNot.Add(decimal.NewFromFloat(order.Goods[i].GoodCompute.PartnerBonusNot))
partnerBonusExpense = partnerBonusExpense.Add(decimal.NewFromFloat(order.Goods[i].GoodCompute.PartnerBonusExpense))
}
//汇总赋值
order.OrderCompute.PartnerBonusExpense, _ = partnerBonusExpense.Round(2).BigFloat().Float64()
order.OrderCompute.PartnerBonusHas, _ = partnerBonusHas.Round(2).BigFloat().Float64()
order.OrderCompute.PartnerBonusNot, _ = partnerBonusNot.Round(2).BigFloat().Float64()
order.OrderCompute.PlanPartnerBonus, _ = planPartnerBonus.Round(2).BigFloat().Float64()
order.OrderCompute.PlanOrderAmount, _ = planOrderAmount.Round(2).BigFloat().Float64()
order.OrderCompute.PlanOrderCount = planOrderCount
if hasUsePartnerBonus {
order.OrderCompute.UsePartnerBonus, _ = usePartnerBonus.Round(2).BigFloat().Float64()
} else {
//订单中的货品列表中合伙人分成没有调整值的情况下,对订单的调整值设置为负值用以标识
order.OrderCompute.UsePartnerBonus = -1
}
if hasUseOrderAmount {
order.OrderCompute.UseOrderAmount, _ = useOrderAmount.Round(2).BigFloat().Float64()
//计算业务员的抽成
order.OrderCompute.SalesmanBonus, _ = useOrderAmount.
Mul(decimal.NewFromFloat(order.SalesmanBonusPercent)).
Div(decimal.NewFromInt(100)).
Round(2).BigFloat().Float64()
} else {
//订单中的货品列表中货品总金额没有调整值的情况下,对订单的调整值设置为负值用以标识
order.OrderCompute.UseOrderAmount = -1
order.OrderCompute.SalesmanBonus, _ = planOrderAmount.
Mul(decimal.NewFromFloat(order.SalesmanBonusPercent)).
Div(decimal.NewFromInt(100)).
Round(2).BigFloat().Float64()
}
if HasUseOrderCount {
order.OrderCompute.UseOrderCount = useOrderCount
} else {
order.OrderCompute.UseOrderCount = -1
}
return nil
}
type OrderBaseFindOneQuery struct {
OrderId int64
CompanyId int64
}
type OrderBaseFindQuery struct {
PartnerId int64
OrderCode string
DeliveryCode string
Offset int
Limit int
OrderType int
CompanyId int64
}
type OrderBaseRepository interface {
Save(order *OrderBase) error
FindOne(qureyOptions OrderBaseFindOneQuery) (*OrderBase, error)
Find(queryOptions OrderBaseFindQuery) ([]OrderBase, int, error)
Remove(id int64, companyId int64) error
}