using log4net; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Quartz; using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.IO; using System.Linq; using System.Text; namespace JobReqWebData { public class JobXmcSend : IJob { private ILog log = LogManager.GetLogger(typeof(JobXmcSend)); public void Execute(IJobExecutionContext context) { log.Debug($"Execute开始"); try { string connStr = context.JobDetail.JobDataMap.GetString("ConnectString"); string sqlQuery = context.JobDetail.JobDataMap.GetString("QuerySql"); string reqUrl = context.JobDetail.JobDataMap.GetString("ReqUrl"); string custname = context.JobDetail.JobDataMap.GetString("custname"); string custpsw = context.JobDetail.JobDataMap.GetString("custpsw"); int reqTimeout = Convert.ToInt32(context.JobDetail.JobDataMap.GetString("RequestTimeout")); //log.Debug($"连接字符串:{connStr}"); //log.Debug($"查询SQL:{sqlQuery}"); //log.Debug($"请求URL:{reqUrl}"); //log.Debug($"用户:{custname}"); //log.Debug($"密码:{custpsw}"); //log.Debug($"请求超时:{reqTimeout}"); SqlConnection conn = new SqlConnection(connStr); SqlDataAdapter dataAdapter = new SqlDataAdapter(sqlQuery, conn); DataTable tQuery = new DataTable(); dataAdapter.Fill(tQuery); if (tQuery.Rows.Count == 0) { log.Debug("没有要发送的数据!"); return; } var json = DataTableToJsonHelper.ConvertToJson(tQuery); //log.Debug($"jsonStr:{json.ToString(Formatting.None)}"); JObject reqObj = new JObject(); reqObj.Add("custname", new JValue(custname)); reqObj.Add("custpsw", new JValue(custpsw)); reqObj.Add("bslist", json); var sendstr = reqObj.ToString(Formatting.None); log.Debug($"发送包:{sendstr}"); //发送请求数据 string rtn = WebRequestHelper.DoPost(reqUrl, sendstr, reqTimeout * 1000); var objRtn = JsonConvert.DeserializeAnonymousType(rtn, new { status = "", message = "" }); log.Debug($"爬取服务返回:{objRtn.message}(Code:{objRtn.status})"); } catch (Exception ex) { log.Error(ex.Message); log.Error(ex.StackTrace); } } } public class JobXmcGet : IJob { private ILog log = LogManager.GetLogger(typeof(JobXmcGet)); public void Execute(IJobExecutionContext context) { try { string connStr = context.JobDetail.JobDataMap.GetString("ConnectString"); string custname = context.JobDetail.JobDataMap.GetString("custname"); string custpsw = context.JobDetail.JobDataMap.GetString("custpsw"); string reqUrl = context.JobDetail.JobDataMap.GetString("ReqUrl"); int reqTimeout = Convert.ToInt32(context.JobDetail.JobDataMap.GetString("RequestTimeout")); //log.Debug($"用户:{custname}"); //log.Debug($"密码:{custpsw}"); //log.Debug($"请求超时:{reqTimeout}"); //访问url获取爬取的数据 Dictionary dic = new Dictionary(); dic.Add("custname", custname); dic.Add("custpsw", custpsw); string strContent = string.Empty; foreach (var item in dic) { if (strContent.Length > 0) { strContent += "&"; } strContent += $"{item.Key}={item.Value}"; } log.Debug($"请求数据:{strContent}"); string rtn = WebRequestHelper.DoPost(reqUrl, dic, reqTimeout * 1000); //string rtn = File.ReadAllText(@"H:\DS7\JobScheduler\bin\Debug\test1.txt"); log.Debug($"数据返回:{rtn}"); var parseJsonObj = JsonConvert.DeserializeObject(rtn); parseJsonObj.connStr = connStr; if (parseJsonObj.XMC.Count > 0) { //var ClearSql = parseJsonObj.Clear_SQL(); //log.Debug($"ClearSql:{ClearSql}"); //20200228 修改算法 将所有结果按提单号分组 var TaskSql = parseJsonObj.SaveToOpTask_SQL(); //log.Debug($"TaskSql:{TaskSql}"); /* SqlConnection conn = new SqlConnection(connStr); SqlDataAdapter dataAdapter = new SqlDataAdapter(sqlQuery, conn); DataTable tQuery = new DataTable(); dataAdapter.Fill(tQuery);*/ using (SqlConnection dbcon = new SqlConnection(connStr)) { dbcon.Open(); using (SqlTransaction trans = dbcon.BeginTransaction()) { //增加判断 如果 foreach (string TaskSqlitem in TaskSql) { if (!string.IsNullOrWhiteSpace(TaskSqlitem)) { //log.Debug($"TaskSqlitem:{TaskSqlitem}"); SqlCommand cmd = new SqlCommand(TaskSqlitem, dbcon, trans); cmd.ExecuteNonQuery(); } } trans.Commit(); } dbcon.Close(); } } } catch (Exception ex) { log.Debug($"catch:{ex.Message}"); log.Error(ex.Message); log.Error(ex.StackTrace); } } } }