feat(api): add referral percentage and only first purchase fields to user model and requests
This commit is contained in:
parent
4438b05272
commit
aea20ffd5e
@ -37,6 +37,8 @@ type (
|
|||||||
Avatar string `json:"avatar"`
|
Avatar string `json:"avatar"`
|
||||||
Balance int64 `json:"balance"`
|
Balance int64 `json:"balance"`
|
||||||
Commission int64 `json:"commission"`
|
Commission int64 `json:"commission"`
|
||||||
|
ReferralPercentage uint8 `json:"referral_percentage"`
|
||||||
|
OnlyFirstPurchase bool `json:"only_first_purchase"`
|
||||||
GiftAmount int64 `json:"gift_amount"`
|
GiftAmount int64 `json:"gift_amount"`
|
||||||
Telegram int64 `json:"telegram"`
|
Telegram int64 `json:"telegram"`
|
||||||
ReferCode string `json:"refer_code"`
|
ReferCode string `json:"refer_code"`
|
||||||
@ -58,6 +60,8 @@ type (
|
|||||||
Password string `json:"password"`
|
Password string `json:"password"`
|
||||||
ProductId int64 `json:"product_id"`
|
ProductId int64 `json:"product_id"`
|
||||||
Duration int64 `json:"duration"`
|
Duration int64 `json:"duration"`
|
||||||
|
ReferralPercentage uint8 `json:"referral_percentage"`
|
||||||
|
OnlyFirstPurchase bool `json:"only_first_purchase"`
|
||||||
RefererUser string `json:"referer_user"`
|
RefererUser string `json:"referer_user"`
|
||||||
ReferCode string `json:"refer_code"`
|
ReferCode string `json:"refer_code"`
|
||||||
Balance int64 `json:"balance"`
|
Balance int64 `json:"balance"`
|
||||||
|
|||||||
@ -14,6 +14,8 @@ type (
|
|||||||
Avatar string `json:"avatar"`
|
Avatar string `json:"avatar"`
|
||||||
Balance int64 `json:"balance"`
|
Balance int64 `json:"balance"`
|
||||||
Commission int64 `json:"commission"`
|
Commission int64 `json:"commission"`
|
||||||
|
ReferralPercentage uint8 `json:"referral_percentage"`
|
||||||
|
OnlyFirstPurchase bool `json:"only_first_purchase"`
|
||||||
GiftAmount int64 `json:"gift_amount"`
|
GiftAmount int64 `json:"gift_amount"`
|
||||||
Telegram int64 `json:"telegram"`
|
Telegram int64 `json:"telegram"`
|
||||||
ReferCode string `json:"refer_code"`
|
ReferCode string `json:"refer_code"`
|
||||||
|
|||||||
3
initialize/migrate/database/02108_user_referral.down.sql
Normal file
3
initialize/migrate/database/02108_user_referral.down.sql
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
ALTER TABLE `u`
|
||||||
|
DROP COLUMN `referral_percentage`,
|
||||||
|
DROP COLUMN `only_first_purchase`;
|
||||||
7
initialize/migrate/database/02108_user_referral.up.sql
Normal file
7
initialize/migrate/database/02108_user_referral.up.sql
Normal 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`;
|
||||||
@ -40,6 +40,8 @@ func (l *CreateUserLogic) CreateUser(req *types.CreateUserRequest) error {
|
|||||||
pwd := tool.EncodePassWord(req.Password)
|
pwd := tool.EncodePassWord(req.Password)
|
||||||
newUser := &user.User{
|
newUser := &user.User{
|
||||||
Password: pwd,
|
Password: pwd,
|
||||||
|
ReferralPercentage: req.ReferralPercentage,
|
||||||
|
OnlyFirstPurchase: &req.OnlyFirstPurchase,
|
||||||
ReferCode: req.ReferCode,
|
ReferCode: req.ReferCode,
|
||||||
Balance: req.Balance,
|
Balance: req.Balance,
|
||||||
IsAdmin: &req.IsAdmin,
|
IsAdmin: &req.IsAdmin,
|
||||||
|
|||||||
@ -41,20 +41,20 @@ func (l *GetUserListLogic) GetUserList(req *types.GetUserListRequest) (*types.Ge
|
|||||||
userRespList := make([]types.User, 0, len(list))
|
userRespList := make([]types.User, 0, len(list))
|
||||||
|
|
||||||
for _, item := range list {
|
for _, item := range list {
|
||||||
var user types.User
|
var u types.User
|
||||||
tool.DeepCopy(&user, item)
|
tool.DeepCopy(&u, item)
|
||||||
|
|
||||||
// 处理 AuthMethods
|
// 处理 AuthMethods
|
||||||
authMethods := make([]types.UserAuthMethod, len(user.AuthMethods)) // 直接创建目标 slice
|
authMethods := make([]types.UserAuthMethod, len(u.AuthMethods)) // 直接创建目标 slice
|
||||||
for i, method := range user.AuthMethods {
|
for i, method := range u.AuthMethods {
|
||||||
tool.DeepCopy(&authMethods[i], method)
|
tool.DeepCopy(&authMethods[i], method)
|
||||||
if method.AuthType == "mobile" {
|
if method.AuthType == "mobile" {
|
||||||
authMethods[i].AuthIdentifier = phone.FormatToInternational(method.AuthIdentifier)
|
authMethods[i].AuthIdentifier = phone.FormatToInternational(method.AuthIdentifier)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
user.AuthMethods = authMethods
|
u.AuthMethods = authMethods
|
||||||
|
|
||||||
userRespList = append(userRespList, user)
|
userRespList = append(userRespList, u)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &types.GetUserListResponse{
|
return &types.GetUserListResponse{
|
||||||
|
|||||||
@ -44,6 +44,8 @@ func (l *UpdateUserBasicInfoLogic) UpdateUserBasicInfo(req *types.UpdateUserBasi
|
|||||||
userInfo.Balance = req.Balance
|
userInfo.Balance = req.Balance
|
||||||
userInfo.GiftAmount = req.GiftAmount
|
userInfo.GiftAmount = req.GiftAmount
|
||||||
userInfo.Commission = req.Commission
|
userInfo.Commission = req.Commission
|
||||||
|
userInfo.OnlyFirstPurchase = &req.OnlyFirstPurchase
|
||||||
|
userInfo.ReferralPercentage = req.ReferralPercentage
|
||||||
|
|
||||||
if req.Password != "" {
|
if req.Password != "" {
|
||||||
if userInfo.Id == 2 && isDemo {
|
if userInfo.Id == 2 && isDemo {
|
||||||
|
|||||||
@ -12,6 +12,8 @@ type User struct {
|
|||||||
ReferCode string `gorm:"type:varchar(20);default:'';comment:Referral Code"`
|
ReferCode string `gorm:"type:varchar(20);default:'';comment:Referral Code"`
|
||||||
RefererId int64 `gorm:"index:idx_referer;comment:Referrer ID"`
|
RefererId int64 `gorm:"index:idx_referer;comment:Referrer ID"`
|
||||||
Commission int64 `gorm:"default:0;comment:Commission"` // Commission Amount
|
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"`
|
GiftAmount int64 `gorm:"default:0;comment:User Gift Amount"`
|
||||||
Enable *bool `gorm:"default:true;not null;comment:Is Account Enabled"`
|
Enable *bool `gorm:"default:true;not null;comment:Is Account Enabled"`
|
||||||
IsAdmin *bool `gorm:"default:false;not null;comment:Is Admin"`
|
IsAdmin *bool `gorm:"default:false;not null;comment:Is Admin"`
|
||||||
|
|||||||
@ -414,6 +414,8 @@ type CreateUserRequest struct {
|
|||||||
Password string `json:"password"`
|
Password string `json:"password"`
|
||||||
ProductId int64 `json:"product_id"`
|
ProductId int64 `json:"product_id"`
|
||||||
Duration int64 `json:"duration"`
|
Duration int64 `json:"duration"`
|
||||||
|
ReferralPercentage uint8 `json:"referral_percentage"`
|
||||||
|
OnlyFirstPurchase bool `json:"only_first_purchase"`
|
||||||
RefererUser string `json:"referer_user"`
|
RefererUser string `json:"referer_user"`
|
||||||
ReferCode string `json:"refer_code"`
|
ReferCode string `json:"refer_code"`
|
||||||
Balance int64 `json:"balance"`
|
Balance int64 `json:"balance"`
|
||||||
@ -2239,6 +2241,8 @@ type UpdateUserBasiceInfoRequest struct {
|
|||||||
Avatar string `json:"avatar"`
|
Avatar string `json:"avatar"`
|
||||||
Balance int64 `json:"balance"`
|
Balance int64 `json:"balance"`
|
||||||
Commission int64 `json:"commission"`
|
Commission int64 `json:"commission"`
|
||||||
|
ReferralPercentage uint8 `json:"referral_percentage"`
|
||||||
|
OnlyFirstPurchase bool `json:"only_first_purchase"`
|
||||||
GiftAmount int64 `json:"gift_amount"`
|
GiftAmount int64 `json:"gift_amount"`
|
||||||
Telegram int64 `json:"telegram"`
|
Telegram int64 `json:"telegram"`
|
||||||
ReferCode string `json:"refer_code"`
|
ReferCode string `json:"refer_code"`
|
||||||
@ -2285,6 +2289,8 @@ type User struct {
|
|||||||
Avatar string `json:"avatar"`
|
Avatar string `json:"avatar"`
|
||||||
Balance int64 `json:"balance"`
|
Balance int64 `json:"balance"`
|
||||||
Commission int64 `json:"commission"`
|
Commission int64 `json:"commission"`
|
||||||
|
ReferralPercentage uint8 `json:"referral_percentage"`
|
||||||
|
OnlyFirstPurchase bool `json:"only_first_purchase"`
|
||||||
GiftAmount int64 `json:"gift_amount"`
|
GiftAmount int64 `json:"gift_amount"`
|
||||||
Telegram int64 `json:"telegram"`
|
Telegram int64 `json:"telegram"`
|
||||||
ReferCode string `json:"refer_code"`
|
ReferCode string `json:"refer_code"`
|
||||||
|
|||||||
@ -368,7 +368,14 @@ func (l *ActivateOrderLogic) handleCommission(ctx context.Context, userInfo *use
|
|||||||
return
|
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
|
// Use transaction for commission updates
|
||||||
err = l.svc.DB.Transaction(func(tx *gorm.DB) error {
|
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 {
|
func (l *ActivateOrderLogic) shouldProcessCommission(userInfo *user.User, orderInfo *order.Order, isNewPurchase bool) bool {
|
||||||
return userInfo.RefererId != 0 &&
|
return userInfo.RefererId != 0 &&
|
||||||
l.svc.Config.Invite.ReferralPercentage != 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
|
// calculateCommission computes the commission amount based on order price and referral percentage
|
||||||
func (l *ActivateOrderLogic) calculateCommission(price int64) int64 {
|
func (l *ActivateOrderLogic) calculateCommission(price int64, percentage uint8) int64 {
|
||||||
return int64(float64(price) * (float64(l.svc.Config.Invite.ReferralPercentage) / 100))
|
return int64(float64(price) * (float64(percentage) / 100))
|
||||||
}
|
}
|
||||||
|
|
||||||
// clearServerCache clears user list cache for all servers associated with the subscription
|
// clearServerCache clears user list cache for all servers associated with the subscription
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user