87 lines
2.6 KiB
Go
87 lines
2.6 KiB
Go
package console
|
|
|
|
import (
|
|
"PaymentCenter/app/constants/common"
|
|
"PaymentCenter/app/data"
|
|
"PaymentCenter/app/http/entities"
|
|
"PaymentCenter/app/models/ordersmodel"
|
|
"PaymentCenter/app/utils"
|
|
"github.com/qit-team/snow-core/command"
|
|
"strconv"
|
|
"time"
|
|
"xorm.io/builder"
|
|
)
|
|
|
|
func RegisterCommand(c *command.Command) {
|
|
c.AddFunc("test", test)
|
|
c.AddFunc("closeOrder", closeOrder)
|
|
}
|
|
|
|
// 关闭长时间支付中的订单
|
|
func closeOrder() {
|
|
var now = time.Now().Format(time.DateTime)
|
|
utils.Log(nil, "关闭订单", now)
|
|
// 查询未支付的订单
|
|
repo := data.NewOrderRepo(ordersmodel.GetInstance().GetDb())
|
|
// 拼接条件
|
|
cond := builder.NewCond()
|
|
cond = cond.And(builder.Eq{"status": common.ORDER_STATUS_PAYING}, builder.Lt{"create_time": time.Now().Add(-time.Hour)})
|
|
|
|
order := make([]ordersmodel.Orders, 0)
|
|
total, err := repo.OrderList(cond, entities.PageRequest{}, &order)
|
|
if err != nil {
|
|
utils.Log(nil, "关闭订单,查询未支付订单失败", err)
|
|
} else if total > 0 {
|
|
orderIds := make([]int64, 0)
|
|
for _, v := range order {
|
|
orderIds = append(orderIds, v.Id)
|
|
}
|
|
// 修改订单状态为关闭
|
|
cond = builder.NewCond()
|
|
cond = cond.And(builder.In("id", orderIds))
|
|
_, err = repo.OrderUpdate(&ordersmodel.Orders{Status: common.ORDER_STATUS_CLOSE}, cond, "status")
|
|
if err != nil {
|
|
utils.Log(nil, "关闭订单,修改订单状态失败", err)
|
|
return
|
|
}
|
|
}
|
|
utils.Log(nil, "关闭订单,修改订单状态成功", "count="+strconv.Itoa(len(order)))
|
|
}
|
|
|
|
// 定时查询支付中的订单, 主动查询订单支付状态
|
|
func queryOrder() {
|
|
var now = time.Now().Format(time.DateTime)
|
|
utils.Log(nil, "主动查询订单支付状态", now)
|
|
// 查询未支付的订单
|
|
repo := data.NewOrderRepo(ordersmodel.GetInstance().GetDb())
|
|
// 拼接条件
|
|
cond := builder.NewCond()
|
|
cond = cond.And(builder.Eq{"status": common.ORDER_STATUS_PAYING}, builder.Gt{"create_time": time.Now().Add(-time.Second)})
|
|
order := make([]ordersmodel.Orders, 0)
|
|
total, err := repo.OrderList(cond, entities.PageRequest{}, &order)
|
|
if err != nil {
|
|
utils.Log(nil, "主动查询订单支付状态,查询未付中订单失败", err)
|
|
return
|
|
} else if total > 0 {
|
|
// 发起查询上游支付
|
|
for _, v := range order {
|
|
go func(order ordersmodel.Orders) {
|
|
// 发起查询
|
|
utils.Log(nil, "主动查询订单支付状态,发起查询", order.Id)
|
|
// 解析上游结果
|
|
|
|
// 修改订单状态
|
|
}(v)
|
|
}
|
|
}
|
|
}
|
|
|
|
// 回调下游
|
|
func callbackOrder() {
|
|
var now = time.Now().Format(time.DateTime)
|
|
utils.Log(nil, "回调下游", now)
|
|
// 查询回调失败的订单
|
|
|
|
// 发起回调
|
|
}
|