using log4net; using Newtonsoft.Json; using Quartz; using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.IO; using System.Linq; using System.Reflection; using System.Text; namespace JobReqWebData { public class JobGetImpData : IJob { private ILog log = LogManager.GetLogger(typeof(JobGetImpData)); private const string CfgFileName = "requestjson.cfg"; private static string CfgFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, CfgFileName); public void Execute(IJobExecutionContext context) { var requestjson = File.ReadAllText(CfgFilePath); if (string.IsNullOrEmpty(requestjson)) return; try { string connStr = context.JobDetail.JobDataMap.GetString("ConnectString"); string reqUrl = context.JobDetail.JobDataMap.GetString("ReqUrl"); string sqlQuery = context.JobDetail.JobDataMap.GetString("QuerySql"); string YGTUser = context.JobDetail.JobDataMap.GetString("YGTUser"); string YGTPsw = context.JobDetail.JobDataMap.GetString("YGTPsw"); string customer = context.JobDetail.JobDataMap.GetString("Customer"); string password = context.JobDetail.JobDataMap.GetString("Password"); int reqTimeout = Convert.ToInt32(context.JobDetail.JobDataMap.GetString("RequestTimeout")); string Yardid = context.JobDetail.JobDataMap.GetString("Yardid"); int SleepTime = Convert.ToInt32(context.JobDetail.JobDataMap.GetString("SleepTime")); YGTUser = YGTUser.Replace("\r\n", "\\"); YGTUser = YGTUser.Replace("\n", "\\"); YGTUser = YGTUser.Replace("\r", " "); string[] YGTUserList = YGTUser.Split('\\'); YGTPsw = YGTPsw.Replace("\r\n", "\\"); YGTPsw = YGTPsw.Replace("\n", "\\"); YGTPsw = YGTPsw.Replace("\r", " "); string[] YGTPswList = YGTPsw.Split('\\'); log.Debug($"连接字符串:{connStr}"); log.Debug($"请求URL:{reqUrl}"); log.Debug($"请求JSON:{requestjson}"); 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 mblno = ""; int YGTindex = 0; foreach (DataRow row in tQuery.Rows) { foreach (DataColumn col in tQuery.Columns) { if (string.IsNullOrEmpty(YGTUserList[YGTindex]) || string.IsNullOrEmpty(YGTPswList[YGTindex])) YGTindex = YGTindex + 1; if (YGTindex >= YGTUserList.Length) YGTindex = 0; mblno = row["MBLNO"].ToString(); Dictionary dic = new Dictionary(); dic.Add("webusername", YGTUserList[YGTindex]); dic.Add("webuserpass", YGTPswList[YGTindex]); dic.Add("mblno", mblno); dic.Add("yardid", Yardid); dic.Add("isweb","0"); dic.Add("custname", customer); dic.Add("psw", password); dic.Add("jck", "jk"); string rtn = WebRequestHelper.DoPost(reqUrl, dic, reqTimeout * 1000); YGTindex = YGTindex + 1; log.Debug($"数据返回:{rtn}"); var parseJsonObj = JsonConvert.DeserializeObject(rtn); if (parseJsonObj.status == "1") { var parseETAJsonObj = JsonConvert.DeserializeObject(rtn); //入库 if (parseETAJsonObj.messgae != null) { using (SqlConnection dbcon = new SqlConnection(connStr)) { dbcon.Open(); var bsno = GETBSNO(mblno, dbcon); if (!string.IsNullOrEmpty(bsno)) { if (parseETAJsonObj.messgae.originalInfos!=null&&parseETAJsonObj.messgae.originalInfos.Count != 0) { string sql = " delete from [op_status] where [STATUS]='舱单' and bsno='" + bsno + "' insert into op_status (ST_ID,BSNO,STATUS,ISCOMP,COMPTIME,COMPOP,INPUTTIME,STTYPE) Select NEWID(),BSNO,'舱单',1,GETDATE(),'ADMIN',GETDATE(),'1' FROM V_OP_BS WHERE BSNO='" + bsno + "'"; SqlCommand cmd = new SqlCommand(sql, dbcon); cmd.ExecuteNonQuery(); } if (parseETAJsonObj.messgae.changeBno!=null&&!string.IsNullOrEmpty(parseETAJsonObj.messgae.changeBno.billTime)) { string sql = " delete from [op_status] where [STATUS]='换单' and bsno='" + bsno + "' insert into op_status (ST_ID,BSNO,STATUS,ISCOMP,COMPTIME,COMPOP,INPUTTIME,STTYPE) Select NEWID(),BSNO,'换单',1,'" + parseETAJsonObj.messgae.changeBno.billTime + "','ADMIN',GETDATE(),'1' FROM V_OP_BS WHERE BSNO='" + bsno + "'"; SqlCommand cmd = new SqlCommand(sql, dbcon); cmd.ExecuteNonQuery(); } if (parseETAJsonObj.messgae.customsRelease!=null&&!string.IsNullOrEmpty(parseETAJsonObj.messgae.customsRelease.permitTime)) { string sql = " delete from [op_status] where [STATUS]='海关放行' and bsno='" + bsno + "' insert into op_status (ST_ID,BSNO,STATUS,ISCOMP,COMPTIME,COMPOP,INPUTTIME,STTYPE) Select NEWID(),BSNO,'海关放行',1,'" + parseETAJsonObj.messgae.customsRelease.permitTime + "','ADMIN',GETDATE(),'1' FROM V_OP_BS WHERE BSNO='" + bsno + "'"; SqlCommand cmd = new SqlCommand(sql, dbcon); cmd.ExecuteNonQuery(); } } dbcon.Close(); } } } else { log.Debug($"返回数据为空"); } System.Threading.Thread.Sleep(1000* SleepTime); } } } catch (Exception ex) { log.Error(ex.Message); log.Error(ex.StackTrace); } } static public string GETBSNO(string MBLNO, SqlConnection dbcon) { var strSql = new StringBuilder(); strSql.Append("SELECT BSNO from V_OP_BS where MBLNO='" + MBLNO + "'"); SqlDataAdapter adapter = new SqlDataAdapter(strSql.ToString(), dbcon); DataTable table = new DataTable(); adapter.Fill(table); var BSNO = ""; if (table.Rows.Count > 0) { foreach (DataRow row in table.Rows) { BSNO = row["BSNO"].ToString(); } } return BSNO; } } }