Browse Source

邮件定时清理

neo 1 year ago
parent
commit
28a6f05f0c
2 changed files with 102 additions and 0 deletions
  1. 19 0
      dev/utils/util_mail.lua
  2. 83 0
      nodes/global/service/mailSrv.lua

+ 19 - 0
dev/utils/util_mail.lua

@@ -129,6 +129,25 @@ function root:user_get_mail_award(uid, id)
129 129
     return code.OK, items
130 130
 end
131 131
 
132
+-- 玩家是否已领取邮件物品
133
+function root:user_is_awarded_mail(uid, id)
134
+    if uid == nil or id == nil then
135
+        return false
136
+    end
137
+    local awardIdList = moduleData:hget_json(uid, "pmail", "awardIdList")
138
+    return table.include(awardIdList, id)
139
+end
140
+-- 玩家新增已领取邮件
141
+function root:user_add_award_mail(uid, id)
142
+    if uid == nil or id == nil then
143
+        return false
144
+    end
145
+    local awardIdList = moduleData:hget_json(uid, "pmail", "awardIdList")
146
+    table.insert(awardIdList, id)
147
+    moduleData:hset(uid, "pmail", "awardIdList", awardIdList)
148
+    return true
149
+end
150
+
132 151
 -- 通知玩家 - 新增邮件
133 152
 function root:on_new_mail(uid, id)
134 153
     if uid == nil or is_robot(uid) or id == nil then

+ 83 - 0
nodes/global/service/mailSrv.lua

@@ -0,0 +1,83 @@
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-20 15:18:25
8
+--]]
9
+local timer = require("timer")
10
+local baseService = require("baseService")
11
+local util_mail = require("utils.util_mail")
12
+local lib_game_mysql = require("lib_game_mysql")
13
+local bagData = require("data.bag")
14
+
15
+local timerDelExpireMail = nil
16
+local timerExpireMail = nil
17
+
18
+local CMD = {}
19
+
20
+-- 清理过期邮件
21
+-- 过期后保留30天 - 永久邮件保留
22
+local function l_del_expire_mail()
23
+	-- 全服/渠道邮件且有过期时间要马上删除
24
+	local currTime = skynet_time()
25
+	local sql =
26
+		string.format(
27
+		"delete from mdl_mail where (uid=0 or uid=-1) and expireTime >0 and expireTime < %s;",
28
+		tostring(currTime)
29
+	)
30
+	local ret = lib_game_mysql:query(sql)
31
+	log.info("l_del_expire_mail sql[%s] ret[%s]", tostring(sql), tostring(ret))
32
+
33
+	-- 删除保留邮件
34
+	local expireTime = currTime - 3600 * 24 * 30
35
+	sql = string.format("delete from mdl_mail where expireTime >0 and expireTime < %s;", tostring(expireTime))
36
+	ret = lib_game_mysql:query(sql)
37
+	log.info("l_del_expire_mail sql[%s] ret[%s]", tostring(sql), tostring(ret))
38
+end
39
+
40
+-- 过期且有物品未领取
41
+local function l_expire_items_mail()
42
+	local currTime = skynet_time()
43
+	local sql =
44
+		string.format(
45
+		"SELECT * FROM `mdl_mail` WHERE expireTime < %s and JSON_LENGTH(items) > 0 limit 100;",
46
+		tostring(currTime)
47
+	)
48
+	log.info("l_expire_items_mail sql[%s]", tostring(sql))
49
+
50
+	local ret = lib_game_mysql:query(sql)
51
+	if ret.errno and ret.errno > 0 then
52
+		log.error("l_expire_items_mail 获取过期物品邮件列表失败 ret[%s] sql[%s]", tostring(ret), tostring(sql))
53
+		return
54
+	end
55
+	if not is_empty(ret) then
56
+		for k, v in ipairs(ret) do
57
+			local id = tonumber(v.id)
58
+			local uid = tonumber(v.uid)
59
+			-- 玩家邮件且未领取
60
+			if not is_robot(uid) and not util_mail:user_is_awarded_mail(uid, id) then
61
+				util_mail:user_add_award_mail(uid, id)
62
+				-- 发放邮件物品
63
+				local items = cjson_decode(v.items)
64
+				local keyEvent = string.format("mail-expire-award-%s", tostring(id))
65
+				bagData:add_items(uid, items, keyEvent)
66
+			end
67
+		end
68
+	end
69
+end
70
+
71
+function CMD.onStart()
72
+	timerDelExpireMail = timer.timeOut(60, l_del_expire_mail)
73
+	timerExpireMail = timer.timeOut(60, l_expire_items_mail)
74
+end
75
+
76
+function CMD.onStop()
77
+	-- 取消定时器
78
+	timerDelExpireMail.func = nil
79
+
80
+	timerExpireMail.func = nil
81
+end
82
+
83
+baseService.start(CMD, ".mailCenter", true)