feat(api): add referral percentage and only first purchase fields to user model and requests
This commit is contained in:
parent
4438b05272
commit
aea20ffd5e
@ -32,17 +32,19 @@ type (
|
|||||||
Id int64 `form:"id" validate:"required"`
|
Id int64 `form:"id" validate:"required"`
|
||||||
}
|
}
|
||||||
UpdateUserBasiceInfoRequest {
|
UpdateUserBasiceInfoRequest {
|
||||||
UserId int64 `json:"user_id" validate:"required"`
|
UserId int64 `json:"user_id" validate:"required"`
|
||||||
Password string `json:"password"`
|
Password string `json:"password"`
|
||||||
Avatar string `json:"avatar"`
|
Avatar string `json:"avatar"`
|
||||||
Balance int64 `json:"balance"`
|
Balance int64 `json:"balance"`
|
||||||
Commission int64 `json:"commission"`
|
Commission int64 `json:"commission"`
|
||||||
GiftAmount int64 `json:"gift_amount"`
|
ReferralPercentage uint8 `json:"referral_percentage"`
|
||||||
Telegram int64 `json:"telegram"`
|
OnlyFirstPurchase bool `json:"only_first_purchase"`
|
||||||
ReferCode string `json:"refer_code"`
|
GiftAmount int64 `json:"gift_amount"`
|
||||||
RefererId int64 `json:"referer_id"`
|
Telegram int64 `json:"telegram"`
|
||||||
Enable bool `json:"enable"`
|
ReferCode string `json:"refer_code"`
|
||||||
IsAdmin bool `json:"is_admin"`
|
RefererId int64 `json:"referer_id"`
|
||||||
|
Enable bool `json:"enable"`
|
||||||
|
IsAdmin bool `json:"is_admin"`
|
||||||
}
|
}
|
||||||
UpdateUserNotifySettingRequest {
|
UpdateUserNotifySettingRequest {
|
||||||
UserId int64 `json:"user_id" validate:"required"`
|
UserId int64 `json:"user_id" validate:"required"`
|
||||||
@ -52,18 +54,20 @@ type (
|
|||||||
EnableTradeNotify bool `json:"enable_trade_notify"`
|
EnableTradeNotify bool `json:"enable_trade_notify"`
|
||||||
}
|
}
|
||||||
CreateUserRequest {
|
CreateUserRequest {
|
||||||
Email string `json:"email"`
|
Email string `json:"email"`
|
||||||
Telephone string `json:"telephone"`
|
Telephone string `json:"telephone"`
|
||||||
TelephoneAreaCode string `json:"telephone_area_code"`
|
TelephoneAreaCode string `json:"telephone_area_code"`
|
||||||
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"`
|
||||||
RefererUser string `json:"referer_user"`
|
ReferralPercentage uint8 `json:"referral_percentage"`
|
||||||
ReferCode string `json:"refer_code"`
|
OnlyFirstPurchase bool `json:"only_first_purchase"`
|
||||||
Balance int64 `json:"balance"`
|
RefererUser string `json:"referer_user"`
|
||||||
Commission int64 `json:"commission"`
|
ReferCode string `json:"refer_code"`
|
||||||
GiftAmount int64 `json:"gift_amount"`
|
Balance int64 `json:"balance"`
|
||||||
IsAdmin bool `json:"is_admin"`
|
Commission int64 `json:"commission"`
|
||||||
|
GiftAmount int64 `json:"gift_amount"`
|
||||||
|
IsAdmin bool `json:"is_admin"`
|
||||||
}
|
}
|
||||||
UserSubscribeDetail {
|
UserSubscribeDetail {
|
||||||
Id int64 `json:"id"`
|
Id int64 `json:"id"`
|
||||||
|
|||||||
@ -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`;
|
||||||
@ -39,10 +39,12 @@ 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,
|
||||||
ReferCode: req.ReferCode,
|
ReferralPercentage: req.ReferralPercentage,
|
||||||
Balance: req.Balance,
|
OnlyFirstPurchase: &req.OnlyFirstPurchase,
|
||||||
IsAdmin: &req.IsAdmin,
|
ReferCode: req.ReferCode,
|
||||||
|
Balance: req.Balance,
|
||||||
|
IsAdmin: &req.IsAdmin,
|
||||||
}
|
}
|
||||||
var ams []user.AuthMethods
|
var ams []user.AuthMethods
|
||||||
|
|
||||||
|
|||||||
@ -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 {
|
||||||
|
|||||||
@ -11,7 +11,9 @@ type User struct {
|
|||||||
Balance int64 `gorm:"default:0;comment:User Balance"` // User Balance Amount
|
Balance int64 `gorm:"default:0;comment:User Balance"` // User Balance Amount
|
||||||
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"`
|
||||||
|
|||||||
@ -408,18 +408,20 @@ type CreateUserAuthMethodRequest struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type CreateUserRequest struct {
|
type CreateUserRequest struct {
|
||||||
Email string `json:"email"`
|
Email string `json:"email"`
|
||||||
Telephone string `json:"telephone"`
|
Telephone string `json:"telephone"`
|
||||||
TelephoneAreaCode string `json:"telephone_area_code"`
|
TelephoneAreaCode string `json:"telephone_area_code"`
|
||||||
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"`
|
||||||
RefererUser string `json:"referer_user"`
|
ReferralPercentage uint8 `json:"referral_percentage"`
|
||||||
ReferCode string `json:"refer_code"`
|
OnlyFirstPurchase bool `json:"only_first_purchase"`
|
||||||
Balance int64 `json:"balance"`
|
RefererUser string `json:"referer_user"`
|
||||||
Commission int64 `json:"commission"`
|
ReferCode string `json:"refer_code"`
|
||||||
GiftAmount int64 `json:"gift_amount"`
|
Balance int64 `json:"balance"`
|
||||||
IsAdmin bool `json:"is_admin"`
|
Commission int64 `json:"commission"`
|
||||||
|
GiftAmount int64 `json:"gift_amount"`
|
||||||
|
IsAdmin bool `json:"is_admin"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type CreateUserSubscribeRequest struct {
|
type CreateUserSubscribeRequest struct {
|
||||||
@ -2234,17 +2236,19 @@ type UpdateUserAuthMethodRequest struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type UpdateUserBasiceInfoRequest struct {
|
type UpdateUserBasiceInfoRequest struct {
|
||||||
UserId int64 `json:"user_id" validate:"required"`
|
UserId int64 `json:"user_id" validate:"required"`
|
||||||
Password string `json:"password"`
|
Password string `json:"password"`
|
||||||
Avatar string `json:"avatar"`
|
Avatar string `json:"avatar"`
|
||||||
Balance int64 `json:"balance"`
|
Balance int64 `json:"balance"`
|
||||||
Commission int64 `json:"commission"`
|
Commission int64 `json:"commission"`
|
||||||
GiftAmount int64 `json:"gift_amount"`
|
ReferralPercentage uint8 `json:"referral_percentage"`
|
||||||
Telegram int64 `json:"telegram"`
|
OnlyFirstPurchase bool `json:"only_first_purchase"`
|
||||||
ReferCode string `json:"refer_code"`
|
GiftAmount int64 `json:"gift_amount"`
|
||||||
RefererId int64 `json:"referer_id"`
|
Telegram int64 `json:"telegram"`
|
||||||
Enable bool `json:"enable"`
|
ReferCode string `json:"refer_code"`
|
||||||
IsAdmin bool `json:"is_admin"`
|
RefererId int64 `json:"referer_id"`
|
||||||
|
Enable bool `json:"enable"`
|
||||||
|
IsAdmin bool `json:"is_admin"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type UpdateUserNotifyRequest struct {
|
type UpdateUserNotifyRequest struct {
|
||||||
@ -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