forked from ixre/go2o
-
Notifications
You must be signed in to change notification settings - Fork 0
/
partner.go
372 lines (317 loc) · 8.54 KB
/
partner.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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/**
* Copyright 2014 @ z3q.net.
* name :
* author : jarryliu
* date : 2013-12-12 16:55
* description :
* history :
*/
package partner
import (
"errors"
"fmt"
"go2o/src/core/domain/interface/enum"
"go2o/src/core/domain/interface/member"
"go2o/src/core/domain/interface/partner"
"go2o/src/core/domain/interface/partner/mss"
"go2o/src/core/domain/interface/partner/user"
mssImpl "go2o/src/core/domain/partner/mss"
userImpl "go2o/src/core/domain/partner/user"
"go2o/src/core/infrastructure"
"go2o/src/core/infrastructure/domain"
"go2o/src/core/variable"
"time"
)
var _ partner.IPartner = new(Partner)
type Partner struct {
_value *partner.ValuePartner
_saleConf *partner.SaleConf
_siteConf *partner.SiteConf
_apiInfo *partner.ApiInfo
_shops []partner.IShop
_host string
_rep partner.IPartnerRep
_userRep user.IUserRep
_memberRep member.IMemberRep
_userManager user.IUserManager
_confManager partner.IConfManager
_levelManager partner.ILevelManager
_kvManager partner.IKvManager
_memberKvManager partner.IKvManager
_mssManager mss.IMssManager
_mssRep mss.IMssRep
}
func NewPartner(v *partner.ValuePartner, rep partner.IPartnerRep, userRep user.IUserRep,
memberRep member.IMemberRep, mssRep mss.IMssRep) (partner.IPartner, error) {
var err error
if v == nil {
err = errors.New("101:no such partner")
return nil, err
}
if time.Now().Unix() > v.ExpiresTime {
err = errors.New("103: partner is expires")
}
return &Partner{
_value: v,
_rep: rep,
_userRep: userRep,
_memberRep: memberRep,
_mssRep: mssRep,
}, err
}
func (this *Partner) clearShopCache() {
this._shops = nil
}
func (this *Partner) GetAggregateRootId() int {
return this._value.Id
}
func (this *Partner) GetValue() partner.ValuePartner {
return *this._value
}
func (this *Partner) SetValue(v *partner.ValuePartner) error {
tv := this._value
if v.Id == tv.Id {
tv.Name = v.Name
tv.Address = v.Address
if v.LastLoginTime > 0 {
tv.LastLoginTime = v.LastLoginTime
}
if v.LoginTime > 0 {
tv.LoginTime = v.LoginTime
}
if len(v.Logo) != 0 {
tv.Logo = v.Logo
}
tv.Phone = v.Phone
tv.Pwd = v.Pwd
tv.UpdateTime = time.Now().Unix()
}
return nil
}
// 保存
func (this *Partner) Save() (int, error) {
var id int = this.GetAggregateRootId()
if id > 0 {
return this._rep.SavePartner(this._value)
}
return this.createPartner()
}
// 创建商户
func (this *Partner) createPartner() (int, error) {
if id := this.GetAggregateRootId(); id > 0 {
return id, nil
}
v := this._value
id, err := this._rep.SavePartner(v)
if err != nil {
return id, err
}
//todo:事务
// 初始化商户信息
this._value.Id = id
// SiteConf
this._siteConf = &partner.SiteConf{
IndexTitle: "线上商店-" + v.Name,
SubTitle: "线上商店-" + v.Name,
Logo: v.Logo,
State: 1,
StateHtml: "",
}
err = this._rep.SaveSiteConf(id, this._siteConf)
this._siteConf.PartnerId = id
// SaleConf
this._saleConf = &partner.SaleConf{
AutoSetupOrder: 1,
IntegralBackNum: 0,
}
err = this._rep.SaveSaleConf(id, this._saleConf)
this._saleConf.PartnerId = id
// 创建API
this._apiInfo = &partner.ApiInfo{
ApiId: domain.NewApiId(id),
ApiSecret: domain.NewSecret(id),
WhiteList: "*",
Enabled: 1,
}
err = this._rep.SaveApiInfo(id, this._apiInfo)
return id, err
}
// 获取商户的域名
func (this *Partner) GetMajorHost() string {
if len(this._host) == 0 {
host := this._rep.GetPartnerMajorHost(this.GetAggregateRootId())
if len(host) == 0 {
host = fmt.Sprintf("%s.%s", this._value.Usr, infrastructure.GetApp().
Config().GetString(variable.ServerDomain))
}
this._host = host
}
return this._host
}
// 获取销售配置
func (this *Partner) GetSaleConf() partner.SaleConf {
if this._saleConf == nil {
//10%分成
//0.2, #上级
//0.1, #上上级
//0.8 #消费者自己
this._saleConf = this._rep.GetSaleConf(
this.GetAggregateRootId())
this.verifySaleConf(this._saleConf)
}
return *this._saleConf
}
// 保存销售配置
func (this *Partner) SaveSaleConf(v *partner.SaleConf) error {
this.GetSaleConf()
if v.RegisterMode == partner.ModeRegisterClosed ||
v.RegisterMode == partner.ModeRegisterNormal ||
v.RegisterMode == partner.ModeRegisterMustInvitation ||
v.RegisterMode == partner.ModeRegisterMustRedirect {
this._saleConf.RegisterMode = v.RegisterMode
} else {
return partner.ErrRegisterMode
}
if v.FlowConvertCsn < 0 || v.PresentConvertCsn < 0 ||
v.ApplyCsn < 0 || v.TransCsn < 0 ||
v.FlowConvertCsn > 1 || v.PresentConvertCsn > 1 ||
v.ApplyCsn > 1 || v.TransCsn > 1 {
return partner.ErrSalesPercent
}
this.verifySaleConf(v)
this._saleConf = v
this._saleConf.PartnerId = this._value.Id
return this._rep.SaveSaleConf(this.GetAggregateRootId(), this._saleConf)
}
// 验证销售设置
func (this *Partner) verifySaleConf(v *partner.SaleConf) {
if v.OrderTimeOutMinute <= 0 {
v.OrderTimeOutMinute = 1440 // 一天
}
if v.OrderConfirmAfterMinute <= 0 {
v.OrderConfirmAfterMinute = 60 // 一小时后自动确认
}
if v.OrderTimeOutReceiveHour <= 0 {
v.OrderTimeOutReceiveHour = 7 * 24 // 7天后自动确认
}
}
// 获取站点配置
func (this *Partner) GetSiteConf() partner.SiteConf {
if this._siteConf == nil {
this._siteConf = this._rep.GetSiteConf(this.GetAggregateRootId())
}
return *this._siteConf
}
// 保存站点配置
func (this *Partner) SaveSiteConf(v *partner.SiteConf) error {
this._siteConf = v
this._siteConf.PartnerId = this._value.Id
return this._rep.SaveSiteConf(this.GetAggregateRootId(), this._siteConf)
}
// 获取API信息
func (this *Partner) GetApiInfo() partner.ApiInfo {
if this._apiInfo == nil {
this._apiInfo = this._rep.GetApiInfo(this.GetAggregateRootId())
}
return *this._apiInfo
}
// 保存API信息
func (this *Partner) SaveApiInfo(v *partner.ApiInfo) error {
this._apiInfo = v
this._apiInfo.PartnerId = this._value.Id
return this._rep.SaveApiInfo(this.GetAggregateRootId(), this._apiInfo)
}
// 新建商店
func (this *Partner) CreateShop(v *partner.ValueShop) partner.IShop {
v.CreateTime = time.Now().Unix()
v.PartnerId = this.GetAggregateRootId()
return newShop(this, v, this._rep)
}
// 获取所有商店
func (this *Partner) GetShops() []partner.IShop {
if this._shops == nil {
shops := this._rep.GetShopsOfPartner(this.GetAggregateRootId())
this._shops = make([]partner.IShop, len(shops))
for i, v := range shops {
this._shops[i] = this.CreateShop(v)
}
}
return this._shops
}
// 获取营业中的商店
func (this *Partner) GetBusinessInShops() []partner.IShop {
var list []partner.IShop = make([]partner.IShop, 0)
for _, v := range this._shops {
if v.GetValue().State == enum.ShopBusinessIn {
list = append(list, v)
}
}
return list
}
// 获取商店
func (this *Partner) GetShop(shopId int) partner.IShop {
// v := this.rep.GetValueShop(this.GetAggregateRootId(), shopId)
// if v == nil {
// return nil
// }
// return this.CreateShop(v)
shops := this.GetShops()
for _, v := range shops {
if v.GetValue().Id == shopId {
return v
}
}
return nil
}
// 删除门店
func (this *Partner) DeleteShop(shopId int) error {
//todo : 检测订单数量
return this._rep.DeleteShop(this.GetAggregateRootId(), shopId)
}
// 返回用户服务
func (this *Partner) UserManager() user.IUserManager {
if this._userManager == nil {
this._userManager = userImpl.NewUserManager(
this.GetAggregateRootId(),
this._userRep)
}
return this._userManager
}
// 返回设置服务
func (this *Partner) ConfManager() partner.IConfManager {
if this._confManager == nil {
this._confManager = &ConfManager{
_partnerId: this.GetAggregateRootId(),
_rep: this._rep,
}
}
return this._confManager
}
// 获取会员管理服务
func (this *Partner) LevelManager() partner.ILevelManager {
if this._levelManager == nil {
this._levelManager = NewLevelManager(this.GetAggregateRootId(), this._memberRep)
}
return this._levelManager
}
// 获取键值管理器
func (this *Partner) KvManager() partner.IKvManager {
if this._kvManager == nil {
this._kvManager = newKvManager(this, "kvset")
}
return this._kvManager
}
// 获取用户键值管理器
func (this *Partner) MemberKvManager() partner.IKvManager {
if this._memberKvManager == nil {
this._memberKvManager = newKvManager(this, "kvset_member")
}
return this._memberKvManager
}
// 消息系统管理器
func (this *Partner) MssManager() mss.IMssManager {
if this._mssManager == nil {
this._mssManager = mssImpl.NewMssManager(this, this._mssRep, this._rep)
}
return this._mssManager
}