using Aliyun.OTS; using Aliyun.OTS.DataModel; using Aliyun.OTS.Request; using Quartz; using System; using WX.CRM.BLL.Wx; using WX.CRM.Common; using WX.CRM.Model.Entity; namespace WX.CRM.CRMServices.WeiXin { public class WxMessage { public void Start() { var para = "content="; var url = Utility.GetSettingByKey("WxOts") ?? "http://localhost:18739/WeiXinService.svc/"; if (string.IsNullOrEmpty(url)) { throw new Exception("wxots接口配置错误"); } try { var bll = new WX_MESSAGE_BL(); var list = bll.GetList(p => p.YUNSTATUS == 0 || p.YUNSTATUS == 1); foreach (var wxMessage in list) { //para += Utility.ObjectToJson(wxMessage); //var resultJson = Utility.PostData(url + "Put?" + para, Encoding.UTF8); //var resultJson = Utility.PostData(url + "Put", para, Encoding.UTF8); //var result = Utility.JSONToObject(resultJson); //if (result.result) //{ // wxMessage.YUNSTATUS = 2; // bll.Update(wxMessage); //} //LogHelper.Info("wxots入库结果:" + result.result.ToString() + "," + result.retcode); PutRow(wxMessage); wxMessage.YUNSTATUS = 2; bll.Update(wxMessage); } LogHelper.Info("wxots调用成功"); } catch (Exception e) { LogHelper.Error("微信调度错误:" + e.ToString()); } } private void PutRow(WX_MESSAGE data) { OTSClient otsClient = Config.GetClient(); //var msg = Utility.JSONToObject(data); LogHelper.Info("需要写入的数据:" + Utility.ObjectToJson(data)); var msg = data; #region 写入message // 定义行的主键,必须与创建表时的TableMeta中定义的一致 PrimaryKey primaryKey = new PrimaryKey { { "UserName", new ColumnValue(msg.USERNAME) }, { "Talker", new ColumnValue(msg.TALKER) }, { "CreateTime", new ColumnValue(Convert.ToInt64(msg.CREATETIME)) }, { "MsgSvrId", new ColumnValue(msg.MSGSVRID.ToString()) } }; // 定义要写入改行的属性列 AttributeColumns attribute = new AttributeColumns { {"NickName", string.IsNullOrEmpty(msg.NICKNAME) ? new ColumnValue(string.Empty) : new ColumnValue(msg.NICKNAME)}, {"Content", string.IsNullOrEmpty(msg.MSG_CONTENT) ? new ColumnValue(string.Empty) : new ColumnValue(msg.MSG_CONTENT)}, {"ImgPath", string.IsNullOrEmpty(msg.IMGPATH) ? new ColumnValue(string.Empty) : new ColumnValue(msg.IMGPATH)}, {"Url", string.IsNullOrEmpty(msg.MSG_URL) ? new ColumnValue(string.Empty) : new ColumnValue(msg.MSG_URL)}, {"Type", msg.MSG_TYPE.HasValue ? new ColumnValue(string.Empty) : new ColumnValue(Convert.ToInt32(msg.MSG_TYPE))}, {"Status", msg.STATUS.HasValue ? new ColumnValue(string.Empty) : new ColumnValue(Convert.ToInt32(msg.STATUS))}, {"RESERVED", string.IsNullOrEmpty(msg.RESERVED) ? new ColumnValue(string.Empty) : new ColumnValue(msg.RESERVED)}, {"LVBUFFER", string.IsNullOrEmpty(msg.LVBUFFER) ? new ColumnValue(string.Empty) : new ColumnValue(msg.LVBUFFER)}, {"TRANSCONTENT", string.IsNullOrEmpty(msg.TRANSCONTENT) ? new ColumnValue(string.Empty) : new ColumnValue(msg.TRANSCONTENT)}, {"TRANSBRANDWORDING", string.IsNullOrEmpty(msg.TRANSBRANDWORDING) ? new ColumnValue(string.Empty) : new ColumnValue(msg.TRANSBRANDWORDING)}, {"BIZCLIENTMSGID", string.IsNullOrEmpty(msg.BIZCLIENTMSGID) ? new ColumnValue(string.Empty) : new ColumnValue(msg.BIZCLIENTMSGID)}, {"BIZCHATUSERID", string.IsNullOrEmpty(msg.BIZCHATUSERID) ? new ColumnValue(string.Empty) : new ColumnValue(msg.BIZCHATUSERID)}, {"ISSEND", msg.ISSEND.HasValue ? new ColumnValue(Convert.ToInt32(msg.ISSEND.Value)) : new ColumnValue(string.Empty)}, {"BIZCHATID", msg.BIZCHATID.HasValue ? new ColumnValue(Convert.ToInt32(msg.BIZCHATID.Value)) : new ColumnValue(string.Empty)}, {"MSGSEQ", msg.MSGSEQ.HasValue ? new ColumnValue(Convert.ToInt32(msg.MSGSEQ.Value)) : new ColumnValue(string.Empty)}, {"FLAG", msg.FLAG.HasValue ? new ColumnValue(Convert.ToInt32(msg.FLAG.Value)) : new ColumnValue(string.Empty)} }; PutRowRequest request = new PutRowRequest("message", new Condition(RowExistenceExpectation.IGNORE), primaryKey, attribute); otsClient.PutRow(request); LogHelper.Info("ostx调用成功"); #endregion } } public class WxMessageJob : IJob { static bool _isNotice = false; public void Execute(JobExecutionContext context) { if (_isNotice) return; _isNotice = true; try { new WxMessage().Start(); } catch (Exception e) { WX.CRM.Common.LogHelper.Error("WX.CRM.CRMServices.WeiXin.WxMessage().Execute:" + e); } finally { _isNotice = false; } } } public class WxMessageResult { public bool result { get; set; } public int retcode { get; set; } } public class Config { private static string AccessKeyId = string.IsNullOrEmpty(Utility.GetSettingByKey("AliAppKey")) ? "iLCVNfsiH4F1JLNU" : Utility.GetSettingByKey("AliAppKey"); private static string AccessKeySecret = string.IsNullOrEmpty(Utility.GetSettingByKey("AliAppSecret")) ? "24DOemjGGeQeG2WJNzHBqi1wNtySgB" : Utility.GetSettingByKey("AliAppSecret"); private static string Endpoint = string.IsNullOrEmpty(Utility.GetSettingByKey("AliAppEndpoint")) ? "http://WX-Message.cn-shenzhen.ots.aliyuncs.com" : Utility.GetSettingByKey("AliAppEndpoint"); private static string InstanceName = string.IsNullOrEmpty(Utility.GetSettingByKey("AliAppInstanceName")) ? "WX-Message" : Utility.GetSettingByKey("AliAppInstanceName"); private static OTSClient OtsClient = null; public static OTSClient GetClient() { if (OtsClient != null) { return OtsClient; } OTSClientConfig config = new OTSClientConfig(Endpoint, AccessKeyId, AccessKeySecret, InstanceName); config.OTSDebugLogHandler = null; config.OTSErrorLogHandler = null; OtsClient = new OTSClient(config); return OtsClient; } } }