后台,添加定时任务
This commit is contained in:
parent
fb5c0e458b
commit
490e2fa15f
|
@ -4,10 +4,17 @@ import (
|
|||
"PaymentCenter/app/constants/common"
|
||||
"PaymentCenter/app/data"
|
||||
"PaymentCenter/app/http/entities"
|
||||
"PaymentCenter/app/models/orderlogmodel"
|
||||
"PaymentCenter/app/models/ordersmodel"
|
||||
"PaymentCenter/app/third/paymentService"
|
||||
"PaymentCenter/app/third/paymentService/payCommon"
|
||||
"PaymentCenter/app/utils"
|
||||
"PaymentCenter/config"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"github.com/qit-team/snow-core/command"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
"xorm.io/builder"
|
||||
)
|
||||
|
@ -25,7 +32,7 @@ func closeOrder() {
|
|||
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)})
|
||||
cond = cond.And(builder.Eq{"status": common.ORDER_STATUS_PAYING}, builder.Lt{"create_time": time.Now().Add(-time.Second * time.Duration(config.GetConf().CronConfig.CloseOrderTime))})
|
||||
|
||||
order := make([]ordersmodel.Orders, 0)
|
||||
total, err := repo.OrderList(cond, entities.PageRequest{}, &order)
|
||||
|
@ -48,31 +55,119 @@ func closeOrder() {
|
|||
utils.Log(nil, "关闭订单,修改订单状态成功", "count="+strconv.Itoa(len(order)))
|
||||
}
|
||||
|
||||
// 定时查询支付中的订单, 主动查询订单支付状态
|
||||
// 主动查询订单支付状态
|
||||
func queryOrder() {
|
||||
var now = time.Now().Format(time.DateTime)
|
||||
utils.Log(nil, "主动查询订单支付状态", now)
|
||||
ctx := context.Background()
|
||||
// 查询未支付的订单
|
||||
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)
|
||||
config.GetConf()
|
||||
cond = cond.And(builder.Eq{"status": common.ORDER_STATUS_PAYING}, builder.Gt{"orders.create_time": time.Now().Add(-time.Second * time.Duration(config.GetConf().CronConfig.QueryOrderTime))})
|
||||
order := make([]ordersmodel.OrdersLeftPayChannelList, 0)
|
||||
err := repo.OrdersLeftPayChannelList(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)
|
||||
// 解析上游结果
|
||||
} else if len(order) > 0 {
|
||||
ch := make(chan struct{}, config.GetConf().CronConfig.ConcurrentNumber)
|
||||
wg := sync.WaitGroup{}
|
||||
|
||||
// 修改订单状态
|
||||
}(v)
|
||||
for index := range order {
|
||||
ch <- struct{}{}
|
||||
wg.Add(1)
|
||||
orderInfo := order[index]
|
||||
|
||||
// 发起查询上游支付
|
||||
go func(orderInfo ordersmodel.OrdersLeftPayChannelList) {
|
||||
defer func() {
|
||||
<-ch
|
||||
wg.Done()
|
||||
}()
|
||||
|
||||
query := paymentService.PayOrderQueryRequest{
|
||||
OrderId: orderInfo.Id,
|
||||
}
|
||||
switch orderInfo.ChannelType {
|
||||
case common.PAY_CHANNEL_WECHAT_H5, common.PAY_CHANNEL_WECHAT_JSAPI, common.PAY_CHANNEL_WECHAT_NATIVE, common.PAY_CHANNEL_WECHAT_APP, common.PAY_CHANNEL_WECHAT_MINI:
|
||||
_ = json.Unmarshal([]byte(orderInfo.ExtJson), &query.Wx)
|
||||
query.PayChannel = payCommon.PAY_CHANNLE_TYPE_WECHAT
|
||||
case common.PAY_CHANNEL_ALIPAY_JSAPI, common.PAY_CHANNEL_ALIPAY_WEB, common.PAY_CHANNEL_ALIPAY_MINI:
|
||||
query.PayChannel = payCommon.PAY_CHANNLE_TYPE_ZFB
|
||||
_ = json.Unmarshal([]byte(orderInfo.ExtJson), &query.Ali)
|
||||
}
|
||||
|
||||
// 发起查询
|
||||
result := paymentService.PayOrderQuery(ctx, query)
|
||||
utils.Log(nil, "主动查询订单支付状态,上游返回数据", result)
|
||||
// 查询成功,校验状态
|
||||
var status int
|
||||
if result.Code == payCommon.PAY_SUCCESS_CODE {
|
||||
switch result.Result.TradeState {
|
||||
case "SUCCESS":
|
||||
// 成功
|
||||
status = common.ORDER_STATUS_PAYED
|
||||
case "REFUND":
|
||||
// 退款
|
||||
|
||||
case "NOTPAY":
|
||||
// 未支付
|
||||
return
|
||||
case "CLOSED":
|
||||
// 关闭
|
||||
status = common.ORDER_STATUS_CLOSE
|
||||
|
||||
}
|
||||
// 回调通知下游 todo
|
||||
|
||||
// 更新订单状态 todo
|
||||
orderUpdate := ordersmodel.Orders{
|
||||
Id: orderInfo.Id,
|
||||
Status: status,
|
||||
}
|
||||
|
||||
session := ordersmodel.GetInstance().GetDb().NewSession()
|
||||
if err = session.Begin(); err != nil {
|
||||
utils.Log(nil, "主动查询订单支付状态,更新订单状态失败", err)
|
||||
return
|
||||
}
|
||||
defer func() {
|
||||
if err != nil {
|
||||
session.Rollback()
|
||||
} else {
|
||||
err = session.Commit()
|
||||
}
|
||||
}()
|
||||
orderLogRepo := data.NewOrderLogRepo(session)
|
||||
orderRepo := data.NewOrderRepo(session)
|
||||
|
||||
conn := builder.NewCond()
|
||||
conn = conn.And(builder.Eq{"id": orderInfo.Id})
|
||||
_, err = orderRepo.OrderUpdate(&orderUpdate, conn)
|
||||
if err != nil {
|
||||
utils.Log(nil, "主动查询订单支付状态,更新订单状态失败", err)
|
||||
return
|
||||
}
|
||||
|
||||
// 写入支付日志
|
||||
log := orderlogmodel.OrderLog{
|
||||
OrderId: orderInfo.Id,
|
||||
PayCallback: "",
|
||||
Status: 0,
|
||||
MerchantParam: "",
|
||||
MerchantCallback: "",
|
||||
}
|
||||
_, err = orderLogRepo.OrderLogInsertOne(&log)
|
||||
if err != nil {
|
||||
utils.Log(nil, "主动查询订单支付状态,写入支付日志失败", err)
|
||||
}
|
||||
|
||||
}
|
||||
}(orderInfo)
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package console
|
||||
|
||||
import (
|
||||
"PaymentCenter/config"
|
||||
"fmt"
|
||||
"github.com/robfig/cron"
|
||||
)
|
||||
|
||||
|
@ -9,6 +11,7 @@ import (
|
|||
* @wiki https://godoc.org/github.com/robfig/cron
|
||||
*/
|
||||
func RegisterSchedule(c *cron.Cron) {
|
||||
fmt.Println(config.GetConf().CronConfig)
|
||||
//c.AddFunc("0 30 * * * *", test)
|
||||
//c.AddFunc("@hourly", test)
|
||||
//c.AddFunc("@every 10s", test)
|
||||
|
|
|
@ -22,7 +22,7 @@ const (
|
|||
ADMIN_USER_NAME = "User-Name"
|
||||
ADMIN_USER_INCLUDEUSERS = "Include-Users"
|
||||
|
||||
// '订单状态,待支付、支付中、支付成功、支付失败、订单关闭',
|
||||
// '订单状态,1待支付、2支付中、3支付成功、4支付失败、5订单关闭',
|
||||
ORDER_STATUS_WAITPAY = 1
|
||||
ORDER_STATUS_PAYING = 2
|
||||
ORDER_STATUS_PAYED = 3
|
||||
|
|
|
@ -49,3 +49,13 @@ func (m *OrderRepo) OrdersBackendList(conn builder.Cond, pageFilter entities.Pag
|
|||
Join("left", "pay_channel", "pay_channel.id = orders.pay_id")
|
||||
return repo.Desc("create_time").FindAndCount(orderList)
|
||||
}
|
||||
|
||||
func (m *OrderRepo) OrdersLeftPayChannelList(conn builder.Cond, pageFilter entities.PageRequest, orderList *[]ordersmodel.OrdersLeftPayChannelList) error {
|
||||
repo := m.repo.Select(`orders.*,pay_channel.channel_type,pay_channel.app_id ,pay_channel.ext_json`).
|
||||
Where(conn)
|
||||
if pageFilter.Page > 0 {
|
||||
repo = repo.Limit(pageFilter.PageSize, pageFilter.PageSize*(pageFilter.Page-1))
|
||||
}
|
||||
repo = repo.Join("left", "pay_channel", "pay_channel.id = orders.pay_id")
|
||||
return repo.Find(orderList)
|
||||
}
|
||||
|
|
|
@ -30,7 +30,7 @@ func (p *PayChannelResponse) ResponseFromDb(db paychannelmodel.PayChannel) {
|
|||
p.ExpireTime = db.ExpireTime.Format("2006-01-02 15:04:05")
|
||||
p.CreateTime = db.CreateTime.Format("2006-01-02 15:04:05")
|
||||
|
||||
switch p.ChannelType {
|
||||
switch db.ChannelType {
|
||||
case common.PAY_CHANNEL_WECHAT_H5, common.PAY_CHANNEL_WECHAT_JSAPI, common.PAY_CHANNEL_WECHAT_NATIVE, common.PAY_CHANNEL_WECHAT_APP, common.PAY_CHANNEL_WECHAT_MINI:
|
||||
_ = json.Unmarshal([]byte(db.ExtJson), &p.WechatPayChannel)
|
||||
case common.PAY_CHANNEL_ALIPAY_JSAPI, common.PAY_CHANNEL_ALIPAY_WEB, common.PAY_CHANNEL_ALIPAY_MINI:
|
||||
|
|
|
@ -36,6 +36,12 @@ type OrdersBackendList struct {
|
|||
PayName string `xorm:"'pay_name' varchar(128)"`
|
||||
AppName string `xorm:"'app_name' varchar(128)"`
|
||||
}
|
||||
type OrdersLeftPayChannelList struct {
|
||||
Orders `xorm:"extends"`
|
||||
ChannelType int `xorm:"'channel_type' int(11)"`
|
||||
AppId string `xorm:"'app_id' varchar(255)"`
|
||||
ExtJson string `xorm:"'ext_json' JSON"`
|
||||
}
|
||||
|
||||
// 表名
|
||||
func (m *Orders) TableName() string {
|
||||
|
|
|
@ -37,6 +37,7 @@ type Config struct {
|
|||
Jwt Jwt `toml:"Jwt"`
|
||||
AliOss AliOss `toml:"AliOss"`
|
||||
AdminGate []string `toml:"AdminGate"`
|
||||
CronConfig CronConfig `toml:"CronConfig"`
|
||||
}
|
||||
|
||||
type AliOss struct {
|
||||
|
@ -83,6 +84,12 @@ type Nacos struct {
|
|||
Port int64
|
||||
}
|
||||
|
||||
type CronConfig struct {
|
||||
CloseOrderTime int `toml:"CloseOrderTime"`
|
||||
QueryOrderTime int `toml:"QueryOrderTime"`
|
||||
ConcurrentNumber int `toml:"ConcurrentNumber"`
|
||||
}
|
||||
|
||||
func newConfig() *Config {
|
||||
return new(Config)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue