order_base.go
9.3 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
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) DeleteGoods(goodIds []int64) {
var newGoods []OrderGood
for i := range order.Goods {
for _, goodId := range goodIds {
order.Goods[i].Id = goodId
continue
}
newGoods = append(newGoods, order.Goods[i])
}
order.Goods = newGoods
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)) {
//调整值非负值得情况
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)) {
hasUsePartnerBonus = true
usePartnerBonus = usePartnerBonus.Add(goodUsePartnerBonus)
} else {
usePartnerBonus = usePartnerBonus.Add(decimal.NewFromFloat(order.Goods[i].GoodCompute.PlanPartnerBonus))
}
if order.Goods[i].UseGoodNumber >= 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
}