company.go
10.1 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
package company
import (
"github.com/tiptok/gocomm/pkg/log"
"github.com/tiptok/gocomm/sync/task"
"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"
"strings"
)
// 公司属性 合伙人类型/区域
func Property(header *protocol.RequestHeader, request *protocolx.PropertyRequest) (rsp *protocolx.PropertyResponse, err error) {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
)
rsp = &protocolx.PropertyResponse{}
if err = transactionContext.StartTransaction(); err != nil {
log.Error(err)
return nil, err
}
defer func() {
transactionContext.RollbackTransaction()
}()
//并行查询区域/合伙人类型
task.Parallel(
func() {
if (request.Action & 0x01) > 0 {
rsp.Districts, _ = getDistricts(header.UserId, transactionContext)
}
},
func() {
if (request.Action & 0x02) > 0 {
rsp.JoinWays, _ = getJoinWays(transactionContext)
}
},
)
err = transactionContext.CommitTransaction()
return
}
func Partners(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 = getPartners(header.UserId, request, transactionContext); err != nil {
log.Error(err)
return
}
if rsp.Partners == nil {
rsp.Partners = []struct{}{}
}
err = transactionContext.CommitTransaction()
return
}
func Statistics(header *protocol.RequestHeader, request *protocolx.StatisticsRequest) (rsp *protocolx.StatisticsResponse, err error) {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
)
rsp = &protocolx.StatisticsResponse{}
if err = transactionContext.StartTransaction(); err != nil {
log.Error(err)
return nil, err
}
defer func() {
transactionContext.RollbackTransaction()
}()
rsp.Statistics, err = getStatistics(header.UserId, transactionContext)
if err != nil {
log.Error(err)
return
}
err = transactionContext.CommitTransaction()
return
}
// 获取区域列表
func getDistricts(userId int64, transactionContext *transaction.TransactionContext) (districts []protocolx.Districts, err error) {
var (
user *domain.Users
partnerInfos []*domain.PartnerInfo
UsersRepository, _ = factory.CreateUsersRepository(transactionContext)
PartnerInfoRepository, _ = factory.CreatePartnerInfoRepositoryIn(transactionContext)
)
if user, err = UsersRepository.FindOne(map[string]interface{}{"id": userId}); err != nil {
log.Error(err)
return
}
if user == nil || len(user.AccessPartners) == 0 {
return
}
if _, partnerInfos, err = PartnerInfoRepository.Find(map[string]interface{}{
"inPartnerIds": user.AccessPartnerIds(),
"sortByCreateTime": domain.ASC},
); err != nil {
return
}
for i := range partnerInfos {
partner := partnerInfos[i]
include := false
if partner.RegionInfo == nil {
continue
}
for j := range districts {
if strings.TrimSpace(districts[j].Name) == strings.TrimSpace(partner.RegionInfo.RegionName) {
include = true
break
}
}
if !include {
districts = append(districts, protocolx.Districts{
Id: partner.RegionInfo.RegionId,
Name: partner.RegionInfo.RegionName,
})
}
}
return
}
// 获取合伙人类型列表
func getJoinWays(transactionContext *transaction.TransactionContext) (joinWays []protocolx.JoinWays, err error) {
var (
CategoryInfoRepository, _ = factory.CreatePartnerCategoryInfoRepository(transactionContext)
categories []*domain.PartnerCategoryInfo
)
if _, categories, err = CategoryInfoRepository.Find(nil); err != nil && len(categories) == 0 {
return
}
for i := range categories {
c := categories[i]
joinWays = append(joinWays, protocolx.JoinWays{Type: c.Id, Name: c.Name})
}
return
}
/**
* @Author SteveChan
* @Description //TODO 获取公司统计,增加销售日期限制
* @Date 02:45 2021/2/3
* @Param
* @return
**/
func getStatistics(userId int64, transactionContext *transaction.TransactionContext) (interface{}, error) {
var (
UsersRepository, _ = factory.CreateUsersRepository(transactionContext)
PartnerInfoDao, _ = factory.CreatePartnerInfoDao(transactionContext)
OrderBaseDao, _ = factory.CreateOrderBaseDao(transactionContext)
partnerIds []int64
)
if user, e := UsersRepository.FindOne(map[string]interface{}{"id": userId}); e != nil || user == nil {
return struct {
}{}, nil
} else {
partnerIds = user.AccessPartnerIds()
}
Statistics := make(map[string]interface{})
// TODO:分组统计
if count, e := PartnerInfoDao.PartnerStatic(map[string]interface{}{"inPartnerIds": partnerIds, "inPartnerCategory": domain.Career}); e == nil {
Statistics["careerCount"] = count
}
if count, e := PartnerInfoDao.PartnerStatic(map[string]interface{}{"inPartnerIds": partnerIds, "inPartnerCategory": domain.Business}); e == nil {
Statistics["businessCount"] = count
}
if count, e := PartnerInfoDao.PartnerStatic(map[string]interface{}{"inPartnerIds": partnerIds, "inPartnerCategory": domain.Develop}); e == nil {
Statistics["developCount"] = count
}
if count, e := PartnerInfoDao.PartnerStatic(map[string]interface{}{"inPartnerIds": partnerIds, "inPartnerCategory": domain.App}); e == nil {
Statistics["appCount"] = count
}
if bonus, e := OrderBaseDao.OrderBonusStatics(domain.OrderBonusQuery{InPartnerIds: partnerIds, OrderTypes: domain.UserOrderTypes(domain.Career), PartnerCategoryId: domain.Career, EndTime: utils.GetDayEnd().Unix() * 1000}); e == nil {
Statistics["careerOrdersMoney"] = bonus.TotalOrderAmount
Statistics["careerDividend"] = bonus.Bonus
}
if bonus, e := OrderBaseDao.OrderBonusStatics(domain.OrderBonusQuery{InPartnerIds: partnerIds, OrderTypes: domain.UserOrderTypes(domain.Career), PartnerCategoryId: domain.Business, EndTime: utils.GetDayEnd().Unix() * 1000}); e == nil {
Statistics["businessOrdersMoney"] = bonus.TotalOrderAmount
Statistics["businessDividend"] = bonus.Bonus
}
if bonus, e := OrderBaseDao.OrderBonusStatics(domain.OrderBonusQuery{InPartnerIds: partnerIds, OrderTypes: domain.UserOrderTypes(domain.Career), PartnerCategoryId: domain.Develop, EndTime: utils.GetDayEnd().Unix() * 1000}); e == nil {
Statistics["developOrdersMoney"] = bonus.TotalOrderAmount
Statistics["developDividend"] = bonus.Bonus
}
if bonus, e := OrderBaseDao.OrderBonusStatics(domain.OrderBonusQuery{InPartnerIds: partnerIds, OrderTypes: domain.UserOrderTypes(domain.Career), PartnerCategoryId: domain.App, EndTime: utils.GetDayEnd().Unix() * 1000}); e == nil {
Statistics["appOrdersMoney"] = bonus.TotalOrderAmount
Statistics["appDividend"] = bonus.Bonus
}
Statistics["totalCount"] = len(partnerIds)
return Statistics, nil
}
func getPartners(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
}
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 := newQueryOption(request)
if partners, e := PartnerInfo.Partners(user.AccessPartnerIds(), queryOption); e == nil {
var array []interface{}
for i := range partners {
mapPartner := make(map[string]interface{})
p := partners[i]
mapPartner["uid"] = p.Id
mapPartner["uname"] = p.PartnerName
mapPartner["joinWays"] = newJoinWays(mapCategoryInfo, p)
if p.RegionInfo != nil {
mapPartner["district"] = protocolx.Districts{Id: p.RegionInfo.RegionId, Name: p.RegionInfo.RegionName}
}
mapPartner["cooperationTime"] = p.CooperateTime.Unix() * 1000
mapPartner["dividend"] = utils.Decimal(p.Bonus)
mapPartner["ordersCount"] = p.Total
mapPartner["ordersMoney"] = utils.Decimal(p.Amount)
array = append(array, mapPartner)
}
return array, nil
}
return nil, nil
}
// 查询参数
func newQueryOption(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 {
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
}
return queryOption
}
func newJoinWays(mapCategoryInfo map[int64]*domain.PartnerCategoryInfo, p *domain.PartnerStatics) []protocolx.JoinWays {
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})
}
}
return joinWays
}