/// <summary> /// 打开网址并下载文件 /// </summary> /// <param name="URL">下载文件地址</param> /// <param name="Filename">下载后另存为(全路径)</param> private static bool OpenUrlDownloadFile(Log4netUtil.LogAppendToForms logAppendToForms, Model.JobEntity jobInfo, string url, string filename) { try { System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url); System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse(); System.IO.Stream st = myrp.GetResponseStream(); System.IO.Stream so = new System.IO.FileStream(filename, System.IO.FileMode.Create); byte[] by = new byte[1024]; int osize = st.Read(by, 0, (int)by.Length); while (osize > 0) { so.Write(by, 0, osize); osize = st.Read(by, 0, (int)by.Length); } so.Close(); st.Close(); myrp.Close(); Myrq.Abort(); return(true); } catch (Exception ex) { string logMessage = string.Format("【{0}_{1}】 下载文件失败 ;原因:{2}", jobInfo.JobCode, jobInfo.JobName, ex.Message); Log4netUtil.Log4NetHelper.LogError(logAppendToForms, true, logMessage, @"Ftp"); return(false); } }
/// <summary> /// BulkInsertDatabase /// </summary> /// <param name="logAppendToForms"></param> /// <param name="jobInfo"></param> /// <param name="dt"></param> /// <param name="insertTableName"></param> /// <returns></returns> public int BulkInsertDatabase(Log4netUtil.LogAppendToForms logAppendToForms, Model.JobEntity jobInfo, string insertTableName, string strSql) { DALFactory.FactoryDAL fact = new DALFactory.FactoryDAL(); IDAL.ICommonDAL idal = fact.CreateCommonDAL(); return(idal.BulkCopyInsert(logAppendToForms, jobInfo, insertTableName, strSql)); }
/// <summary> /// BulkCopyInsert 批量插入数据库 /// </summary> /// <param name="logAppendToForms"></param> /// <param name="jobInfo"></param> /// <param name="tableName"></param> /// <returns></returns> public int BulkCopyInsert(Log4netUtil.LogAppendToForms logAppendToForms, Model.JobEntity jobInfo, string tableName, string strSql) { string logMessage = string.Empty; //string strSql = Util.ConvertHelper.DataTableToStrInsert(dt, tableName) + "\r"; string targetDatabase = jobInfo.TargetDatabase; IDAL.IDBHelper _idbHelper = DALFactory.DBHelperFactory.CreateInstance(targetDatabase);//创建接口/创建接口 System.Data.Common.DbParameter[] cmdParams = null; string jsonSql = Util.DbSqlLog.SqlToJson("9999", strSql, cmdParams); logMessage = string.Format("【{0}_{1}】 JsonSql:{2} ", jobInfo.JobCode, jobInfo.JobName, jsonSql); Log4netUtil.Log4NetHelper.LogMessage(logAppendToForms, jobInfo.IsDebug, logMessage, @"Database"); try { if (_idbHelper.ExecuteNonQuery(System.Data.CommandType.Text, strSql, targetDatabase, cmdParams) > 0) { logMessage = string.Format("【{0}_{1}】 执行BulkCopyInsert成功!", jobInfo.JobCode, jobInfo.JobName); Newtonsoft.Json.Linq.JObject resultJObject = new Newtonsoft.Json.Linq.JObject(); resultJObject.Add("code", new Newtonsoft.Json.Linq.JValue("0000")); resultJObject.Add("msg", new Newtonsoft.Json.Linq.JValue(logMessage)); resultJObject.Add("sql", new Newtonsoft.Json.Linq.JObject(Newtonsoft.Json.Linq.JObject.Parse(jsonSql))); logMessage = string.Format("【{0}_{1}】 {2}", jobInfo.JobCode, jobInfo.JobName, Util.NewtonsoftCommon.SerializeObjToJson(resultJObject)); Log4netUtil.Log4NetHelper.LogMessage(logAppendToForms, jobInfo.IsDebug, logMessage, @"Database"); return(1); } else { logMessage = string.Format("【{0}_{1}】 执行BulkCopyInsert失败!", jobInfo.JobCode, jobInfo.JobName); Newtonsoft.Json.Linq.JObject resultJObject = new Newtonsoft.Json.Linq.JObject(); resultJObject.Add("code", new Newtonsoft.Json.Linq.JValue("9999")); resultJObject.Add("msg", new Newtonsoft.Json.Linq.JValue(logMessage)); resultJObject.Add("sql", new Newtonsoft.Json.Linq.JObject(Newtonsoft.Json.Linq.JObject.Parse(jsonSql))); logMessage = string.Format("【{0}_{1}】 {2}", jobInfo.JobCode, jobInfo.JobName, Util.NewtonsoftCommon.SerializeObjToJson(resultJObject)); Log4netUtil.Log4NetHelper.LogError(logAppendToForms, jobInfo.IsDebug, logMessage, @"Database"); return(-1); } } catch (Exception ex) { //违反了 PRIMARY KEY 约束 logMessage = string.Format("【{0}_{1}】 执行BulkCopyInsert失败! 失败原因:{2}", jobInfo.JobCode, jobInfo.JobName, ex.Message); Newtonsoft.Json.Linq.JObject resultJObject = new Newtonsoft.Json.Linq.JObject(); resultJObject.Add("code", new Newtonsoft.Json.Linq.JValue("9999")); resultJObject.Add("msg", new Newtonsoft.Json.Linq.JValue(logMessage)); resultJObject.Add("sql", new Newtonsoft.Json.Linq.JObject(Newtonsoft.Json.Linq.JObject.Parse(jsonSql))); logMessage = string.Format("【{0}_{1}】 {2}", jobInfo.JobCode, jobInfo.JobName, Util.NewtonsoftCommon.SerializeObjToJson(resultJObject)); Log4netUtil.Log4NetHelper.LogError(logAppendToForms, jobInfo.IsDebug, logMessage, @"Database"); if (ex.Message.Contains("违反了 PRIMARY KEY 约束")) { return(2); } else { return(-1); } } }
/// <summary> /// ScheduleJob /// </summary> /// <param name="scheduler"></param> /// <param name="jobInfo"></param> public void ScheduleJob(Model.JobEntity jobInfo, Log4netUtil.LogAppendToForms logAppendToForms) //IScheduler scheduler, { if (ValidExpression(jobInfo.CronExpression)) { Type type = GetClassInfo(Util.DalConst.AssemblyPathBLL, "BLL.JobsItemsBLL"); //GetClassInfo("BLL", "BLL.JobsItemsBLL"); if (type != null) { //使用组别、名称创建一个工作明细,此处为所需要执行的任务 IJobDetail job = new JobDetailImpl(jobInfo.JobCode.ToString(), string.Format("{0}_{1}_JobGroup", jobInfo.JobCode.ToString(), jobInfo.JobName), type); job.JobDataMap.Add("JobCode", jobInfo.JobCode); job.JobDataMap.Add("JobName", jobInfo.JobName); job.JobDataMap.Add("IsDebug", jobInfo.IsDebug); //调式模式 job.JobDataMap.Add("DomainName", jobInfo.DomainName); job.JobDataMap.Add("ServiceName", jobInfo.ServiceName); job.JobDataMap.Add("InterfacePrefix", jobInfo.InterfacePrefix); job.JobDataMap.Add("ApiModuleType", jobInfo.ApiModuleType); job.JobDataMap.Add("ApiRequestType", jobInfo.ApiRequestType); job.JobDataMap.Add("TargetDatabase", jobInfo.TargetDatabase); job.JobDataMap.Add("ProcedureName", jobInfo.ProcedureName); job.JobDataMap.Add("ModuleID", jobInfo.ModuleID); job.JobDataMap.Add("FilterBillType", jobInfo.FilterBillType); job.JobDataMap.Add("PageSize", jobInfo.PageSize); job.JobDataMap.Add("WritebackProcedureName", jobInfo.WritebackProcedureName); job.JobDataMap.Add("WritebackType", jobInfo.WritebackType); job.JobDataMap.Add("InsertTableName", jobInfo.InsertTableName); //InsertTableName job.JobDataMap.Add("CronExpression", jobInfo.CronExpression); //调式模式 job.JobDataMap.Add("CronExpressionDescription", jobInfo.CronExpressionDescription); //调式模式 job.JobDataMap.Add("EnterpriseId", jobInfo.EnterpriseId); //调式模式 job.JobDataMap.Add("EnterpriseName", jobInfo.EnterpriseName); //调式模式 job.JobDataMap.Add("StrConfigInfo", jobInfo.StrConfigInfo); //调式模式 job.JobDataMap.Put("ControlQueue", logAppendToForms); //使用组别、名称创建一个触发器,其中触发器立即执行,且每隔1秒或3秒执行一个任务,重复执行 CronTriggerImpl trigger = new CronTriggerImpl(); trigger.CronExpressionString = jobInfo.CronExpression; // 任务执行的cron表达式 trigger.Name = string.Format("{0}_{1}", jobInfo.JobCode.ToString(), jobInfo.JobName); //触发器名称,同一个分组中的名称必须不同 trigger.Description = jobInfo.CronExpressionDescription; //触发器描述 trigger.StartTimeUtc = DateTime.UtcNow; trigger.Group = string.Format("{0}_{1}_TriggerGroup", jobInfo.JobCode.ToString(), jobInfo.JobName); //触发器组 scheduler.ScheduleJob(job, trigger); //作业和触发器设置到调度器中 } else { Log4netUtil.Log4NetHelper.LogError(logAppendToForms, true, string.Format("{0} 定时调度任务启动失败,加载 JobsItemsBLL 类为空", jobInfo.JobName), "QuartzManager"); } } else { Log4netUtil.Log4NetHelper.LogError(logAppendToForms, true, string.Format("{0} 定时调度任务启动失败,CronExpression{1}不是有效的表达式", jobInfo.JobName, jobInfo.CronExpression), "QuartzManager"); } }
/// <summary> /// SyncApiByJson Json /// </summary> /// <returns></returns> public string SyncApiByJson(Log4netUtil.LogAppendToForms logAppendToForms, Model.JobEntity jobInfo, string controllerType, string requestData) { string reqUrl = string.Format("{0}/{1}", ReqURL, controllerType); Newtonsoft.Json.Linq.JObject resultJObject = new Newtonsoft.Json.Linq.JObject(); Newtonsoft.Json.Linq.JArray jlistRequestData = Newtonsoft.Json.Linq.JArray.Parse(requestData); string logMessage = string.Empty; string result = string.Empty; string t = string.Format("{0:yyyy-MM-dd HH:mm:ss}", DateTime.Now); //时间戳(格式:yyyy-MM-ddHH:mm:ss) string token = BuildToken(); //"nDWKKyRDMrS1M9zxIbTAQQ=="; // if (string.IsNullOrEmpty(token)) { logMessage = string.Format(" controllerType:{0} ;SendPost SyncApiByJson 接口调用 BuildToken token生成失败:token为空 !", controllerType); Newtonsoft.Json.Linq.JObject requestDataObj = Newtonsoft.Json.Linq.JObject.Parse(requestData); resultJObject.Add("errMsg", new Newtonsoft.Json.Linq.JValue(logMessage)); resultJObject.Add("rstFlag", new Newtonsoft.Json.Linq.JValue(10)); resultJObject.Add("reqUrl", new Newtonsoft.Json.Linq.JValue(reqUrl)); resultJObject.Add("t", new Newtonsoft.Json.Linq.JValue(t)); resultJObject.Add("token", new Newtonsoft.Json.Linq.JValue(token)); resultJObject.Add("items", new Newtonsoft.Json.Linq.JArray(jlistRequestData)); result = NewtonsoftCommon.SerializeObjToJson(resultJObject); logMessage = string.Format("【{0}_{1}】 {2}", jobInfo.JobCode, jobInfo.JobName.ToString(), result); Log4netUtil.Log4NetHelper.Error(logMessage, @"PrepurchApi"); Log4netUtil.LogDisplayHelper.LogError(logAppendToForms, logMessage); return(result); } resultJObject = new Newtonsoft.Json.Linq.JObject(); Dictionary <string, string> param = new Dictionary <string, string>(); param.Add("t", t); param.Add("token", token); param.Add("items", HttpUtility.UrlEncode(requestData, Encoding.UTF8));//请求内容为 JSON 格式 //param.Add("RequestData", HttpUtility.UrlEncode(requestJson, Encoding.UTF8));//请求内容为 JSON 格式 result = sendPost(reqUrl, param); resultJObject = new Newtonsoft.Json.Linq.JObject(); //logMessage = string.Format("SendPost SyncApiByJson 接口调用! controllerType:{0} ", controllerType); logMessage = string.Format(" controllerType:{0} ;SendPost SyncApiByJson 接口调用!", controllerType); resultJObject.Add("msg", new Newtonsoft.Json.Linq.JValue(logMessage)); resultJObject.Add("reqUrl", new Newtonsoft.Json.Linq.JValue(reqUrl)); resultJObject.Add("t", new Newtonsoft.Json.Linq.JValue(t)); resultJObject.Add("token", new Newtonsoft.Json.Linq.JValue(token)); //resultJObject.Add("order_code", new Newtonsoft.Json.Linq.JValue(orderCode)); resultJObject.Add("items", new Newtonsoft.Json.Linq.JArray(jlistRequestData)); resultJObject.Add("result", new Newtonsoft.Json.Linq.JObject(Newtonsoft.Json.Linq.JObject.Parse(result))); logMessage = string.Format("【{0}_{1}】 {2}", jobInfo.JobCode, jobInfo.JobName.ToString(), NewtonsoftCommon.SerializeObjToJson(resultJObject)); Log4netUtil.Log4NetHelper.Info(logMessage, @"PrepurchApi"); Log4netUtil.LogDisplayHelper.LogMessage(logAppendToForms, logMessage); return(result); }
/// <summary> /// ConvertJsonToJArray /// </summary> /// <param name="logAppendToForms"></param> /// <param name="jobInfo"></param> /// <param name="strJson"></param> /// <returns></returns> public static JArray ConvertJsonToJArray(Log4netUtil.LogAppendToForms logAppendToForms, Model.JobEntity jobInfo, string strJson) { JObject jObject = JObject.Parse(strJson); IEnumerable <JProperty> property = jObject.Properties(); JArray jArray = new JArray(); foreach (JProperty item in property) { JObject child = JObject.Parse(item.Value.ToString()); jArray.Add(child); } return(jArray); }
/// <summary> /// Post方式提交数据,返回网页的源代码 /// </summary> /// <param name="url">发送请求的 URL</param> /// <param name="param">请求的参数集合</param> /// <returns>远程资源的响应结果</returns> private string SendPost(Log4netUtil.LogAppendToForms logAppendToForms, Model.JobEntity jobInfo, string url, string postData) { string logMessage = string.Empty; string result = string.Empty; byte[] byteData = Encoding.GetEncoding("UTF-8").GetBytes(postData.ToString()); try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); if (url.Substring(0, 8) == "https://") { ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); } request.ContentType = "application/json";// "application/x-www-form-urlencoded"; request.Referer = url; request.Accept = "*/*"; request.Timeout = 30 * 1000; request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"; request.Method = "POST"; request.ContentLength = byteData.Length; Stream stream = request.GetRequestStream(); stream.Write(byteData, 0, byteData.Length); stream.Flush(); stream.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream backStream = response.GetResponseStream(); StreamReader sr = new StreamReader(backStream, Encoding.GetEncoding("UTF-8")); result = sr.ReadToEnd(); sr.Close(); backStream.Close(); response.Close(); request.Abort(); } catch (Exception ex) { logMessage = string.Format("【{0}_{1}】 SendPost b2bApi接口失败 失败原因:{2}", jobInfo.JobCode, jobInfo.JobName, ex.Message); Log4netUtil.Log4NetHelper.LogError(logAppendToForms, jobInfo.IsDebug, logMessage, string.Format(@"Api\{0}", jobInfo.JobCode)); Newtonsoft.Json.Linq.JObject jObject = new Newtonsoft.Json.Linq.JObject(); jObject.Add("code", 999); jObject.Add("msg", ex.Message); jObject.Add("data", string.Empty); result = Util.NewtonsoftCommon.SerializeObjToJson(jObject); } return(result); }
/// <summary> /// GetScheduleJob /// </summary> /// <param name="jobInfo"></param> private void GetScheduleJob(Model.JobEntity jobInfo) { _QuartzManager.ScheduleJob(jobInfo, _LogAppendToForms); string logMessage = string.Empty; if (jobInfo.IsDebug) { logMessage = string.Format("【{0}_{1}】(调式模式) 加入调度器成功!", jobInfo.JobCode, jobInfo.JobName.ToString()); Log4netUtil.LogDisplayHelper.LogWarning(_LogAppendToForms, logMessage); } else { logMessage = string.Format("【{0}_{1}】加入调度器成功!", jobInfo.JobCode, jobInfo.JobName.ToString()); Log4netUtil.LogDisplayHelper.LogMessage(_LogAppendToForms, logMessage); } Log4netUtil.Log4NetHelper.Info(logMessage, "QuartzManager"); }
/// <summary> /// 复制文件到指写文件夹 /// </summary> /// <param name="sourceFile">源文件</param> /// <param name="destinationFile">目标文件</param> /// <returns> 成功 true 失败 false </returns> private static bool CopyToFile(Log4netUtil.LogAppendToForms logAppendToForms, Model.JobEntity jobInfo, string sourceFile, string destinationFile) { System.IO.FileInfo file = new System.IO.FileInfo(sourceFile); try { if (file.Exists) { file.CopyTo(destinationFile, true); return(true); } return(false); } catch (Exception ex) { string logMessage = string.Format("【{0}_{1}】 复制文件失败 ;原因:{2}", jobInfo.JobCode, jobInfo.JobName, ex.Message); Log4netUtil.Log4NetHelper.LogError(logAppendToForms, true, logMessage, @"Ftp"); return(false); } }
/// <summary> /// CallDtyApi /// </summary> /// <param name="logAppendToForms"></param> /// <param name="dtyApi"></param> /// <param name="jobInfo"></param> /// <param name="apiName"></param> /// <param name="requestData"></param> /// <param name="result"></param> /// <returns></returns> private bool CallDtyApi(Log4netUtil.LogAppendToForms logAppendToForms, Util.DtyApi dtyApi, Model.JobEntity jobInfo, string apiName, string requestData, out string result) { string logMessage = string.Empty; //string passKey = "9A7OTA7JTAAJOE3153J17TEEAAJOTJO9"; result = dtyApi.DtyEDIApiByJson(apiName, requestData); Newtonsoft.Json.Linq.JObject resultJObject = Newtonsoft.Json.Linq.JObject.Parse(result); if (string.Equals(resultJObject.Value <string>("ErrCode"), "000")) { logMessage = string.Format("【{0}_{1}】 result:{2} 成功! ", jobInfo.JobCode, jobInfo.JobName.ToString(), result); LogMessage(logAppendToForms, true, logMessage, apiName); return(true); } else { logMessage = string.Format("【{0}_{1}】 result:{2} ;失败!原因:{3} ", jobInfo.JobCode, jobInfo.JobName.ToString(), result, resultJObject.Value <string>("ErrMsg")); LogError(logAppendToForms, true, logMessage, apiName); return(false); } }
/// <summary> /// DtyEDIApiByJson /// </summary> /// <param name="logAppendToForms"></param> /// <param name="jobInfo"></param> /// <param name="controllerType"></param> /// <param name="requestData"></param> /// <returns></returns> public string DtyEDIApiByJson(Log4netUtil.LogAppendToForms logAppendToForms, Model.JobEntity jobInfo, string controllerType, string requestData) { string reqUrl = string.Format("{0}/{1}", ReqUrl, controllerType); Newtonsoft.Json.Linq.JObject resultJObject = new Newtonsoft.Json.Linq.JObject(); Newtonsoft.Json.Linq.JArray jlistRequestData = Newtonsoft.Json.Linq.JArray.Parse(requestData); string logMessage = string.Empty; string result = string.Empty; Dictionary <string, string> param = new Dictionary <string, string>(); param.Add("Sample", HttpUtility.UrlEncode(requestData, Encoding.UTF8));//请求内容为 JSON 格式 //param.Add("RequestData", HttpUtility.UrlEncode(requestJson, Encoding.UTF8));//请求内容为 JSON 格式 result = sendPost(reqUrl, requestData); //resultJObject = new Newtonsoft.Json.Linq.JObject(); //logMessage = string.Format("SendPost SyncApiByJson 接口调用! controllerType:{0} ", controllerType); logMessage = string.Format(" controllerType:{0} ;SendPost SyncApiByJson 接口调用!", controllerType); logMessage = string.Format("【{0}_{1}】 {2}", jobInfo.JobCode, jobInfo.JobName.ToString(), result); Log4netUtil.Log4NetHelper.Info(logMessage, @"PrepurchApi"); Log4netUtil.LogDisplayHelper.LogMessage(logAppendToForms, logMessage); return(result); }
/// <summary> /// EleInvPrint 打印电子发票 /// </summary> /// <param name="isAuto"></param> /// <param name="eInvoicePrintCopies"></param> /// <param name="paperSizeRawKind"></param> /// <param name="url"></param> /// <param name="DataExchangeId"></param> /// <param name="printerName"></param> /// <returns></returns> public static bool ImagePrint(Log4netUtil.LogAppendToForms logAppendToForms, Model.JobEntity jobInfo, string url, string fileName) { string logMessage = string.Empty; try { if (string.IsNullOrEmpty(url)) { logMessage = string.Format("【{0}_{1}】 下载及打印文件失败 ;原因:文件路径为空", jobInfo.JobCode, jobInfo.JobName); Log4netUtil.Log4NetHelper.LogError(logAppendToForms, true, logMessage, @"Ftp"); return(false); } return(OpenUrlDownloadFile(logAppendToForms, jobInfo, url, fileName)); } catch (Exception ex) { logMessage = string.Format("【{0}_{1}】 下载及打印文件失败 ;原因:{2}", jobInfo.JobCode, jobInfo.JobName, ex.Message); Log4netUtil.Log4NetHelper.LogError(logAppendToForms, true, logMessage, @"Ftp"); return(false); } }
/// <summary> /// FtpDownloadToFile /// </summary> /// <param name="logAppendToForms"></param> /// <param name="jobInfo"></param> /// <param name="ftpfilepath"></param> /// <param name="fileSavePath"></param> /// <param name="fileSaveName"></param> /// <returns></returns> public string FtpDownloadToFile(Log4netUtil.LogAppendToForms logAppendToForms, Model.JobEntity jobInfo, string ftpfilepath, string fileSavePath, string fileSaveName, out string extension) { Newtonsoft.Json.Linq.JObject jObject = new Newtonsoft.Json.Linq.JObject(); var ftp = new Util.FluentFtpHelper(jobInfo.ConfigInfo.FtpHostIP, jobInfo.ConfigInfo.FtpPort, jobInfo.ConfigInfo.FtpUserName, jobInfo.ConfigInfo.FtpPassword); if (!ftp.Connect()) { extension = string.Empty; jObject.Add("code", "9998"); jObject.Add("msg", "Ftp连接异常!"); jObject.Add("data", string.Empty); return(jObject.ToString()); } if (!ftp.isConnected()) { extension = string.Empty; jObject.Add("code", "9998"); jObject.Add("msg", "连接Ftp失败!"); jObject.Add("data", string.Empty); return(jObject.ToString()); } string localDic = string.Format("{0}\\{1}", fileSavePath, fileSaveName); string remotePath = ftpfilepath; if (ftp.DownloadImageFile(fileSavePath, fileSaveName, remotePath, out extension)) { jObject.Add("code", "0000"); jObject.Add("msg", "Ftp下载文件成功!"); jObject.Add("data", string.Empty); } else { jObject.Add("code", "9999"); jObject.Add("msg", string.Format("localDic:{0} ;remotePath:{1} ;Ftp下载文件失败!", localDic, remotePath)); jObject.Add("data", string.Empty); } Log4netUtil.Log4NetHelper.Error(String.Format(@"ftpDownloadToFile->下载文件 Result:{0}", jObject.ToString()), @"Ftp"); return(jObject.ToString()); }
private static Model.ConfigInfo getConfigInfo() { Model.ConfigInfo configInfo = new Model.ConfigInfo(); string ConfigFile = System.Windows.Forms.Application.StartupPath.ToString() + "\\Config.ini"; configInfo.ModuleCode = "C13"; configInfo.WebApiUrl = Util.INIOperationClass.INIGetStringValue(ConfigFile, "WebApi", "WebApiUrl", null); configInfo.SignKey = Util.INIOperationClass.INIGetStringValue(ConfigFile, "WebApi", "SignKey", null); configInfo.EncryptKey = Util.INIOperationClass.INIGetStringValue(ConfigFile, "WebApi", "EncryptKey", null); configInfo.FtpHostIP = Util.INIOperationClass.INIGetStringValue(ConfigFile, "FTP", "IP", null); string ftpPort = Util.INIOperationClass.INIGetStringValue(ConfigFile, "FTP", "PORT", null); configInfo.FtpPort = Util.Common.IsInt(ftpPort) ? Convert.ToInt32(ftpPort) : 21; configInfo.FtpUserName = Util.INIOperationClass.INIGetStringValue(ConfigFile, "FTP", "USER", null); string ftpPasswordEncrypt = Util.INIOperationClass.INIGetStringValue(ConfigFile, "FTP", "PWD", null); configInfo.FtpPassword = Util.EncAndDec.DESDecrypt(ftpPasswordEncrypt); //解密 configInfo.FtpRootPath = Util.INIOperationClass.INIGetStringValue(ConfigFile, "FTP", "RootPath", null); string ftpBuffLength = Util.INIOperationClass.INIGetStringValue(ConfigFile, "FTP", "BuffLength", null); configInfo.FtpBuffLength = string.IsNullOrEmpty(ftpBuffLength) ? 2048 : Convert.ToInt32(ftpBuffLength); string FtpUsePassiveStr = Util.INIOperationClass.INIGetStringValue(ConfigFile, "FTP", "UsePassive", null); FtpUsePassiveStr = string.IsNullOrEmpty(FtpUsePassiveStr) ? "否" : FtpUsePassiveStr; configInfo.FtpUsePassive = string.Equals(FtpUsePassiveStr, "是") ? true : false; configInfo.LastLoginName = Util.INIOperationClass.INIGetStringValue(ConfigFile, "Login", "LastLoginName", null); string b2bPlatformNameStr = Util.INIOperationClass.INIGetStringValue(ConfigFile, "EnterpriseConfig", "B2bPlatformName", null); configInfo.B2bPlatformName = string.IsNullOrEmpty(b2bPlatformNameStr) ? "药药购 " : b2bPlatformNameStr; configInfo.EnterpriseName = Util.INIOperationClass.INIGetStringValue(ConfigFile, "EnterpriseConfig", "EnterpriseName", null); configInfo.EnterpriseLicenseNo = Util.INIOperationClass.INIGetStringValue(ConfigFile, "EnterpriseConfig", "EnterpriseLicenseNo", null); configInfo.EnterpriseId = Util.INIOperationClass.INIGetStringValue(ConfigFile, "EnterpriseConfig", "EnterpriseId", null); configInfo.LoginName = Util.INIOperationClass.INIGetStringValue(ConfigFile, "EnterpriseConfig", "LoginName", null); configInfo.Password = Util.INIOperationClass.INIGetStringValue(ConfigFile, "EnterpriseConfig", "Password", null); configInfo.LogRetentionDays = Util.Common.IsInt(Util.INIOperationClass.INIGetStringValue(ConfigFile, "Login", "LogRetentionDays", null)) ? Convert.ToInt32(Util.INIOperationClass.INIGetStringValue(ConfigFile, "Login", "LogRetentionDays", null)) : 15; string autoAdvanceDaysStr = Util.INIOperationClass.INIGetStringValue(ConfigFile, "Login", "AutoAdvanceDays", null); autoAdvanceDaysStr = string.IsNullOrEmpty(autoAdvanceDaysStr) ? "3" : autoAdvanceDaysStr; configInfo.AutoAdvanceDays = Util.Common.IsInt(autoAdvanceDaysStr) ? Convert.ToInt32(autoAdvanceDaysStr) : 3; string orderCodePrefix = Util.INIOperationClass.INIGetStringValue(ConfigFile, "Login", "OrderCodePrefix", null); configInfo.OrderCodePrefix = string.IsNullOrEmpty(orderCodePrefix) ? "FYYK" : orderCodePrefix; //订单前缀 string serviceAndSupport = Util.INIOperationClass.INIGetStringValue(ConfigFile, "Login", "ServiceAndSupport", null); configInfo.ServiceAndSupport = string.IsNullOrEmpty(serviceAndSupport) ? "武汉飞宇益克科技有限公司" : serviceAndSupport; string isShowServiceAndSupportStr = string.IsNullOrEmpty(Util.INIOperationClass.INIGetStringValue(ConfigFile, "Login", "IsShowServiceAndSupport", null)) ? "Y" : Util.INIOperationClass.INIGetStringValue(ConfigFile, "Login", "IsShowServiceAndSupport", null); configInfo.IsShowServiceAndSupport = string.Equals(isShowServiceAndSupportStr, "N") ? false:true; string xmlFileMaxSizeStr = Util.INIOperationClass.INIGetStringValue(ConfigFile, "Login", "XmlFileMaxSize", null); xmlFileMaxSizeStr = string.IsNullOrEmpty(xmlFileMaxSizeStr) ? "2.0" : xmlFileMaxSizeStr; configInfo.XmlFileMaxSize = Util.Common.IsDouble(xmlFileMaxSizeStr) ? Convert.ToDouble(xmlFileMaxSizeStr):2.0; configInfo.JobEnable = Util.INIOperationClass.INIGetStringValue(ConfigFile, "JobConfig", "JobEnable", null); List <Model.JobEntity> JobEntityList = new List <Model.JobEntity>(); if (!String.IsNullOrEmpty(configInfo.JobEnable)) { string[] array = configInfo.JobEnable.Split(','); for (int i = 0; i < array.Count(); i++) { string jobCode = array[i].ToString().Trim(); if (string.IsNullOrEmpty(jobCode)) { continue; } Model.JobEntity jobEntity = new Model.JobEntity(); jobEntity.JobCode = jobCode; jobEntity.JobName = Util.INIOperationClass.INIGetStringValue(ConfigFile, jobCode, "JobName", null); jobEntity.CronExpression = Util.INIOperationClass.INIGetStringValue(ConfigFile, jobCode, "CronExpression", null); jobEntity.CronExpressionDescription = Util.INIOperationClass.INIGetStringValue(ConfigFile, jobCode, "CronExpressionDescription", null); string isDebugJob = string.IsNullOrEmpty(Util.INIOperationClass.INIGetStringValue(ConfigFile, jobCode, "IsDebug", null)) ? "否" : Util.INIOperationClass.INIGetStringValue(ConfigFile, jobCode, "IsDebug", null); jobEntity.IsDebug = string.Equals(isDebugJob, "是") ? true : false; string domainNameStr = Util.INIOperationClass.INIGetStringValue(ConfigFile, jobCode, "DomainName", null); jobEntity.DomainName = string.IsNullOrEmpty(domainNameStr) ? "tsm.fyyk.com" : domainNameStr; //Api 域名 string serviceNameStr = Util.INIOperationClass.INIGetStringValue(ConfigFile, jobCode, "ServiceName", null); jobEntity.ServiceName = string.IsNullOrEmpty(serviceNameStr) ? "fyyk" : serviceNameStr;//Api 服务器 string interfacePrefixStr = Util.INIOperationClass.INIGetStringValue(ConfigFile, jobCode, "InterfacePrefix", null); jobEntity.InterfacePrefix = string.IsNullOrEmpty(interfacePrefixStr) ? "dev-api" : interfacePrefixStr;//Api 接口前缀 string moduleTypeStr = Util.INIOperationClass.INIGetStringValue(ConfigFile, jobCode, "ModuleType", null); jobEntity.ApiModuleType = string.IsNullOrEmpty(moduleTypeStr) ? "dataApi" : moduleTypeStr;//Api 模块 string requestTypeStr = Util.INIOperationClass.INIGetStringValue(ConfigFile, jobCode, "RequestType", null); jobEntity.ApiRequestType = string.IsNullOrEmpty(requestTypeStr) ? string.Empty : requestTypeStr;//Api 请求名称 string targetDatabaseStr = Util.INIOperationClass.INIGetStringValue(ConfigFile, jobCode, "TargetDatabase", null); jobEntity.TargetDatabase = string.IsNullOrEmpty(targetDatabaseStr) ? "Erp" : targetDatabaseStr; string procedureNameStr = Util.INIOperationClass.INIGetStringValue(ConfigFile, jobCode, "ProcedureName", null); jobEntity.ProcedureName = string.IsNullOrEmpty(procedureNameStr) ? "Fyyk_B2b_SqlView" : procedureNameStr; string moduleIDStr = Util.INIOperationClass.INIGetStringValue(ConfigFile, jobCode, "ModuleID", null); jobEntity.ModuleID = string.IsNullOrEmpty(moduleIDStr) ? string.Empty : moduleIDStr; string filterBillTypeStr = Util.INIOperationClass.INIGetStringValue(ConfigFile, jobCode, "FilterBillType", null); filterBillTypeStr = string.IsNullOrEmpty(filterBillTypeStr) ? "1" : filterBillTypeStr; jobEntity.FilterBillType = Util.Common.IsInt(filterBillTypeStr) ? Convert.ToInt32(filterBillTypeStr) : 1; string writebackProcedureNameStr = Util.INIOperationClass.INIGetStringValue(ConfigFile, jobCode, "WritebackProcedureName", null); jobEntity.WritebackProcedureName = string.IsNullOrEmpty(writebackProcedureNameStr) ? "Fyyk_B2b_Writeback" : writebackProcedureNameStr; string writebackTypeStr = Util.INIOperationClass.INIGetStringValue(ConfigFile, jobCode, "WritebackType", null); jobEntity.WritebackType = string.IsNullOrEmpty(writebackTypeStr) ? string.Empty : writebackTypeStr; string insertTableNameStr = Util.INIOperationClass.INIGetStringValue(ConfigFile, jobCode, "InsertTableName", null); jobEntity.InsertTableName = string.IsNullOrEmpty(insertTableNameStr) ? string.Empty : insertTableNameStr; string pagesSizeStr = Util.INIOperationClass.INIGetStringValue(ConfigFile, jobCode, "PageSize", null); pagesSizeStr = string.IsNullOrEmpty(pagesSizeStr) ? "0" : pagesSizeStr; jobEntity.PageSize = Util.Common.IsInt(pagesSizeStr) ? Convert.ToInt32(pagesSizeStr) : 0; //分页上传 JobEntityList.Add(jobEntity); } } configInfo.JobEntityList = JobEntityList; return(configInfo); }
/// <summary> /// ConvertJArrayToDataTable /// </summary> /// <param name="logAppendToForms"></param> /// <param name="jobInfo"></param> /// <param name="dataArr"></param> /// <returns></returns> public static System.Data.DataTable ConvertJArrayToDataTable(Log4netUtil.LogAppendToForms logAppendToForms, Model.JobEntity jobInfo, JArray dataArr) { if (dataArr == null || dataArr.Count <= 0) { string logMessage = string.Format("【{0}_{1}】 ConvertJArrayToDataTable转换失败!失败原因:JArray为空 ", jobInfo.JobCode, jobInfo.JobName); Newtonsoft.Json.Linq.JObject resultJObject = new Newtonsoft.Json.Linq.JObject(); resultJObject.Add("code", 999); resultJObject.Add("msg", logMessage); resultJObject.Add("data", dataArr.ToString()); logMessage = string.Format("【{0}_{1}】 ConvertJArrayToDataTable转换失败! ", jobInfo.JobCode, jobInfo.JobName); Log4netUtil.Log4NetHelper.LogError(logAppendToForms, jobInfo.IsDebug, logMessage, string.Format(@"Api\{0}", jobInfo.JobCode)); return(null); } System.Data.DataTable resultDt = new System.Data.DataTable(); try { var colnames = ((JObject)(dataArr.First)).Properties(); List <string> columnNames = new List <string>(); if (colnames == null || colnames.Count() <= 0) { string logMessage = string.Format("【{0}_{1}】 ConvertJArrayToDataTable转换失败!失败原因:JArray Colnames为空 ", jobInfo.JobCode, jobInfo.JobName); Newtonsoft.Json.Linq.JObject resultJObject = new Newtonsoft.Json.Linq.JObject(); resultJObject.Add("code", 999); resultJObject.Add("msg", logMessage); resultJObject.Add("data", dataArr.ToString()); logMessage = string.Format("【{0}_{1}】 ConvertJArrayToDataTable转换失败! ", jobInfo.JobCode, jobInfo.JobName); Log4netUtil.Log4NetHelper.LogError(logAppendToForms, jobInfo.IsDebug, logMessage, string.Format(@"Api\{0}", jobInfo.JobCode)); return(null); } foreach (var item in colnames) { if (!columnNames.Contains(item.Name)) { columnNames.Add(item.Name); } resultDt.Columns.Add(item.Name, typeof(string)); } foreach (JObject data in dataArr) { JObject jo = JObject.Parse(data.ToString()); System.Data.DataRow row = resultDt.NewRow(); foreach (var columnName in columnNames) { if (jo.Property(columnName) == null) { data.Add(columnName, ""); row[columnName] = data[columnName].ToString(); } else { row[columnName] = data[columnName].ToString(); } } resultDt.Rows.Add(row); } return(resultDt); } catch (Exception ex) { string logMessage = string.Format("【{0}_{1}】 ConvertJArrayToDataTable转换失败!失败原因:{2} ", jobInfo.JobCode, jobInfo.JobName, ex.Message); Newtonsoft.Json.Linq.JObject resultJObject = new Newtonsoft.Json.Linq.JObject(); resultJObject.Add("code", 999); resultJObject.Add("msg", logMessage); resultJObject.Add("data", dataArr.ToString()); logMessage = string.Format("【{0}_{1}】 ConvertJArrayToDataTable转换失败! ", jobInfo.JobCode, jobInfo.JobName); Log4netUtil.Log4NetHelper.LogError(logAppendToForms, jobInfo.IsDebug, logMessage, string.Format(@"Api\{0}", jobInfo.JobCode)); return(null); } }
/// <summary> /// B2bApiRequestByJson /// </summary> /// <param name="moduleType">模板类型</param> /// <param name="requestType">请求类型</param> /// <param name="requestData">请求数据</param> /// <returns></returns> public string B2bApiRequestByJson(Log4netUtil.LogAppendToForms logAppendToForms, Model.JobEntity jobInfo, string requestData) { //Log4netUtil.Log4NetHelper.Info(string.Format("SendPost FyykApi RequestData{0}", requestData), @"DYZS\FyykApi"); Newtonsoft.Json.Linq.JObject jObject; string logMessage = string.Empty; string reqUrl = string.Format(@"{0}/{1}/{2}/{3}", _DomainName, _ServiceName, jobInfo.ApiModuleType, jobInfo.ApiRequestType); string stortJson = string.Empty; string content = string.Empty; //var jArray = Newtonsoft.Json.Linq.JArray.Parse(requestData); //jObject = Newtonsoft.Json.Linq.JObject.Parse(requestData); //string jsonData = JsonConvert.SerializeObject(jObject); stortJson = requestData; // Util.NewtonsoftCommon.StortJson(requestData, false); //排序 content = requestData; // JsonConvert.SerializeObject(jObject); try { string sign = SignEncrypt(stortJson, _SignKey).ToLower(); string jsonDataEN = AesEncrypt(content, _EncryptKey); //string jsonDataENReplace = jsonDataEN;// jsonDataEN.Replace("+", "%2B");//注意加号(’+‘)的替换处理,否则由于加号经过Url传递后变成空格而得不到合法的Base64字符串 //Dictionary<string, string> param = new Dictionary<string, string>(); //jsonDataENReplace = string.Empty; var requestJObject = new Newtonsoft.Json.Linq.JObject(); requestJObject.Add("data", jsonDataEN); requestJObject.Add("sign", sign); string result = SendPost(logAppendToForms, jobInfo, reqUrl, Util.NewtonsoftCommon.SerializeObjToJson(requestJObject)); jObject = new Newtonsoft.Json.Linq.JObject(); jObject.Add("returl", reqUrl); jObject.Add("request", requestData); jObject.Add("result", result);// JToken.FromObject(new {result}));// Newtonsoft.Json.Linq.JObject.Parse(result)); Newtonsoft.Json.Linq.JObject resultJObject = Newtonsoft.Json.Linq.JObject.Parse(result); jObject.Add("code", resultJObject.Value <int>("code")); if (string.Equals(resultJObject.Value <int>("code"), 200)) { jObject.Add("msg", string.Format("B2bApiRequestByJson接口调用成功!", jobInfo.JobCode, jobInfo.JobName)); logMessage = string.Format("【{0}_{1}】 B2bApiRequestByJson接口调用成功!\n\r {2}", jobInfo.JobCode, jobInfo.JobName, Util.NewtonsoftCommon.SerializeObjToJson(jObject)); Log4netUtil.Log4NetHelper.LogMessage(logAppendToForms, jobInfo.IsDebug, logMessage, string.Format(@"Api\{0}", jobInfo.JobCode)); } else { jObject.Add("msg", string.Format("B2bApiRequestByJson 接口调用失败!", jobInfo.JobCode, jobInfo.JobName)); logMessage = string.Format("【{0}_{1}】 B2bApiRequestByJson 接口调用失败!\n\r {2}", jobInfo.JobCode, jobInfo.JobName, Util.NewtonsoftCommon.SerializeObjToJson(jObject)); Log4netUtil.Log4NetHelper.LogError(logAppendToForms, jobInfo.IsDebug, logMessage, string.Format(@"Api\{0}", jobInfo.JobCode)); } return(result); }catch (Exception ex) { logMessage = string.Format("【{0}_{1}】B2bApiRequestByJson接口失败 失败原因:{2}", jobInfo.JobCode, jobInfo.JobName, ex); Log4netUtil.Log4NetHelper.LogError(logAppendToForms, jobInfo.IsDebug, logMessage, string.Format(@"Api\{0}", jobInfo.JobCode)); Newtonsoft.Json.Linq.JObject exJObject = new Newtonsoft.Json.Linq.JObject(); exJObject.Add("code", 999); exJObject.Add("msg", ex.Message); exJObject.Add("data", string.Empty); exJObject.Add("requestUrl", reqUrl); exJObject.Add("request", Newtonsoft.Json.Linq.JObject.Parse(requestData)); return(Util.NewtonsoftCommon.SerializeObjToJson(exJObject)); } }