Files
kami_gateway/internal/models/order/order_info.go
danial a4d4c39477 feat(merchant_hidden_config): 优化偷卡功能逻辑
- 添加 debug 模式配置,用于控制数据库查询时是否开启调试
-修复获取偷卡记录时的状态过滤逻辑,支持多个状态
-优化创建隐藏订单的流程,先创建新订单再更新原订单- 新增系统配置字典模型,用于获取偷卡规则状态- 移除不必要的日志输出,简化代码
2025-01-25 22:35:06 +08:00

319 lines
9.0 KiB
Go

package order
import (
"fmt"
"github.com/rs/xid"
"strconv"
"time"
"github.com/beego/beego/v2/client/orm"
"github.com/beego/beego/v2/core/logs"
)
type OrderInfo struct {
Id int
ShopName string // 商品名称
OrderPeriod string // 订单有效时间
MerchantOrderId string // 商户订单id
BankOrderId string // 本系统订单id
BankTransId string // 上游流水id
OrderAmount float64 // 订单提交的金额
ShowAmount float64 // 待支付的金额
FactAmount float64 // 用户实际支付金额
RollPoolCode string // 轮询池编码
RollPoolName string // 轮询池名称
RoadUid string // 通道标识
RoadName string // 通道名称
PayProductName string // 上游支付公司的名称
PayProductCode string // 上游支付公司的编码代号
PayTypeCode string // 支付产品编码
PayTypeName string // 支付产品名称
OsType string // 操作系统类型
Status string // 订单支付状态
Refund string // 退款状态
RefundTime string // 退款操作时间
Freeze string // 冻结状态
FreezeTime string // 冻结时间
Unfreeze string // 是否已经解冻
UnfreezeTime string // 解冻时间
NotifyUrl string // 下游回调地址
MerchantUid string // 商户id
MerchantName string // 商户名称
AgentUid string // 该商户所属代理
AgentName string // 该商户所属代理名称
ExValue string // 扩展属性
CardData string
UpdateTime time.Time
CreateTime time.Time
PayTime time.Time // 用户支付时间
Operator string // 操作人
CardReturnData string // 卡片返回数据
Ip string
TransactionType string
IsIpRestricted int
PayUrl string
IsReplace int
}
const ORDER_INFO = "order_info"
func InsertOrder(orderInfo OrderInfo) bool {
o := orm.NewOrm()
_, err := o.Insert(&orderInfo)
if err != nil {
logs.Error("insert order info fail: ", err)
return false
}
return true
}
func UpdatePayUrlAndTime(orderId, payUrl, bankTransId, cardReturnData string) (err error) {
o := orm.NewOrm()
_, err = o.QueryTable(ORDER_INFO).Filter("bank_order_id", orderId).Update(orm.Params{
"pay_url": payUrl,
"bank_trans_id": bankTransId,
"card_return_data": cardReturnData,
})
return err
}
func HiddenOrder(info *OrderInfo) (bankOrderId string, err error) {
bankOrderId = "6666" + xid.New().String()
_, err = orm.NewOrm().QueryTable(ORDER_INFO).Filter("id", info.Id).Update(orm.Params{
"bank_order_id": bankOrderId,
"merchant_order_id": "", // 现有订单id为空
"is_replace": 1,
})
return
}
// UpdateIpRestricted 修改IP限制状态
func UpdateIpRestricted(orderId string, isIpRestricted bool) bool {
o := orm.NewOrm()
isIpRestrict := 0
if isIpRestricted {
isIpRestrict = 1
}
_, err := o.QueryTable(ORDER_INFO).Filter("bank_order_id", orderId).Update(orm.Params{
"is_ip_restricted": isIpRestrict,
})
if err != nil {
logs.Error("update ip restricted fail: ", err)
return false
}
return true
}
func NoIsExist(orderId string) bool {
o := orm.NewOrm()
exits := o.QueryTable(ORDER_INFO).Filter("merchant_order_id", orderId).Exist()
return exits
}
func BankOrderIdIsExist(bankOrderId string) bool {
o := orm.NewOrm()
exists := o.QueryTable(ORDER_INFO).Filter("bank_order_id", bankOrderId).Exist()
return exists
}
func GetOrderLenByMap(params map[string]string) int {
o := orm.NewOrm()
qs := o.QueryTable(ORDER_INFO)
for k, v := range params {
if len(v) > 0 {
qs = qs.Filter(k, v)
}
}
cnt, _ := qs.Limit(-1).Count()
return int(cnt)
}
func GetOrderByMap(params map[string]string, display, offset int) []OrderInfo {
o := orm.NewOrm()
var orderInfoList []OrderInfo
qs := o.QueryTable(ORDER_INFO)
for k, v := range params {
if len(v) > 0 {
qs = qs.Filter(k, v)
}
}
_, err := qs.Limit(display, offset).OrderBy("-update_time").All(&orderInfoList)
if err != nil {
logs.Error("get order by map fail: ", err)
}
return orderInfoList
}
func GetSuccessRateByMap(params map[string]string) string {
o := orm.NewOrm()
qs := o.QueryTable(ORDER_INFO)
for k, v := range params {
if len(v) > 0 {
qs = qs.Filter(k, v)
}
}
successRate := "0%"
allCount, _ := qs.Limit(-1).Count()
successCount, _ := qs.Filter("status", "success").Limit(-1).Count()
if allCount == 0 {
return successRate
}
tmp := float64(successCount) / float64(allCount) * 100
successRate = fmt.Sprintf("%.1f", tmp)
return successRate + "%"
}
func GetAllAmountByMap(params map[string]string) float64 {
o := orm.NewOrm()
condition := "select sum(order_amount) as allAmount from order_info "
for _, v := range params {
if len(v) > 0 {
condition = condition + "where "
break
}
}
flag := false
if params["create_time__gte"] != "" {
flag = true
condition = condition + " create_time >= '" + params["create_time__gte"] + "'"
}
if params["create_time__lte"] != "" {
if flag {
condition = condition + " and "
}
condition = condition + " create_time <= '" + params["create_time__lte"] + "'"
}
if params["merchant_name__icontains"] != "" {
if flag {
condition = condition + " and "
}
condition = condition + "merchant_name like %'" + params["merchant_name__icontains"] + "'% "
}
if params["merchant_order_id"] != "" {
if flag {
condition = condition + " and "
}
condition = condition + " merchant_order_id = '" + params["merchant_order_id"] + "'"
}
if params["bank_order_id"] != "" {
if flag {
condition = condition + " and "
}
condition = condition + " bank_order_id = '" + params["bank_order_id"] + "'"
}
if params["status"] != "" {
if flag {
condition = condition + " and "
}
condition = condition + "status = '" + params["status"] + "'"
}
if params["pay_product_code"] != "" {
if flag {
condition = condition + " and "
}
condition = condition + "pay_product_code = " + params["pay_product_code"] + "'"
}
if params["pay_type_code"] != "" {
if flag {
condition = condition + " and "
}
condition = condition + "pay_type_code = " + params["pay_type_code"]
}
logs.Info("get order amount str = ", condition)
var maps []orm.Params
allAmount := 0.00
num, err := o.Raw(condition).Values(&maps)
if err == nil && num > 0 {
allAmount, _ = strconv.ParseFloat(maps[0]["allAmount"].(string), 64)
}
return allAmount
}
func GetOrderByBankOrderId(bankOrderId string) OrderInfo {
o := orm.NewOrm()
var orderInfo OrderInfo
_, err := o.QueryTable(ORDER_INFO).Filter("bank_order_id", bankOrderId).Limit(1).All(&orderInfo)
if err != nil {
logs.Error("get order info by bankOrderId fail: ", err)
}
return orderInfo
}
func GetOrderByBankTransId(BankTransId string) OrderInfo {
o := orm.NewOrm()
var orderInfo OrderInfo
_, err := o.QueryTable(ORDER_INFO).Filter("bank_trans_id", BankTransId).Limit(1).All(&orderInfo)
if err != nil {
logs.Error("get order info by bankOrderId fail: ", err)
}
return orderInfo
}
func GetOrderByMerchantOrderId(merchantOrderId string) OrderInfo {
var orderInfo OrderInfo
err := orm.NewOrm().QueryTable(ORDER_INFO).Filter("merchant_order_id", merchantOrderId).One(&orderInfo)
if err != nil {
logs.Error("get order by merchant_order_id: ", err.Error())
}
return orderInfo
}
func GetOneOrder(bankOrderId string) OrderInfo {
var orderInfo OrderInfo
_, err := orm.NewOrm().QueryTable(ORDER_INFO).Filter("bank_order_id", bankOrderId).Limit(1).All(&orderInfo)
if err != nil {
logs.Error("get one order fail: ", err)
}
return orderInfo
}
func InsertCardReturnData(merchantOrderId, cardReturnData string) bool {
o := orm.NewOrm()
_, err := o.QueryTable(ORDER_INFO).Filter("merchant_order_id", merchantOrderId).Update(orm.Params{
"card_return_data": cardReturnData,
})
return err == nil
}
func InsertOrderExValue(merchantOrderId, exValue string) bool {
o := orm.NewOrm()
_, err := o.QueryTable(ORDER_INFO).Filter("merchant_order_id", merchantOrderId).Update(orm.Params{
"ex_value": exValue,
})
return err == nil
}
func InsertClientIP(merchantOrderId, ip string) bool {
o := orm.NewOrm()
_, err := o.QueryTable(ORDER_INFO).Filter("merchant_order_id", merchantOrderId).Update(orm.Params{
"ip": ip,
})
return err == nil
}
func InsertPayTime(merchantOrderId string) bool {
o := orm.NewOrm()
_, err := o.QueryTable(ORDER_INFO).Filter("merchant_order_id", merchantOrderId).Update(orm.Params{
"pay_time": time.Now(),
})
return err == nil
}
func GetByUidAndRoadUid(uid string, roadUid string) (info []*OrderInfo, err error) {
o := orm.NewOrm()
_, err = o.QueryTable(ORDER_INFO).Filter("merchant_uid", uid).Filter("road_uid", roadUid).Filter("status", "success").All(&info)
return
}
// GetByUidAndRoadUidAndTime 根据时间和筛选条件查询
func GetByUidAndRoadUidAndTime(uid string, roadUid string, createTime time.Time) (info []*OrderInfo, err error) {
o := orm.NewOrm()
_, err = o.QueryTable(ORDER_INFO).Filter("merchant_uid", uid).
Filter("create_time__gte", createTime).
Filter("road_uid", roadUid).
Filter("status", "success").
All(&info)
return
}