refactor(log): consolidate logging models and update related logic for improved clarity and functionality

This commit is contained in:
Chang lue Tsen
2025-08-20 13:36:06 -04:00
parent 4f32d67113
commit 87c771bbd4
41 changed files with 1082 additions and 707 deletions
+20 -8
View File
@@ -3,7 +3,9 @@ package order
import (
"context"
"encoding/json"
"time"
"github.com/perfect-panel/server/internal/model/log"
"github.com/perfect-panel/server/internal/model/user"
"github.com/perfect-panel/server/pkg/payment/stripe"
"gorm.io/gorm"
@@ -92,15 +94,25 @@ func (l *CloseOrderLogic) CloseOrder(req *types.CloseOrderRequest) error {
return err
}
// Record the deduction refund log
giftAmountLog := &user.GiftAmountLog{
UserId: orderInfo.UserId,
OrderNo: orderInfo.OrderNo,
Amount: orderInfo.GiftAmount,
Type: 1,
Balance: deduction,
Remark: "Order cancellation refund",
giftLog := log.Gift{
Type: log.GiftTypeIncrease,
OrderNo: orderInfo.OrderNo,
SubscribeId: 0,
Amount: orderInfo.GiftAmount,
Balance: deduction,
Remark: "Order cancellation refund",
CreatedAt: time.Now().UnixMilli(),
}
err = tx.Model(&user.GiftAmountLog{}).Create(giftAmountLog).Error
content, _ := giftLog.Marshal()
err = tx.Model(&log.SystemLog{}).Create(&log.SystemLog{
Id: 0,
Type: log.TypeGift.Uint8(),
Date: time.Now().Format(time.DateOnly),
ObjectID: userInfo.Id,
Content: string(content),
}).Error
if err != nil {
l.Errorw("[CloseOrder] Record cancellation refund log failed",
logger.Field("error", err.Error()),
+18 -9
View File
@@ -5,6 +5,7 @@ import (
"encoding/json"
"time"
"github.com/perfect-panel/server/internal/model/log"
"github.com/perfect-panel/server/pkg/constant"
"github.com/hibiken/asynq"
@@ -196,18 +197,26 @@ func (l *PurchaseLogic) Purchase(req *types.PurchaseOrderRequest) (resp *types.P
return e
}
// create deduction record
giftAmountLog := user.GiftAmountLog{
UserId: orderInfo.UserId,
OrderNo: orderInfo.OrderNo,
Amount: orderInfo.GiftAmount,
Type: 2,
Balance: u.GiftAmount,
Remark: "Purchase order deduction",
giftLog := log.Gift{
Type: log.GiftTypeReduce,
OrderNo: orderInfo.OrderNo,
SubscribeId: 0,
Amount: orderInfo.GiftAmount,
Balance: u.GiftAmount,
Remark: "Purchase order deduction",
CreatedAt: time.Now().UnixMilli(),
}
if e := db.Model(&user.GiftAmountLog{}).Create(&giftAmountLog).Error; e != nil {
content, _ := giftLog.Marshal()
if e := db.Model(&log.SystemLog{}).Create(&log.SystemLog{
Type: log.TypeGift.Uint8(),
Date: time.Now().Format(time.DateOnly),
ObjectID: u.Id,
Content: string(content),
}).Error; e != nil {
l.Errorw("[Purchase] Database insert error",
logger.Field("error", e.Error()),
logger.Field("deductionLog", giftAmountLog),
logger.Field("deductionLog", giftLog),
)
return e
}
+18 -9
View File
@@ -5,6 +5,7 @@ import (
"encoding/json"
"time"
"github.com/perfect-panel/server/internal/model/log"
"github.com/perfect-panel/server/pkg/constant"
"gorm.io/gorm"
@@ -163,16 +164,24 @@ func (l *RenewalLogic) Renewal(req *types.RenewalOrderRequest) (resp *types.Rene
return err
}
// create deduction record
deductionLog := user.GiftAmountLog{
UserId: orderInfo.UserId,
OrderNo: orderInfo.OrderNo,
Amount: orderInfo.GiftAmount,
Type: 2,
Balance: u.GiftAmount,
Remark: "Renewal order deduction",
giftLog := log.Gift{
Type: log.GiftTypeReduce,
OrderNo: orderInfo.OrderNo,
SubscribeId: 0,
Amount: orderInfo.GiftAmount,
Balance: u.GiftAmount,
Remark: "Renewal order deduction",
CreatedAt: time.Now().UnixMilli(),
}
if err := db.Model(&user.GiftAmountLog{}).Create(&deductionLog).Error; err != nil {
l.Errorw("[Renewal] Database insert error", logger.Field("error", err.Error()), logger.Field("deductionLog", deductionLog))
content, _ := giftLog.Marshal()
if err := db.Model(&log.SystemLog{}).Create(&log.SystemLog{
Type: log.TypeGift.Uint8(),
Date: time.Now().Format(time.DateOnly),
ObjectID: u.Id,
Content: string(content),
}).Error; err != nil {
l.Errorw("[Renewal] Database insert error", logger.Field("error", err.Error()), logger.Field("deductionLog", giftLog))
return err
}
}
@@ -5,6 +5,7 @@ import (
"encoding/json"
"time"
"github.com/perfect-panel/server/internal/model/log"
"github.com/perfect-panel/server/pkg/constant"
"github.com/perfect-panel/server/pkg/xerr"
@@ -104,16 +105,24 @@ func (l *ResetTrafficLogic) ResetTraffic(req *types.ResetTrafficOrderRequest) (r
return err
}
// create deduction record
deductionLog := user.GiftAmountLog{
UserId: orderInfo.UserId,
OrderNo: orderInfo.OrderNo,
Amount: orderInfo.GiftAmount,
Type: 2,
Balance: u.GiftAmount,
Remark: "ResetTraffic order deduction",
giftLog := log.Gift{
Type: log.GiftTypeReduce,
OrderNo: orderInfo.OrderNo,
SubscribeId: 0,
Amount: orderInfo.GiftAmount,
Balance: u.GiftAmount,
Remark: "Renewal order deduction",
CreatedAt: time.Now().UnixMilli(),
}
if err := db.Model(&user.GiftAmountLog{}).Create(&deductionLog).Error; err != nil {
l.Errorw("[ResetTraffic] Database insert error", logger.Field("error", err.Error()), logger.Field("deductionLog", deductionLog))
content, _ := giftLog.Marshal()
if err = db.Model(&log.SystemLog{}).Create(&log.SystemLog{
Type: log.TypeGift.Uint8(),
Date: time.Now().Format(time.DateOnly),
ObjectID: u.Id,
Content: string(content),
}).Error; err != nil {
l.Errorw("[ResetTraffic] Database insert error", logger.Field("error", err.Error()), logger.Field("deductionLog", content))
return err
}
}