company_v2.go
5.5 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
package company
import (
"github.com/tiptok/gocomm/pkg/log"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/factory"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/transaction"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/utils"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/protocol"
protocolx "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/protocol/company"
userx "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/protocol/user"
)
func PartnersV2(header *protocol.RequestHeader, request *protocolx.PartnersRequest) (rsp *protocolx.PartnersResponse, err error) {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
)
rsp = &protocolx.PartnersResponse{}
if err = transactionContext.StartTransaction(); err != nil {
log.Error(err)
return nil, err
}
defer func() {
transactionContext.RollbackTransaction()
}()
if rsp.Partners, err = getPartnersV2(header.UserId, request, transactionContext); err != nil {
log.Error(err)
return
}
if rsp.Partners == nil {
rsp.Partners = []struct{}{}
}
err = transactionContext.CommitTransaction()
return
}
func getPartnersV2(userId int64, request *protocolx.PartnersRequest, transactionContext *transaction.TransactionContext) (interface{}, error) {
var (
UsersRepository, _ = factory.CreateUsersRepository(transactionContext)
PartnerInfo, _ = factory.CreatePartnerInfoDao(transactionContext)
user *domain.Users
err error
PartnerCategoryInfoRepository, _ = factory.CreatePartnerCategoryInfoRepository(transactionContext)
)
if user, err = UsersRepository.FindOne(map[string]interface{}{"id": userId}); err != nil {
return nil, err
}
var company *domain.Company
var CompanyResponsitory, _ = factory.CreateCompanyRepository(transactionContext)
if company, err = CompanyResponsitory.FindOne(map[string]interface{}{"id": user.CompanyId}); err != nil {
return nil, err
}
if len(user.AccessPartnerIds()) == 0 {
return nil, nil
}
mapCategoryInfo := make(map[int64]*domain.PartnerCategoryInfo)
if _, categories, e := PartnerCategoryInfoRepository.Find(nil); e == nil {
for i := range categories {
mapCategoryInfo[categories[i].Id] = categories[i]
}
}
queryOption := make(map[string]interface{})
if len(request.Districts) > 0 {
var districts []string
for i := 0; i < len(request.Districts); i++ {
districts = append(districts, request.Districts[i].Name)
}
queryOption["districts"] = districts
}
if len(request.JoinWays) > 0 {
var joinWays []int64
for i := 0; i < len(request.JoinWays); i++ {
joinWays = append(joinWays, request.JoinWays[i].Type)
}
queryOption["joinWays"] = joinWays
}
if request.StartTime > 0 {
queryOption["startTime"] = request.StartTime / 1000
}
if request.EndTime > 0 {
queryOption["endTime"] = request.EndTime / 1000
}
queryOption["limit"] = request.PageSize
queryOption["offset"] = request.PageSize * request.PageIndex
queryOption["sortByBonus"] = domain.DESC
if request.SortBy != 0 {
queryOption["sortByBonus"] = domain.ASC
}
if partners, e := PartnerInfo.Partners(user.AccessPartnerIds(), queryOption); e == nil {
var array []interface{}
for i := range partners {
mapPartners := make(map[string]interface{})
p := partners[i]
mapPartners["uid"] = p.Id
mapPartners["uname"] = p.PartnerName
var joinWays []protocolx.JoinWays
for j := range p.PartnerCategoryInfos {
c := p.PartnerCategoryInfos[j]
if v, ok := mapCategoryInfo[c.Id]; ok {
joinWays = append(joinWays, protocolx.JoinWays{Type: v.Id, Name: v.Name})
}
}
mapPartners["joinWays"] = joinWays
mapPartners["dividend"] = utils.Decimal(p.Bonus)
mapPartners["ordersCount"] = p.Total
mapPartners["ordersMoney"] = utils.Decimal(p.Amount)
mapPartners["phone"] = p.Account
partnerCompany := userx.Company{
Id: company.Id,
Name: company.Name,
Phone: company.Phone,
SerialNo: p.Id,
CooperateTime: p.CooperateTime.Unix() * 1000,
MiniProgram: nil,
District: map[string]interface{}{"id": p.RegionInfo.RegionId, "name": p.RegionInfo.RegionName},
}
if len(p.Salesman) > 0 {
partnerCompany.Salesman = map[string]interface{}{"uname": p.Salesman[0].Name, "phone": p.Salesman[0].Telephone}
} else {
partnerCompany.Salesman = map[string]interface{}{}
}
mapPartners["company"] = partnerCompany
array = append(array, mapPartners)
}
return array, nil
}
return nil, nil
}
func funcPartnerCompanyInfo(companyId int64, transactionContext *transaction.TransactionContext) (partnerCompany userx.Company, err error) {
var (
CompanyResponsitory, _ = factory.CreateCompanyRepository(transactionContext)
company *domain.Company
)
if company, err = CompanyResponsitory.FindOne(map[string]interface{}{"id": companyId}); err != nil {
return
}
partnerCompany = userx.Company{
Id: company.Id,
Name: company.Name,
Phone: company.Phone,
//SerialNo: partnerInfo.Id,
//CooperateTime: partnerInfo.CooperateTime.Unix() * 1000,
MiniProgram: nil,
//District: map[string]interface{}{"id": partnerInfo.RegionInfo.RegionId, "name": partnerInfo.RegionInfo.RegionName},
}
//if len(partnerInfo.Salesman) > 0 {
// u.CooperateCompany.Salesman = map[string]interface{}{"uname": partnerInfo.Salesman[0].Name, "phone": partnerInfo.Salesman[0].Telephone}
//} else {
// u.CooperateCompany.Salesman = map[string]interface{}{}
//}
//rspMap["user"] = u
//rsp = rspMap
return
}