作者 yangfu

增加:公司合伙人列表

... ... @@ -5,6 +5,7 @@ import (
"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"
... ... @@ -181,10 +182,11 @@ func getStatistics(userId int64, transactionContext *transaction.TransactionCont
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
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
... ... @@ -192,8 +194,65 @@ func getPartners(userId int64, request *protocolx.PartnersRequest, transactionCo
if len(user.AccessPartnerIds()) == 0 {
return nil, nil
}
if cc, e := PartnerInfo.PartnerStatics(nil); e == nil {
return cc, 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 {
queryOption["joinWays"] = request.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
if p.RegionInfo != nil {
mapPartners["district"] = protocolx.Districts{Id: p.RegionInfo.RegionId, Name: p.RegionInfo.RegionName}
}
mapPartners["cooperationTime"] = p.CooperateTime.Unix() * 1000
mapPartners["dividend"] = p.BonusExpense
mapPartners["ordersCount"] = p.Total
mapPartners["ordersMoney"] = utils.Decimal(p.Amount)
array = append(array, mapPartners)
}
return array, nil
}
return nil, nil
}
... ...
... ... @@ -8,4 +8,6 @@ type PartnerStatics struct {
Bonus float64 `json:"bonus"`
// 分红支出
BonusExpense float64 `json:"bonus_expense"`
// 订单数
Total float64 `json:"total"`
}
... ...
... ... @@ -8,6 +8,7 @@ import (
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/models"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/transaction"
. "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/utils"
"strings"
)
type PartnerInfoDao struct {
... ... @@ -55,29 +56,73 @@ func (dao *PartnerInfoDao) PartnerStatic(queryOptions map[string]interface{}) (c
return
}
func (dao *PartnerInfoDao) PartnerStatics(queryOptions map[string]interface{}) (statics []*domain.PartnerStatics, err error) {
func (dao *PartnerInfoDao) Partners(partnerIds []int64, queryOptions map[string]interface{}) (statics []*domain.PartnerStatics, err error) {
tx := dao.transactionContext.PgDd
//m := new(models.PartnerInfo)
sql := fmt.Sprintf(`select A.*,
(B.amount1+C.amount2) amount,(B.bonus1+C.bonus2) bonus,(B.bonus_expense1 + C.bonus_expense2) bonus_expense
from
var sql = strings.Builder{}
var filterPartners = strings.Builder{}
if districts, ok := queryOptions["districts"]; ok {
districts, ok := districts.([]string)
if ok && len(districts) > 0 {
var filterDistricts = strings.Builder{}
for i := range districts {
filterDistricts.WriteString(fmt.Sprintf(` region_info @>'{"regionName":"%v"}'`, districts[i]))
if i != (len(districts) - 1) {
filterDistricts.WriteString(" or ")
}
}
filterPartners.WriteString(fmt.Sprintf(" and (%v)", filterDistricts.String()))
}
}
if joinWays, ok := queryOptions["joinWays"]; ok {
joinWays, ok := joinWays.([]int64)
if ok && len(joinWays) > 0 {
var filterJoinWays = strings.Builder{}
for i := range joinWays {
filterJoinWays.WriteString(fmt.Sprintf(` partner_category_infos @>'[{"id":%v}]'`, joinWays[i]))
if i != (len(joinWays) - 1) {
filterJoinWays.WriteString(" or ")
}
}
filterPartners.WriteString(fmt.Sprintf(" and (%v)", filterJoinWays.String()))
}
}
if startTime, ok := queryOptions["startTime"]; ok {
filterPartners.WriteString(fmt.Sprintf(" and cooperate_time>=to_timestamp(%v)", startTime))
}
if endTime, ok := queryOptions["endTime"]; ok {
filterPartners.WriteString(fmt.Sprintf(" and cooperate_time<to_timestamp(%v)", endTime))
}
sql.WriteString(fmt.Sprintf(`
SELECT A.*,B.total,B.amount,COALESCE(B.bonus,0) bonus,B.bonus_expense
FROM
(
select * from partner_info
WHERE (id in (1,2))
SELECT * FROM partner_info
WHERE (id in (?)) %v
)
A left join
(
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"
WHERE (partner_id in (1,2)) and order_type =1
GROUP BY partner_id
) B on A."id" = B.partner_id
left join
(
select partner_info_id,count(*) total2,0 amount2, sum(bonus) bonus2, sum(bonus_expense) bonus_expense2 from business_bonus
WHERE (partner_info_id in (1,2)) and is_disable=1
GROUP BY partner_info_id
) C on A."id"= C."partner_info_id"`)
tx.Query(&statics, sql)
SELECT partner_id,count(*) total,sum(amount) amount,sum(bonus) bonus,sum(bonus_expense) bonus_expense FROM
(
SELECT partner_id,plan_order_amount amount, plan_partner_bonus bonus,partner_bonus_expense bonus_expense FROM "order_base" AS "order_base"
WHERE (partner_id in (?)) and order_type =1
UNION
SELECT partner_info_id partner_id,0 amount, bonus bonus, bonus_expense bonus_expense FROM business_bonus
WHERE (partner_info_id in (?)) and is_disable=1
) B
GROUP BY partner_id
) B on A."id" = B.partner_id`, filterPartners.String()))
if sortByBonus, ok := queryOptions["sortByBonus"]; ok {
sql.WriteString(fmt.Sprintf(" \norder by bonus %v", sortByBonus))
}
if limit, ok := queryOptions["limit"]; ok {
sql.WriteString(fmt.Sprintf(" \nLIMIT %v", limit))
if offset, ok := queryOptions["offset"]; ok {
sql.WriteString(fmt.Sprintf(" \nOFFSET %v", offset))
}
}
_, err = tx.Query(&statics, sql.String(), pg.In(partnerIds), pg.In(partnerIds), pg.In(partnerIds))
return
}
... ...
... ... @@ -12,7 +12,7 @@ type PartnersRequest struct {
// 区域(空或不传,即所有区域)
Districts []Districts `json:"districts"`
// 合作类型(空或不传,即所有类型)
JoinWays []JoinWays `json:"joinWays"`
JoinWays []int64 `json:"joinWays"`
// 分红排序(0.从多到少 1.从少到多)
SortBy int `json:"sortBy"`
}
... ...