From 0199dc7fa259de48ad1b9b4eb92f9c0375827cb8 Mon Sep 17 00:00:00 2001 From: Chang lue Tsen Date: Fri, 5 Sep 2025 11:00:19 -0400 Subject: [PATCH] feat(log): add logging for balance, gift amount, and commission adjustments --- .../admin/user/updateUserBasicInfoLogic.go | 84 ++++++++++++++++++- internal/model/log/log.go | 3 + 2 files changed, 84 insertions(+), 3 deletions(-) diff --git a/internal/logic/admin/user/updateUserBasicInfoLogic.go b/internal/logic/admin/user/updateUserBasicInfoLogic.go index 3b451c3..d370a14 100644 --- a/internal/logic/admin/user/updateUserBasicInfoLogic.go +++ b/internal/logic/admin/user/updateUserBasicInfoLogic.go @@ -4,7 +4,9 @@ import ( "context" "os" "strings" + "time" + "github.com/perfect-panel/server/internal/model/log" "github.com/perfect-panel/server/internal/svc" "github.com/perfect-panel/server/internal/types" "github.com/perfect-panel/server/pkg/logger" @@ -41,9 +43,85 @@ func (l *UpdateUserBasicInfoLogic) UpdateUserBasicInfo(req *types.UpdateUserBasi if req.Avatar != "" && !tool.IsValidImageSize(req.Avatar, 1024) { return errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "Invalid Image Size") } - userInfo.Balance = req.Balance - userInfo.GiftAmount = req.GiftAmount - userInfo.Commission = req.Commission + + if userInfo.Balance != req.Balance { + change := req.Balance - userInfo.Balance + balanceLog := log.Balance{ + Type: log.BalanceTypeAdjust, + Amount: change, + 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, + Balance: req.GiftAmount, + Remark: "Admin adjustment", + Timestamp: time.Now().UnixMilli(), + } + content, _ := giftLog.Marshal() + // Add gift amount change log + err = l.svcCtx.LogModel.Insert(l.ctx, &log.SystemLog{ + Type: log.TypeGift.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.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 = l.svcCtx.LogModel.Insert(l.ctx, &log.SystemLog{ + Type: log.TypeCommission.Uint8(), + Date: time.Now().Format(time.DateOnly), + ObjectID: userInfo.Id, + Content: string(content), + }) + if err != nil { + l.Errorw("[UpdateUserBasicInfoLogic] Insert Commission Log Error:", logger.Field("err", err.Error()), logger.Field("userId", req.UserId)) + return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseInsertError), "Insert Commission Log Error") + } + userInfo.Commission = req.Commission + } + userInfo.OnlyFirstPurchase = &req.OnlyFirstPurchase userInfo.ReferralPercentage = req.ReferralPercentage diff --git a/internal/model/log/log.go b/internal/model/log/log.go index f53d5d1..810602b 100644 --- a/internal/model/log/log.go +++ b/internal/model/log/log.go @@ -40,10 +40,13 @@ const ( BalanceTypeWithdraw uint16 = 322 // Withdraw BalanceTypePayment uint16 = 323 // Payment BalanceTypeRefund uint16 = 324 // Refund + BalanceTypeAdjust uint16 = 326 // Admin Adjust BalanceTypeReward uint16 = 325 // Reward CommissionTypePurchase uint16 = 331 // Purchase CommissionTypeRenewal uint16 = 332 // Renewal CommissionTypeRefund uint16 = 333 // Refund + commissionTypeWithdraw uint16 = 334 // withdraw + CommissionTypeAdjust uint16 = 335 // Admin Adjust GiftTypeIncrease uint16 = 341 // Increase GiftTypeReduce uint16 = 342 // Reduce )