diff --git a/app/models/orders/orders.go b/app/models/orders/orders.go index 457d25f..161cad3 100644 --- a/app/models/orders/orders.go +++ b/app/models/orders/orders.go @@ -307,13 +307,17 @@ func (m *ordersModel) CancelOrder(orderId int64) (affected int64, err error) { orders.Status = 5 orders.TransferStatus = 6 // 1.成功 2.充值中 3. 等待充值 4.充值失败 5.异常需要人工处理 6.取消订单 7.订单入队 orders.FailReason = "订单取消" - affected, err = session.ID(orders.Id).Where("transfer_status not in (1,2,4,5,6)").Update(orders) + affected, err = session.ForUpdate().ID(orders.Id).Where("transfer_status not in (1,2,4,5,6)").Update(orders) if err != nil || affected == 0 { err = errors.New("订单正在充值或者已经完成") orders = nil return } err = session.Commit() + if err != nil { + session.Rollback() + return + } return } @@ -342,3 +346,15 @@ func (m *ordersModel) SetDealingOrder(orders *Orders) (affected int64, err error err = session.Commit() return } + +func (m *ordersModel) GetTimeoutOrder( + limit int, +) (orders []*Orders, err error) { + orders = make([]*Orders, 0) + sql := "1=1" + var args []interface{} + sql += " and transfer_status not in (1,2,4,5,6)" + sql += " and created_at <= DATE_SUB(NOW(), INTERVAL 10 MINUTE)" + err = m.GetDb().Where(sql, args...).OrderBy("created_at").Limit(limit).Find(&orders) + return +} diff --git a/app/services/transfersys/transfersys_timeout.go b/app/services/transfersys/transfersys_timeout.go new file mode 100644 index 0000000..142dfd5 --- /dev/null +++ b/app/services/transfersys/transfersys_timeout.go @@ -0,0 +1,39 @@ +package transfersys + +import ( + "context" + "fmt" + "strconv" + "time" + + models "com.snow.auto_monitor/app/models/orders" + "github.com/qit-team/snow-core/log/logger" +) + +func DoTimeout() { + fmt.Println("开始执行周期任务:DoTimeout") + + // 创建一个新的Ticker,每3秒钟触发一次 + ticker := time.NewTicker(3 * time.Second) + defer ticker.Stop() // 在函数结束时停止Ticker + for range ticker.C { + res, err := models.GetInstance().GetTimeoutOrder(10) + if err != nil { + fmt.Println(err.Error()) + continue + } + for i := 0; i < len(res); i++ { + models.GetInstance().CancelOrder(res[i].Id) + err = AddCallback(strconv.Itoa(int(res[i].Id))) + if err != nil { + logger.Error(context.TODO(), "order timeout", err) + continue + } + logger.Info(context.TODO(), "order timeout", "orderId: "+strconv.FormatInt(res[i].Id, 10)) + } + } +} + +func init() { + go DoTimeout() +}