157 lines
6.5 KiB
C#
157 lines
6.5 KiB
C#
using Quartz;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Data;
|
||
using System.Data.SqlClient;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using WeChatServerServices.Model;
|
||
using WX.CRM.Common;
|
||
using WX.CRM.DAL;
|
||
|
||
namespace WeChatServerServices.WeiXin.GenHtml
|
||
{
|
||
public class GenQunFaHtml
|
||
{
|
||
WX.CRM.IBLL.Base.IBAS_PARAMETER_Q paraq = new WX.CRM.BLL.Base.BAS_PARAMETER_BL();
|
||
WX.CRM.IBLL.Base.IBAS_PARAMETER para = new WX.CRM.BLL.Base.BAS_PARAMETER_BL();
|
||
WX.CRM.IBLL.SYQ.ISYQManager syq = new WX.CRM.BLL.SYQ.SYQManager_BL();
|
||
|
||
private static string _shareName;
|
||
private static string _isShare;
|
||
public GenQunFaHtml()
|
||
{
|
||
_isShare = Utility.GetSettingByKey("IsShare");
|
||
if (!string.IsNullOrEmpty(_isShare))
|
||
{
|
||
//LogHelper.Info(_isShare);
|
||
try
|
||
{
|
||
_shareName = Utility.GetSettingByKey("NetUseShareName") ?? @"\\192.168.1.171\weixin";
|
||
string user = Utility.GetSettingByKey("NetUseUser") ?? @"192.168.1.171\admin";
|
||
string pwd = Utility.GetSettingByKey("NetUsePwd") ?? "read,./1";
|
||
NetUseHelper.Build(_shareName, user, pwd, string.Empty);//不指定盘符,避免引起盘符被占用的错误
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
LogHelper.Error(e.ToString());
|
||
}
|
||
}
|
||
}
|
||
public void GenHtmlFile()
|
||
{
|
||
try
|
||
{
|
||
syq.SYQErro();
|
||
string rootPath = Utility.GetSettingByKey("WxGenChatHtmlRootPath"); //html文件生成的根目录
|
||
if (!string.IsNullOrEmpty(_shareName))
|
||
{
|
||
rootPath = _shareName + @"\Message\";
|
||
}
|
||
|
||
string maxPkidStr = paraq.GetModel_Patameter("WxGenHtmlMaxQunFaRecordPkid").PARAVALUE;
|
||
if (string.IsNullOrEmpty(maxPkidStr))
|
||
throw new Exception("没配置WxGenHtmlMaxQunFaRecordPkid参数!");
|
||
int maxPkid = int.Parse(maxPkidStr);
|
||
var waitGenHtmlList = GetWaitMessage(maxPkid);
|
||
if (waitGenHtmlList == null || !waitGenHtmlList.Any())
|
||
return;
|
||
maxPkid = waitGenHtmlList.Select(p => p.Id).Max();
|
||
|
||
#region 写入文本库
|
||
List<string> usernams = waitGenHtmlList.Select(p => p.username).Distinct().ToList();
|
||
foreach (string username in usernams)
|
||
{
|
||
var genLis = waitGenHtmlList.Where(p => p.username == username).OrderBy(o => o.createTime).ToList();
|
||
foreach (var genMessage in genLis)
|
||
{
|
||
try
|
||
{
|
||
//var time = DateTimeTool.GetTimeFromLinuxTime(genMessage.createTime);
|
||
var dir = string.Format("{0}/{1}", rootPath, username);
|
||
var path = string.Format("{0}_qunfa.json", genMessage.talker);
|
||
var content = Utility.ConvertToJSON(genMessage);
|
||
if (File.Exists(Path.Combine(dir, path)))
|
||
FileUnit.AppendFile(dir, path, "," + content);
|
||
else
|
||
FileUnit.AppendFile(dir, path, content);
|
||
}
|
||
catch
|
||
{
|
||
LogHelper.Error("出错消息记录【id:" + genMessage.Id + "msgSvrId:" + genMessage.msgSvrId + "rootPath:" + rootPath + ",username:" + username + ",talker:" + genMessage.talker + "】");
|
||
}
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
string val = maxPkid.ToString();
|
||
para.Update_ParameterValueByKey("WxGenHtmlMaxQunFaRecordPkid", val);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.Error("【WX.CRM.CRMServices.WeiXin.GenHtml.GenQunFaHtml.GenHtmlFile()】" + ex.Message + ex.StackTrace);
|
||
}
|
||
}
|
||
#region 获取待生成消息
|
||
|
||
private IList<GenMessage> GetWaitMessage(int maxPkid)
|
||
{
|
||
var sql = "select top 1000 [id],[clientid],[filename],[tolist],[createtime],[msgtype],[jobusername],[cTime] from wx_gen_msqunfa where id > @maxPkid order by id asc";
|
||
var p = new List<SqlParameter>() { new SqlParameter("@maxPkid", maxPkid) };
|
||
var ds = SqlHelper.GetDataSet(SqlHelper.DatabaseType.AYCRM, sql, CommandType.Text, p.ToArray());
|
||
var list = new List<GenMessage>();
|
||
if (ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
|
||
{
|
||
foreach (DataRow dataRow in ds.Tables[0].Rows)
|
||
{
|
||
string talker = dataRow["tolist"].ToString();
|
||
foreach (var item in talker.Split(';'))
|
||
{
|
||
var message = new GenMessage
|
||
{
|
||
Id = int.Parse(string.Format("{0}", dataRow["Id"])),
|
||
msgSvrId = string.Format("{0}", dataRow["clientid"]),
|
||
type = int.Parse(string.Format("{0}", dataRow["msgtype"])),
|
||
isSend = 1,
|
||
createTime = long.Parse(string.Format("{0}", dataRow["createtime"])),
|
||
content = dataRow["filename"].ToString(),
|
||
username = dataRow["jobusername"].ToString(),
|
||
nickname = "",
|
||
url = ""
|
||
};
|
||
if (string.IsNullOrEmpty(item))
|
||
continue;
|
||
message.talker = item;
|
||
list.Add(message);
|
||
}
|
||
|
||
}
|
||
}
|
||
return list;
|
||
}
|
||
#endregion
|
||
}
|
||
public class GenQunFaHtmlJob : IJob
|
||
{
|
||
static bool isRuning = false;
|
||
public void Execute(JobExecutionContext context)
|
||
{
|
||
if (isRuning)
|
||
return;
|
||
isRuning = true;
|
||
try
|
||
{
|
||
new GenQunFaHtml().GenHtmlFile();
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
LogHelper.Error("【WeChatServerServices.WeiXin.GenHtml.GenQunFaHtmlJob.Execute()】 " + e.ToString());
|
||
}
|
||
finally
|
||
{
|
||
isRuning = false;
|
||
}
|
||
}
|
||
}
|
||
}
|