作者 yangfu

增加:公司统计

... ... @@ -64,7 +64,11 @@ func Statistics(header *protocol.RequestHeader, request *protocolx.StatisticsReq
defer func() {
transactionContext.RollbackTransaction()
}()
rsp.Statistics, err = getStatistics(header.UserId, transactionContext)
if err != nil {
log.Error(err)
return
}
err = transactionContext.CommitTransaction()
return
}
... ... @@ -127,3 +131,47 @@ func getJoinWays(transactionContext *transaction.TransactionContext) (joinWays [
}
return
}
// 获取公司统计
func getStatistics(userId int64, transactionContext *transaction.TransactionContext) (interface{}, error) {
var (
UsersRepository, _ = factory.CreateUsersRepository(transactionContext)
PartnerInfoDao, _ = factory.CreatePartnerInfoDao(transactionContext)
OrderBaseDao, _ = factory.CreateOrderBaseDao(transactionContext)
BusinessBonusDao, _ = factory.CreateBusinessBonusDao(transactionContext)
//user *domain.Users
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{})
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}); e == nil {
Statistics["careerOrdersMoney"] = bonus.TotalOrderAmount
Statistics["careerDividend"] = bonus.BonusExpense
}
if businessBonus, e := BusinessBonusDao.OrderBonusStatics(domain.OrderBonusQuery{InPartnerIds: partnerIds, IsDisable: 1}); e == nil {
Statistics["businessDividend"] = businessBonus.BonusExpense
Statistics["businessOrdersMoney"] = businessBonus.TotalOrderAmount
}
Statistics["developDividend"] = 0
Statistics["developOrdersMoney"] = 0
Statistics["appDividend"] = 0
Statistics["appOrdersMoney"] = 0
return Statistics, nil
}
... ...
... ... @@ -12,3 +12,7 @@ func CreatePartnerInfoDao(ctx *transaction.TransactionContext) (*dao.PartnerInfo
func CreateOrderBaseDao(ctx *transaction.TransactionContext) (*dao.OrderBaseDao, error) {
return dao.NewOrderBaseDao(ctx)
}
func CreateBusinessBonusDao(ctx *transaction.TransactionContext) (*dao.BusinessBonusDao, error) {
return dao.NewBusinessBonusDao(ctx)
}
... ...
... ... @@ -26,6 +26,7 @@ type OrderBonusQuery struct {
PartnerId int64 `json:"partnerId,omitempty"`
CompanyId int64 `json:"companyId,omitempty"`
InPartnerIds []int64 `json:"inPartnerIds,omitempty"`
IsDisable int `json:"isDisable,omitempty"`
}
// 订单分红统计-应答
... ... @@ -36,4 +37,6 @@ type OrderBonusResponse struct {
Total int64 `json:"companyId,omitempty"`
// 分红支出
BonusExpense float64 `json:"bonusExpense,omitempty"`
// 订单金额
TotalOrderAmount float64 `json:"totalOrderAmount"`
}
... ...
... ... @@ -21,3 +21,10 @@ func (m *PartnerCategoryInfo) Identify() interface{} {
}
return m.Id
}
const (
Career = iota + 1
Business
Develop
App
)
... ...
package dao
import (
"fmt"
"github.com/go-pg/pg/v10"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/models"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/transaction"
)
type BusinessBonusDao struct {
transactionContext *transaction.TransactionContext
}
//订单分红统计
func (dao *BusinessBonusDao) OrderBonusStatics(option domain.OrderBonusQuery) (rsp domain.OrderBonusResponse, err error) {
rsp = domain.OrderBonusResponse{}
if option.PartnerId == 0 && option.CompanyId == 0 && len(option.InPartnerIds) == 0 {
return
}
tx := dao.transactionContext.PgTx
q := tx.Model(new(models.BusinessBonus))
q.ColumnExpr("count(*) count")
q.ColumnExpr("sum(bonus) bonus")
q.ColumnExpr("sum(bonus_expense) bonus_expense")
if option.PartnerId > 0 {
q.Where(`"business_bonus".partner_info_id =?`, option.PartnerId)
}
if option.CompanyId > 0 {
q.Where(`"business_bonus".company_id =?`, option.CompanyId)
}
if option.IsDisable > 0 {
q.Where(`"business_bonus".is_disable =?`, option.IsDisable)
}
if len(option.InPartnerIds) > 0 {
q.Where(`"business_bonus".partner_info_id in (?)`, pg.In(option.InPartnerIds))
}
err = q.Select(&rsp.Total, &rsp.Bonus, &rsp.BonusExpense)
return
}
func NewBusinessBonusDao(transactionContext *transaction.TransactionContext) (*BusinessBonusDao, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &BusinessBonusDao{
transactionContext: transactionContext,
}, nil
}
}
... ...
... ... @@ -55,6 +55,7 @@ func (dao *OrderBaseDao) OrderBonusStatics(option domain.OrderBonusQuery) (rsp d
q.ColumnExpr("count(*) count")
q.ColumnExpr("sum(plan_partner_bonus) bonus")
q.ColumnExpr("sum(partner_bonus_expense) bonus_expense")
q.ColumnExpr("sum(plan_order_amount) total_order_amount")
if option.PartnerId > 0 {
q.Where(`"order_base".partner_id =?`, option.PartnerId)
}
... ... @@ -64,7 +65,7 @@ func (dao *OrderBaseDao) OrderBonusStatics(option domain.OrderBonusQuery) (rsp d
if len(option.InPartnerIds) > 0 {
q.Where(`"order_base".partner_id in (?)`, pg.In(option.InPartnerIds))
}
err = q.Select(&rsp.Total, &rsp.Bonus, &rsp.BonusExpense)
err = q.Select(&rsp.Total, &rsp.Bonus, &rsp.BonusExpense, &rsp.TotalOrderAmount)
return
}
... ...
... ... @@ -2,6 +2,8 @@ package dao
import (
"fmt"
"github.com/go-pg/pg/v10"
"github.com/go-pg/pg/v10/orm"
"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"
... ... @@ -25,6 +27,33 @@ func (dao *PartnerInfoDao) Update(queryOptions map[string]interface{}) error {
return err
}
func (dao *PartnerInfoDao) PartnerStatic(queryOptions map[string]interface{}) (count int, err error) {
tx := dao.transactionContext.PgTx
m := new(models.PartnerInfo)
query := NewQuery(tx.Model(m), queryOptions)
query.ColumnExpr("count(*) count")
if inPartnerIds, ok := queryOptions["inPartnerIds"]; ok {
query.Where("id in (?)", pg.In(inPartnerIds.([]int64)))
}
if inPartnerCategory, ok := queryOptions["inPartnerCategory"]; ok {
query.Where(`partner_info.partner_category_infos @>'[{"id":?}]'`, inPartnerCategory)
}
// 多个合伙人类型
if inPartnerCategory, ok := queryOptions["inPartnerCategories"]; ok {
query.Query = query.WhereGroup(func(q *orm.Query) (*orm.Query, error) {
if arrayInPartnerCategory, ok := inPartnerCategory.([]int64); ok {
for i := range arrayInPartnerCategory {
q.WhereOr(`partner_info.partner_category_infos @>'[{"id":?}]')`, arrayInPartnerCategory[i])
}
}
return q, nil
})
}
err = query.Select(&count)
return
}
//func(dao *PartnerInfoDao)PartnerCompanyInfo(queryOptions map[string]interface{})(values map[string]interface{},err error){
//// tx := dao.transactionContext.PgTx
//// m := new(models.PartnerInfo)
... ...
... ... @@ -2,6 +2,7 @@ package repository
import (
"github.com/go-pg/pg/v10"
"github.com/go-pg/pg/v10/orm"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/models"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/transaction"
... ... @@ -86,6 +87,17 @@ func (repository *PartnerInfoRepository) Find(queryOptions map[string]interface{
if inPartnerIds, ok := queryOptions["inPartnerIds"]; ok {
query.Where("id in (?)", pg.In(inPartnerIds.([]int64)))
}
// 合伙人类型
if inPartnerCategory, ok := queryOptions["inPartnerCategory"]; ok {
query.Query = query.WhereGroup(func(q *orm.Query) (*orm.Query, error) {
if arrayInPartnerCategory, ok := inPartnerCategory.([]int64); ok {
for i := range arrayInPartnerCategory {
q.WhereOr(`partner_info.partner_category_infos @>'[{"id":?}]')`, arrayInPartnerCategory[i])
}
}
return q, nil
})
}
var err error
if query.AffectRow, err = query.SelectAndCount(); err != nil {
return 0, PartnerInfos, err
... ...
package company
type StatisticsResponse struct {
// 唯一标识
Id int64 `json:"id"`
// 统计对象
Statistics interface{} `json:"statistics"`
}
... ...