TG.WXCRM.V4/WeChatServerServices/WxJob/Wx_AliveRedisToOracle.cs

119 lines
3.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using AY.CRM.BLL.Wx;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AY.CRM.BLL.Wx;
using WX.CRM.Model.DTO;
using WX.CRM.Model.Entity;
using DAL.Redis;
using Newtonsoft.Json;
using WeChatServerServices.WeiXin.GenHtml;
using WX.CRM.BLL.Base;
using WX.CRM.BLL.Wx;
using WX.CRM.Common;
using WX.CRM.DAL;
using WX.CRM.DAL.Redis;
using message = WeChatServerServices.Model.message;
using rcontact = WeChatServerServices.Model.rcontact;
using WX.CRM.DataSynFactory.Templates;
using WX.CRM.DataSynFactory;
namespace WeChatServerServices.WxJob
{
public class Wx_AliveRedisToOracle
{
private readonly WX_ALIVEIMEI_BL _aliveImeiBll = new WX_ALIVEIMEI_BL();
private readonly WX_ALIVE_BL _aliveBll = new WX_ALIVE_BL();
private readonly PubSub _sub = new PubSub();
public void Start()
{
SubMessage<alive>("alive", WxAlive);
}
private async void SubMessage<T>(string key, Action<T> action)
{
await SubMessageAsync(key, action);
}
/// <summary>
/// 轮训读取Redis数据Alive并入库
/// </summary>
private async Task SubMessageAsync<T>(string key, Action<T> action)
{
RedisList<alive> redisList = new RedisList<alive>();
WX.CRM.Common.LogHelper.Info("sub");
await _sub.SubscribeAsync("sub:" + key, async (c, v) =>
{
WX.CRM.Common.LogHelper.Info("sub"+key);
alive info = null;
do
{
info = await redisList.RightPopLeftPushAsync(key, key + "bak");
if (info != null)
WxAlive(info);
} while (info != null);
});
}
private void WxAlive(alive info)
{
try
{
WX.CRM.Common.LogHelper.Info("userame:" + info.username);
#region imei
if (!string.IsNullOrWhiteSpace(info.imei))
{
var model = _aliveImeiBll.Get(p => p.IMEI == info.imei);
if (model != null)
{
model.USERNAME = info.username;
model.LASTTIME = DateTime.Parse(info.lasttime);
_aliveImeiBll.Update(model);
}
else
{
model = new WX_ALIVEIMEI()
{
IMEI = info.imei,
USERNAME = info.username,
LASTTIME = DateTime.Parse(info.lasttime)
};
_aliveImeiBll.Add(model);
}
}
#endregion
#region alive
if (!string.IsNullOrWhiteSpace(info.username))
{
var model = _aliveBll.Get(p => p.USERNAME == info.username);
if (model != null)
{
model.LASTTIME = DateTime.Parse(info.lasttime);
_aliveBll.Update(model);
}
else
{
model = new WX_ALIVE
{
USERNAME = info.username,
LASTTIME = DateTime.Parse(info.lasttime)
};
_aliveBll.Add(model);
}
}
#endregion
}
catch (Exception ex)
{
LogHelper.Error(ex.ToString());
}
}
}
}