Files
kami_gateway/internal/models/order/order_profit_info.go
danial e88ff05a14 refactor(trace): 重命名 otel 包为 otelTrace并更新相关引用
- 将内部使用的 otel 包重命名为 otelTrace
- 更新了所有引用该包的文件中的导入路径
- 修改了部分函数和变量名称以适应新的包名
2025-02-23 21:56:29 +08:00

149 lines
4.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package order
import (
"context"
"gateway/internal/otelTrace"
"time"
"github.com/beego/beego/v2/client/orm"
"go.uber.org/zap"
)
type OrderProfitInfo struct {
Id int
MerchantName string
MerchantUid string
AgentName string
AgentUid string
PayProductCode string
PayProductName string
PayTypeCode string
PayTypeName string
Status string
MerchantOrderId string
BankOrderId string
BankTransId string
OrderAmount float64
ShowAmount float64
FactAmount float64
UserInAmount float64
SupplierRate float64
PlatformRate float64
AgentRate float64
AllProfit float64
SupplierProfit float64
PlatformProfit float64
AgentProfit float64
CreateTime time.Time
UpdateTime time.Time
}
const ORDER_PROFIT_INFO = "order_profit_info"
func GetOrderProfitByBankOrderId(ctx context.Context, bankOrderId string) OrderProfitInfo {
o := orm.NewOrm()
var orderProfit OrderProfitInfo
_, err := o.QueryTable(ORDER_PROFIT_INFO).Filter("bank_order_id", bankOrderId).Limit(1).AllWithCtx(ctx, &orderProfit)
if err != nil {
otelTrace.Logger.WithContext(ctx).Error("GetOrderProfitByBankOrderId fail", zap.Error(err))
}
return orderProfit
}
func GetOrderProfitByMerchantOrderId(ctx context.Context, merchantOrderId string) OrderProfitInfo {
o := orm.NewOrm()
var orderProfit OrderProfitInfo
_, err := o.QueryTable(ORDER_PROFIT_INFO).Filter("merchant_order_id", merchantOrderId).Limit(1).AllWithCtx(ctx, &orderProfit)
if err != nil {
otelTrace.Logger.WithContext(ctx).Error("GetOrderProfitByBankOrderId fail", zap.Error(err))
}
return orderProfit
}
func GetOrderProfitLenByMap(params map[string]string) int {
o := orm.NewOrm()
qs := o.QueryTable(ORDER_PROFIT_INFO)
for k, v := range params {
if len(v) > 0 {
qs = qs.Filter(k, v)
}
}
cnt, _ := qs.Limit(-1).Count()
return int(cnt)
}
func GetOrderProfitByMap(ctx context.Context, params map[string]string, display, offset int) []OrderProfitInfo {
o := orm.NewOrm()
var orderProfitInfoList []OrderProfitInfo
qs := o.QueryTable(ORDER_PROFIT_INFO)
for k, v := range params {
if len(v) > 0 {
qs = qs.Filter(k, v)
}
}
_, err := qs.Limit(display, offset).OrderBy("-update_time").AllWithCtx(ctx, &orderProfitInfoList)
if err != nil {
otelTrace.Logger.WithContext(ctx).Error("get order by map fail: ", zap.Error(err))
}
return orderProfitInfoList
}
// InsertOrderAndOrderProfit 插入支付订单记录和订单利润记录,保证一致性
func InsertOrderAndOrderProfit(ctx context.Context, orderInfo OrderInfo, orderProfitInfo OrderProfitInfo) bool {
if orderInfo.ExValue == "" {
orderInfo.ExValue = "{}"
}
err := orm.NewOrm().DoTx(func(ctx context.Context, txOrm orm.TxOrmer) error {
if _, err := txOrm.Insert(&orderInfo); err != nil {
otelTrace.Logger.WithContext(ctx).Error("insert orderInfo fail: ", zap.Error(err))
return err
}
if _, err := txOrm.Insert(&orderProfitInfo); err != nil {
otelTrace.Logger.WithContext(ctx).Error("insert orderProfit fail: ", zap.Error(err))
return err
}
return nil
})
if err != nil {
return false
}
return true
}
// InsertOrderProfit 创建订单利润表
func InsertOrderProfit(ctx context.Context, orderProfitInfo *OrderProfitInfo) (err error) {
err = orm.NewOrm().DoTx(func(ctx context.Context, txOrm orm.TxOrmer) error {
if _, err := txOrm.Insert(orderProfitInfo); err != nil {
otelTrace.Logger.WithContext(ctx).Error("insert orderProfit fail: ", zap.Error(err))
return err
}
return nil
})
return
}
func SwitchOrderAndOrderProfitStatus(ctx context.Context, merchantOrderId, status string) bool {
o := orm.NewOrm()
err := o.DoTx(func(ctx context.Context, txOrm orm.TxOrmer) error {
if _, err := txOrm.QueryTable(ORDER_INFO).Filter("merchant_order_id", merchantOrderId).Update(orm.Params{
"status": status,
}); err != nil {
otelTrace.Logger.WithContext(ctx).Error("insert orderInfo fail: ", zap.Error(err))
return err
}
if _, err := txOrm.QueryTable(ORDER_PROFIT_INFO).Filter("merchant_order_id", merchantOrderId).Update(orm.Params{
"status": status,
}); err != nil {
otelTrace.Logger.WithContext(ctx).Error("insert orderInfo fail: ", zap.Error(err))
return err
}
return nil
})
if err != nil {
return false
}
return true
}