f
This commit is contained in:
@@ -4,6 +4,9 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"qnc-server/common/globalkey"
|
||||
|
||||
"github.com/Masterminds/squirrel"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/stores/cache"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
@@ -18,6 +21,8 @@ type (
|
||||
agentModel
|
||||
// UpdateInTransaction 在事务中更新刚插入的记录,避免 UpdateWithVersion 中的 FindOne 缓存问题
|
||||
UpdateInTransaction(ctx context.Context, session sqlx.Session, data *Agent) error
|
||||
// CountSubordinatesByTeamLeaderIds 按团队首领 id 统计未删除的直属代理数量(team_leader_id = 传入 id)
|
||||
CountSubordinatesByTeamLeaderIds(ctx context.Context, leaderIds []string) (map[string]int64, error)
|
||||
}
|
||||
|
||||
customAgentModel struct {
|
||||
@@ -63,3 +68,36 @@ func (m *customAgentModel) UpdateInTransaction(ctx context.Context, session sqlx
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type teamLeaderSubCountRow struct {
|
||||
TeamLeaderId string `db:"team_leader_id"`
|
||||
Cnt int64 `db:"cnt"`
|
||||
}
|
||||
|
||||
func (m *customAgentModel) CountSubordinatesByTeamLeaderIds(ctx context.Context, leaderIds []string) (map[string]int64, error) {
|
||||
out := make(map[string]int64)
|
||||
if len(leaderIds) == 0 {
|
||||
return out, nil
|
||||
}
|
||||
// 不含「自己挂在自己名下」的记录,否则首领会在自己的下级列表里且下级数量虚高
|
||||
qb := squirrel.Select("team_leader_id", "COUNT(*) AS cnt").
|
||||
From(m.table).
|
||||
Where(squirrel.Eq{"del_state": globalkey.DelStateNo}).
|
||||
Where(squirrel.Eq{"team_leader_id": leaderIds}).
|
||||
Where("`id` <> `team_leader_id`").
|
||||
GroupBy("team_leader_id")
|
||||
query, args, err := qb.ToSql()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var rows []teamLeaderSubCountRow
|
||||
if err := m.QueryRowsNoCacheCtx(ctx, &rows, query, args...); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, r := range rows {
|
||||
if r.TeamLeaderId != "" {
|
||||
out[r.TeamLeaderId] = r.Cnt
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
@@ -1,6 +1,12 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"qnc-server/common/globalkey"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/stores/cache"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
)
|
||||
@@ -12,6 +18,10 @@ type (
|
||||
// and implement the added methods in customProductFeatureModel.
|
||||
ProductFeatureModel interface {
|
||||
productFeatureModel
|
||||
// SumFeatureCostPriceByProductId 汇总该产品下已关联且未删除的模块成本(feature.cost_price 之和)
|
||||
SumFeatureCostPriceByProductId(ctx context.Context, productId string) (float64, error)
|
||||
// SumFeatureCostPriceByProductIds 批量汇总,未出现在结果中的产品视为 0
|
||||
SumFeatureCostPriceByProductIds(ctx context.Context, productIds []string) (map[string]float64, error)
|
||||
}
|
||||
|
||||
customProductFeatureModel struct {
|
||||
@@ -25,3 +35,48 @@ func NewProductFeatureModel(conn sqlx.SqlConn, c cache.CacheConf) ProductFeature
|
||||
defaultProductFeatureModel: newProductFeatureModel(conn, c),
|
||||
}
|
||||
}
|
||||
|
||||
func (m *customProductFeatureModel) SumFeatureCostPriceByProductId(ctx context.Context, productId string) (float64, error) {
|
||||
query := `
|
||||
SELECT COALESCE(SUM(f.cost_price), 0)
|
||||
FROM product_feature pf
|
||||
INNER JOIN feature f ON f.id = pf.feature_id AND f.del_state = ?
|
||||
WHERE pf.product_id = ? AND pf.del_state = ?`
|
||||
var sum float64
|
||||
err := m.QueryRowNoCacheCtx(ctx, &sum, query, globalkey.DelStateNo, productId, globalkey.DelStateNo)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return sum, nil
|
||||
}
|
||||
|
||||
func (m *customProductFeatureModel) SumFeatureCostPriceByProductIds(ctx context.Context, productIds []string) (map[string]float64, error) {
|
||||
out := make(map[string]float64)
|
||||
if len(productIds) == 0 {
|
||||
return out, nil
|
||||
}
|
||||
placeholders := strings.TrimSuffix(strings.Repeat("?,", len(productIds)), ",")
|
||||
query := fmt.Sprintf(`
|
||||
SELECT pf.product_id, COALESCE(SUM(f.cost_price), 0) AS total
|
||||
FROM product_feature pf
|
||||
INNER JOIN feature f ON f.id = pf.feature_id AND f.del_state = ?
|
||||
WHERE pf.del_state = ? AND pf.product_id IN (%s)
|
||||
GROUP BY pf.product_id`, placeholders)
|
||||
args := make([]interface{}, 0, 2+len(productIds))
|
||||
args = append(args, globalkey.DelStateNo, globalkey.DelStateNo)
|
||||
for _, id := range productIds {
|
||||
args = append(args, id)
|
||||
}
|
||||
var rows []struct {
|
||||
ProductId string `db:"product_id"`
|
||||
Total float64 `db:"total"`
|
||||
}
|
||||
err := m.QueryRowsNoCacheCtx(ctx, &rows, query, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for i := range rows {
|
||||
out[rows[i].ProductId] = rows[i].Total
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user