pmail.lua 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. --[[
  2. Descripttion:邮件
  3. version:
  4. Author: Neo,Huang
  5. Date: 2022-08-17 16:04:20
  6. LastEditors: Neo,Huang
  7. LastEditTime: 2022-08-19 20:12:34
  8. --]]
  9. local code = require("code")
  10. local util_mail = require("utils.util_mail")
  11. local timeUtil = require("utils.timeUtil")
  12. local itemUtil = require("utils.itemUtil")
  13. local bagData = require("data.bag")
  14. local root = class("mailSelf", require("base.baseModule"))
  15. function root:ctor(uid)
  16. root.super.ctor(self, "tb_mail_self", "uid", true, nil, nil)
  17. self.uid = uid
  18. end
  19. function root:getInitColumnNameOptions()
  20. return {
  21. uid = "bigint(20) unsigned NOT NULL COMMENT '用户ID'",
  22. readIdList = "JSON COMMENT '已读邮件ID列表'",
  23. delIdList = "JSON COMMENT '已删邮件ID列表'",
  24. awardIdList = "JSON COMMENT '已领取奖励邮件ID列表'"
  25. }
  26. end
  27. -- 读邮件 - 新增
  28. function root:add_read_mail(id)
  29. if is_empty(id) then
  30. return false
  31. end
  32. local readIdList = self:redis_get_key_info("readIdList")
  33. if table.include(readIdList, id) then
  34. return false
  35. end
  36. table.insetr(readIdList, id)
  37. self:redis_update_key_info("readIdList", readIdList)
  38. end
  39. -- 是否已读
  40. function root:is_read(id)
  41. if is_empty(id) then
  42. return false
  43. end
  44. local readIdList = self:redis_get_key_info("readIdList")
  45. if table.include(readIdList, id) then
  46. return true
  47. end
  48. return false
  49. end
  50. -- 读邮件 - 新增
  51. function root:add_del_mail(id)
  52. if is_empty(id) then
  53. return false
  54. end
  55. local delIdList = self:redis_get_key_info("delIdList")
  56. if table.include(delIdList, id) then
  57. return false
  58. end
  59. table.insetr(delIdList, id)
  60. self:redis_update_key_info("delIdList", delIdList)
  61. end
  62. -- 是否已读
  63. function root:is_del(id)
  64. if is_empty(id) then
  65. return false
  66. end
  67. local delIdList = self:redis_get_key_info("delIdList")
  68. if table.include(delIdList, id) then
  69. return true
  70. end
  71. return false
  72. end
  73. -- 奖励 - 新增
  74. function root:add_award_mail(id)
  75. if is_empty(id) then
  76. return false
  77. end
  78. local awardIdList = self:redis_get_key_info("awardIdList")
  79. if table.include(awardIdList, id) then
  80. return false
  81. end
  82. table.insetr(awardIdList, id)
  83. self:redis_update_key_info("awardIdList", awardIdList)
  84. return true
  85. end
  86. -- 是否已读
  87. function root:is_awarded(id)
  88. if is_empty(id) then
  89. return false
  90. end
  91. local awardIdList = self:redis_get_key_info("awardIdList")
  92. if table.include(awardIdList, id) then
  93. return true
  94. end
  95. return false
  96. end
  97. ----------------------------------------
  98. -- 接口
  99. ----------------------------------------
  100. -- 模块信息
  101. function root:itf_get_info(msg)
  102. local lastTime = msg.lastTime or 0
  103. local list = util_mail:user_get_mail_info_list(self.uid, lastTime)
  104. local mailList = {}
  105. if not is_empty(list) then
  106. for _, v in ipairs(list) do
  107. if not self:is_del(v.id) then
  108. local info = {
  109. id = v.id,
  110. title = v.title,
  111. cnt = v.cnt,
  112. ty = v.ty,
  113. createTime = v.createTime,
  114. expireTime = v.expireTime,
  115. status = v.status,
  116. statusItems = self:is_awarded(v.id) and 1 or 0
  117. }
  118. if not is_empty(v.items) then
  119. info.items = cjson_decode(v.items)
  120. end
  121. info.status = self:is_read(v.id) and 1 or 0
  122. table.insert(mailList, info)
  123. end
  124. end
  125. end
  126. local ret = {
  127. mailList = mailList,
  128. sysTime = timeUtil.now(self.uid)
  129. }
  130. return code.OK, ret
  131. end
  132. -- 读取
  133. function root:itf_read(msg)
  134. local idList = msg.idList
  135. if msg == nil or is_empty(idList) then
  136. return code.PARAMTER_ERROR
  137. end
  138. for _, id in ipairs(idList) do
  139. self:add_read_mail(id)
  140. end
  141. return code.OK
  142. end
  143. -- 删除
  144. function root:itf_del(msg)
  145. local idList = msg.idList
  146. if msg == nil or is_empty(idList) then
  147. return code.PARAMTER_ERROR
  148. end
  149. for _, id in ipairs(idList) do
  150. self:add_del_mail(id)
  151. end
  152. return code.OK
  153. end
  154. -- 获取邮件奖励
  155. function root:itf_get_award(msg)
  156. local idList = msg.idList
  157. if msg == nil or is_empty(idList) then
  158. return code.PARAMTER_ERROR
  159. end
  160. local awardIdList = self:redis_get_key_info("awardIdList")
  161. local mailItems = nil
  162. for _, id in ipairs(idList) do
  163. -- 是否已领取
  164. if not self:is_awarded(id) then
  165. local errCode, items = util_mail:user_get_mail_award(self.uid, id)
  166. if not code.is_not_ok(errCode) then
  167. table.insert(awardIdList, id)
  168. if not is_empty(items) then
  169. -- 发放邮件物品
  170. local keyEvent = string.format("mail-award-%s", tostring(id))
  171. bagData:add_items(self.uid, items, keyEvent)
  172. end
  173. mailItems = itemUtil.items_merge(mailItems, items)
  174. end
  175. end
  176. end
  177. self:redis_update_key_info("awardIdList", awardIdList)
  178. return code.OK, {items = mailItems}
  179. end
  180. return root