66 lines
2.0 KiB
Go
66 lines
2.0 KiB
Go
package rechargeser
|
|
|
|
import (
|
|
"91jd-server/models/commod"
|
|
"91jd-server/models/v/advanceordermod"
|
|
"91jd-server/models/v/productmod"
|
|
"time"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
// applyAdvanceDisplay changes response-only prices after a successful deposit.
|
|
// Checkout still loads the original product and order from the database.
|
|
func applyAdvanceDisplay(uid uint64, products []productmod.Product, now time.Time) error {
|
|
if uid == 0 {
|
|
return nil
|
|
}
|
|
ids := make([]primitive.ObjectID, 0)
|
|
for _, p := range products {
|
|
if balanceCardEligible(p, now) {
|
|
ids = append(ids, p.ID)
|
|
}
|
|
}
|
|
if len(ids) == 0 {
|
|
return nil
|
|
}
|
|
orders, err := advanceordermod.QueryAllDocument(bson.M{
|
|
"uid": uid, "productID": bson.M{"$in": ids},
|
|
"status": bson.M{"$in": []int{advanceordermod.AdvanceSUCCESS, advanceordermod.BalanceProcessing}},
|
|
"endTime": bson.M{"$gt": now},
|
|
}, options.Find().SetLimit(int64(len(ids))).SetMaxTime(3*time.Second).
|
|
SetProjection(bson.M{"productID": 1, "_id": 0}))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
pending := make(map[primitive.ObjectID]bool, len(orders))
|
|
for _, order := range orders {
|
|
pending[order.ProductID] = true
|
|
}
|
|
for i := range products {
|
|
if pending[products[i].ID] && balanceCardEligible(products[i], now) {
|
|
setBalanceDisplay(&products[i])
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func balanceCardEligible(p productmod.Product, now time.Time) bool {
|
|
return p.ProductType == commod.AdvanceCard && p.BalanceAmount > 0 &&
|
|
!now.Before(p.StartTime) && now.Before(p.EndTime)
|
|
}
|
|
|
|
func setBalanceDisplay(p *productmod.Product) {
|
|
price := p.BalanceAmount
|
|
// Compact cards read advanceAmount when their presale context is absent.
|
|
// Like upgrade pricing, normalize this response field, not stored amounts.
|
|
p.AdvanceAmount = price
|
|
p.DiscountedPrice, p.DiscountedPriceIos, p.DiscountedPriceAnd = price, &price, &price
|
|
p.IsUpgrade = false
|
|
if p.EndBGImg != "" {
|
|
p.BGImg = p.EndBGImg
|
|
}
|
|
}
|