MailManager.lua 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. --邮件管理
  2. local LuaMongo = _G.lua_mongo
  3. local Log = require("common.Log")
  4. local DB = require("common.DB")
  5. local Util = require("common.Util")
  6. local Msg = require("core.Msg")
  7. local ObjHuman = require("core.ObjHuman")
  8. local RoleSystemLogic = require("roleSystem.RoleSystemLogic")
  9. local RoleSystemDefine = require("roleSystem.RoleSystemDefine")
  10. local RoleDBLogic = require("role.RoleDBLogic")
  11. local MailExcel = require("excel.mail")
  12. SYSTEM = 1 -- 系统邮件
  13. GONGGAO = 2 -- 公告
  14. MAIL_MAX_CNT = 100 -- 邮件最大值
  15. local FIELD_ID = {_id = nil}
  16. local FIELD_RECEIVER = {uuid = nil,type = nil}
  17. --后面如果fbAccount有值应该把区服id一起
  18. function add(type,receiverUuid,title,content,items,senderName,sender,time,fbTime,fbContent,fbAccount,expireTime)
  19. if _G.is_middle == true or type == GONGGAO then
  20. return
  21. end
  22. local mail = {}
  23. mail.type = type
  24. mail.receiverUuid = receiverUuid
  25. mail.title = title
  26. mail.content = content
  27. mail.read = nil
  28. mail.senderName = title
  29. mail.head = 0
  30. mail.fbTime = fbTime
  31. mail.fbContent = fbContent
  32. --后面用到
  33. local fields = {head = 1,lv = 1}
  34. if fbAccount ~= nil then
  35. local db = RoleDBLogic.getDbByAccount(fbAccount,fields)
  36. mail.fbHead = db.head
  37. mail.fbLv = db.lv
  38. end
  39. if sender and sender.db and sender.db.identity then
  40. mail.identity = sender.db.identity
  41. end
  42. if sender and sender.db and sender.db.head then
  43. mail.head = sender.db.head
  44. end
  45. if time == nil then
  46. mail.time = os.time()
  47. else
  48. mail.time = time
  49. end
  50. if expireTime then
  51. mail.expireTime = expireTime
  52. end
  53. if items then
  54. mail.items = {}
  55. for _,item in ipairs(items) do
  56. mail.items[#mail.items + 1] = {item[1],item[2]}
  57. end
  58. end
  59. if items == nil then
  60. mail.get = 1
  61. mail.flag = nil
  62. else
  63. mail.get = nil
  64. mail.flag = 1
  65. end
  66. LuaMongo.insert(DB.db_mail, mail)
  67. RoleSystemLogic.onDotByUuid(receiverUuid, RoleSystemDefine.ROLE_SYS_ID_204)
  68. return mail
  69. end
  70. -- condition = {startLv = 1, endLv = 100, lastLoginTime = xxx}
  71. local MailLvQuery = {lv = {["$gte"] = 0, ["$lte"] = 0}}
  72. local MailLvField = {lastLoginTime = 1, lastLogoutTime = 1}
  73. function sendMailByCondition(condition, title, content, gridList,fbTime,fbContent,fbAccount,expireTime)
  74. local startLv = condition.startLv and tonumber(condition.startLv)
  75. local endLv = condition.endLv and tonumber(condition.endLv)
  76. local lastLoginTime = condition.lastLoginTime and tonumber(condition.lastLoginTime) or 0
  77. if startLv == nil or endLv == nil or lastLoginTime == nil then
  78. return
  79. end
  80. local successCount = 0
  81. local failCount = 0
  82. --Log.write(Log.LOGID_ERR_PCALL, "mail sendMailByCondition" , "")
  83. -- 在线的只要等级符合直接发
  84. for uuid, oHuman in pairs(ObjHuman.onlineUuid) do
  85. if oHuman.db.lv >= startLv and
  86. oHuman.db.lv <= endLv then
  87. p_ret, err = pcall(add, SYSTEM, oHuman.db._id, title, content, gridList, condition.senderName, nil, nil,fbTime,fbContent,fbAccount,expireTime)
  88. if p_ret then
  89. successCount = successCount + 1
  90. --Log.write(Log.LOGID_ERR_PCALL, "mail rolename=" .. oHuman.db.name, err)
  91. else
  92. failCount = failCount + 1
  93. --Log.write(Log.LOGID_ERR_PCALL, "mail rolename=" .. oHuman.db.name, err)
  94. end
  95. end
  96. end
  97. -- 不在线的,还需要验证x天内登录
  98. MailLvQuery.lv["$gte"] = startLv
  99. MailLvQuery.lv["$lte"] = endLv
  100. LuaMongo.find(DB.db_char, MailLvQuery, MailLvField)
  101. while true do
  102. local data = {}
  103. if not LuaMongo.next(data) then
  104. break
  105. end
  106. if not ObjHuman.onlineUuid[data._id] and
  107. (lastLoginTime == 0 or
  108. (data.lastLoginTime and data.lastLoginTime >= lastLoginTime) or
  109. (data.lastLogoutTime and data.lastLogoutTime >= lastLoginTime)
  110. ) then
  111. p_ret, err = pcall(add, SYSTEM, data._id, title, content, gridList, condition.senderName, nil, nil,fbTime,fbContent,fbAccount)
  112. if p_ret then
  113. successCount = successCount + 1
  114. else
  115. failCount = failCount + 1
  116. Log.write(Log.LOGID_ERR_PCALL, "mail role uuid=" .. data._id, err)
  117. end
  118. end
  119. end
  120. return successCount, failCount
  121. end
  122. function del(mailUuid)
  123. FIELD_ID._id = mailUuid
  124. LuaMongo.remove(DB.db_mail, FIELD_ID)
  125. end
  126. function getMail(mailUuid)
  127. FIELD_ID._id = mailUuid
  128. LuaMongo.find(DB.db_mail,FIELD_ID)
  129. local mail = {}
  130. if not LuaMongo.next(mail) then
  131. return nil
  132. end
  133. return mail
  134. end
  135. local function cmpMail(a, b)
  136. return a.time > b.time
  137. end
  138. local mails = {}
  139. function getMails(receiverUuid,mailType)
  140. for key in ipairs(mails) do
  141. mails[key] = nil
  142. end
  143. FIELD_RECEIVER.receiverUuid = receiverUuid
  144. FIELD_RECEIVER.type = mailType
  145. LuaMongo.find(DB.db_mail,{["$query"]=FIELD_RECEIVER})
  146. local lastTime = os.time() - 7 * 86400
  147. local mailCnt = 0
  148. while true do
  149. local mail = {}
  150. if not LuaMongo.next(mail) then
  151. break
  152. end
  153. if mail.expireTime then -- 有指定过期时间
  154. if mail.time > os.time() - mail.expireTime then
  155. mailCnt = mailCnt + 1
  156. mails[mailCnt] = mail
  157. end
  158. elseif mail.time > lastTime then -- 没有就用默认过期时间
  159. mailCnt = mailCnt + 1
  160. mails[mailCnt] = mail
  161. end
  162. end
  163. if mailCnt > MAIL_MAX_CNT then
  164. table.sort(mails, cmpMail)
  165. for i = MAIL_MAX_CNT + 1, mailCnt do
  166. mails[i] = nil
  167. end
  168. end
  169. return mails
  170. end
  171. function saveMail(mail)
  172. FIELD_ID._id = mail._id
  173. LuaMongo.update(DB.db_mail, FIELD_ID, mail)
  174. end
  175. function delAll(uuid,mailType)
  176. local QueryMailByUuid = {get = {["$exists"] = 0}, read = {["$exists"] = 1}}
  177. QueryMailByUuid.receiverUuid = uuid
  178. QueryMailByUuid.type = mailType
  179. QueryMailByUuid.get["$exists"] = 1
  180. LuaMongo.remove(DB.db_mail, QueryMailByUuid)
  181. end