pay.lua 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. --[[
  2. Descripttion:支付
  3. version:
  4. Author: Neo,Huang
  5. Date: 2022-03-14 20:21:10
  6. LastEditors: Neo,Huang
  7. LastEditTime: 2022-03-14 20:21:10
  8. --]]
  9. local timeUtil = require("utils.timeUtil")
  10. local util_user = require("utils.util_user")
  11. local shopAdapt = require("adapt.shopAdapt")
  12. local moduleData = require("data.module")
  13. local MODULE_NAME = "pay"
  14. local root = {}
  15. -- 支付金额:分
  16. -- 新增支付
  17. function root:user_add_pay(uid, gid, pennys)
  18. log.info("user_add_pay uid[%s] gid[%s] pennys[%s]", tostring(uid), tostring(gid), tostring(pennys))
  19. if uid == nil or gid == nil then
  20. return
  21. end
  22. -- 总金额
  23. pennys = pennys or shopAdapt:goods_get_rmb(gid)
  24. local totalAmount = moduleData:hget_int(uid, MODULE_NAME, "totalAmount")
  25. totalAmount = totalAmount + pennys
  26. moduleData:hset(uid, MODULE_NAME, "totalAmount", totalAmount)
  27. -- 购买商品列表
  28. local currTime = timeUtil.now(uid)
  29. local goodsList = moduleData:hget_json(uid, MODULE_NAME, "goodsList")
  30. table.insert(goodsList, {gid = gid, amount = pennys, time = currTime})
  31. moduleData:hset(uid, MODULE_NAME, "goodsList", goodsList)
  32. -- 当天信息
  33. local dayInfo = self:get_day_info(uid)
  34. dayInfo.lastPayTime = currTime
  35. if dayInfo.goodsList == nil then
  36. dayInfo.goodsList = {}
  37. end
  38. table.insert(dayInfo.goodsList, {gid = gid, amount = pennys, time = currTime})
  39. moduleData:hset(uid, MODULE_NAME, "dayInfo", dayInfo)
  40. return true
  41. end
  42. -- 累计支付金额
  43. function root:user_get_total_pay_count(uid)
  44. if uid == nil then
  45. return
  46. end
  47. return moduleData:hget_int(uid, MODULE_NAME, "totalAmount")
  48. end
  49. -- 是否首次付费
  50. function root:is_first_pay(uid)
  51. local goodsList = moduleData:hget_json(uid, MODULE_NAME, "goodsList")
  52. return is_empty(goodsList)
  53. end
  54. -- 是否商品首次付费
  55. function root:is_goods_first_pay(uid, gid)
  56. local goodsList = moduleData:hget_json(uid, MODULE_NAME, "goodsList")
  57. for _, v in ipairs(goodsList) do
  58. if v.gid == gid then
  59. return false
  60. end
  61. end
  62. return true
  63. end
  64. -- 商品购买次数
  65. function root:get_goods_pay_times(uid, gid)
  66. local times = 0
  67. local goodsList = moduleData:hget_json(uid, MODULE_NAME, "goodsList")
  68. for _, v in ipairs(goodsList) do
  69. if v.gid == gid then
  70. times = times + 1
  71. end
  72. end
  73. return times
  74. end
  75. ----------------------------------------
  76. -- 当天
  77. ----------------------------------------
  78. -- 获取信息
  79. function root:get_day_info(uid)
  80. local currTime = timeUtil.now(uid)
  81. local dayInfo = moduleData:hget_json(uid, MODULE_NAME, "dayInfo")
  82. if dayInfo.lastPayTime and not timeUtil.is_same_day(dayInfo.lastPayTime, currTime) then
  83. dayInfo = {}
  84. end
  85. return dayInfo
  86. end
  87. -- 支付金额
  88. function root:get_day_total_amount(uid)
  89. local dayInfo = self:get_day_info(uid)
  90. local amount = 0
  91. if not is_empty(dayInfo.goodsList) then
  92. for _, v in ipairs(dayInfo.goodsList) do
  93. amount = amount + v.amount
  94. end
  95. end
  96. return amount
  97. end
  98. ----------------------------------------
  99. -- 商品定制
  100. ----------------------------------------
  101. -- 定制商品物品
  102. function root:update_goods_pre_custom_items(uid, gid, items)
  103. if uid == nil or gid == nil or is_empty(items) then
  104. return false
  105. end
  106. local goodsPreCustomItems = moduleData:hget_json(uid, MODULE_NAME, "goodsPreCustomItems")
  107. local isMatch = false
  108. for k, v in ipairs(goodsPreCustomItems) do
  109. if v.gid == gid then
  110. isMatch = true
  111. v.items = items
  112. break
  113. end
  114. end
  115. if not isMatch then
  116. table.insert(goodsPreCustomItems, {gid = gid, items = items})
  117. end
  118. moduleData:hset(uid, MODULE_NAME, "goodsPreCustomItems", goodsPreCustomItems)
  119. return true
  120. end
  121. -- 打包 - 商品定制信息
  122. function root:pack_goods_pre_custom_info_list(uid)
  123. if uid == nil then
  124. return
  125. end
  126. local goodsPreCustomItems = moduleData:hget_json(uid, MODULE_NAME, "goodsPreCustomItems")
  127. if not is_empty(goodsPreCustomItems) then
  128. return goodsPreCustomItems
  129. end
  130. end
  131. -- 获取商品定制物品
  132. function root:get_goods_pre_custom_items(uid, gid)
  133. if uid == nil or gid == nil then
  134. return
  135. end
  136. local goodsPreCustomItems = moduleData:hget_json(uid, MODULE_NAME, "goodsPreCustomItems")
  137. for k, v in ipairs(goodsPreCustomItems) do
  138. if v.gid == gid then
  139. return v.items
  140. end
  141. end
  142. end
  143. -- 删除商品定制物品
  144. function root:del_goods_pre_custom_items(uid, gid)
  145. if uid == nil or gid == nil then
  146. return false
  147. end
  148. local goodsPreCustomItems = moduleData:hget_json(uid, MODULE_NAME, "goodsPreCustomItems")
  149. for k, v in ipairs(goodsPreCustomItems) do
  150. if v.gid == gid then
  151. table.remove(goodsPreCustomItems, k)
  152. moduleData:hset(uid, MODULE_NAME, "goodsPreCustomItems", goodsPreCustomItems)
  153. return true
  154. end
  155. end
  156. return false
  157. end
  158. -- 支付 - 更新商品定制物品
  159. function root:update_goods_pay_custom_items(uid, gid, items)
  160. if uid == nil or gid == nil then
  161. return false
  162. end
  163. local goodsPayCustomItems = moduleData:hget_json(uid, MODULE_NAME, "goodsPayCustomItems")
  164. local isMatch = false
  165. for k, v in ipairs(goodsPayCustomItems) do
  166. if v.gid == gid then
  167. isMatch = true
  168. if is_empty(items) then
  169. table.remove(goodsPayCustomItems, k)
  170. else
  171. v.items = items
  172. end
  173. break
  174. end
  175. end
  176. if not isMatch then
  177. table.insert(goodsPayCustomItems, {gid = gid, items = items})
  178. end
  179. moduleData:hset(uid, MODULE_NAME, "goodsPayCustomItems", goodsPayCustomItems)
  180. return true
  181. end
  182. -- 打包 - 商品定制信息
  183. function root:pack_goods_pay_custom_info_list(uid)
  184. if uid == nil then
  185. return
  186. end
  187. local goodsPayCustomItems = moduleData:hget_json(uid, MODULE_NAME, "goodsPayCustomItems")
  188. if not is_empty(goodsPayCustomItems) then
  189. return goodsPayCustomItems
  190. end
  191. end
  192. return root