feat(api): add referral percentage and only first purchase fields to user model and requests

This commit is contained in:
Chang lue Tsen 2025-09-01 09:03:39 -04:00
parent 4438b05272
commit aea20ffd5e
10 changed files with 96 additions and 61 deletions

View File

@ -37,6 +37,8 @@ type (
Avatar string `json:"avatar"`
Balance int64 `json:"balance"`
Commission int64 `json:"commission"`
ReferralPercentage uint8 `json:"referral_percentage"`
OnlyFirstPurchase bool `json:"only_first_purchase"`
GiftAmount int64 `json:"gift_amount"`
Telegram int64 `json:"telegram"`
ReferCode string `json:"refer_code"`
@ -58,6 +60,8 @@ type (
Password string `json:"password"`
ProductId int64 `json:"product_id"`
Duration int64 `json:"duration"`
ReferralPercentage uint8 `json:"referral_percentage"`
OnlyFirstPurchase bool `json:"only_first_purchase"`
RefererUser string `json:"referer_user"`
ReferCode string `json:"refer_code"`
Balance int64 `json:"balance"`

View File

@ -14,6 +14,8 @@ type (
Avatar string `json:"avatar"`
Balance int64 `json:"balance"`
Commission int64 `json:"commission"`
ReferralPercentage uint8 `json:"referral_percentage"`
OnlyFirstPurchase bool `json:"only_first_purchase"`
GiftAmount int64 `json:"gift_amount"`
Telegram int64 `json:"telegram"`
ReferCode string `json:"refer_code"`

View File

@ -0,0 +1,3 @@
ALTER TABLE `u`
DROP COLUMN `referral_percentage`,
DROP COLUMN `only_first_purchase`;

View File

@ -0,0 +1,7 @@
ALTER TABLE `u`
ADD COLUMN `referral_percentage` TINYINT UNSIGNED NOT NULL DEFAULT 0
COMMENT 'Referral Percentage'
AFTER `commission`,
ADD COLUMN `only_first_purchase` TINYINT(1) NOT NULL DEFAULT 1
COMMENT 'Only First Purchase'
AFTER `referral_percentage`;

View File

@ -40,6 +40,8 @@ func (l *CreateUserLogic) CreateUser(req *types.CreateUserRequest) error {
pwd := tool.EncodePassWord(req.Password)
newUser := &user.User{
Password: pwd,
ReferralPercentage: req.ReferralPercentage,
OnlyFirstPurchase: &req.OnlyFirstPurchase,
ReferCode: req.ReferCode,
Balance: req.Balance,
IsAdmin: &req.IsAdmin,

View File

@ -41,20 +41,20 @@ func (l *GetUserListLogic) GetUserList(req *types.GetUserListRequest) (*types.Ge
userRespList := make([]types.User, 0, len(list))
for _, item := range list {
var user types.User
tool.DeepCopy(&user, item)
var u types.User
tool.DeepCopy(&u, item)
// 处理 AuthMethods
authMethods := make([]types.UserAuthMethod, len(user.AuthMethods)) // 直接创建目标 slice
for i, method := range user.AuthMethods {
authMethods := make([]types.UserAuthMethod, len(u.AuthMethods)) // 直接创建目标 slice
for i, method := range u.AuthMethods {
tool.DeepCopy(&authMethods[i], method)
if method.AuthType == "mobile" {
authMethods[i].AuthIdentifier = phone.FormatToInternational(method.AuthIdentifier)
}
}
user.AuthMethods = authMethods
u.AuthMethods = authMethods
userRespList = append(userRespList, user)
userRespList = append(userRespList, u)
}
return &types.GetUserListResponse{

View File

@ -44,6 +44,8 @@ func (l *UpdateUserBasicInfoLogic) UpdateUserBasicInfo(req *types.UpdateUserBasi
userInfo.Balance = req.Balance
userInfo.GiftAmount = req.GiftAmount
userInfo.Commission = req.Commission
userInfo.OnlyFirstPurchase = &req.OnlyFirstPurchase
userInfo.ReferralPercentage = req.ReferralPercentage
if req.Password != "" {
if userInfo.Id == 2 && isDemo {

View File

@ -12,6 +12,8 @@ type User struct {
ReferCode string `gorm:"type:varchar(20);default:'';comment:Referral Code"`
RefererId int64 `gorm:"index:idx_referer;comment:Referrer ID"`
Commission int64 `gorm:"default:0;comment:Commission"` // Commission Amount
ReferralPercentage uint8 `gorm:"default:0;comment:Referral"` // Referral Percentage
OnlyFirstPurchase *bool `gorm:"default:true;not null;comment:Only First Purchase"` // Only First Purchase Referral
GiftAmount int64 `gorm:"default:0;comment:User Gift Amount"`
Enable *bool `gorm:"default:true;not null;comment:Is Account Enabled"`
IsAdmin *bool `gorm:"default:false;not null;comment:Is Admin"`

View File

@ -414,6 +414,8 @@ type CreateUserRequest struct {
Password string `json:"password"`
ProductId int64 `json:"product_id"`
Duration int64 `json:"duration"`
ReferralPercentage uint8 `json:"referral_percentage"`
OnlyFirstPurchase bool `json:"only_first_purchase"`
RefererUser string `json:"referer_user"`
ReferCode string `json:"refer_code"`
Balance int64 `json:"balance"`
@ -2239,6 +2241,8 @@ type UpdateUserBasiceInfoRequest struct {
Avatar string `json:"avatar"`
Balance int64 `json:"balance"`
Commission int64 `json:"commission"`
ReferralPercentage uint8 `json:"referral_percentage"`
OnlyFirstPurchase bool `json:"only_first_purchase"`
GiftAmount int64 `json:"gift_amount"`
Telegram int64 `json:"telegram"`
ReferCode string `json:"refer_code"`
@ -2285,6 +2289,8 @@ type User struct {
Avatar string `json:"avatar"`
Balance int64 `json:"balance"`
Commission int64 `json:"commission"`
ReferralPercentage uint8 `json:"referral_percentage"`
OnlyFirstPurchase bool `json:"only_first_purchase"`
GiftAmount int64 `json:"gift_amount"`
Telegram int64 `json:"telegram"`
ReferCode string `json:"refer_code"`

View File

@ -368,7 +368,14 @@ func (l *ActivateOrderLogic) handleCommission(ctx context.Context, userInfo *use
return
}
amount := l.calculateCommission(orderInfo.Price)
var referralPercentage uint8
if userInfo.ReferralPercentage != 0 {
referralPercentage = userInfo.ReferralPercentage
} else {
referralPercentage = uint8(l.svc.Config.Invite.ReferralPercentage)
}
amount := l.calculateCommission(orderInfo.Price, referralPercentage)
// Use transaction for commission updates
err = l.svc.DB.Transaction(func(tx *gorm.DB) error {
@ -420,12 +427,12 @@ func (l *ActivateOrderLogic) handleCommission(ctx context.Context, userInfo *use
func (l *ActivateOrderLogic) shouldProcessCommission(userInfo *user.User, orderInfo *order.Order, isNewPurchase bool) bool {
return userInfo.RefererId != 0 &&
l.svc.Config.Invite.ReferralPercentage != 0 &&
(!l.svc.Config.Invite.OnlyFirstPurchase || (isNewPurchase && orderInfo.IsNew))
(!l.svc.Config.Invite.OnlyFirstPurchase || (isNewPurchase && orderInfo.IsNew) || !*userInfo.OnlyFirstPurchase)
}
// calculateCommission computes the commission amount based on order price and referral percentage
func (l *ActivateOrderLogic) calculateCommission(price int64) int64 {
return int64(float64(price) * (float64(l.svc.Config.Invite.ReferralPercentage) / 100))
func (l *ActivateOrderLogic) calculateCommission(price int64, percentage uint8) int64 {
return int64(float64(price) * (float64(percentage) / 100))
}
// clearServerCache clears user list cache for all servers associated with the subscription