正在显示
6 个修改的文件
包含
163 行增加
和
5 行删除
@@ -2,6 +2,7 @@ package auth | @@ -2,6 +2,7 @@ package auth | ||
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "fmt" | 4 | "fmt" |
5 | + "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/factory" | ||
5 | "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/partnerInfo/query" | 6 | "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/partnerInfo/query" |
6 | "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/partnerInfo/service" | 7 | "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/partnerInfo/service" |
7 | "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain" | 8 | "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain" |
@@ -14,14 +15,36 @@ import ( | @@ -14,14 +15,36 @@ import ( | ||
14 | 15 | ||
15 | func Login(header *protocol.RequestHeader, request *protocol.LoginRequest) (rsp *protocol.LoginResponse, err error) { | 16 | func Login(header *protocol.RequestHeader, request *protocol.LoginRequest) (rsp *protocol.LoginResponse, err error) { |
16 | var ( | 17 | var ( |
17 | - PartnerInfoService = service.NewPartnerInfoService(nil) | ||
18 | - partnerInfo *domain.PartnerInfo | ||
19 | - result bool = false | 18 | + PartnerInfoService = service.NewPartnerInfoService(nil) |
19 | + partnerInfo *domain.PartnerInfo | ||
20 | + result bool = false | ||
21 | + transactionContext, _ = factory.CreateTransactionContext(nil) | ||
22 | + PartnerSubAccountRepository, _ = factory.CreatePartnerSubAccountRepository(transactionContext) | ||
23 | + partnerSubAccount *domain.PartnerSubAccount | ||
20 | ) | 24 | ) |
25 | + if err = transactionContext.StartTransaction(); err != nil { | ||
26 | + return nil, err | ||
27 | + } | ||
28 | + defer func() { | ||
29 | + if err != nil { | ||
30 | + transactionContext.RollbackTransaction() | ||
31 | + return | ||
32 | + } | ||
33 | + err = transactionContext.CommitTransaction() | ||
34 | + }() | ||
21 | rsp = &protocol.LoginResponse{} | 35 | rsp = &protocol.LoginResponse{} |
22 | if partnerInfo, err = PartnerInfoService.GetPartnerInfo(&query.GetPartnerInfoQuery{Account: request.Phone}); err != nil { | 36 | if partnerInfo, err = PartnerInfoService.GetPartnerInfo(&query.GetPartnerInfoQuery{Account: request.Phone}); err != nil { |
23 | - err = protocol.NewErrWithMessage(502, err) //账号不存在 | ||
24 | - return | 37 | + //子账号 |
38 | + if partnerSubAccount, err = PartnerSubAccountRepository.FindOne(map[string]interface{}{"account": request.Phone}); err == nil { | ||
39 | + partnerInfo = &domain.PartnerInfo{ | ||
40 | + Id: partnerSubAccount.PartnerId, | ||
41 | + Account: partnerSubAccount.Account, | ||
42 | + Password: partnerSubAccount.Password, | ||
43 | + } | ||
44 | + } else { | ||
45 | + err = protocol.NewErrWithMessage(502, err) //账号不存在 | ||
46 | + return | ||
47 | + } | ||
25 | } | 48 | } |
26 | switch request.GrantType { | 49 | switch request.GrantType { |
27 | case protocol.LoginByPassword: | 50 | case protocol.LoginByPassword: |
@@ -37,3 +37,7 @@ func CreateOrderPaymentRepository(transactionContext *transaction.TransactionCon | @@ -37,3 +37,7 @@ func CreateOrderPaymentRepository(transactionContext *transaction.TransactionCon | ||
37 | func CreatePartnerInfoRepositoryIn(transactionContext *transaction.TransactionContext) (domain.PartnerInfoRepository, error) { | 37 | func CreatePartnerInfoRepositoryIn(transactionContext *transaction.TransactionContext) (domain.PartnerInfoRepository, error) { |
38 | return repository.NewPartnerInfoRepository(transactionContext) | 38 | return repository.NewPartnerInfoRepository(transactionContext) |
39 | } | 39 | } |
40 | + | ||
41 | +func CreatePartnerSubAccountRepository(transactionContext *transaction.TransactionContext) (domain.PartnerSubAccountRepository, error) { | ||
42 | + return repository.NewPartnerSubAccountRepository(transactionContext) | ||
43 | +} |
pkg/domain/partner_sub_account.go
0 → 100644
1 | +package domain | ||
2 | + | ||
3 | +//合伙人子账号 | ||
4 | +type PartnerSubAccount struct { | ||
5 | + Id int64 `json:"id"` | ||
6 | + Account string `json:"account"` | ||
7 | + Password string `json:"password"` | ||
8 | + PartnerId int64 `json:"partnerId"` //绑定合伙人账号 | ||
9 | +} | ||
10 | + | ||
11 | +type PartnerSubAccountRepository interface { | ||
12 | + Save(dm *PartnerSubAccount) (*PartnerSubAccount, error) | ||
13 | + Remove(dm *PartnerSubAccount) (*PartnerSubAccount, error) | ||
14 | + FindOne(queryOptions map[string]interface{}) (*PartnerSubAccount, error) | ||
15 | + Find(queryOptions map[string]interface{}) (int64, []*PartnerSubAccount, error) | ||
16 | +} | ||
17 | + | ||
18 | +func (m *PartnerSubAccount) Identify() interface{} { | ||
19 | + if m.Id == 0 { | ||
20 | + return nil | ||
21 | + } | ||
22 | + return m.Id | ||
23 | +} |
@@ -27,6 +27,7 @@ func init() { | @@ -27,6 +27,7 @@ func init() { | ||
27 | (*models.PartnerInfo)(nil), | 27 | (*models.PartnerInfo)(nil), |
28 | (*models.Order)(nil), | 28 | (*models.Order)(nil), |
29 | (*models.OrderPayment)(nil), | 29 | (*models.OrderPayment)(nil), |
30 | + (*models.PartnerSubAccount)(nil), | ||
30 | } { | 31 | } { |
31 | err := DB.CreateTable(model, &orm.CreateTableOptions{ | 32 | err := DB.CreateTable(model, &orm.CreateTableOptions{ |
32 | Temp: false, | 33 | Temp: false, |
1 | +package repository | ||
2 | + | ||
3 | +import ( | ||
4 | + "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain" | ||
5 | + "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/models" | ||
6 | + "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/transaction" | ||
7 | + . "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/utils" | ||
8 | +) | ||
9 | + | ||
10 | +type PartnerSubAccountRepository struct { | ||
11 | + transactionContext *transaction.TransactionContext | ||
12 | +} | ||
13 | + | ||
14 | +func (repository *PartnerSubAccountRepository) Save(dm *domain.PartnerSubAccount) (*domain.PartnerSubAccount, error) { | ||
15 | + var ( | ||
16 | + err error | ||
17 | + m = &models.PartnerSubAccount{} | ||
18 | + tx = repository.transactionContext.PgTx | ||
19 | + ) | ||
20 | + if err = GobModelTransform(m, dm); err != nil { | ||
21 | + return nil, err | ||
22 | + } | ||
23 | + if dm.Identify() == nil { | ||
24 | + if dm.Id, err = NewSnowflakeId(); err != nil { | ||
25 | + return dm, err | ||
26 | + } | ||
27 | + m.Id = dm.Id | ||
28 | + if err = tx.Insert(m); err != nil { | ||
29 | + return nil, err | ||
30 | + } | ||
31 | + return dm, nil | ||
32 | + } | ||
33 | + if err = tx.Update(m); err != nil { | ||
34 | + return nil, err | ||
35 | + } | ||
36 | + return dm, nil | ||
37 | +} | ||
38 | + | ||
39 | +func (repository *PartnerSubAccountRepository) Remove(PartnerSubAccount *domain.PartnerSubAccount) (*domain.PartnerSubAccount, error) { | ||
40 | + var ( | ||
41 | + tx = repository.transactionContext.PgTx | ||
42 | + PartnerSubAccountModel = &models.PartnerSubAccount{Id: PartnerSubAccount.Identify().(int64)} | ||
43 | + ) | ||
44 | + if _, err := tx.Model(PartnerSubAccountModel).Where("id = ?", PartnerSubAccount.Id).Delete(); err != nil { | ||
45 | + return PartnerSubAccount, err | ||
46 | + } | ||
47 | + return PartnerSubAccount, nil | ||
48 | +} | ||
49 | + | ||
50 | +func (repository *PartnerSubAccountRepository) FindOne(queryOptions map[string]interface{}) (*domain.PartnerSubAccount, error) { | ||
51 | + tx := repository.transactionContext.PgTx | ||
52 | + PartnerSubAccountModel := new(models.PartnerSubAccount) | ||
53 | + query := NewQuery(tx.Model(PartnerSubAccountModel), queryOptions) | ||
54 | + query.SetWhere("id = ?", "id") | ||
55 | + query.SetWhere("account = ?", "account") | ||
56 | + if err := query.First(); err != nil { | ||
57 | + return nil, query.HandleError(err, "没有此合伙人") | ||
58 | + } | ||
59 | + if PartnerSubAccountModel.Id == 0 { | ||
60 | + return nil, nil | ||
61 | + } | ||
62 | + return repository.transformPgModelToDomainModel(PartnerSubAccountModel) | ||
63 | +} | ||
64 | + | ||
65 | +func (repository *PartnerSubAccountRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.PartnerSubAccount, error) { | ||
66 | + tx := repository.transactionContext.PgTx | ||
67 | + var PartnerSubAccountModels []*models.PartnerSubAccount | ||
68 | + PartnerSubAccounts := make([]*domain.PartnerSubAccount, 0) | ||
69 | + query := NewQuery(tx.Model(&PartnerSubAccountModels), queryOptions) | ||
70 | + query. | ||
71 | + SetWhere("account = ?", "account"). | ||
72 | + SetLimit() | ||
73 | + var err error | ||
74 | + if query.AffectRow, err = query.SelectAndCount(); err != nil { | ||
75 | + return 0, PartnerSubAccounts, err | ||
76 | + } | ||
77 | + for _, PartnerSubAccountModel := range PartnerSubAccountModels { | ||
78 | + if PartnerSubAccount, err := repository.transformPgModelToDomainModel(PartnerSubAccountModel); err != nil { | ||
79 | + return 0, PartnerSubAccounts, err | ||
80 | + } else { | ||
81 | + PartnerSubAccounts = append(PartnerSubAccounts, PartnerSubAccount) | ||
82 | + } | ||
83 | + } | ||
84 | + return int64(query.AffectRow), PartnerSubAccounts, nil | ||
85 | +} | ||
86 | + | ||
87 | +func (repository *PartnerSubAccountRepository) transformPgModelToDomainModel(PartnerSubAccountModel *models.PartnerSubAccount) (*domain.PartnerSubAccount, error) { | ||
88 | + m := &domain.PartnerSubAccount{} | ||
89 | + err := GobModelTransform(m, PartnerSubAccountModel) | ||
90 | + return m, err | ||
91 | +} | ||
92 | + | ||
93 | +func NewPartnerSubAccountRepository(transactionContext *transaction.TransactionContext) (*PartnerSubAccountRepository, error) { | ||
94 | + if transactionContext == nil { | ||
95 | + return nil, ERR_EMPTY_TC | ||
96 | + } | ||
97 | + return &PartnerSubAccountRepository{transactionContext: transactionContext}, nil | ||
98 | +} |
-
请 注册 或 登录 后发表评论