MailManager.lua 5.7 KB

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