aozhiwei f650fb85ec 1
2024-08-05 11:42:38 +08:00

175 lines
4.2 KiB
Go

package task
import (
"q5"
"f5"
"main/constant"
"fmt"
"main/mt"
"strings"
"jccommon"
)
type itemSoldOut struct {
Buyer string `json:"buyer"`
Passport string `json:"passport"`
OrderId string `json:"orderId"`
Currency string `json:"currency"`
Amount string `json:"amount"`
}
type recharge struct {
}
func (this *recharge) init() {
mt.Table.Contract.Traverse(func (ele *mt.Contract) bool {
if ele.GetName() == jccommon.CONTRACT_NAME_GameItemMall {
go this.process(ele.GetNetId(), ele.GetAddress())
}
return true
})
}
func (this *recharge) unInit() {
}
func (this *recharge) process(netId int32, contractAddress string) {
key := fmt.Sprintf("recharge.lastIdx.%d.%s", netId, contractAddress)
if dbLastIdxStr, err := jccommon.GetDbParam(constant.BCEVENT_DB, key); err == nil {
f5.GetGoStyleDb().IncrementLoad(
constant.BCEVENT_DB,
"recharge." + q5.ToString(netId) + "." + contractAddress,
"t_blockchain_event",
q5.ToInt64(dbLastIdxStr),
func (lastIdx int64, maxIdx int64) (string, []string) {
sql := fmt.Sprintf(`
SELECT * FROM t_blockchain_event
WHERE idx > %d AND idx <= %d AND net_id = %d AND contract_address = ? AND event_name = ?
ORDER BY idx LIMIT 1000
`,
lastIdx,
maxIdx,
netId)
params := []string{
contractAddress,
"ItemSoldOut",
}
return sql, params
},
func (newLastIdx int64) {
jccommon.SaveDbParam(constant.BCEVENT_DB, key, q5.ToString(newLastIdx))
},
this.saveToDb)
} else {
panic(fmt.Sprintf("recharge.process getDBParam error %s", err))
}
}
func (this *recharge) saveToDb(ds *f5.DataSet) bool {
f5.GetSysLog().Info("load ItemSoldOut idx:%s contract_name:%s event_name:%s",
ds.GetByName("idx"),
ds.GetByName("contract_name"),
ds.GetByName("event_name"))
idx := ds.GetByName("idx")
txhash := ds.GetByName("txhash")
logIndex := ds.GetByName("log_index")
netId := ds.GetByName("net_id")
eventName := ds.GetByName("event_name")
contractAddress := ds.GetByName("contract_address")
returnValues := ds.GetByName("return_values")
createTime := ds.GetByName("createtime")
modifyTime := ds.GetByName("modifytime")
decodeJsonOk := false
p := new(itemSoldOut)
if q5.DecodeJson(returnValues, p) == nil {
decodeJsonOk = true
orderExists := false
if this.updateOrderStatus(txhash, p.OrderId, q5.ToInt32(createTime), &orderExists) != nil {
return false
}
status := 0
if orderExists {
status = 1
}
var dbErr error
f5.GetGoStyleDb().Upsert(
constant.BCEVENT_DB,
"t_recharge",
[][]string{
{"txhash", txhash},
{"log_index", logIndex},
{"net_id", netId},
{"contract_address", contractAddress},
},
[][]string{},
[][]string{
{"txhash", txhash},
{"log_index", logIndex},
{"net_id", netId},
{"contract_address", strings.ToLower(contractAddress)},
{"src_idx", idx},
{"buyer", p.Buyer},
{"passport", p.Passport},
{"order_id", p.OrderId},
{"currency", p.Currency},
{"amount", p.Amount},
{"status", q5.ToString(status)},
{"createtime", createTime},
{"modifytime", modifyTime},
},
func (err error, lastInsertId int64, rowsAffected int64) {
dbErr = err
})
if dbErr != nil {
return false
}
}
if !decodeJsonOk {
jccommon.AddDbLog(constant.BCEVENT_DB, "recharge", "decodeJsonError",
map[string]string{
"param1": txhash,
"param2": netId,
"param3": eventName,
"param4": idx,
"param5": contractAddress,
})
}
return true
}
func (this *recharge) updateOrderStatus(txhash string, orderId string, payTime int32, orderExists *bool) error {
*orderExists = false
var resultErr error
f5.GetGoStyleDb().OrmSelectOne(
constant.BCNFT_DB,
"t_recharge_order",
[][]string{
{"order_id", orderId},
},
func (err error, ds *f5.DataSet) {
resultErr = err
if err != nil {
return
}
*orderExists = ds.Next()
})
if resultErr == nil && *orderExists {
f5.GetGoStyleDb().Update(
constant.BCNFT_DB,
"t_recharge_order",
[][]string{
{"order_id", orderId},
},
[][]string{
{"txhash", txhash},
{"pay_status", q5.ToString(1)},
{"pay_time", q5.ToString(payTime)},
},
func (err error, lastInsertId int64, rowsAffected int64) {
resultErr = err
})
}
return resultErr
}