ComplianceServer/oldcode/NetCore.Service/QW/MainCorpThread.cs

325 lines
16 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.Common;
using NetCore.Model.enums;
using NetCore.Model.qw;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
namespace NetCore.Service.QW
{
/// <summary>
/// 主要线程执行
/// </summary>
public class MainCorpThread
{
public string threadKey { get; set; }
public string corpid { get; set; }
/// <summary>
/// 生成的ID
/// </summary>
public int seq { get; set; }
/// <summary>
/// 表名称
/// </summary>
public string tablename { get; set; }
/// <summary>
/// 企业名称
/// </summary>
public string name { 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_excute_log log { get; set; }
public List<hg_keyword> words { get; set; }
public string keyName { 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);
hg_man_config_bll _manconfigbll = new hg_man_config_bll(comcode);
ww_message_bll _msgbll = new ww_message_bll(comcode);
hg_message_word_bll _hgmsgwordbll = new hg_message_word_bll(comcode);
try
{
while (true)
{
needSleep = false;//默认不需要 睡眠
int sleepTime = _config_bll.GetIntConfig(BasConfigEnum.ManThreadSleepTime);
string qwUrl = ConfigHelper.GetSectionValue($"{comcode}_Config:fileConfig:qwUrl");
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 = seq;
hg_message_bll _hgmsgbll = new hg_message_bll(comcode);
hg_ordervoice_bll _ordervoice = new hg_ordervoice_bll(comcode);
hg_man_config manconfig = _manconfigbll.GetNowManConfig(corpid);//获取企业微信的当前配置
#region
DateTime now = DateTime.Now;//如果没有配置,那就从今天开始运算
var todaytime = new DateTime(now.Year, now.Month, now.Day);
if (manconfig == null)//找不到配置,进行插入操作
{
_manconfigbll.Add(new hg_man_config()
{
corpid = corpid,
seq = 0,
tablename = $"ww_message_{now.ToString("yyyyMM")}",
name = name,
checkdate = todaytime,
});
manconfig = _manconfigbll.GetNowManConfig(corpid);//获取企业微信的当前配置
}
tablename = manconfig.tablename;
seq = manconfig.seq;
checkdate = manconfig.checkdate;
#endregion
log = new hg_excute_log()
{
corpid = corpid,
starttime = DateTime.Now,
name = name,
seq = seq,
tablename = tablename
};
Console.WriteLine(string.Format("线程开始:corpid:{0},name:{1},seq:{2},talename:{3},matchstr:{4}", corpid, name, seq, tablename, matchstr));
List<corptable> nowtable = _config_bll.GetCorpTable(corpid);//本月及本月之前的表
if (!nowtable.Any())
{
Console.WriteLine(string.Format("没有消息表,corpid:{0},name:{1},seq:{2},talename:{3},matchstr:{4}", corpid, name, seq, tablename, matchstr));
log.status = 500;
log.message = "can't find message tables!";
GoLog();
return;
}
if (nowtable.FirstOrDefault(m => m.table_name == manconfig.tablename) == null)
{
Console.WriteLine(string.Format("没有消息表" + manconfig.tablename + ",corpid:{0},name:{1},seq:{2},talename:{3},matchstr:{4}", corpid, name, seq, tablename, matchstr));
log.status = 500;
log.message = "can't find message tables!";
GoLog();
return;
}
string nowtablename = string.Format("ww_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<ww_message> hgmsgList = _msgbll.GetList(tablename, corpid, hgclearcount, seq, cusmsgdeedhg, checkdate);///获取消息数据量
int hegRows = 0;
if (hgmsgList.Any())
{
foreach (ww_message item in hgmsgList)
{
SeqMax = item.seq > SeqMax ? item.seq : SeqMax;
if (cusmsgdeedhg == 0 && item.fromer.Length == 32)//客户发得消息,不做合规
continue;
bool weiguile = false;//违规了
List<hg_message_word> wordlist = new List<hg_message_word>();
if (item.msgtype == "voice")//语音的先入库,到时循环去读取并调用
{
var voiceUrl = qwUrl + $"/playamr?corpid={corpid}&fname={item.nfile}";
_ordervoice.Add(new hg_ordervoice()
{
checkdate = checkdate,
corpid = corpid,
msgtype = item.msgtype,
md5sum = item.nfile.Replace(".amr", "").Replace(".AMR", ""),
nfile = item.nfile,
msgid = item.msgid,
seq = item.seq,
tablename = tablename,
msgctime = item.ctime,
voiceUrl = voiceUrl,
tolist = item.tolist,
fromer = item.fromer
});
continue;
}
foreach (hg_keyword word in words)
{
//if (item.content.Contains(word.keyword))//发现违规关键字
if (KeyWordHelper.IsViolation(item.content, word.keyword))
{
wordlist.Add(new hg_message_word() { keyword = word.keyword, msgid = item.msgid });
weiguile = true;
}
}
if (weiguile)
{
hegRows++;
_hgmsgwordbll.AddList(wordlist.ToArray());//新增消息违规关键字表
string word = string.Join("|", wordlist.Select(m => m.keyword));//关键词
hg_message msg = new hg_message()
{
action = item.action,
//fromer = item.fromer,
//tolist = item.tolist,
content = item.content,
corpid = corpid,
cusname = "",
ext = item.ext,
hgstatus = 90,
//issend = 1,
kehuname = "",
msgid = item.msgid,
msgtype = item.msgtype,
nfile = item.nfile,
roomid = item.roomid,
seq = item.seq,
checkdate = checkdate,
msgtime = item.msgtime,
word = word,
ctime = item.ctime
};
if (string.IsNullOrEmpty(item.roomid))//非群得合规表
{
if (item.fromer.Length == 32)//客户发得消息
{
msg.issend = 0;
msg.fromer = item.tolist;
msg.tolist = item.fromer;
}
else
{
msg.issend = 1;
msg.fromer = item.fromer;
msg.tolist = item.tolist;
}
_hgmsgbll.Add(msg);
}
else
{
_hgmsgbll.AddGroupMsg(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("ww_message_{0}", checkdate.AddDays(1).ToString("yyyyMM"));//当前时间应该的名称
_manconfigbll.update(new hg_man_config() { corpid = corpid, name = name, seq = SeqMax, tablename = tablename, checkdate = checkdate, status = 1 });//这天进行完成
_manconfigbll.Add(new hg_man_config()
{
corpid = corpid,
seq = 0,
tablename = tablename,
name = name,
checkdate = checkdate.AddDays(1)//下一天
});
checkdate = checkdate.AddDays(1);
}
else
{
needSleep = true;
}
}
_manconfigbll.update(new hg_man_config() { corpid = corpid, name = name, seq = SeqMax, tablename = tablename, checkdate = checkdate, status = 0 });
log.status = 200;
log.message = string.Format("seq {0}~{1},rows count {2} ,hg {3} rows,checkdate:{4}", seq, SeqMax, hgmsgList.Count, hegRows, checkdate.ToString("yyyy-MM-dd"));
GoLog();
seq = SeqMax;//修改最大值,下次继续
_hgmsgbll.Clear();
_ordervoice.Clear();
}
else
{
Console.WriteLine("无关键词");
}
}
catch (Exception e)
{
Console.WriteLine(string.Format("线程错误:corpid:{0},name:{1},seq:{2},talename:{3},matchstr:{4},erro:", corpid, name, seq, tablename, matchstr, e.ToString()));
log.status = 500;
log.message = "system erro:" + e.ToString();
GoLog();
LogHelper.Error(e.ToString());
}
finally
{
Console.WriteLine(string.Format("线程结束:corpid:{0},name:{1},seq:{2},talename:{3},matchstr:{4},信息:{5},checkdate:{6}", corpid, name, seq, tablename, matchstr, log.message, checkdate.ToString("yyyy-MM-dd")));
}
if (needSleep)
{
Console.WriteLine(string.Format("{0}没数据了休息{1}分钟", name, 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.Log(log);
}
}
}