TG.WXCRM.V4/WebHelper/DoItems/WxHongBaoNotice.cs

91 lines
2.9 KiB
C#

using Ninject;
using System;
using System.Collections.Generic;
using WX.CRM.IBLL.Wx;
using WX.CRM.WebHelper.UtilityModel;
namespace WX.CRM.WebHelper.DoItems
{
public class WxHongBaoNotice : IPopupMessage
{
private readonly IWX_HONGBAO _wxHongbao;
private static volatile WxHongBaoNotice instance = null;
public WxHongBaoNotice()
{
_wxHongbao = Infrastructure.NinjectControllerFactory.ninjectKernel.Get<IWX_HONGBAO>();
}
private static object locked = new Object();
public static WxHongBaoNotice Instance
{
get
{
if (instance == null)
{
lock (locked)
{
if (instance == null)
instance = new WxHongBaoNotice();
}
}
return instance;
}
}
public string GetUrl()
{
return @"../Weixin/WorkAccount/ShowHongBao";//红包提醒
}
private Boolean NeedRefresh = true;
private DateTime LastLoadTime = DateTime.Now;
Dictionary<decimal, UnlineMsg> UserRes = new Dictionary<decimal, UnlineMsg>();
public void SetNeedRefresh()
{
NeedRefresh = true;
}
public string GetMessage(decimal UserId)
{
if (((TimeSpan)(DateTime.Now - LastLoadTime)).TotalMinutes >= 1)
SetNeedRefresh();
if (NeedRefresh)
LoadResource(UserId);
if (!UserRes.ContainsKey(UserId))
return string.Empty;
UnlineMsg ResInfo = UserRes[UserId];
return string.Format(ResInfo.message);
}
private void LoadResource(decimal UserId)
{
lock (this)
{
if (!NeedRefresh)
return;
//重新加载未查看分配资源到UserRes
UserRes.Clear();
//查数据库
//var lists = _allocateRes.UnlineNoticeGet();
var lists = _wxHongbao.GetHonBaoNotice();
foreach (var model in lists)
{
UnlineMsg resinfo = null;
if (!UserRes.ContainsKey(model.inneruserid))
{
resinfo = new UnlineMsg
{
userid = model.inneruserid,
message = model.content
};
UserRes.Add(model.inneruserid, resinfo);
}
else
{
resinfo = UserRes[model.inneruserid];
}
}
//把NeedRefresh置为false
NeedRefresh = false;
LastLoadTime = DateTime.Now;
}
}
}
}