This commit is contained in:
aozhiwei 2020-12-15 10:53:38 +08:00
parent 8bef024538
commit 4a39e83922

View File

@ -4,6 +4,7 @@ import (
"sync" "sync"
"sync/atomic" "sync/atomic"
"net/http" "net/http"
"database/sql"
"q5" "q5"
"f5" "f5"
) )
@ -134,8 +135,7 @@ func (this *OrderMgr) SetDBIdx(instanceId int32, idx int64) {
func (this *OrderMgr) FetchEventOneDB(conf *MtwMysqlConf, conn *q5.Mysql) bool { func (this *OrderMgr) FetchEventOneDB(conf *MtwMysqlConf, conn *q5.Mysql) bool {
lastIdx := this.GetDBIdx(conf.GetInstanceId()) lastIdx := this.GetDBIdx(conf.GetInstanceId())
for true { for true {
rows, err := conn.Query("SELECT idx, account_id, orderid, roleid, server_id, " + rows, err := conn.Query(this.GenSelect() +
" channel, poly_sdk_channel, unified_channel, try_count, price, createtime " +
"FROM `orderinfo` " + "FROM `orderinfo` " +
"WHERE idx > ? AND sp_pay_result = 1 AND status = 0 " + "WHERE idx > ? AND sp_pay_result = 1 AND status = 0 " +
"LIMIT 0, 1000;", "LIMIT 0, 1000;",
@ -150,35 +150,12 @@ func (this *OrderMgr) FetchEventOneDB(conf *MtwMysqlConf, conn *q5.Mysql) bool {
defer rows.Close() defer rows.Close()
hasData := false hasData := false
for rows.Next() { for rows.Next() {
var idx int64
var accountId string
var orderId string
var roleId string
var serverId int32
var channel int32
var polySdkChannel int32
var unifiedChannel int32
var tryCount int32
var price int32
var createTime int64
hasData = true hasData = true
rows.Scan(&idx, &accountId, &orderId, &roleId, &serverId, &channel, orderInfo := new(OrderInfo)
&polySdkChannel, &unifiedChannel, &tryCount, &price, createTime) orderInfo.ScanFromRows(rows)
this.AddOrder( this.AddOrder(conf.GetInstanceId(), orderInfo)
conf.GetInstanceId(), if orderInfo.idx > lastIdx {
idx, lastIdx = orderInfo.idx
accountId,
orderId,
roleId,
serverId,
channel,
polySdkChannel,
unifiedChannel,
tryCount,
price,
createTime)
if idx > lastIdx {
lastIdx = idx
} }
} }
if !hasData { if !hasData {
@ -218,24 +195,12 @@ func (this *OrderMgr) reissueOrder(w* http.ResponseWriter, r *http.Request) {
return return
} }
{ {
row := conn.QueryRow("SELECT idx, account_id, orderid, roleid, server_id, " + row := conn.QueryRow(this.GenSelect() +
" channel, poly_sdk_channel, unified_channel, try_count, price, createtime " +
"FROM `orderinfo` " + "FROM `orderinfo` " +
"WHERE orderid = '?';", "WHERE orderid = '?';",
orderId) orderId)
var idx int64 orderInfo := new(OrderInfo)
var accountId string if err := orderInfo.ScanFromRow(row); err != nil {
var orderId string
var roleId string
var serverId int32
var channel int32
var polySdkChannel int32
var unifiedChannel int32
var tryCount int32
var price int32
var createTime int64
if err := row.Scan(&idx, &accountId, &orderId, &roleId, &serverId, &channel,
&polySdkChannel, &unifiedChannel, &tryCount, &price, createTime); err != nil {
q5.ResponseErr(w, 3, "订单不存在") q5.ResponseErr(w, 3, "订单不存在")
return return
} }
@ -244,53 +209,58 @@ func (this *OrderMgr) reissueOrder(w* http.ResponseWriter, r *http.Request) {
q5.ResponseErr(w, 4, "更新订单状态失败") q5.ResponseErr(w, 4, "更新订单状态失败")
return return
} }
this.AddOrder( this.AddOrder(dbConf.GetInstanceId(), orderInfo)
dbConf.GetInstanceId(),
idx,
accountId,
orderId,
roleId,
serverId,
channel,
polySdkChannel,
unifiedChannel,
tryCount,
price,
createTime)
q5.ResponseOk(w) q5.ResponseOk(w)
} }
} }
func (this *OrderMgr) AddOrder( func (this *OrderMgr) AddOrder(
dbInstanceId int32, dbInstanceId int32,
idx int64, orderInfo *OrderInfo) {
accountId string,
orderId string,
roleId string,
serverId int32,
channel int32,
polySdkChannel int32,
unifiedChannel int32,
tryCount int32,
price int32,
createTime int64) {
this.orderMutex.Lock() this.orderMutex.Lock()
defer this.orderMutex.Unlock() defer this.orderMutex.Unlock()
if _, ok := this.orderHash[orderId]; ok { if _, ok := this.orderHash[orderInfo.orderId]; ok {
panic("orderId已经存在" + orderId) panic("orderId已经存在" + orderInfo.orderId)
} }
orderInfo := new(OrderInfo)
orderInfo.dbInstanceId = dbInstanceId orderInfo.dbInstanceId = dbInstanceId
orderInfo.idx = idx
orderInfo.accountId = accountId
orderInfo.roleId = roleId
orderInfo.serverId = serverId
orderInfo.channel = channel
orderInfo.polySdkChannel = polySdkChannel
orderInfo.unifiedChannel = unifiedChannel
orderInfo.tryCount = tryCount
orderInfo.price = price
orderInfo.createTime = createTime
this.orderList = append(this.orderList, orderInfo) this.orderList = append(this.orderList, orderInfo)
this.orderHash[orderId] = orderInfo this.orderHash[orderInfo.orderId] = orderInfo
}
func (this *OrderMgr) GenSelect() string {
selectSql := "SELECT idx, account_id, orderid, roleid, server_id, " +
"channel, poly_sdk_channel, unified_channel, try_count, price, createtime "
return selectSql
}
func (this *OrderInfo) ScanFromRows(rows *sql.Rows) (error) {
err := rows.Scan(
&this.idx,
&this.accountId,
&this.orderId,
&this.roleId,
&this.serverId,
&this.channel,
&this.polySdkChannel,
&this.unifiedChannel,
&this.tryCount,
&this.price,
&this.createTime)
return err
}
func (this *OrderInfo) ScanFromRow(row *sql.Row) (error) {
err := row.Scan(
&this.idx,
&this.accountId,
&this.orderId,
&this.roleId,
&this.serverId,
&this.channel,
&this.polySdkChannel,
&this.unifiedChannel,
&this.tryCount,
&this.price,
&this.createTime)
return err
} }