ComplianceServer/oldcode/NetCore.Service/WX/MainMsgTread.cs

264 lines
13 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 NetCore.BLL;
using NetCore.BLL.wx;
using NetCore.Common;
using NetCore.Model.enums;
using NetCore.Model.qw;
using NetCore.Model.wx;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
namespace NetCore.Service.WX
{
public class MainMsgTread
{
/// <summary>
/// 生成的ID
/// </summary>
public int msgPKID { get; set; }
/// <summary>
/// 表名称
/// </summary>
public string tablename { get; set; }
/// <summary>
/// 随机数
/// </summary>
public string matchstr { get; set; }
/// <summary>
/// 合规清洗取得数据量
/// </summary>
public int hgclearcount { get; set; }
/// <summary>
/// 客户发得消息是否需要合规默认0不需要 1需要
/// </summary>
public int cusmsgdeedhg { get; set; }
public hg_excete_log_crm log { get; set; }
public List<hg_keyword> words { get; set; }
public string keyName { get; set; }
public string threadKey { get; set; }
public DateTime checkdate { get; set; }
/// <summary>
/// 是否需要sleep
/// </summary>
private bool needSleep { get; set; }
public string comcode { get; set; }
public void execute()
{
hg_keyword_bll _keyword = new hg_keyword_bll(comcode);
bas_config_bll _config_bll = new bas_config_bll(comcode);
wx_hg_man_config_bll _manconfigbll = new wx_hg_man_config_bll(comcode);
wx_message_bll _msgbll = new wx_message_bll(comcode);
wx_hg_message_word_bll _hgmsgwordbll = new wx_hg_message_word_bll(comcode);
try
{
while (true)
{
needSleep = false;//默认不需要 睡眠
int sleepTime = _config_bll.GetIntConfig(BasConfigEnum.ManThreadSleepTime);
try
{
words = _keyword.GetWordListByParameter();//查询重新查询出所有的关键词
if (words.Any())//有关键词才开始行动
{
Console.WriteLine(string.Format("---关键词数量:{0}", words.Count));
bas_config config = _config_bll.GetConfig(BasConfigEnum.HgClearCount);
hgclearcount = config != null ? Convert.ToInt32(config.value) : 1000;//重配置中获取最新的数量配置
int SeqMax = msgPKID;
wx_hg_message_bll _hgmsgbll = new wx_hg_message_bll(comcode);
wx_hg_ordervoice_bll _ordervoice = new wx_hg_ordervoice_bll(comcode);
wx_hg_man_config manconfig = _manconfigbll.GetNowManConfig();//获取企业微信的当前配置
#region
DateTime now = DateTime.Now;//如果没有配置,那就从今天开始运算
var todaytime = new DateTime(now.Year, now.Month, now.Day);
if (manconfig == null)//找不到配置,进行插入操作
{
_manconfigbll.Add(new wx_hg_man_config()
{
pkid = 0,
tablename = $"wx_message{now.ToString("yyyyMM")}",
checkdate = todaytime,
});
manconfig = _manconfigbll.GetNowManConfig();//获取企业微信的当前配置
}
tablename = manconfig.tablename;
msgPKID = manconfig.pkid;
checkdate = manconfig.checkdate;
#endregion
log = new hg_excete_log_crm()
{
starttime = DateTime.Now,
tablename = tablename,
type = "txt"
};
Console.WriteLine(string.Format("微信文本合规线程开始:pkid:{0},talename:{1},matchstr:{2}", msgPKID, tablename, matchstr));
string nowtablename = string.Format("wx_message{0}", DateTime.Now.ToString("yyyyMM"));//当前时间应该的名称
//if (string.IsNullOrEmpty(tablename))
//{
// tablename = $"ww_message_{checkdate.ToString("yyyyMM")}";//当前检查库
// //tablename = nowtable.OrderBy(m => m.table_name).FirstOrDefault().table_name;
//}
List<wx_message> hgmsgList = _msgbll.GetList(tablename, hgclearcount, msgPKID, cusmsgdeedhg, checkdate);///获取消息数据量
int hegRows = 0;
if (hgmsgList.Any())
{
foreach (wx_message item in hgmsgList)
{
SeqMax = item.pkid > SeqMax ? item.pkid : SeqMax;
bool weiguile = false;//违规了
List<wx_hg_message_word> wordlist = new List<wx_hg_message_word>();
if (item.msgtype == 34)//语音的先入库,到时循环去读取并调用
{
_ordervoice.Add(new wx_hg_ordervoice()
{
ishg = 0,
msgsvrid = item.msgsvrid,
pkid = item.pkid,
talename = tablename,
talker = item.talker,
transstatus = -1,
username = item.username,
checkdate = checkdate,
msgtype = item.msgtype,
nickname = item.nickname
});
continue;
}
foreach (hg_keyword word in words)
{
if (KeyWordHelper.IsViolation(item.msgcontent, word.keyword))//发现违规关键字
{
wordlist.Add(new wx_hg_message_word() { keyword = word.keyword, msgsvrid = item.msgsvrid });
weiguile = true;
}
}
if (weiguile)
{
hegRows++;
_hgmsgwordbll.AddList(wordlist.ToArray());//新增消息违规关键字表
string word = string.Join("|", wordlist.Select(m => m.keyword));//关键词
hg_wx_message msg = new hg_wx_message()
{
word = word,
ctime = item.ctime,
hgstatus = 0,
imgpath = item.imgpath,
issend = item.issend,
msgcontent = item.msgcontent,
msgsvrid = item.msgsvrid,
msgtype = item.msgtype,
pkid = item.pkid,
checkdate = checkdate,
talker = item.talker,
nickname = item.nickname,
username = item.username,
};
_hgmsgbll.Add(msg);
}
}
_hgmsgbll.StartInsert();
_ordervoice.StartInsert();
}
else
{
if (todaytime != checkdate)//比如过度到了第二天
{
//corptable nexttable = nowtable.Where(m => String.Compare(m.table_name, tablename) == 1).OrderBy(m => m.table_name).FirstOrDefault();//取大于当前tablename得数据
//if (nexttable != null)
//{
// tablename = nexttable.table_name;//跳到下一个表
//}
tablename = string.Format("wx_message{0}", checkdate.AddDays(1).ToString("yyyyMM"));//当前时间应该的名称
_manconfigbll.update(new wx_hg_man_config() { pkid = SeqMax, tablename = tablename, checkdate = checkdate, status = 1 });//这天进行完成
_manconfigbll.Add(new wx_hg_man_config()
{
pkid = 0,
status = 0,
ctime = DateTime.Now,
tablename = tablename,
checkdate = checkdate.AddDays(1)//下一天
});
checkdate = checkdate.AddDays(1);
SeqMax = 0;
}
else
{
needSleep = true;
}
}
_manconfigbll.update(new wx_hg_man_config() { pkid = SeqMax, tablename = tablename, checkdate = checkdate, status = 0 });
log.status = 200;
log.message = string.Format("pkid {0}~{1},rows count {2} ,hg {3} rows,checkdate:{4}", msgPKID, SeqMax, hgmsgList.Count, hegRows, checkdate.ToString("yyyy-MM-dd"));
GoLog();
msgPKID = SeqMax;//修改最大值,下次继续
_hgmsgbll.Clear();
_ordervoice.Clear();
}
else
{
Console.WriteLine("无关键词");
}
}
catch (Exception e)
{
Console.WriteLine(string.Format("微信文本合规线程错误:pkid:{0},talename:{1},matchstr:{2},erro:{3}", msgPKID, tablename, matchstr, e.ToString()));
log.status = 500;
log.message = "system erro:" + e.ToString();
GoLog();
LogHelper.Error(e.ToString());
}
finally
{
Console.WriteLine(string.Format("微信文本合规线程结束:pkid:{0},talename:{1},matchstr:{2},信息:{3},checkdate:{4}", msgPKID, tablename, matchstr, log.message, checkdate.ToString("yyyy-MM-dd")));
}
if (needSleep)
{
Console.WriteLine(string.Format("微信文本合规线程 没数据了休息{0}分钟", sleepTime));
Thread.Sleep((sleepTime == 0 ? 3 : sleepTime) * 60 * 1000);//默认3分钟执行一次从数据库获取
}
else
Thread.Sleep(5000);//如果有数据就只休息5秒钟
}
}
catch (Exception aa)
{
Program.ToFinishMan(threadKey);
LogHelper.Error(aa.ToString());
}
}
/// <summary>
/// 执行日志记录
/// </summary>
public void GoLog()
{
log.endtime = DateTime.Now;
log.exscend = (int)(log.endtime.Value - log.starttime.Value).TotalSeconds;
hg_excute_log_bll bll = new hg_excute_log_bll(comcode);
bll.LogWX(log);
}
}
}