149 lines
4.3 KiB
Go
149 lines
4.3 KiB
Go
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
|
||
}
|