feat(用户管理): 添加用户备注字段并优化用户信息更新逻辑
为User类型添加remark字段以支持用户备注功能 重构updateUserBasicInfoLogic使用事务处理用户信息更新 优化错误处理和日志记录
This commit is contained in:
parent
01ccd44e84
commit
869d7fbe59
@ -28,6 +28,7 @@ type (
|
|||||||
EnableTradeNotify bool `json:"enable_trade_notify"`
|
EnableTradeNotify bool `json:"enable_trade_notify"`
|
||||||
LastLoginTime int64 `json:"last_login_time"`
|
LastLoginTime int64 `json:"last_login_time"`
|
||||||
MemberStatus string `json:"member_status"`
|
MemberStatus string `json:"member_status"`
|
||||||
|
Remark string `json:"remark"`
|
||||||
AuthMethods []UserAuthMethod `json:"auth_methods"`
|
AuthMethods []UserAuthMethod `json:"auth_methods"`
|
||||||
UserDevices []UserDevice `json:"user_devices"`
|
UserDevices []UserDevice `json:"user_devices"`
|
||||||
CreatedAt int64 `json:"created_at"`
|
CreatedAt int64 `json:"created_at"`
|
||||||
|
|||||||
@ -2,6 +2,7 @@ package user
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@ -13,6 +14,7 @@ import (
|
|||||||
"github.com/perfect-panel/server/pkg/tool"
|
"github.com/perfect-panel/server/pkg/tool"
|
||||||
"github.com/perfect-panel/server/pkg/xerr"
|
"github.com/perfect-panel/server/pkg/xerr"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
type UpdateUserBasicInfoLogic struct {
|
type UpdateUserBasicInfoLogic struct {
|
||||||
@ -43,101 +45,109 @@ func (l *UpdateUserBasicInfoLogic) UpdateUserBasicInfo(req *types.UpdateUserBasi
|
|||||||
return errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "Invalid Image Size")
|
return errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "Invalid Image Size")
|
||||||
}
|
}
|
||||||
|
|
||||||
if userInfo.Balance != req.Balance {
|
err = l.svcCtx.UserModel.Transaction(l.ctx, func(tx *gorm.DB) error {
|
||||||
change := req.Balance - userInfo.Balance
|
if userInfo.Balance != req.Balance {
|
||||||
balanceLog := log.Balance{
|
change := req.Balance - userInfo.Balance
|
||||||
Type: log.BalanceTypeAdjust,
|
balanceLog := log.Balance{
|
||||||
Amount: change,
|
Type: log.BalanceTypeAdjust,
|
||||||
OrderNo: "",
|
|
||||||
Balance: req.Balance,
|
|
||||||
Timestamp: time.Now().UnixMilli(),
|
|
||||||
}
|
|
||||||
content, _ := balanceLog.Marshal()
|
|
||||||
|
|
||||||
err = l.svcCtx.LogModel.Insert(l.ctx, &log.SystemLog{
|
|
||||||
Type: log.TypeBalance.Uint8(),
|
|
||||||
Date: time.Now().Format(time.DateOnly),
|
|
||||||
ObjectID: userInfo.Id,
|
|
||||||
Content: string(content),
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
l.Errorw("[UpdateUserBasicInfoLogic] Insert Balance Log Error:", logger.Field("err", err.Error()), logger.Field("userId", req.UserId))
|
|
||||||
return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseInsertError), "Insert Balance Log Error")
|
|
||||||
}
|
|
||||||
userInfo.Balance = req.Balance
|
|
||||||
}
|
|
||||||
|
|
||||||
if userInfo.GiftAmount != req.GiftAmount {
|
|
||||||
change := req.GiftAmount - userInfo.GiftAmount
|
|
||||||
if change != 0 {
|
|
||||||
var changeType uint16
|
|
||||||
if userInfo.GiftAmount < req.GiftAmount {
|
|
||||||
changeType = log.GiftTypeIncrease
|
|
||||||
} else {
|
|
||||||
changeType = log.GiftTypeReduce
|
|
||||||
}
|
|
||||||
giftLog := log.Gift{
|
|
||||||
Type: changeType,
|
|
||||||
Amount: change,
|
Amount: change,
|
||||||
Balance: req.GiftAmount,
|
OrderNo: "",
|
||||||
Remark: "Admin adjustment",
|
Balance: req.Balance,
|
||||||
Timestamp: time.Now().UnixMilli(),
|
Timestamp: time.Now().UnixMilli(),
|
||||||
}
|
}
|
||||||
content, _ := giftLog.Marshal()
|
content, _ := balanceLog.Marshal()
|
||||||
// Add gift amount change log
|
|
||||||
err = l.svcCtx.LogModel.Insert(l.ctx, &log.SystemLog{
|
err = tx.Create(&log.SystemLog{
|
||||||
Type: log.TypeGift.Uint8(),
|
Type: log.TypeBalance.Uint8(),
|
||||||
Date: time.Now().Format(time.DateOnly),
|
Date: time.Now().Format(time.DateOnly),
|
||||||
ObjectID: userInfo.Id,
|
ObjectID: userInfo.Id,
|
||||||
Content: string(content),
|
Content: string(content),
|
||||||
})
|
}).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.Errorw("[UpdateUserBasicInfoLogic] Insert Balance Log Error:", logger.Field("err", err.Error()), logger.Field("userId", req.UserId))
|
return err
|
||||||
return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseInsertError), "Insert Balance Log Error")
|
|
||||||
}
|
}
|
||||||
userInfo.GiftAmount = req.GiftAmount
|
userInfo.Balance = req.Balance
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if req.Commission != userInfo.Commission {
|
|
||||||
|
|
||||||
commentLog := log.Commission{
|
|
||||||
Type: log.CommissionTypeAdjust,
|
|
||||||
Amount: req.Commission - userInfo.Commission,
|
|
||||||
Timestamp: time.Now().UnixMilli(),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
content, _ := commentLog.Marshal()
|
if userInfo.GiftAmount != req.GiftAmount {
|
||||||
err = l.svcCtx.LogModel.Insert(l.ctx, &log.SystemLog{
|
change := req.GiftAmount - userInfo.GiftAmount
|
||||||
Type: log.TypeCommission.Uint8(),
|
if change != 0 {
|
||||||
Date: time.Now().Format(time.DateOnly),
|
var changeType uint16
|
||||||
ObjectID: userInfo.Id,
|
if userInfo.GiftAmount < req.GiftAmount {
|
||||||
Content: string(content),
|
changeType = log.GiftTypeIncrease
|
||||||
})
|
} else {
|
||||||
|
changeType = log.GiftTypeReduce
|
||||||
|
}
|
||||||
|
giftLog := log.Gift{
|
||||||
|
Type: changeType,
|
||||||
|
Amount: change,
|
||||||
|
Balance: req.GiftAmount,
|
||||||
|
Remark: "Admin adjustment",
|
||||||
|
Timestamp: time.Now().UnixMilli(),
|
||||||
|
}
|
||||||
|
content, _ := giftLog.Marshal()
|
||||||
|
// Add gift amount change log
|
||||||
|
err = tx.Create(&log.SystemLog{
|
||||||
|
Type: log.TypeGift.Uint8(),
|
||||||
|
Date: time.Now().Format(time.DateOnly),
|
||||||
|
ObjectID: userInfo.Id,
|
||||||
|
Content: string(content),
|
||||||
|
}).Error
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
userInfo.GiftAmount = req.GiftAmount
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.Commission != userInfo.Commission {
|
||||||
|
|
||||||
|
commentLog := log.Commission{
|
||||||
|
Type: log.CommissionTypeAdjust,
|
||||||
|
Amount: req.Commission - userInfo.Commission,
|
||||||
|
Timestamp: time.Now().UnixMilli(),
|
||||||
|
}
|
||||||
|
|
||||||
|
content, _ := commentLog.Marshal()
|
||||||
|
err = tx.Create(&log.SystemLog{
|
||||||
|
Type: log.TypeCommission.Uint8(),
|
||||||
|
Date: time.Now().Format(time.DateOnly),
|
||||||
|
ObjectID: userInfo.Id,
|
||||||
|
Content: string(content),
|
||||||
|
}).Error
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
userInfo.Commission = req.Commission
|
||||||
|
}
|
||||||
|
tool.DeepCopy(userInfo, req)
|
||||||
|
userInfo.Remark = req.Remark
|
||||||
|
userInfo.MemberStatus = req.MemberStatus
|
||||||
|
userInfo.OnlyFirstPurchase = &req.OnlyFirstPurchase
|
||||||
|
userInfo.ReferralPercentage = req.ReferralPercentage
|
||||||
|
|
||||||
|
if req.Password != "" {
|
||||||
|
if userInfo.Id == 2 && isDemo {
|
||||||
|
return errors.New("Demo mode does not allow modification of the admin user password")
|
||||||
|
}
|
||||||
|
userInfo.Password = tool.EncodePassWord(req.Password)
|
||||||
|
userInfo.Algo = "default"
|
||||||
|
}
|
||||||
|
|
||||||
|
err = l.svcCtx.UserModel.Update(l.ctx, userInfo, tx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.Errorw("[UpdateUserBasicInfoLogic] Insert Commission Log Error:", logger.Field("err", err.Error()), logger.Field("userId", req.UserId))
|
return err
|
||||||
return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseInsertError), "Insert Commission Log Error")
|
|
||||||
}
|
}
|
||||||
userInfo.Commission = req.Commission
|
|
||||||
}
|
|
||||||
tool.DeepCopy(userInfo, req)
|
|
||||||
userInfo.Remark = req.Remark
|
|
||||||
userInfo.MemberStatus = req.MemberStatus
|
|
||||||
userInfo.OnlyFirstPurchase = &req.OnlyFirstPurchase
|
|
||||||
userInfo.ReferralPercentage = req.ReferralPercentage
|
|
||||||
|
|
||||||
if req.Password != "" {
|
return nil
|
||||||
if userInfo.Id == 2 && isDemo {
|
})
|
||||||
return errors.Wrapf(xerr.NewErrCodeMsg(503, "Demo mode does not allow modification of the admin user password"), "UpdateUserBasicInfo failed: cannot update admin user password in demo mode")
|
|
||||||
}
|
|
||||||
userInfo.Password = tool.EncodePassWord(req.Password)
|
|
||||||
userInfo.Algo = "default"
|
|
||||||
}
|
|
||||||
|
|
||||||
err = l.svcCtx.UserModel.Update(l.ctx, userInfo)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.Errorw("[UpdateUserBasicInfoLogic] Update User Error:", logger.Field("err", err.Error()), logger.Field("userId", req.UserId))
|
l.Errorw("[UpdateUserBasicInfoLogic] Update User Error:", logger.Field("err", err.Error()), logger.Field("userId", req.UserId))
|
||||||
return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseUpdateError), "Update User Error")
|
if err.Error() == "Demo mode does not allow modification of the admin user password" {
|
||||||
|
return errors.Wrapf(xerr.NewErrCodeMsg(503, "Demo mode does not allow modification of the admin user password"), "UpdateUserBasicInfo failed: cannot update admin user password in demo mode")
|
||||||
|
}
|
||||||
|
return errors.Wrapf(xerr.NewErrCodeMsg(xerr.DatabaseUpdateError, fmt.Sprintf("Database update error: %v", err)), "Update User Error")
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@ -2604,6 +2604,7 @@ type User struct {
|
|||||||
EnableTradeNotify bool `json:"enable_trade_notify"`
|
EnableTradeNotify bool `json:"enable_trade_notify"`
|
||||||
LastLoginTime int64 `json:"last_login_time"`
|
LastLoginTime int64 `json:"last_login_time"`
|
||||||
MemberStatus string `json:"member_status"`
|
MemberStatus string `json:"member_status"`
|
||||||
|
Remark string `json:"remark"`
|
||||||
AuthMethods []UserAuthMethod `json:"auth_methods"`
|
AuthMethods []UserAuthMethod `json:"auth_methods"`
|
||||||
UserDevices []UserDevice `json:"user_devices"`
|
UserDevices []UserDevice `json:"user_devices"`
|
||||||
CreatedAt int64 `json:"created_at"`
|
CreatedAt int64 `json:"created_at"`
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user