正在显示
4 个修改的文件
包含
131 行增加
和
25 行删除
@@ -5,6 +5,7 @@ import ( | @@ -5,6 +5,7 @@ import ( | ||
5 | "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/factory" | 5 | "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/factory" |
6 | "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain" | 6 | "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain" |
7 | "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/transaction" | 7 | "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/transaction" |
8 | + "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/utils" | ||
8 | "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/protocol" | 9 | "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/protocol" |
9 | protocolx "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/protocol/company" | 10 | protocolx "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/protocol/company" |
10 | "strings" | 11 | "strings" |
@@ -181,10 +182,11 @@ func getStatistics(userId int64, transactionContext *transaction.TransactionCont | @@ -181,10 +182,11 @@ func getStatistics(userId int64, transactionContext *transaction.TransactionCont | ||
181 | 182 | ||
182 | func getPartners(userId int64, request *protocolx.PartnersRequest, transactionContext *transaction.TransactionContext) (interface{}, error) { | 183 | func getPartners(userId int64, request *protocolx.PartnersRequest, transactionContext *transaction.TransactionContext) (interface{}, error) { |
183 | var ( | 184 | var ( |
184 | - UsersRepository, _ = factory.CreateUsersRepository(transactionContext) | ||
185 | - PartnerInfo, _ = factory.CreatePartnerInfoDao(transactionContext) | ||
186 | - user *domain.Users | ||
187 | - err error | 185 | + UsersRepository, _ = factory.CreateUsersRepository(transactionContext) |
186 | + PartnerInfo, _ = factory.CreatePartnerInfoDao(transactionContext) | ||
187 | + user *domain.Users | ||
188 | + err error | ||
189 | + PartnerCategoryInfoRepository, _ = factory.CreatePartnerCategoryInfoRepository(transactionContext) | ||
188 | ) | 190 | ) |
189 | if user, err = UsersRepository.FindOne(map[string]interface{}{"id": userId}); err != nil { | 191 | if user, err = UsersRepository.FindOne(map[string]interface{}{"id": userId}); err != nil { |
190 | return nil, err | 192 | return nil, err |
@@ -192,8 +194,65 @@ func getPartners(userId int64, request *protocolx.PartnersRequest, transactionCo | @@ -192,8 +194,65 @@ func getPartners(userId int64, request *protocolx.PartnersRequest, transactionCo | ||
192 | if len(user.AccessPartnerIds()) == 0 { | 194 | if len(user.AccessPartnerIds()) == 0 { |
193 | return nil, nil | 195 | return nil, nil |
194 | } | 196 | } |
195 | - if cc, e := PartnerInfo.PartnerStatics(nil); e == nil { | ||
196 | - return cc, nil | 197 | + mapCategoryInfo := make(map[int64]*domain.PartnerCategoryInfo) |
198 | + if _, categories, e := PartnerCategoryInfoRepository.Find(nil); e == nil { | ||
199 | + for i := range categories { | ||
200 | + mapCategoryInfo[categories[i].Id] = categories[i] | ||
201 | + } | ||
202 | + } | ||
203 | + | ||
204 | + queryOption := make(map[string]interface{}) | ||
205 | + | ||
206 | + if len(request.Districts) > 0 { | ||
207 | + var districts []string | ||
208 | + for i := 0; i < len(request.Districts); i++ { | ||
209 | + districts = append(districts, request.Districts[i].Name) | ||
210 | + } | ||
211 | + queryOption["districts"] = districts | ||
212 | + } | ||
213 | + if len(request.JoinWays) > 0 { | ||
214 | + queryOption["joinWays"] = request.JoinWays | ||
215 | + } | ||
216 | + if request.StartTime > 0 { | ||
217 | + queryOption["startTime"] = request.StartTime / 1000 | ||
218 | + } | ||
219 | + if request.EndTime > 0 { | ||
220 | + queryOption["endTime"] = request.EndTime / 1000 | ||
221 | + } | ||
222 | + queryOption["limit"] = request.PageSize | ||
223 | + queryOption["offset"] = request.PageSize * request.PageIndex | ||
224 | + | ||
225 | + queryOption["sortByBonus"] = domain.DESC | ||
226 | + if request.SortBy != 0 { | ||
227 | + queryOption["sortByBonus"] = domain.ASC | ||
228 | + } | ||
229 | + | ||
230 | + if partners, e := PartnerInfo.Partners(user.AccessPartnerIds(), queryOption); e == nil { | ||
231 | + var array []interface{} | ||
232 | + for i := range partners { | ||
233 | + mapPartners := make(map[string]interface{}) | ||
234 | + p := partners[i] | ||
235 | + mapPartners["uid"] = p.Id | ||
236 | + mapPartners["uname"] = p.PartnerName | ||
237 | + | ||
238 | + var joinWays []protocolx.JoinWays | ||
239 | + for j := range p.PartnerCategoryInfos { | ||
240 | + c := p.PartnerCategoryInfos[j] | ||
241 | + if v, ok := mapCategoryInfo[c.Id]; ok { | ||
242 | + joinWays = append(joinWays, protocolx.JoinWays{Type: v.Id, Name: v.Name}) | ||
243 | + } | ||
244 | + } | ||
245 | + mapPartners["joinWays"] = joinWays | ||
246 | + if p.RegionInfo != nil { | ||
247 | + mapPartners["district"] = protocolx.Districts{Id: p.RegionInfo.RegionId, Name: p.RegionInfo.RegionName} | ||
248 | + } | ||
249 | + mapPartners["cooperationTime"] = p.CooperateTime.Unix() * 1000 | ||
250 | + mapPartners["dividend"] = p.BonusExpense | ||
251 | + mapPartners["ordersCount"] = p.Total | ||
252 | + mapPartners["ordersMoney"] = utils.Decimal(p.Amount) | ||
253 | + array = append(array, mapPartners) | ||
254 | + } | ||
255 | + return array, nil | ||
197 | } | 256 | } |
198 | return nil, nil | 257 | return nil, nil |
199 | } | 258 | } |
@@ -8,4 +8,6 @@ type PartnerStatics struct { | @@ -8,4 +8,6 @@ type PartnerStatics struct { | ||
8 | Bonus float64 `json:"bonus"` | 8 | Bonus float64 `json:"bonus"` |
9 | // 分红支出 | 9 | // 分红支出 |
10 | BonusExpense float64 `json:"bonus_expense"` | 10 | BonusExpense float64 `json:"bonus_expense"` |
11 | + // 订单数 | ||
12 | + Total float64 `json:"total"` | ||
11 | } | 13 | } |
@@ -8,6 +8,7 @@ import ( | @@ -8,6 +8,7 @@ import ( | ||
8 | "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/models" | 8 | "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/models" |
9 | "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/transaction" | 9 | "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/transaction" |
10 | . "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/utils" | 10 | . "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/utils" |
11 | + "strings" | ||
11 | ) | 12 | ) |
12 | 13 | ||
13 | type PartnerInfoDao struct { | 14 | type PartnerInfoDao struct { |
@@ -55,29 +56,73 @@ func (dao *PartnerInfoDao) PartnerStatic(queryOptions map[string]interface{}) (c | @@ -55,29 +56,73 @@ func (dao *PartnerInfoDao) PartnerStatic(queryOptions map[string]interface{}) (c | ||
55 | return | 56 | return |
56 | } | 57 | } |
57 | 58 | ||
58 | -func (dao *PartnerInfoDao) PartnerStatics(queryOptions map[string]interface{}) (statics []*domain.PartnerStatics, err error) { | 59 | +func (dao *PartnerInfoDao) Partners(partnerIds []int64, queryOptions map[string]interface{}) (statics []*domain.PartnerStatics, err error) { |
59 | tx := dao.transactionContext.PgDd | 60 | tx := dao.transactionContext.PgDd |
60 | - //m := new(models.PartnerInfo) | ||
61 | - sql := fmt.Sprintf(`select A.*, | ||
62 | -(B.amount1+C.amount2) amount,(B.bonus1+C.bonus2) bonus,(B.bonus_expense1 + C.bonus_expense2) bonus_expense | ||
63 | -from | 61 | + var sql = strings.Builder{} |
62 | + var filterPartners = strings.Builder{} | ||
63 | + | ||
64 | + if districts, ok := queryOptions["districts"]; ok { | ||
65 | + districts, ok := districts.([]string) | ||
66 | + if ok && len(districts) > 0 { | ||
67 | + var filterDistricts = strings.Builder{} | ||
68 | + for i := range districts { | ||
69 | + filterDistricts.WriteString(fmt.Sprintf(` region_info @>'{"regionName":"%v"}'`, districts[i])) | ||
70 | + if i != (len(districts) - 1) { | ||
71 | + filterDistricts.WriteString(" or ") | ||
72 | + } | ||
73 | + } | ||
74 | + filterPartners.WriteString(fmt.Sprintf(" and (%v)", filterDistricts.String())) | ||
75 | + } | ||
76 | + } | ||
77 | + if joinWays, ok := queryOptions["joinWays"]; ok { | ||
78 | + joinWays, ok := joinWays.([]int64) | ||
79 | + if ok && len(joinWays) > 0 { | ||
80 | + var filterJoinWays = strings.Builder{} | ||
81 | + for i := range joinWays { | ||
82 | + filterJoinWays.WriteString(fmt.Sprintf(` partner_category_infos @>'[{"id":%v}]'`, joinWays[i])) | ||
83 | + if i != (len(joinWays) - 1) { | ||
84 | + filterJoinWays.WriteString(" or ") | ||
85 | + } | ||
86 | + } | ||
87 | + filterPartners.WriteString(fmt.Sprintf(" and (%v)", filterJoinWays.String())) | ||
88 | + } | ||
89 | + } | ||
90 | + if startTime, ok := queryOptions["startTime"]; ok { | ||
91 | + filterPartners.WriteString(fmt.Sprintf(" and cooperate_time>=to_timestamp(%v)", startTime)) | ||
92 | + } | ||
93 | + if endTime, ok := queryOptions["endTime"]; ok { | ||
94 | + filterPartners.WriteString(fmt.Sprintf(" and cooperate_time<to_timestamp(%v)", endTime)) | ||
95 | + } | ||
96 | + | ||
97 | + sql.WriteString(fmt.Sprintf(` | ||
98 | +SELECT A.*,B.total,B.amount,COALESCE(B.bonus,0) bonus,B.bonus_expense | ||
99 | +FROM | ||
64 | ( | 100 | ( |
65 | - select * from partner_info | ||
66 | - WHERE (id in (1,2)) | 101 | + SELECT * FROM partner_info |
102 | + WHERE (id in (?)) %v | ||
67 | ) | 103 | ) |
68 | A left join | 104 | A left join |
69 | ( | 105 | ( |
70 | - SELECT partner_id,count(*) total1,sum(plan_order_amount) amount1, sum(plan_partner_bonus) bonus1, sum(partner_bonus_expense) bonus_expense1 FROM "order_base" AS "order_base" | ||
71 | - WHERE (partner_id in (1,2)) and order_type =1 | ||
72 | - GROUP BY partner_id | ||
73 | -) B on A."id" = B.partner_id | ||
74 | -left join | ||
75 | -( | ||
76 | - select partner_info_id,count(*) total2,0 amount2, sum(bonus) bonus2, sum(bonus_expense) bonus_expense2 from business_bonus | ||
77 | - WHERE (partner_info_id in (1,2)) and is_disable=1 | ||
78 | - GROUP BY partner_info_id | ||
79 | -) C on A."id"= C."partner_info_id"`) | ||
80 | - tx.Query(&statics, sql) | 106 | + SELECT partner_id,count(*) total,sum(amount) amount,sum(bonus) bonus,sum(bonus_expense) bonus_expense FROM |
107 | + ( | ||
108 | + SELECT partner_id,plan_order_amount amount, plan_partner_bonus bonus,partner_bonus_expense bonus_expense FROM "order_base" AS "order_base" | ||
109 | + WHERE (partner_id in (?)) and order_type =1 | ||
110 | + UNION | ||
111 | + SELECT partner_info_id partner_id,0 amount, bonus bonus, bonus_expense bonus_expense FROM business_bonus | ||
112 | + WHERE (partner_info_id in (?)) and is_disable=1 | ||
113 | + ) B | ||
114 | +GROUP BY partner_id | ||
115 | +) B on A."id" = B.partner_id`, filterPartners.String())) | ||
116 | + if sortByBonus, ok := queryOptions["sortByBonus"]; ok { | ||
117 | + sql.WriteString(fmt.Sprintf(" \norder by bonus %v", sortByBonus)) | ||
118 | + } | ||
119 | + if limit, ok := queryOptions["limit"]; ok { | ||
120 | + sql.WriteString(fmt.Sprintf(" \nLIMIT %v", limit)) | ||
121 | + if offset, ok := queryOptions["offset"]; ok { | ||
122 | + sql.WriteString(fmt.Sprintf(" \nOFFSET %v", offset)) | ||
123 | + } | ||
124 | + } | ||
125 | + _, err = tx.Query(&statics, sql.String(), pg.In(partnerIds), pg.In(partnerIds), pg.In(partnerIds)) | ||
81 | return | 126 | return |
82 | } | 127 | } |
83 | 128 |
@@ -12,7 +12,7 @@ type PartnersRequest struct { | @@ -12,7 +12,7 @@ type PartnersRequest struct { | ||
12 | // 区域(空或不传,即所有区域) | 12 | // 区域(空或不传,即所有区域) |
13 | Districts []Districts `json:"districts"` | 13 | Districts []Districts `json:"districts"` |
14 | // 合作类型(空或不传,即所有类型) | 14 | // 合作类型(空或不传,即所有类型) |
15 | - JoinWays []JoinWays `json:"joinWays"` | 15 | + JoinWays []int64 `json:"joinWays"` |
16 | // 分红排序(0.从多到少 1.从少到多) | 16 | // 分红排序(0.从多到少 1.从少到多) |
17 | SortBy int `json:"sortBy"` | 17 | SortBy int `json:"sortBy"` |
18 | } | 18 | } |
-
请 注册 或 登录 后发表评论