util_mail.lua 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. --[[
  2. Descripttion:邮件
  3. version:
  4. Author: Neo,Huang
  5. Date: 2022-08-19 20:10:44
  6. LastEditors: Neo,Huang
  7. LastEditTime: 2022-08-19 20:10:56
  8. --]]
  9. local code = require("code")
  10. local util_user = require("utils.util_user")
  11. local nodeMgr = require("nodeMgr")
  12. local lib_game_mysql = require("lib_game_mysql")
  13. local util_global = require("utils.util_global")
  14. local mailAdapt = require("adapt.mailAdapt")
  15. local globalData = require("data.global")
  16. local moduleData = require("data.module")
  17. local root = {}
  18. -- 新增邮件
  19. -- uid:0为全服邮件 >1000000玩家邮件 -1:渠道邮件
  20. -- status: 邮件状态 个位:0:新增 1:已读 2:删除 十位:奖励状态 0:未领取 1:已领取
  21. function root:add_mail(uid, channel, title, cnt, items, ty, createTime, expireTime, src)
  22. if uid == nil or title == nil or is_empty(cnt) then
  23. return false
  24. end
  25. local id = util_global:gen_mail_id()
  26. channel = channel or 0
  27. ty = ty or 1
  28. createTime = createTime or skynet_time()
  29. expireTime = expireTime or (createTime + 7 * 24 * 60 * 60)
  30. src = src or "sys"
  31. -- 邮件物品
  32. if is_empty(items) then
  33. items = {}
  34. end
  35. items = cjson_encode(items)
  36. -- 插入数据库
  37. local values =
  38. string.format(
  39. "id=%s, uid=%s, channel=%s, title='%s', cnt='%s', items='%s', ty=%s, createTime=%s, expireTime=%s, src='%s'",
  40. tostring(id),
  41. tostring(uid),
  42. tostring(channel),
  43. tostring(title),
  44. tostring(cnt),
  45. tostring(items),
  46. tostring(ty),
  47. tostring(createTime),
  48. tostring(expireTime),
  49. tostring(src)
  50. )
  51. local sql = string.format("insert into mdl_mail set %s;", tostring(values))
  52. local ret = lib_game_mysql:query(sql)
  53. if ret.errno and ret.errno > 0 then
  54. log.error("add_mail 新增邮件失败 ret[%s] sql[%s]", tostring(ret), tostring(sql))
  55. return false
  56. end
  57. -- 通知新邮件
  58. if uid == 0 then
  59. -- 全服
  60. nodeMgr.broadcast_game_agent("mail", "add_new_mail", {id = id})
  61. elseif uid == -1 then
  62. -- 渠道
  63. nodeMgr.broadcast_game_agent("mail", "add_new_mail", {id = id, channel = channel})
  64. elseif not is_robot(uid) then
  65. -- 通知玩家 - 新增个人邮件
  66. self:on_new_mail(uid, id)
  67. end
  68. return true
  69. end
  70. -- 获取玩家邮件
  71. function root:user_get_mail_info_list(uid, lastTime)
  72. if uid == nil then
  73. return
  74. end
  75. local currTime = skynet_time()
  76. local channel = moduleData:get_channel(uid)
  77. lastTime = lastTime or 0
  78. -- 全局邮件,个人邮件
  79. local sectionUser =
  80. string.format("((`uid`=%s or `uid`=0) and `expireTime` >= %s)", tostring(uid), tostring(currTime))
  81. -- 渠道邮件
  82. local sectionChannel =
  83. string.format("(`uid`=-1 and `channel` = %s and `expireTime` >= %s)", tostring(channel), tostring(currTime))
  84. local sql =
  85. string.format(
  86. "SELECT * FROM `mdl_mail` WHERE createTime >= %s and (%s or %s);",
  87. lastTime,
  88. sectionUser,
  89. sectionChannel
  90. )
  91. -- log.info("user_get_mail_info_list uid[%s] sql[%s]", tostring(uid), tostring(sql))
  92. local ret = lib_game_mysql:query(sql)
  93. if ret.errno and ret.errno > 0 then
  94. log.error("user_get_mail_info_list 获取玩家邮件列表失败 ret[%s] sql[%s]", tostring(ret), tostring(sql))
  95. return
  96. end
  97. return ret
  98. end
  99. -- 玩家获取邮件物品
  100. function root:user_get_mail_award(uid, id)
  101. if uid == nil or id == nil then
  102. return code.PARAMTER_ERROR
  103. end
  104. -- 获取邮件信息
  105. local sql = string.format("SELECT * FROM `mdl_mail` WHERE id=%s;", tostring(id))
  106. log.info("user_get_mail_award uid[%s] sql[%s]", tostring(uid), tostring(sql))
  107. local ret = lib_game_mysql:query(sql)
  108. if is_empty(ret) or (ret.errno and ret.errno > 0) then
  109. log.error("user_get_mail_award 邮件不存在 ret[%s] sql[%s]", tostring(ret), tostring(sql))
  110. return code.MAIL.NOT_FOUND
  111. end
  112. -- 邮件没有物品
  113. if is_empty(ret[1].items) then
  114. log.error("user_get_mail_award 邮件物品不存在 ret[%s] sql[%s]", tostring(ret), tostring(sql))
  115. return code.MAIL.NOT_MORE_ITEMS
  116. end
  117. local items = cjson_decode(ret[1].items)
  118. return code.OK, items
  119. end
  120. -- 通知玩家 - 新增邮件
  121. function root:on_new_mail(uid, id)
  122. if uid == nil or is_robot(uid) or id == nil then
  123. return false
  124. end
  125. local pack = {id = id}
  126. util_user:user_proto_notify(uid, "on_new_mail", pack)
  127. return true
  128. end
  129. return root