pg_login_services.go
17.9 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
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
package domain_service
import (
"fmt"
"github.com/tiptok/gocomm/xa/eda"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/constant"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/dao"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/models"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/transaction"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/repository"
http_gateway "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/svr"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/utils"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/log"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/protocol"
protocolx "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/protocol/auth"
"strings"
"time"
)
type PgLoginService struct {
Phone string
eda.EventCenterPublisher
PartnerInfo []*domain.PartnerInfo
NormalPartnerInfo []*domain.PartnerInfo
IsPartnerInfo []*domain.PartnerInfo
Users []*domain.Users
IsSenior []*domain.Users
transactionContext *transaction.TransactionContext
// 标识:登录的账号信息是否有可用的公司, true:有 false:没有
HasAvailableCompany bool
HasAvailableManagerCompany bool
}
func (svr *PgLoginService) Init(phone string) (err error) {
var (
PartnerInfoService, _ = repository.NewPartnerInfoRepository(svr.transactionContext)
UsersRepository, _ = repository.NewUsersRepository(svr.transactionContext)
)
svr.Phone = phone
_, svr.PartnerInfo, err = PartnerInfoService.Find(map[string]interface{}{"account": phone, "status": 1, "sortByCreateTime": "ASC"})
_, svr.Users, err = UsersRepository.Find(map[string]interface{}{"phone": phone, "status": 1, "sortByCreateTime": "ASC", "deleteAtIsNull": true})
return nil
}
func (svr *PgLoginService) InitNormal(phone string) (err error) {
var (
PartnerInfoService, _ = repository.NewPartnerInfoRepository(svr.transactionContext)
)
svr.Phone = phone
_, svr.NormalPartnerInfo, err = PartnerInfoService.Find(map[string]interface{}{"account": phone, "sortByCreateTime": "ASC"})
return nil
}
func (svr *PgLoginService) InitSenior(phone string) (err error) {
var (
UsersRepository, _ = repository.NewUsersRepository(svr.transactionContext)
)
svr.Phone = phone
_, svr.IsSenior, err = UsersRepository.Find(map[string]interface{}{"phone": phone, "status": 1, "sortByCreateTime": "ASC", "deleteAtIsNull": true, "isSenior": 1})
return nil
}
func (svr *PgLoginService) InitPartner(phone string) (err error) {
var (
PartnerInfoService, _ = repository.NewPartnerInfoRepository(svr.transactionContext)
)
svr.Phone = phone
_, svr.IsPartnerInfo, err = PartnerInfoService.Find(map[string]interface{}{"account": phone, "status": 1, "sortByCreateTime": "ASC", "isNot": constant.DEFAULT_GUEST_COMPANY})
return nil
}
/**
* @Author SteveChan
* @Description // 判断是否是高管
* @Date 10:44 2021/1/12
* @Param
* @return
**/
func (svr *PgLoginService) CheckIsSenior(phone string, companyId int64) (ok bool, err error) {
var (
UsersRepository, _ = repository.NewUsersRepository(svr.transactionContext)
)
svr.Phone = phone
user, err := UsersRepository.FindOne(map[string]interface{}{"phone": phone, "status": 1, "companyId": companyId, "deleteAtIsNull": true})
if err != nil {
return false, err
} else {
if user.IsSenior == 1 {
ok = true
} else if user.IsSenior == 2 {
ok = false
}
}
return ok, nil
}
/**
* @Author SteveChan
* @Description // 判断是否合伙人
* @Date 13:25 2021/1/13
* @Param
* @return
**/
func (svr *PgLoginService) CheckIsPartner(phone string, companyId int64) (ok bool, err error) {
var (
PartnerInfoRepository, _ = repository.NewPartnerInfoRepository(svr.transactionContext)
)
svr.Phone = phone
user, err := PartnerInfoRepository.FindOne(map[string]interface{}{"account": phone, "status": 1})
if err != nil {
return false, err
}
if user != nil {
ok = true
} else {
ok = false
}
return ok, nil
}
//合伙人登录
func (svr *PgLoginService) PartnerLogin(phone string, password string) (err error) {
if len(svr.PartnerInfo) == 0 {
//子账号
err = protocol.NewErrWithMessage(502, err) //账号不存在
return
}
if !strings.EqualFold(svr.PartnerInfo[0].Password, password) {
err = protocol.NewCustomMessage(1, "密码输入有误!")
return
}
return
}
//管理层登录
func (svr *PgLoginService) ManagerLogin(phone string, password string) (err error) {
if len(svr.Users) == 0 {
//子账号
err = protocol.NewErrWithMessage(502, err) //账号不存在
return
}
var openid int64
ucenerSvr := http_gateway.NewHttplibUCenterApiServiceGateway()
openid, err = ucenerSvr.ServerLogin(phone, password, 1)
if err == nil && openid > 0 {
_, err = svr.transactionContext.PgTx.Exec("update users set open_id=? where phone=?", openid, phone)
}
return
}
//合伙人统计信息
func (svr *PgLoginService) PartnerStaticInfo() (interface{}, error) {
response := make(map[string]interface{})
response["id"] = protocolx.AdminTypePartner
response["name"] = protocolx.AdminTypePartnerName
if len(svr.PartnerInfo) == 0 {
response["companys"] = make([]struct{}, 0)
return response, nil
}
var (
OrderDao, _ = dao.NewOrderBaseDao(svr.transactionContext)
BusinessBonusDao, _ = dao.NewBusinessBonusDao(svr.transactionContext)
PartnerCategoryInfoRepository, _ = repository.NewPartnerCategoryInfoRepository(svr.transactionContext)
companyList []*domain.Company
allPartnerCategory []*domain.PartnerCategoryInfo
BusinessBonusRepository, _ = repository.NewBusinessBonusRepository(svr.transactionContext)
UsersRepository, _ = repository.NewUsersRepository(svr.transactionContext)
adminApiGateway = http_gateway.NewHttplibBusinessAdminApiServiceGateway()
)
doGetCompanyIds := func() []int64 {
var companies []int64
for i := range svr.PartnerInfo {
companies = append(companies, svr.PartnerInfo[i].CompanyId)
}
return companies
}
doGetPartnerIds := func() []int64 {
var array []int64
for i := range svr.PartnerInfo {
array = append(array, svr.PartnerInfo[i].Id)
}
return array
}
companyList = svr.GetCompanyList(doGetCompanyIds)
if len(companyList) == 0 {
return response, nil
}
totalBonus, e := OrderDao.OrderBonusStatics(domain.OrderBonusQuery{InPartnerIds: doGetPartnerIds(), OrderTypes: domain.UserOrderTypes(domain.Career)})
if e != nil {
return response, e
}
if businessBonus, e := BusinessBonusDao.OrderBonusStatics(domain.OrderBonusQuery{InPartnerIds: doGetPartnerIds(), IsDisable: 1}); e == nil {
totalBonus.Bonus += businessBonus.Bonus
}
_, allPartnerCategory, e = PartnerCategoryInfoRepository.Find(map[string]interface{}{"sortById": domain.ASC})
var mapPartnerBussinessBonus = make(map[int64]*domain.BusinessBonus)
if _, bussinessBonus, e := BusinessBonusRepository.Find(map[string]interface{}{"inPartnerIds": doGetPartnerIds(), "isDisable": 1}); e == nil {
for _, v := range bussinessBonus {
mapPartnerBussinessBonus[v.PartnerInfoId] = v
}
}
if e != nil {
log.Error(e)
return response, e
}
var mapCompanyAdminUsers map[int64]*domain.Users = make(map[int64]*domain.Users) //key:company_id value:domain.users
if _, adminUsers, e := UsersRepository.Find(map[string]interface{}{"inCompanyIds": doGetCompanyIds(), "adminType": 2, "deleteAtIsNull": true}); e == nil {
for i := range adminUsers {
mapCompanyAdminUsers[adminUsers[i].CompanyId] = adminUsers[i]
}
}
var companys = make([]*Company, 0)
for i := range companyList {
c := companyList[i]
var partner *domain.PartnerInfo
for j := range svr.PartnerInfo {
if svr.PartnerInfo[j].CompanyId == c.Id {
partner = svr.PartnerInfo[j]
break
}
}
if partner == nil {
continue
}
if au, ok := mapCompanyAdminUsers[partner.CompanyId]; ok {
if code, e := adminApiGateway.UserAuth(au.Id, constant.BUSINESS_ADMIN_PLATFORM_ID); e != nil || code != 0 {
log.Debug("【合伙人检查权限】", svr.Phone, "【公司】", c.Id, au.Id, code, e.Error())
continue
} else {
log.Debug("【合伙人检查权限】", svr.Phone, "【公司】", c.Id, au.Id, code, e)
}
} else {
log.Debug("【合伙人检查权限】", svr.Phone, "【公司】", c.Id, partner.Id, "公司管理员不存在")
continue
}
bonus, _ := OrderDao.OrderBonusStatics(domain.OrderBonusQuery{PartnerId: partner.Id, OrderTypes: domain.UserOrderTypes(domain.Career)})
if v, ok := mapPartnerBussinessBonus[partner.Id]; ok {
bonus.Bonus += v.Bonus
}
item := &Company{
CompanyBase: newCompanyBase(c),
IncomePercent: computeBonusPercent(totalBonus.Bonus, bonus.Bonus*100),
DividendMoney: utils.Decimal(bonus.Bonus),
JoinWays: svr.GetJoinWays(allPartnerCategory, partner, bonus.Bonus),
}
companys = append(companys, item)
}
if len(companys) > 0 {
svr.HasAvailableCompany = true
}
if len(companys) > 1 {
var companies = make([]*Company, 0)
for _, company := range companys {
if company.CompanyBase.Id != int64(constant.DEFAULT_GUEST_COMPANY) {
companies = append(companies, company)
}
}
companys = companies
}
response["companys"] = companys
return response, nil
}
// 高管统计信息
func (svr *PgLoginService) ManagerStaticInfo() (interface{}, error) {
response := make(map[string]interface{})
response["id"] = protocolx.AdminTypeManager
response["name"] = protocolx.AdminTypeManagerName
if len(svr.Users) == 0 {
response["companys"] = make([]struct{}, 0)
return response, nil
}
var (
companyList []*domain.Company
adminApiGateway = http_gateway.NewHttplibBusinessAdminApiServiceGateway()
)
doGetCompanyIds := func() []int64 {
var companies []int64
for i := range svr.IsSenior {
companies = append(companies, svr.IsSenior[i].CompanyId)
}
return companies
}
companyList = svr.GetCompanyList(doGetCompanyIds)
var companys = make([]protocol.CompanyBase, 0)
for i := range companyList {
c := companyList[i]
if constant.POSTGRESQL_DB_NAME != "partner_dev" {
//通过企业平台 校验模块权限
var user *domain.Users
for j := range svr.IsSenior {
if svr.IsSenior[j].CompanyId == c.Id {
user = svr.IsSenior[j]
break
}
}
if user != nil {
if code, e := adminApiGateway.UserAuth(user.Id, constant.BUSINESS_ADMIN_PLATFORM_ID); e != nil || code != 0 {
log.Debug("【检查权限】", svr.Phone, "【公司】", c.Id, user.Id, code, e.Error())
continue
} else {
log.Debug("【检查权限】", svr.Phone, "【公司】", c.Id, user.Id, code, e)
}
}
}
item := newCompanyBase(c)
companys = append(companys, item)
}
if len(companys) > 0 {
svr.HasAvailableManagerCompany = true
}
response["companys"] = companys
return response, nil
}
func (svr *PgLoginService) GetCompanyList(funcGetCompanyIds func() []int64) []*domain.Company {
var (
CompanyRepository, _ = repository.NewCompanyRepository(svr.transactionContext)
companyList []*domain.Company
)
var companies []int64 = funcGetCompanyIds()
if len(companies) == 0 {
return companyList
}
if _, v, e := CompanyRepository.Find(map[string]interface{}{"inCompanyIds": companies, "enable": 1, "sortByCreateTime": domain.DESC}); e != nil {
log.Error(e)
return companyList
} else {
companyList = v
}
return companyList
}
func (svr *PgLoginService) GetJoinWays(partnerCategory []*domain.PartnerCategoryInfo, partnerInfo *domain.PartnerInfo, bonus float64) []joinWay {
searchCategory := func(partnerCategory []*domain.PartnerCategoryInfo, id int64) *domain.PartnerCategoryInfo {
for i := range partnerCategory {
if partnerCategory[i].Id == id {
return partnerCategory[i]
}
}
return nil
}
var joinWays []joinWay
for i := range partnerCategory {
c := partnerCategory[i]
searchC := searchCategory(partnerInfo.PartnerCategoryInfos, c.Id)
if searchC != nil {
item := joinWay{
Type: int(c.Id),
Name: c.Name,
}
joinWays = append(joinWays, item)
}
}
for i := range joinWays {
joinWays[i].Percent = computeBonusPercent(float64(len(joinWays)), 1) * 100
}
return joinWays
}
func newCompanyBase(company *domain.Company) protocol.CompanyBase {
return protocol.CompanyBase{
Id: company.Id,
Name: company.Name,
ShortName: company.Abbreviation,
Image: protocol.NewImage(company.Logo),
Phone: company.Phone,
}
}
func computeBonusPercent(totalBonus float64, bonus float64) float64 {
if totalBonus == 0 {
return 0
}
return utils.Decimal(bonus / totalBonus)
}
/**
* @Author SteveChan
* @Description // 注册用户到合伙人
* @Date 13:52 2021/1/13
* @Param
* @return
**/
func (svr *PgLoginService) RegistryUser(phone string) error {
var (
PartnerInfoService, _ = repository.NewPartnerInfoRepository(svr.transactionContext)
UsersRepository, _ = repository.NewUsersRepository(svr.transactionContext)
)
partnerInfo, errPartner := PartnerInfoService.FindOne(map[string]interface{}{"account": phone, "isNot": constant.DEFAULT_GUEST_COMPANY}) // 合伙人是否存在
guestInfo, _ := PartnerInfoService.FindOne(map[string]interface{}{"account": phone, "companyId": constant.DEFAULT_GUEST_COMPANY}) // 合伙人是否存在游客公司
user, errUser := UsersRepository.FindOne(map[string]interface{}{"phone": phone, "deleteAtIsNull": true}) // 用户是否存在
senior, errSenior := UsersRepository.FindOne(map[string]interface{}{"phone": phone, "isSenior": 2, "deleteAtIsNull": true}) // 用户是否是高管
fmt.Print("存在合伙人身份:", partnerInfo != nil, "\n")
fmt.Print("用户存在:", user != nil, "\n")
fmt.Print("高管:", senior != nil, "\n")
// 游客公司合伙人存在,更新合伙人
//if guestInfo != nil {
// _, err := svr.transactionContext.PgTx.Model(&models.PartnerInfo{
// CompanyId: int64(constant.DEFAULT_GUEST_COMPANY),
// PartnerName: phone,
// Account: phone,
// Password: "7c4a8d09ca3762af61e59520943dc26494f8941b",
// PartnerCategory: 1,
// RegionInfo: &domain.RegionInfo{
// RegionName: "客户区域",
// RegionId: 0,
// },
// Status: guestInfo.Status,
// CooperateTime: time.Now(),
// CreateAt: time.Now(),
// UpdateAt: time.Now(),
// PartnerCategoryInfos: []*models.PartnerCategoryInfo{&models.PartnerCategoryInfo{Id: 1, Code: phone}},
// Salesman: []*domain.Salesman{&domain.Salesman{Name: phone, Telephone: phone}}}).
// Where("partner_info.account = ?", phone).
// Where("partner_info.company_id = ?", constant.DEFAULT_GUEST_COMPANY).
// Update()
// if err != nil {
// return err
// }
//}
// 合伙人存在、用户存在、是高管(高管合伙人)
// 合伙人不存在、用户存在、是高管(高管)|| partnerInfo == nil && user != nil && senior != nil
// 合伙人存在、用户存在、非高管(合伙人)
// 游客公司合伙人存在
if partnerInfo != nil && user != nil && senior != nil || partnerInfo != nil && user != nil && senior == nil || guestInfo != nil {
return nil
}
// 合伙人不存在、用户存在、非高管(普通用户)-> 注册用户
// 合伙人不存在、用户不存在(游客)-> 注册用户
if errPartner != nil && errUser == nil && errSenior == nil || errPartner != nil && errUser != nil {
id := time.Now().Unix()
errPartner = svr.transactionContext.PgDd.Insert(&models.PartnerInfo{
Id: id,
CompanyId: int64(constant.DEFAULT_GUEST_COMPANY),
PartnerName: phone,
Account: phone,
Password: "7c4a8d09ca3762af61e59520943dc26494f8941b",
Status: 1,
PartnerCategory: 1,
RegionInfo: &domain.RegionInfo{
RegionName: "客户区域",
RegionId: 0,
},
CooperateTime: time.Now(),
CreateAt: time.Now(),
UpdateAt: time.Now(),
PartnerCategoryInfos: []*models.PartnerCategoryInfo{&models.PartnerCategoryInfo{Id: 1, Code: phone}},
Salesman: []*domain.Salesman{&domain.Salesman{Name: phone, Telephone: phone}},
})
}
return errPartner
}
// 注册游客到指定的公司
func (svr *PgLoginService) RegistryGuest(phone string) error {
var (
PartnerInfoService, _ = repository.NewPartnerInfoRepository(svr.transactionContext)
UsersRepository, _ = repository.NewUsersRepository(svr.transactionContext)
)
partnerInfo, errPartner := PartnerInfoService.FindOne(map[string]interface{}{"account": phone})
user, errUser := UsersRepository.FindOne(map[string]interface{}{"phone": phone, "deleteAtIsNull": true})
if partnerInfo != nil || user != nil {
return nil
}
if errUser != nil && errPartner != nil {
id := time.Now().Unix()
errPartner = svr.transactionContext.PgDd.Insert(&models.PartnerInfo{
Id: id,
CompanyId: int64(constant.DEFAULT_GUEST_COMPANY),
PartnerName: phone,
Account: phone,
Password: "7c4a8d09ca3762af61e59520943dc26494f8941b",
Status: 1,
PartnerCategory: 1,
RegionInfo: &domain.RegionInfo{
RegionName: "客户区域",
RegionId: 0,
},
CooperateTime: time.Now(),
CreateAt: time.Now(),
UpdateAt: time.Now(),
PartnerCategoryInfos: []*models.PartnerCategoryInfo{&models.PartnerCategoryInfo{Id: 1}},
Salesman: []*domain.Salesman{&domain.Salesman{Name: phone, Telephone: phone}},
})
}
return errPartner
}
type Company struct {
protocol.CompanyBase
// 收入占比
IncomePercent float64 `json:"incomePercent"`
// 分红金额
DividendMoney float64 `json:"dividendMoney"`
JoinWays []joinWay `json:"joinWays"`
}
type incomeInfo struct {
// 收入占比
IncomePercent float64 `json:"incomePercent"`
// 分红金额
DividendMoney float64 `json:"dividendMoney"`
}
type joinWay struct {
Type int `json:"type"`
Name string `json:"name"`
Percent float64 `json:"percent"`
}
func NewPgLoginService(ctx *transaction.TransactionContext) *PgLoginService {
return &PgLoginService{
transactionContext: ctx,
HasAvailableCompany: false,
}
}