company_v2.go
5.4 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
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"
"time"
)
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 request.PageIndex == 0 {
rsp.Timestamp = time.Now().Unix() * 1000
}
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 CompanyRepository, _ = factory.CreateCompanyRepository(transactionContext)
if company, err = CompanyRepository.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 := newQueryOptionV2(request)
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
mapPartners["joinWays"] = newJoinWays(mapCategoryInfo, p)
mapPartners["dividend"] = utils.Decimal(p.Bonus)
mapPartners["ordersCount"] = p.Total
mapPartners["ordersMoney"] = utils.Decimal(p.Amount)
mapPartners["phone"] = p.Account
mapPartners["company"] = newCompany(company, p)
array = append(array, mapPartners)
}
return array, nil
}
return nil, nil
}
// 查询参数
func newQueryOptionV2(request *protocolx.PartnersRequest) map[string]interface{} {
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 {
currentDayEnd := utils.GetDayEnd().Unix() * 1000
if request.EndTime >= currentDayEnd {
request.EndTime = currentDayEnd
}
queryOption["endTime"] = request.EndTime / 1000
}
if request.Keyword != "" {
queryOption["keyword"] = request.Keyword
}
queryOption["limit"] = request.PageSize
queryOption["offset"] = request.PageSize * request.PageIndex
queryOption["sortByBonus"] = domain.DESC
if request.SortBy != 0 {
queryOption["sortByBonus"] = domain.ASC
}
return queryOption
}
func newCompany(company *domain.Company, p *domain.PartnerStatics) userx.Company {
partnerCompany := userx.Company{
Id: company.Id,
Name: company.Name,
Phone: company.Phone,
SerialNo: p.Id,
CareerSerialNo: "",
BusinessSerialNo: "",
DevelopSerialNo: "",
AppSerialNo: "",
CooperateTime: p.CooperateTime.Unix() * 1000,
MiniProgram: nil,
District: map[string]interface{}{"id": p.RegionInfo.RegionId, "name": p.RegionInfo.RegionName},
}
//获取合伙人编号
for _, partnerCategoryInfo := range p.PartnerCategoryInfos {
if partnerCategoryInfo.Id == 1 {
partnerCompany.CareerSerialNo = partnerCategoryInfo.Code
} else if partnerCategoryInfo.Id == 2 {
partnerCompany.BusinessSerialNo = partnerCategoryInfo.Code
} else if partnerCategoryInfo.Id == 3 {
partnerCompany.DevelopSerialNo = partnerCategoryInfo.Code
} else if partnerCategoryInfo.Id == 4 {
partnerCompany.AppSerialNo = partnerCategoryInfo.Code
}
}
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{}{}
}
return partnerCompany
}