d12c340743
将 promo 字段从 Subscribe 顶层移至 SubscribeDiscount(discount[] 项内), 促销查询按 subscribe_id + quantity 维度写入,每个规格独立命中最高优先级规则。 Co-authored-by: multica-agent <github@multica.ai>
92 lines
2.6 KiB
Go
92 lines
2.6 KiB
Go
package subscribe
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
|
|
"github.com/perfect-panel/server/internal/model/subscribe"
|
|
"github.com/perfect-panel/server/internal/svc"
|
|
"github.com/perfect-panel/server/internal/types"
|
|
"github.com/perfect-panel/server/pkg/constant"
|
|
"github.com/perfect-panel/server/pkg/logger"
|
|
"github.com/perfect-panel/server/pkg/tool"
|
|
"github.com/perfect-panel/server/pkg/xerr"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
type QuerySubscribeListLogic struct {
|
|
logger.Logger
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
}
|
|
|
|
// Get subscribe list
|
|
func NewQuerySubscribeListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *QuerySubscribeListLogic {
|
|
return &QuerySubscribeListLogic{
|
|
Logger: logger.WithContext(ctx),
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
}
|
|
}
|
|
|
|
func (l *QuerySubscribeListLogic) QuerySubscribeList(req *types.QuerySubscribeListRequest) (resp *types.QuerySubscribeListResponse, err error) {
|
|
|
|
total, data, err := l.svcCtx.SubscribeModel.FilterList(l.ctx, &subscribe.FilterParams{
|
|
Page: 1,
|
|
Size: 9999,
|
|
Language: req.Language,
|
|
Sell: true,
|
|
DefaultLanguage: true,
|
|
})
|
|
if err != nil {
|
|
l.Errorw("[QuerySubscribeListLogic] Database Error", logger.Field("error", err.Error()))
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "QuerySubscribeList error: %v", err.Error())
|
|
}
|
|
|
|
resp = &types.QuerySubscribeListResponse{
|
|
Total: total,
|
|
}
|
|
list := make([]types.Subscribe, len(data))
|
|
subscribeIDs := make([]int64, 0, len(data))
|
|
for i, item := range data {
|
|
var sub types.Subscribe
|
|
tool.DeepCopy(&sub, item)
|
|
subscribeIDs = append(subscribeIDs, sub.Id)
|
|
if item.Discount != "" {
|
|
var discount []types.SubscribeDiscount
|
|
_ = json.Unmarshal([]byte(item.Discount), &discount)
|
|
sub.Discount = discount
|
|
}
|
|
list[i] = sub
|
|
}
|
|
|
|
promos, err := loadSubscribePromoMap(l.ctx, l.svcCtx, subscribeIDs)
|
|
if err != nil {
|
|
l.Errorw("[QuerySubscribeListLogic] Query Promo Error", logger.Field("error", err.Error()))
|
|
return nil, err
|
|
}
|
|
for i := range list {
|
|
applySubscribeDiscountPromos(&list[i], promos[list[i].Id])
|
|
}
|
|
|
|
// 老版本客户端(无 X-App-Id)去掉每个套餐 discount 的最后一个
|
|
hasAppId, _ := l.ctx.Value(constant.CtxKeyHasAppId).(bool)
|
|
if !hasAppId {
|
|
for i := range list {
|
|
if len(list[i].Discount) > 0 {
|
|
list[i].Discount = list[i].Discount[:len(list[i].Discount)-1]
|
|
}
|
|
}
|
|
}
|
|
|
|
resp.List = list
|
|
resp.Total = int64(len(list))
|
|
return
|
|
}
|
|
|
|
func applySubscribeDiscountPromos(subscribe *types.Subscribe, promoByQuantity map[int64]*types.SubscribePromo) {
|
|
for i := range subscribe.Discount {
|
|
subscribe.Discount[i].Promo = promoByQuantity[subscribe.Discount[i].Quantity]
|
|
}
|
|
}
|