recharge.lua 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. --充值服务
  2. local skynet = require "skynet"
  3. require "skynet.manager"
  4. local redisdriver = require "skynet.db.redis"
  5. local logger = require "logger"
  6. local queue = require "skynet.queue"
  7. local asset = require "model.asset"
  8. local pipeline = require "pipeline"
  9. local synchronized = queue()
  10. local skynet_retpack = skynet.retpack
  11. local trace = logger.trace
  12. local recharge = {}
  13. local CMD = {}
  14. local redis
  15. local INDENT = "indent:"
  16. local KEYGEN_PAY = "%s:%s"
  17. local mq
  18. local REBATE = "rebate:"
  19. function recharge.refresh_data(uid,orderid)
  20. local key = INDENT..string.format(KEYGEN_PAY,uid,orderid)
  21. local flag = redis:hget(key,"flag")
  22. if not flag then
  23. key = REBATE..string.format(KEYGEN_PAY,uid,orderid)
  24. flag = redis:hget(key,"flag")
  25. end
  26. if flag then
  27. flag = tonumber(flag)
  28. trace("充值修改flag:%s",flag)
  29. if flag==1 then
  30. local now = os.time()
  31. redis:hmset(key,
  32. "overTime",now,
  33. "flag",0
  34. )
  35. end
  36. else
  37. trace("充值修改flag失败没有orderid:%s",orderid)
  38. end
  39. end
  40. function recharge.record_data(uid, cfid, orderid, appname, giftid)
  41. local key = INDENT..string.format(KEYGEN_PAY, uid, orderid)
  42. local now = os.time()
  43. redis:hmset(key,
  44. "uid",uid,
  45. "cfid",cfid,
  46. "orderid",orderid,
  47. "writeTime",now,
  48. "flag",1, --1未完成,0完成
  49. "appname", appname,
  50. "giftid", giftid
  51. )
  52. --test recharge
  53. local moneycfg = assert(asset.RechargeConfig_proto[cfid],cfid)
  54. local moneytype = asset.DataConfig_proto[19].data1
  55. local moneynum
  56. if moneytype == 1 then
  57. moneynum = moneycfg.my
  58. elseif moneytype == 2 then
  59. moneynum = moneycfg.rmb
  60. elseif moneytype == 3 then
  61. moneynum = moneycfg.tw
  62. end
  63. skynet.send(mq, "lua", "publish", uid, "recharge", cfid, orderid, moneynum, appname, giftid) -- 派发事件
  64. end
  65. function CMD.orderid_refresh(uid,orderid)
  66. synchronized(function()
  67. recharge.refresh_data(uid,orderid)--在玩家发货成功
  68. end)
  69. end
  70. function CMD.orderid_record(uid, cfid, orderid, appname, giftid)
  71. synchronized(function()
  72. recharge.record_data(uid, cfid, orderid, appname, giftid)--写入玩家成功支付的订单
  73. end)
  74. end
  75. --------------------------sy185的返利接口------------------------
  76. function recharge.rebate_data(uid, money, orderid, vip, gift)--写入玩家返利的订单
  77. local key = REBATE..string.format(KEYGEN_PAY,uid,orderid)
  78. local now = os.time()
  79. redis:hmset(key,
  80. "uid",uid,
  81. "money",money,
  82. "orderid",orderid,
  83. "writeTime",now,
  84. "flag",1, --1未完成,0完成
  85. "vip",vip, --是否加vip经验,0-不加1-加
  86. "gift",gift --是否amount>=5000时,额外赠送50%,0-不赠送1-赠送(满50 元额外赠送50%)
  87. )
  88. skynet.send(mq, "lua", "publish", uid, "recharge.rebate_money", money, orderid, vip, gift) -- 派发事件
  89. end
  90. function CMD.orderid_rebate(uid, cfid, orderid,vip,gift)
  91. synchronized(function()
  92. recharge.rebate_data(uid, cfid, orderid,vip,gift)--写入玩家返利的订单
  93. end)
  94. end
  95. --------------------------------------------------------------------
  96. --获取玩家充值数量
  97. local all_recharge = {}
  98. local stringify = require "stringify"
  99. function recharge.get_all_recharge()
  100. local keys = redis:keys("indent:*")
  101. local block = pipeline(1024)
  102. if next(keys) then
  103. for _, k in pairs(keys) do
  104. block.add("hmget", k, "uid","cfid")
  105. end
  106. end
  107. local resp = block.execute(redis, {})
  108. local num = 0
  109. for i, v in ipairs(resp) do
  110. assert(v.ok)
  111. local rs = v.out
  112. if rs[1] then
  113. local uid = rs[1]
  114. local cfid = rs[2]
  115. all_recharge[uid] = all_recharge[uid] or {}
  116. table.insert(all_recharge[uid],{cfid = cfid,uid = uid})
  117. num = num + 1
  118. end
  119. end
  120. -- trace("___________________________________获取玩家充值数量num:%s,all_recharge:%s",num,stringify(all_recharge))
  121. end
  122. function CMD.get_uid_recharge(uid)
  123. if all_recharge and all_recharge[uid] then
  124. return all_recharge[uid]
  125. end
  126. end
  127. function CMD.start()
  128. local conf = assert(option.redis)
  129. redis = redisdriver.connect(conf)
  130. redis:select(3)--切换到数据库db1
  131. mq = mq or skynet.localname(".mq")
  132. recharge.get_all_recharge()
  133. end
  134. skynet.init(function()
  135. skynet.register(".recharge")
  136. end)
  137. skynet.start(function()
  138. skynet.dispatch("lua", function(session, _, cmd, ...)
  139. local f = assert(CMD[cmd])
  140. if session == 0 then
  141. f(...)
  142. else
  143. skynet_retpack(f(...))
  144. end
  145. end)
  146. end)