844 lines
40 KiB
C#
844 lines
40 KiB
C#
using AY.CRM.BLL.Wx;
|
||
using DAL.Redis;
|
||
using Newtonsoft.Json;
|
||
using Quartz;
|
||
using System;
|
||
using System.Collections.Concurrent;
|
||
using System.Collections.Generic;
|
||
using System.Data;
|
||
using System.Data.SqlClient;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using WX.CRM.BLL.Base;
|
||
using WX.CRM.BLL.TS;
|
||
using WX.CRM.BLL.Wx;
|
||
using WX.CRM.Common;
|
||
using WX.CRM.DAL;
|
||
using WX.CRM.DAL.Redis;
|
||
using WX.CRM.Model.DTO;
|
||
using WX.CRM.Model.DTO.wework;
|
||
using WX.CRM.Model.Entity;
|
||
using message = WeChatServerServices.Model.message;
|
||
using rcontact = WeChatServerServices.Model.rcontact;
|
||
|
||
namespace WeChatServerServices
|
||
{
|
||
public class Subscribe
|
||
{
|
||
private const string QunfaTableFix = "wx_gen_msqunfa_detail";
|
||
private static ConcurrentBag<string> Bag = new ConcurrentBag<string>();
|
||
private static bool isSucess = false;
|
||
private static string _tableMonth = string.Empty;
|
||
private readonly PubSub _sub = new PubSub();
|
||
//private readonly GenHtml _genHtml = new GenHtml();
|
||
private readonly WX_ALIVEIMEI_BL _aliveImeiBll = new WX_ALIVEIMEI_BL();
|
||
private readonly WX_ALIVE_BL _aliveBll = new WX_ALIVE_BL();
|
||
private readonly WX_LASTMSGTIME_BL _lastmsgtime = new WX_LASTMSGTIME_BL();
|
||
private static string lastUserName = string.Empty;
|
||
private DateTime expireDate = DateTime.SpecifyKind(DateTime.Parse(DateTime.Now.ToShortDateString()), DateTimeKind.Utc).AddDays(1);
|
||
//private DateTime expireDate = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc).AddMinutes(5);
|
||
private readonly WX_COMMUNICATION_DAY_BL _communicationDay = new WX_COMMUNICATION_DAY_BL();
|
||
private readonly SEQUENCES_BL _sequencesBl = new SEQUENCES_BL();
|
||
private readonly WX_TS_BATCHMSG_BL _ts_batchmsg_bl = new WX_TS_BATCHMSG_BL();
|
||
private RedisString<string> _redisMsg = new RedisString<string>();
|
||
public void Start()
|
||
{
|
||
//SubMessage<rcontact>("rcontact1", WxWorkAccount);
|
||
//SubMessage<rcontact>("rcontact0", WxRcontact);
|
||
SubMessage("message");
|
||
//SubMessage<alive>("alive", WxAlive);
|
||
SubMessage("filepath");
|
||
SubMessage("weworkmessage");
|
||
SubMessage("weworkfile");
|
||
//SubMessage("messagefriend");
|
||
//_sub.Subscribe("sub:messagefriend", (c, v) => Wx_GetFriendByMsg.ReadQueue());
|
||
}
|
||
|
||
public async Task JobRun()
|
||
{
|
||
if (isSucess)
|
||
return;
|
||
isSucess = true;
|
||
try
|
||
{
|
||
await ActAsync<message>("message", WxMessage);
|
||
await ActAsync<wxfilepath>("filepath", WxFile);
|
||
await ActAsync<StorageMessage>("weworkmessage", WeWorkMessage);
|
||
await ActAsync<weworkfilepath>("weworkfile", WeWorkFile);
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
LogHelper.Error(string.Concat("WeChatServerServices.Subscribe.JobRun", e.ToString() + e.StackTrace));
|
||
}
|
||
finally
|
||
{
|
||
isSucess = false;
|
||
}
|
||
}
|
||
|
||
private async void SubMessage(string key)
|
||
{
|
||
await SubMessageAsync(key);
|
||
}
|
||
private async Task SubMessageAsync(string key)
|
||
{
|
||
//await _sub.SubscribeAsync("sub:" + key, async (c, v) => await ActAsync(key, action));
|
||
await _sub.SubscribeAsync("sub:" + key, async (c, v) => await JobRun());
|
||
}
|
||
|
||
public async void Act<T>(string key, Action<T> action)
|
||
{
|
||
await ActAsync(key, action);
|
||
}
|
||
|
||
private async Task ActAsync<T>(string key, Action<T> action)
|
||
{
|
||
try
|
||
{
|
||
RedisList<T> redisList = new RedisList<T>();
|
||
var len = await redisList.LlenAsync(key);
|
||
for (var i = 0; i < len; i++)
|
||
{
|
||
//var info = await redisList.RightPopLeftPushAsync(key, key + "bak");
|
||
var info = await redisList.RightPopAsync(key);
|
||
if (info != null)
|
||
action(info);
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.Error(ex.ToString());
|
||
}
|
||
}
|
||
|
||
#region rcontact
|
||
private void WxWorkAccount(rcontact info)
|
||
{
|
||
try
|
||
{
|
||
var workaccountBll = new WX_WORKACCOUNT_BL();
|
||
|
||
WX_WORKACCOUNT model = workaccountBll.Get(p => p.ALIAS == info.alias);
|
||
if (model != null)
|
||
{
|
||
#region
|
||
model.USERNAME = info.username;
|
||
model.CONREMARK = info.conRemark;
|
||
model.DOMAINLIST = info.domainList;
|
||
model.NICKNAME = info.nickname;
|
||
model.PYINITIAL = info.pyInitial;
|
||
model.QUANPIN = info.quanPin;
|
||
model.SHOWHEAD = info.showHead;
|
||
model.TYPE = info.type;
|
||
model.WEIBOFLAG = info.weiboFlag;
|
||
model.WEIBONICKNAME = info.weiboNickname;
|
||
model.CONREMARKPYFULL = info.conRemarkPYFull;
|
||
model.CONREMARKPYSHORT = info.conRemarkPYShort;
|
||
model.LVBUFF = null;
|
||
model.VERFYFLAG = info.verifyFlag;
|
||
model.ENCRYPTUSERNAME = info.encryptUsername;
|
||
model.CHATROOMFLAG = info.chatroomFlag;
|
||
model.DELETEFLAG = info.deleteFlag;
|
||
model.CONTACTLABELIDS = info.contactLabelIds;
|
||
#endregion
|
||
workaccountBll.Update(model);
|
||
}
|
||
else
|
||
{
|
||
#region 如果不存在插入到 WX_WORKACCOUNT_NOTE
|
||
var wxWorkAccount = new WX_WORKACCOUNT_NOTE()
|
||
{
|
||
PKID = new SEQUENCES_BL().Seq_base_get(),
|
||
USERNAME = info.username,
|
||
ALIAS = info.alias,
|
||
CONREMARK = info.conRemark,
|
||
DOMAINLIST = info.domainList,
|
||
NICKNAME = info.nickname,
|
||
PYINITIAL = info.pyInitial,
|
||
QUANPIN = info.quanPin,
|
||
SHOWHEAD = info.showHead,
|
||
TYPE = info.type,
|
||
WEIBOFLAG = info.weiboFlag,
|
||
WEIBONICKNAME = info.weiboNickname,
|
||
CONREMARKPYFULL = info.conRemarkPYFull,
|
||
CONREMARKPYSHORT = info.conRemarkPYShort,
|
||
LVBUFF = null,
|
||
VERFYFLAG = info.verifyFlag,
|
||
ENCRYPTUSERNAME = info.encryptUsername,
|
||
CHATROOMFLAG = info.chatroomFlag,
|
||
DELETEFLAG = info.deleteFlag,
|
||
CONTACTLABELIDS = info.contactLabelIds,
|
||
CTIME = DateTime.Now
|
||
};
|
||
new WX_WORKACCOUNT_NOTE_BL().Add(wxWorkAccount);
|
||
|
||
#endregion
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.Error(ex.ToString());
|
||
}
|
||
}
|
||
|
||
private void WxRcontact(rcontact info)
|
||
{
|
||
try
|
||
{
|
||
var rcontactBll = new WX_RCONTACT_BL();
|
||
var rcontact = rcontactBll.Get(p => p.JOBWXUSERNAME == info.jobwxusername && p.USERNAME == info.username);
|
||
if (rcontact != null)
|
||
{
|
||
#region
|
||
rcontact.JOBWXUSERNAME = info.jobwxusername;
|
||
rcontact.USERNAME = info.username;
|
||
rcontact.ALIAS = info.alias;
|
||
rcontact.CONREMARK = info.conRemark;
|
||
rcontact.DOMAINLIST = info.domainList;
|
||
rcontact.NICKNAME = info.nickname;
|
||
rcontact.PYINITIAL = info.pyInitial;
|
||
rcontact.QUANPIN = info.quanPin;
|
||
rcontact.SHOWHEAD = info.showHead;
|
||
rcontact.TYPE = info.type;
|
||
rcontact.WEIBOFLAG = info.weiboFlag;
|
||
rcontact.WEIBONICKNAME = info.weiboNickname;
|
||
rcontact.CONREMARKPYFULL = info.conRemarkPYFull;
|
||
rcontact.CONREMARKPYSHORT = info.conRemarkPYShort;
|
||
rcontact.LVBUFF = null;
|
||
rcontact.VERFYFLAG = info.verifyFlag;
|
||
rcontact.ENCRYPTUSERNAME = info.encryptUsername;
|
||
rcontact.CHATROOMFLAG = info.chatroomFlag;
|
||
rcontact.DELETEFLAG = info.deleteFlag;
|
||
rcontact.CONTACTLABELIDS = info.contactLabelIds;
|
||
#endregion
|
||
if (info.isToBeAddFriends != null && info.isToBeAddFriends.Value == 1)
|
||
rcontact.ISTOBEADDFRIENDS = info.isToBeAddFriends;
|
||
rcontactBll.Update(rcontact);
|
||
}
|
||
else
|
||
{
|
||
DateTime time = DateTime.Now;
|
||
var wxRContact = new WX_RCONTACT()
|
||
{
|
||
#region
|
||
PKID = new SEQUENCES_BL().Seq_base_get(),
|
||
JOBWXUSERNAME = info.jobwxusername,
|
||
USERNAME = info.username,
|
||
ALIAS = info.alias,
|
||
CONREMARK = info.conRemark,
|
||
DOMAINLIST = info.domainList,
|
||
NICKNAME = info.nickname,
|
||
PYINITIAL = info.pyInitial,
|
||
QUANPIN = info.quanPin,
|
||
SHOWHEAD = info.showHead,
|
||
TYPE = info.type,
|
||
WEIBOFLAG = info.weiboFlag,
|
||
WEIBONICKNAME = info.weiboNickname,
|
||
CONREMARKPYFULL = info.conRemarkPYFull,
|
||
CONREMARKPYSHORT = info.conRemarkPYShort,
|
||
LVBUFF = null,
|
||
VERFYFLAG = info.verifyFlag,
|
||
ENCRYPTUSERNAME = info.encryptUsername,
|
||
CHATROOMFLAG = info.chatroomFlag,
|
||
DELETEFLAG = info.deleteFlag,
|
||
CONTACTLABELIDS = info.contactLabelIds,
|
||
CTIME = time,
|
||
RESOURCETAG = null
|
||
#endregion
|
||
};
|
||
if (info.isToBeAddFriends != null && info.isToBeAddFriends.Value == 1)
|
||
wxRContact.ISTOBEADDFRIENDS = info.isToBeAddFriends;
|
||
rcontactBll.Add(wxRContact);
|
||
#region 数据同步
|
||
//try
|
||
//{
|
||
// var workaccountBll = new WX_WORKACCOUNT_BL();
|
||
// var model = workaccountBll.Get(p => p.USERNAME == info.jobwxusername);
|
||
// if (model != null)//能找到客户才插入
|
||
// {
|
||
// web_push_updateUser user = new web_push_updateUser()
|
||
// {
|
||
// Service = model.ALIAS,
|
||
// Userid = info.username,
|
||
// cTime = time
|
||
// };
|
||
// new PushDataServices().SendData<web_push_updateUser>(user, "ZQZX", "web_push_updateUser");
|
||
// }
|
||
//}
|
||
//catch (Exception ex)
|
||
//{
|
||
// LogHelper.Error("数据同步错误:" + ex);
|
||
//}
|
||
#endregion
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.Error(ex.ToString());
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region message
|
||
private void WxMessage(message info)
|
||
{
|
||
try
|
||
{
|
||
//LogHelper.Info("message:" + JsonConvert.SerializeObject(info));
|
||
var tableName = GetTableName();
|
||
//LogHelper.Info("tableName:" + tableName);
|
||
if (string.IsNullOrEmpty(tableName))
|
||
{
|
||
LogHelper.Error("表名为空!");
|
||
return;
|
||
}
|
||
if (info.msgType != "2")
|
||
{
|
||
//AddGenMessage(info);
|
||
AddMessage(info, tableName);
|
||
AddBlackFriend(info);
|
||
}
|
||
else
|
||
{
|
||
AddQunFaMessage(info);//添加群发消息
|
||
AddQunFaMessageDetail(info);//添加群发消息明细
|
||
}
|
||
AddMsgToDay(info);
|
||
AddOrUpdateLastMsgTime(info);
|
||
|
||
//生成html
|
||
//Thread thread = new Thread(() => _genHtml.GenHtmlFile(info));
|
||
//thread.Start();
|
||
//ThreadPool.QueueUserWorkItem(state => { _genHtml.GenHtmlFile(info); });
|
||
//new GenHtml().GenHtmlFile(info);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
new RedisList<message>().LeftPush("messageerr", info);
|
||
LogHelper.Error(ex.ToString());
|
||
}
|
||
}
|
||
private void WeWorkMessage(StorageMessage info)
|
||
{
|
||
try
|
||
{
|
||
var sql = @"INSERT INTO [dbo].[wework_message]
|
||
([id]
|
||
,[receiver]
|
||
,[sendTime]
|
||
,[sender]
|
||
,[sendername]
|
||
,[conversationId]
|
||
,[convType]
|
||
,[contentType]
|
||
,[content]
|
||
,[appinfo]
|
||
,[fileMd5]
|
||
,[vid],[createTime],[url],[smallMd5],[maxMd5])
|
||
VALUES
|
||
( @id
|
||
,@receiver
|
||
, @sendTime
|
||
, @sender
|
||
, @sendername
|
||
, @conversationId
|
||
, @convType
|
||
, @contentType
|
||
, @content
|
||
, @appinfo
|
||
, @fileMd5
|
||
, @vid,@createTime,@url,@smallMd5,@maxMd5)";
|
||
|
||
#region
|
||
var para = new List<SqlParameter>
|
||
{
|
||
new SqlParameter("@id", info.id.ToString()),
|
||
new SqlParameter("@receiver", info.receiver.ToString()),
|
||
new SqlParameter("@sendTime",DateTimeTool.GetTimeFromLinuxShortTime(info.sendTime)),
|
||
new SqlParameter("@sender",info.sender.ToString()),
|
||
new SqlParameter("@sendername",info.sendername),
|
||
new SqlParameter("@conversationId",info.conversationId.ToString()),
|
||
new SqlParameter("@convType",info.convType),
|
||
new SqlParameter("@contentType",info.contentType),
|
||
!string.IsNullOrEmpty(info.content) ? new SqlParameter("@content", info.content) : new SqlParameter("@content", DBNull.Value),
|
||
!string.IsNullOrEmpty(info.appinfo) ? new SqlParameter("@appinfo", info.appinfo) : new SqlParameter("@appinfo", DBNull.Value),
|
||
!string.IsNullOrEmpty(info.fileMd5) ? new SqlParameter("@fileMd5", info.fileMd5) : new SqlParameter("@fileMd5", DBNull.Value),
|
||
new SqlParameter("@vid",info.vid),
|
||
new SqlParameter("@createTime",info.sendTime),
|
||
!string.IsNullOrEmpty(info.url) ? new SqlParameter("@url", info.url) : new SqlParameter("@url", DBNull.Value),
|
||
!string.IsNullOrEmpty(info.smallMd5) ? new SqlParameter("@smallMd5", info.smallMd5) : new SqlParameter("@smallMd5", DBNull.Value),
|
||
!string.IsNullOrEmpty(info.maxMd5) ? new SqlParameter("@maxMd5", info.maxMd5) : new SqlParameter("@maxMd5", DBNull.Value),
|
||
|
||
};
|
||
#endregion
|
||
SqlHelper.ExcuteSPOrSql(SqlHelper.DatabaseType.WeWork, sql, CommandType.Text, para.ToArray());
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
new RedisList<StorageMessage>().LeftPush("workmsgerro", info);
|
||
LogHelper.Error("报错JSON:" + Newtonsoft.Json.JsonConvert.SerializeObject(info));
|
||
LogHelper.Error("AddWeWorkMsg:" + ex.ToString());
|
||
|
||
}
|
||
}
|
||
private void AddBlackFriend(message info)
|
||
{
|
||
try
|
||
{
|
||
if (info.type == 10000 && (info.content.Contains("<a href=\"weixin://findfriend/verifycontact\">发送朋友验证</a>") || info.content.Contains("消息已发出,但被对方拒收了。")))
|
||
{
|
||
_ts_batchmsg_bl.AddBlackFriend(info.username, info.talker);//检测到拉黑或者删除
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.Error(ex.ToString());
|
||
}
|
||
}
|
||
private void AddQunFaMessage(message info)
|
||
{
|
||
try
|
||
{
|
||
var sql = "insert into wx_gen_msqunfa([clientid],[filename],[tolist],[createtime],[msgtype],[jobusername],[cTime])" +
|
||
" values(@clientid, @filename, @tolist, @createtime, @msgtype, @jobusername, @cTime)";
|
||
|
||
#region
|
||
var para = new List<SqlParameter>
|
||
{
|
||
new SqlParameter("@cTime", DateTime.Now),
|
||
info.msgSvrId.HasValue ? new SqlParameter("@clientid", info.msgSvrId) : new SqlParameter("@clientid", DBNull.Value),
|
||
!string.IsNullOrEmpty(info.content) ? new SqlParameter("@filename", info.content) : new SqlParameter("@filename", DBNull.Value),
|
||
info.createTime.HasValue ? new SqlParameter("@createtime", info.createTime) : new SqlParameter("@createtime", DBNull.Value),
|
||
!string.IsNullOrEmpty(info.talker) ? new SqlParameter("@tolist", info.talker) : new SqlParameter("@tolist", DBNull.Value),
|
||
info.type.HasValue? new SqlParameter("@msgtype", info.type) : new SqlParameter("@msgtype", DBNull.Value),
|
||
!string.IsNullOrEmpty(info.username) ? new SqlParameter("@jobusername", info.username) : new SqlParameter("@jobusername", DBNull.Value),
|
||
|
||
};
|
||
#endregion
|
||
SqlHelper.ExcuteSPOrSql(SqlHelper.DatabaseType.AYCRM, sql, CommandType.Text, para.ToArray());
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.Error("AddGenMessage:" + ex.ToString());
|
||
throw;
|
||
}
|
||
}
|
||
private void AddQunFaMessageDetail(message info)
|
||
{
|
||
try
|
||
{
|
||
var createTime = DateTimeTool.GetTimeFromLinuxTime(info.createTime.Value);
|
||
var yearMonth = createTime.ToString("yyyyMM");
|
||
var day = createTime.Day;
|
||
var tableName = GetMsgQunFaTable(yearMonth, day);
|
||
if (string.IsNullOrEmpty(tableName))
|
||
{
|
||
LogHelper.Info("数据:" + JsonConvert.SerializeObject(info));
|
||
throw new Exception("数据库表名获取出错!");
|
||
}
|
||
var sql = new StringBuilder();
|
||
sql.AppendFormat("INSERT INTO [dbo].[{0}]([clientid],[username])VALUES", tableName);
|
||
var para = new List<SqlParameter>();
|
||
var i = 0;
|
||
foreach (var item in info.talker.Split(';'))
|
||
{
|
||
sql.AppendFormat("(@clientid{0},@username{0}),", i);
|
||
para.Add(new SqlParameter(string.Format("@clientid{0}", i), info.msgSvrId));
|
||
para.Add(new SqlParameter(string.Format("@username{0}", i), item));
|
||
i++;
|
||
}
|
||
var exeSql = sql.ToString().Trim(',');
|
||
SqlHelper.ExcuteSPOrSql(SqlHelper.DatabaseType.AYCRM, exeSql, CommandType.Text, para.ToArray());
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.Error("AddQunFaMessageDetail:" + ex.ToString());
|
||
throw;
|
||
}
|
||
}
|
||
private void AddMessage(message info, string tableName)
|
||
{
|
||
using (SqlConnection conn = new SqlConnection(SqlHelper.GetConnecionString(SqlHelper.DatabaseType.AYCRM)))
|
||
{
|
||
conn.Open();
|
||
using (SqlTransaction trans = conn.BeginTransaction())
|
||
{
|
||
try
|
||
{
|
||
var sql = "insert into " + tableName + "(msgid, msgsvrid, status, issend, isshowtimer, createtime,talker, imgpath, reserved, lvbuffer, transcontent, transbrandwording, talkerid, bizclientmsgid, bizchatid, bizchatuserid, msgseq, flag, username, nickname, yunstatus, upstatus, ctime, msgtype, msgcontent, msgurl, filepath)" +
|
||
" values(@msgid,@msgsvrid,@status,@issend,@isshowtimer,@createtime,@talker,@imgpath,@reserved,@lvbuffer,@transcontent,@transbrandwording,@talkerid,@bizclientmsgid,@bizchatid,@bizchatuserid,@msgseq,@flag,@username,@nickname,@yunstatus,@upstatus,@ctime,@msgtype,@msgcontent,@msgurl,@filepath)";
|
||
#region
|
||
|
||
var para = new List<SqlParameter>
|
||
{
|
||
new SqlParameter("@yunstatus", info.yunstatus),
|
||
new SqlParameter("@upstatus", info.upstatus),
|
||
new SqlParameter("@ctime", DateTime.Now),
|
||
new SqlParameter("@lvbuffer", DBNull.Value),
|
||
info.msgId > 0 ? new SqlParameter("@msgid", info.msgId) : new SqlParameter("@msgid", DBNull.Value),
|
||
info.msgSvrId.HasValue ? new SqlParameter("@msgsvrid", info.msgSvrId) : new SqlParameter("@msgsvrid", DBNull.Value),
|
||
info.status.HasValue ? new SqlParameter("@status", info.status) : new SqlParameter("@status", DBNull.Value),
|
||
info.isSend.HasValue ? new SqlParameter("@issend", info.isSend) : new SqlParameter("@issend", DBNull.Value),
|
||
info.isShowTimer.HasValue ? new SqlParameter("@isshowtimer", info.isShowTimer) : new SqlParameter("@isshowtimer", DBNull.Value),
|
||
info.createTime.HasValue ? new SqlParameter("@createtime", info.createTime) : new SqlParameter("@createtime", DBNull.Value),
|
||
!string.IsNullOrEmpty(info.talker) ? new SqlParameter("@talker", info.talker) : new SqlParameter("@talker", DBNull.Value),
|
||
!string.IsNullOrEmpty(info.imgPath) ? new SqlParameter("@imgpath", info.imgPath) : new SqlParameter("@imgpath", DBNull.Value),
|
||
!string.IsNullOrEmpty(info.reserved) ? new SqlParameter("@reserved", info.reserved) : new SqlParameter("@reserved", DBNull.Value),
|
||
!string.IsNullOrEmpty(info.transContent) ? new SqlParameter("@transcontent", info.transContent) : new SqlParameter("@transcontent", DBNull.Value),
|
||
!string.IsNullOrEmpty(info.transBrandWording) ? new SqlParameter("@transbrandwording", info.transBrandWording) : new SqlParameter("@transbrandwording", DBNull.Value),
|
||
info.talkerId.HasValue ? new SqlParameter("@talkerid", info.talkerId) : new SqlParameter("@talkerid", DBNull.Value),
|
||
!string.IsNullOrEmpty(info.bizClientMsgId) ? new SqlParameter("@bizclientmsgid", info.bizClientMsgId) : new SqlParameter("@bizclientmsgid", DBNull.Value),
|
||
info.bizChatId.HasValue ? new SqlParameter("@bizchatid", info.bizChatId) : new SqlParameter("@bizchatid", DBNull.Value),
|
||
!string.IsNullOrEmpty(info.bizChatUserId) ? new SqlParameter("@bizchatuserid", info.bizChatUserId) : new SqlParameter("@bizchatuserid", DBNull.Value),
|
||
info.msgSeq.HasValue ? new SqlParameter("@msgseq", info.msgSeq) : new SqlParameter("@msgseq", DBNull.Value),
|
||
info.flag.HasValue ? new SqlParameter("@flag", info.flag) : new SqlParameter("@flag", DBNull.Value),
|
||
!string.IsNullOrEmpty(info.username) ? new SqlParameter("@username", info.username) : new SqlParameter("@username", DBNull.Value),
|
||
!string.IsNullOrEmpty(info.nickname) ? new SqlParameter("@nickname", info.nickname) : new SqlParameter("@nickname", DBNull.Value),
|
||
info.type.HasValue ? new SqlParameter("@msgtype", info.type) : new SqlParameter("@msgtype", DBNull.Value),
|
||
!string.IsNullOrEmpty(info.content) ? new SqlParameter("@msgcontent", info.content) : new SqlParameter("@msgcontent", DBNull.Value),
|
||
!string.IsNullOrEmpty(info.url) ? new SqlParameter("@msgurl", info.url) : new SqlParameter("@msgurl", DBNull.Value),
|
||
!string.IsNullOrEmpty(info.filepath) ? new SqlParameter("@filepath", info.filepath) : new SqlParameter("@filepath", DBNull.Value)
|
||
};
|
||
#endregion
|
||
//SqlHelper.ExcuteSPOrSql(SqlHelper.DatabaseType.AYCRM, sql, CommandType.Text, para.ToArray());
|
||
SqlHelper.ExecuteSql(conn, trans, sql, para.ToArray());
|
||
|
||
var sql2 = "insert into wx_gen_message([msgsvrid], [type], [issend], [createtime], [talker], [content], [username], [nickname], [ctime], [url],[MsgType])" +
|
||
" values(@msgsvrid, @type, @issend, @createtime, @talker, @content, @username, @nickname, @ctime, @url,@MsgType)";
|
||
#region
|
||
int type = 0;
|
||
if (info.talker.EndsWith("@chatroom"))
|
||
{
|
||
type = 1;
|
||
}
|
||
var para2 = new List<SqlParameter>
|
||
{
|
||
new SqlParameter("@ctime", DateTime.Now),
|
||
info.msgSvrId.HasValue ? new SqlParameter("@msgsvrid", info.msgSvrId) : new SqlParameter("@msgsvrid", DBNull.Value),
|
||
info.isSend.HasValue ? new SqlParameter("@issend", info.isSend) : new SqlParameter("@issend", DBNull.Value),
|
||
info.createTime.HasValue ? new SqlParameter("@createtime", info.createTime) : new SqlParameter("@createtime", DBNull.Value),
|
||
!string.IsNullOrEmpty(info.talker) ? new SqlParameter("@talker", info.talker) : new SqlParameter("@talker", DBNull.Value),
|
||
!string.IsNullOrEmpty(info.username) ? new SqlParameter("@username", info.username) : new SqlParameter("@username", DBNull.Value),
|
||
!string.IsNullOrEmpty(info.nickname) ? new SqlParameter("@nickname", info.nickname) : new SqlParameter("@nickname", DBNull.Value),
|
||
info.type.HasValue ? new SqlParameter("@type", info.type) : new SqlParameter("@type", DBNull.Value),
|
||
!string.IsNullOrEmpty(info.content) ? new SqlParameter("@content", info.content) : new SqlParameter("@content", DBNull.Value),
|
||
!string.IsNullOrEmpty(info.url) ? new SqlParameter("@url", info.url) : new SqlParameter("@url", DBNull.Value),
|
||
new SqlParameter("@MsgType", type)
|
||
};
|
||
#endregion
|
||
//SqlHelper.ExcuteSPOrSql(SqlHelper.DatabaseType.AYCRM, sql, CommandType.Text, para.ToArray());
|
||
SqlHelper.ExecuteSql(conn, trans, sql2, para2.ToArray());
|
||
|
||
trans.Commit();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
//wxmsgid:4901726262668658552|wxid_q8bje4635svf12
|
||
LogHelper.Error("出错message--- wxmsgid:" + info.msgSvrId + "|" + info.username + " ------------" + ex.ToString());
|
||
trans.Rollback();
|
||
var key = "wxmsgid:" + info.msgSvrId + "|" + info.username;
|
||
_redisMsg.Del(key);
|
||
throw;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
private void AddGenMessage(message info)
|
||
{
|
||
try
|
||
{
|
||
var sql = "insert into wx_gen_message([msgsvrid], [type], [issend], [createtime], [talker], [content], [username], [nickname], [ctime], [url])" +
|
||
" values(@msgsvrid, @type, @issend, @createtime, @talker, @content, @username, @nickname, @ctime, @url)";
|
||
#region
|
||
var para = new List<SqlParameter>
|
||
{
|
||
new SqlParameter("@ctime", DateTime.Now),
|
||
info.msgSvrId.HasValue ? new SqlParameter("@msgsvrid", info.msgSvrId) : new SqlParameter("@msgsvrid", DBNull.Value),
|
||
info.isSend.HasValue ? new SqlParameter("@issend", info.isSend) : new SqlParameter("@issend", DBNull.Value),
|
||
info.createTime.HasValue ? new SqlParameter("@createtime", info.createTime) : new SqlParameter("@createtime", DBNull.Value),
|
||
!string.IsNullOrEmpty(info.talker) ? new SqlParameter("@talker", info.talker) : new SqlParameter("@talker", DBNull.Value),
|
||
!string.IsNullOrEmpty(info.username) ? new SqlParameter("@username", info.username) : new SqlParameter("@username", DBNull.Value),
|
||
!string.IsNullOrEmpty(info.nickname) ? new SqlParameter("@nickname", info.nickname) : new SqlParameter("@nickname", DBNull.Value),
|
||
info.type.HasValue ? new SqlParameter("@type", info.type) : new SqlParameter("@type", DBNull.Value),
|
||
!string.IsNullOrEmpty(info.content) ? new SqlParameter("@content", info.content) : new SqlParameter("@content", DBNull.Value),
|
||
!string.IsNullOrEmpty(info.url) ? new SqlParameter("@url", info.url) : new SqlParameter("@url", DBNull.Value)
|
||
};
|
||
#endregion
|
||
SqlHelper.ExcuteSPOrSql(SqlHelper.DatabaseType.AYCRM, sql, CommandType.Text, para.ToArray());
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.Error("出错AddGenMessage:" + info.msgSvrId + ", msgType:" + info.msgType + ",talker:" + info.talker + "," + ex.ToString());
|
||
throw;
|
||
}
|
||
}
|
||
|
||
private void AddOrUpdateLastMsgTime(message info)
|
||
{
|
||
try
|
||
{
|
||
if (!string.IsNullOrWhiteSpace(info.username))
|
||
{
|
||
if (lastUserName != info.username)
|
||
{
|
||
var model = _lastmsgtime.Get(p => p.USERNAME == info.username);
|
||
if (model != null)
|
||
{
|
||
model.LASTTIME = DateTime.Now;
|
||
_lastmsgtime.Update(model);
|
||
}
|
||
else
|
||
{
|
||
model = new WX_LASTMSGTIME() { USERNAME = info.username, LASTTIME = DateTime.Now };
|
||
_lastmsgtime.Add(model);
|
||
}
|
||
|
||
lastUserName = info.username;
|
||
}
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.Error("AddOrUpdateLastMsgTime:" + ex.ToString());
|
||
}
|
||
}
|
||
|
||
private void AddMsgToDay(message info)
|
||
{
|
||
try
|
||
{
|
||
if (info.msgType != "2")
|
||
{
|
||
ExeMsgToDay(info.username, info.talker);
|
||
}
|
||
else
|
||
{
|
||
foreach (var item in info.talker.Split(';'))
|
||
{
|
||
ExeMsgToDay(info.username, item);
|
||
}
|
||
}
|
||
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.Error("AddMsgToDay:" + ex.ToString());
|
||
}
|
||
}
|
||
|
||
private void ExeMsgToDay(string username, string talker)
|
||
{
|
||
RedisString<string> redis = new RedisString<string>("MsgToDay:" + username + "|" + talker);
|
||
var exists = redis.Exists();
|
||
//LogHelper.Info("exists:" + exists.ToString());
|
||
if (!exists)
|
||
{
|
||
//LogHelper.Info("数据进入:username:" + username + "|" + talker);
|
||
//LogHelper.Info("expireDate:" + expireDate.ToString());
|
||
var timeSpan = expireDate - DateTime.Now;
|
||
//LogHelper.Info("timeSpan:" + timeSpan.TotalSeconds);
|
||
var a = redis.Set(string.Empty);
|
||
var b = redis.Expire(timeSpan);
|
||
//LogHelper.Info("a:" + a.ToString() + ",b:" + b.ToString());
|
||
if (a && b)
|
||
{
|
||
var isExists = _communicationDay.Exists(p => p.JOBUSERNAME == username && p.USERNAME == talker);
|
||
if (!isExists)
|
||
{
|
||
var model = new WX_COMMUNICATION_DAY
|
||
{
|
||
PKID = _sequencesBl.Seq_base_get(),
|
||
JOBUSERNAME = username,
|
||
USERNAME = talker,
|
||
DAY1 = 1,
|
||
CTIME = DateTime.Now
|
||
};
|
||
_communicationDay.Add(model);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
private string GetTableName()
|
||
{
|
||
var tableFix = "wx_message";
|
||
var yearMonth = DateTime.Now.ToString("yyyyMM");
|
||
if (yearMonth.Equals(_tableMonth))
|
||
{
|
||
return tableFix + _tableMonth;
|
||
}
|
||
lock (this)
|
||
{
|
||
//调用过程创建表
|
||
var result = DoCreateTable();
|
||
if (result)
|
||
_tableMonth = yearMonth;
|
||
else
|
||
LogHelper.Error("数据库创建有误");
|
||
}
|
||
return tableFix + _tableMonth;
|
||
//return "wx_message201612";
|
||
}
|
||
|
||
private string GetMsgQunFaTable(string yearMonth, int day)
|
||
{
|
||
var tag = day > 15 ? "_1" : "_0";
|
||
var tableName = QunfaTableFix + yearMonth + tag;
|
||
if (Bag.Any(p => p == tableName))
|
||
{
|
||
return tableName;
|
||
}
|
||
lock (this)
|
||
{
|
||
//调用过程创建表
|
||
var result = DoCreateMsgQunFaTable(tableName);
|
||
if (result)
|
||
Bag.Add(tableName);
|
||
else
|
||
{
|
||
LogHelper.Error("数据库创建有误");
|
||
return string.Empty;
|
||
}
|
||
}
|
||
return tableName;
|
||
}
|
||
|
||
private bool DoCreateTable()
|
||
{
|
||
var sql = "CreateMessageTable";
|
||
var para = new List<SqlParameter>()
|
||
{
|
||
new SqlParameter("@result", SqlDbType.VarChar, 0, ParameterDirection.ReturnValue, false, 0, 0, string.Empty, DataRowVersion.Default, null)
|
||
};
|
||
SqlHelper.ExcuteSPOrSql(SqlHelper.DatabaseType.AYCRM, sql, CommandType.StoredProcedure, para.ToArray());
|
||
var result = int.Parse(para[0].Value.ToString());
|
||
return result > 0;
|
||
}
|
||
|
||
private bool DoCreateMsgQunFaTable(string tableName)
|
||
{
|
||
var sql = "CreateMsgQunFaTable";
|
||
var para = new List<SqlParameter>()
|
||
{
|
||
new SqlParameter() {ParameterName = "@tableName", SqlDbType = SqlDbType.VarChar, Value = tableName},
|
||
new SqlParameter("@result", SqlDbType.VarChar, 0, ParameterDirection.ReturnValue, false, 0, 0, string.Empty, DataRowVersion.Default, null)
|
||
};
|
||
SqlHelper.ExcuteSPOrSql(SqlHelper.DatabaseType.AYCRM, sql, CommandType.StoredProcedure, para.ToArray());
|
||
var result = int.Parse(para[1].Value.ToString());
|
||
return result > 0;
|
||
}
|
||
#endregion
|
||
|
||
|
||
|
||
#region file
|
||
|
||
private void WxFile(wxfilepath info)
|
||
{
|
||
try
|
||
{
|
||
var sql = "insert into wx_filepath(FileName,FileUrl,CTime,MsgSvrId,Md5) values(@FileName,@FileUrl,@CTime,@MsgSvrId,@Md5)";
|
||
string md5 = string.Empty;
|
||
int ganindex = info.FileUrl.LastIndexOf("/");
|
||
if (info.FileUrl.IndexOf(".") == -1)
|
||
{
|
||
md5 = info.FileUrl.Substring(ganindex + 1, info.FileUrl.Length - ganindex - 1);
|
||
}
|
||
else
|
||
{
|
||
md5 = info.FileUrl.Substring(ganindex + 1, info.FileUrl.LastIndexOf(".") - ganindex - 1);
|
||
}
|
||
|
||
var para = new List<SqlParameter>
|
||
{
|
||
new SqlParameter("@FileName", info.FileName),
|
||
new SqlParameter("@FileUrl", info.FileUrl),
|
||
new SqlParameter("@CTime", info.CTime),
|
||
new SqlParameter("@MsgSvrId",info.MsgSvrId),
|
||
new SqlParameter("@Md5",md5),
|
||
};
|
||
SqlHelper.ExcuteSPOrSql(SqlHelper.DatabaseType.AYCRM, sql, CommandType.Text, para.ToArray());
|
||
AddMd5(md5, info.FileUrl);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
new RedisList<wxfilepath>().LeftPush("filepatherr", info);
|
||
LogHelper.Error(ex.ToString());
|
||
}
|
||
}
|
||
private bool AddMd5(string md5, string fileurl)
|
||
{
|
||
bool isok = false;
|
||
|
||
try
|
||
{
|
||
|
||
#region
|
||
|
||
var para = new List<SqlParameter>
|
||
{
|
||
new SqlParameter("@md5", md5),
|
||
new SqlParameter("@fileUrl", fileurl)
|
||
};
|
||
|
||
#endregion
|
||
SqlHelper.ExcuteSPOrSql(SqlHelper.DatabaseType.AYCRM, "HasOrAddFileMd5", CommandType.StoredProcedure, para.ToArray());
|
||
isok = true;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.Error(ex.ToString());
|
||
}
|
||
return isok;
|
||
}
|
||
#endregion
|
||
#region file
|
||
|
||
private void WeWorkFile(weworkfilepath info)
|
||
{
|
||
try
|
||
{
|
||
var sql = "insert into wework_file([keymd5],[path],[type]) values(@keymd5,@path,@type)";
|
||
var para = new List<SqlParameter>
|
||
{
|
||
new SqlParameter("@keymd5", info.keymd5),
|
||
new SqlParameter("@path", info.path),
|
||
new SqlParameter("@type", info.type),
|
||
};
|
||
SqlHelper.ExcuteSPOrSql(SqlHelper.DatabaseType.WeWork, sql, CommandType.Text, para.ToArray());
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
new RedisList<weworkfilepath>().LeftPush("filepatherr", info);
|
||
LogHelper.Error(ex.ToString());
|
||
}
|
||
}
|
||
#endregion
|
||
}
|
||
|
||
public class SubscribeJob : IJob
|
||
{
|
||
//private static bool isSucess = false;
|
||
public async void Execute(JobExecutionContext context)
|
||
{
|
||
//if (isSucess)
|
||
// return;
|
||
//isSucess = true;
|
||
try
|
||
{
|
||
await new Subscribe().JobRun();
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
LogHelper.Error(string.Concat("WeChatServerServices.WxJob.WxMessageJob.Execute ", e.ToString() + e.StackTrace));
|
||
}
|
||
finally
|
||
{
|
||
//isSucess = false;
|
||
}
|
||
}
|
||
}
|
||
}
|