YouChuKoffee/app/console/command.go

42 lines
1.0 KiB
Go

package console
import (
"github.com/qit-team/snow-core/command"
"qteam/app/models/ordersmodel"
"strconv"
"strings"
"time"
)
func RegisterCommand(c *command.Command) {
c.AddFunc("test", test)
}
func ClearUnpayOrder() {
var expire = time.Now().Add(-1 * time.Minute).Format(time.DateTime)
var orders []ordersmodel.Orders
ordersmodel.GetInstance().GetDb().Where("state = 1 and create_time <= ?", expire).Select("id,product_id").Find(&orders)
var ids = make([]int, 0)
var prodIds = make([]string, 0)
for _, v := range orders {
ids = append(ids, v.Id)
prodIds = append(prodIds, strconv.Itoa(v.ProductId))
}
if len(prodIds) > 0 {
var session = ordersmodel.GetInstance().GetDb().NewSession()
session.Begin()
_, err := session.In("id", ids).Update(&ordersmodel.Orders{State: 9})
if err == nil {
_, err = session.Exec("update Products set stock=stock+1 where id in (" + strings.Join(prodIds, ",") + ")")
if err == nil {
session.Commit()
} else {
session.Rollback()
}
} else {
session.Rollback()
}
}
}