Files
tianrunCRM/Assets/trCRM/upgradeRes4Dev/priority/lua/db/DBMessage.lua
2020-07-14 22:04:03 +08:00

159 lines
4.0 KiB
Lua

---@class _DBMessage
---@field public CONTENT string
---@field public NOTICETYPE number
---@field public TITLE string
---@field public CREATETIME number
---@field public READFLAG number
---@field public ID number
---@class _DBTaskCust
---@field bookingDone number
---@field custId
---@field companyName
---@field custName
---@field taskId
---@field companyId
---@field phone
---@field dealFlag
---@field bookingDone
---@field loginNo
---@field bookingtime
---@field createtime
---@class _DBReplenish
---@field companyId
---@field productId
---@field wfId
---@field differCount
---@field status
---@field doneTime
---@field startTime
---@field loseTime
---@field loseFlag
---@field note
---@field wfName
---@field productName
---@field loseLimit
---@field avgPrice
DBMessage = {}
local db = {}
local chachePath
DBMessage.MsgType = {
Sys = 2, -- 系统公告
SysNotice = 3, -- 系统通知
Task = 4,
Task4Cust = 5, -- 跟进客户
Task4Support = 6 -- 代办补货
}
DBMessage.ReadFlag = {
unread = 0,
readed = 1
}
DBMessage.BookingDone = {
effect = 0, --
finish = 1,
none = 2
}
DBMessage.init = function()
-- 取得本地缓存的数据
local phone = Prefs.getUserName()
chachePath = Path.Combine(Application.persistentDataPath, "trcrm", phone, "message.chache")
Directory.CreateDirectory(Path.GetDirectoryName(chachePath))
local content = FileEx.ReadAllText(chachePath)
if not isNilOrEmpty(content) then
db = json.decode(content)
else
db.list = {}
db.lastGetTime = {}
end
end
DBMessage.clean = function()
db = {}
db.list = {}
db.lastGetTime = {}
end
DBMessage.onGetMessage = function(type, list)
if not list then
return
end
db.list = db.list or {}
db.list[type] = db.list[type] or {}
db.lastGetTime = db.lastGetTime or {}
if list and #list > 0 then
db.lastGetTime[type] = list[1].CREATETIME -- 更新最后取得的时间
end
---@param v _DBMessage
for i, v in ipairs(list) do
table.insert(db.list[type], v) -- 倒序
end
-- //TODO:保存到本地
end
DBMessage.getMessages = function(type)
return db.list[type] or {}
end
---public 取得最近的一条消息
function DBMessage.getNewestMessage(type)
if type == DBMessage.MsgType.Task then
---@type _DBTaskCust
local cust = DBMessage.getNewestMessage(DBMessage.MsgType.Task4Cust)
---@type _DBReplenish
local replan = DBMessage.getNewestMessage(DBMessage.MsgType.Task4Support)
if cust and replan then
return cust.createtime > replan.startTime and cust or replan
else
return cust and cust or replan
end
return nil
else
local list = db.list[type]
if list then
return list[1]
end
end
return nil
end
function DBMessage.getUnreadNum(type)
local count = 0
if type == DBMessage.MsgType.Sys then
local list = DBMessage.getMessages(type)
---@param v _DBMessage
for i, v in ipairs(list) do
if v.READFLAG == DBMessage.ReadFlag.unread then
count = count + 1
end
end
elseif type == DBMessage.MsgType.Task then
return DBMessage.getUnreadNum(DBMessage.MsgType.Task4Cust) +
DBMessage.getUnreadNum(DBMessage.MsgType.Task4Support)
elseif type == DBMessage.MsgType.Task4Cust then
local list = DBMessage.getMessages(DBMessage.MsgType.Task4Cust)
---@param v _DBTaskCust
for i, v in ipairs(list) do
if v.bookingDone == DBMessage.BookingDone.effect then
count = count + 1
end
end
elseif type == DBMessage.MsgType.Task4Support then
local list = DBMessage.getMessages(DBMessage.MsgType.Task4Cust)
---@param v _DBReplenish
for i, v in ipairs(list) do
if v.status == 0 then
count = count + 1
end
end
end
return count
end
return DBMessage