using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.IO; using System.IO.Compression; using System.Linq; using System.Net; using System.Reflection; using System.Text; using Newtonsoft.Json; using Newtonsoft.Json.Linq; namespace DSWeb.Interface.YZ { public static class DataTableToJsonHelper { public static string ConvertToString(DataTable table) { StringWriter writer = new StringWriter(); JsonWriter jsonWriter = new JsonTextWriter(writer); jsonWriter.WriteStartArray(); foreach (DataRow row in table.Rows) { jsonWriter.WriteStartObject(); foreach (DataColumn col in table.Columns) { jsonWriter.WritePropertyName(col.ColumnName); jsonWriter.WriteValue(row[col.ColumnName]); } jsonWriter.WriteEndObject(); } jsonWriter.WriteEndArray(); return writer.ToString(); } public static JArray ConvertToJson(DataTable table) { JArray jarr = new JArray(); foreach (DataRow row in table.Rows) { JObject jobj = new JObject(); foreach (DataColumn col in table.Columns) { jobj.Add(col.ColumnName, new JValue(row[col.ColumnName])); } jarr.Add(jobj); } return jarr; } } public static class WebRequestHelper { public static string DoGet(string url) { string responseString = "";//post返回的结果 ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, err) => { return true; }; ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url); req.Method = "GET"; req.ContentLength = 0; var response = req.GetResponse(); Stream streamResponse = response.GetResponseStream(); StreamReader streamRead = new StreamReader(streamResponse); responseString = streamRead.ReadToEnd(); response.Close(); streamRead.Close(); return responseString; } public static string DoPost(string url, string json, int timeout = 10000) { string responseString = "";//post返回的结果 ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, err) => { return true; }; ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url); req.Method = "POST"; req.Timeout = timeout; req.Headers.Add("Accept-Encoding", "gzip, deflate"); if (!string.IsNullOrWhiteSpace(json)) { byte[] postBytes = Encoding.UTF8.GetBytes(json); req.ContentType = "application/json; charset=utf-8"; req.ContentLength = Encoding.UTF8.GetByteCount(json); Stream stream = req.GetRequestStream(); stream.Write(postBytes, 0, postBytes.Length); stream.Close(); } else { req.ContentLength = 0; } var response = req.GetResponse(); responseString = GetResponseBody((HttpWebResponse)response); return responseString; } public static string DoPost(string url, Dictionary dic, int timeout = 10000) { string responseString = "";//post返回的结果 ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, err) => { return true; }; ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url); req.Method = "POST"; req.Timeout = timeout; req.Headers.Add("Accept-Encoding", "gzip, deflate"); if (dic.Count > 0) { string strContent = string.Empty; foreach (var item in dic) { if (strContent.Length > 0) { strContent += "&"; } strContent += $"{item.Key}={item.Value}"; } byte[] postBytes = Encoding.UTF8.GetBytes(strContent); req.ContentType = "application/x-www-form-urlencoded"; req.ContentLength = postBytes.Length; Stream stream = req.GetRequestStream(); stream.Write(postBytes, 0, postBytes.Length); stream.Close(); } else { req.ContentLength = 0; } var response = req.GetResponse(); responseString = GetResponseBody((HttpWebResponse)response); return responseString; } private static string GetResponseBody(HttpWebResponse response) { string responseBody = string.Empty; if (response.ContentEncoding.ToLower().Contains("gzip")) { using (GZipStream stream = new GZipStream(response.GetResponseStream(), CompressionMode.Decompress)) { using (StreamReader reader = new StreamReader(stream)) { responseBody = reader.ReadToEnd(); } } } else if (response.ContentEncoding.ToLower().Contains("deflate")) { using (DeflateStream stream = new DeflateStream( response.GetResponseStream(), CompressionMode.Decompress)) { using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) { responseBody = reader.ReadToEnd(); } } } else { using (Stream stream = response.GetResponseStream()) { using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) { responseBody = reader.ReadToEnd(); } } } return responseBody; } } public class Helper { public static int ToInt32(object v) { try { if (v==null) { return 0; } return Convert.ToInt32(v); } catch (Exception) { return 0; } } public static DateTime? ToDateTime(object v) { try { if (v==null||v.ToString()=="") { return null; } return Convert.ToDateTime(v); } catch (Exception) { return null; } } public static decimal ToDecimal(object v) { try { return Convert.ToDecimal(v); } catch (Exception) { return 0; } } public static T DeepCopy(T obj) { //如果是字符串或值类型则直接返回 if (obj is string || obj.GetType().IsValueType) return obj; object retval = Activator.CreateInstance(obj.GetType()); FieldInfo[] fields = obj.GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static); foreach (FieldInfo field in fields) { try { field.SetValue(retval, DeepCopy(field.GetValue(obj))); } catch { } } return (T)retval; } public static List GetMBLNOForWLXX() { string sql = @"select t0.* from op_seae_billtrack_dy t0 left join op_seae_billtrack t1 on t0.MBLNO = t1.MBLNO where (DATEDIFF(day,t0.CreateTime,getDate()) < 15) and (t1.ZhuangZaiFangXing is null or t1.MaTouFangXing <>'Y') and t0.MBLNO <> '' order by t1.TrackTimesWLXX "; List list = new List(); using (SqlDataReader reader = DSSQLHelper.GetReader(sql)) { while (reader.Read()) { op_seae_billtrack_dy dy = new op_seae_billtrack_dy(); dy.GID = reader["GID"].ToString(); dy.UserID = reader["UserID"].ToString(); dy.MBLNO = reader["MBLNO"].ToString(); dy.CreateTime = ToDateTime(reader["CreateTime"].ToString()); if (reader["YARD"]==DBNull.Value) { dy.YARD = ""; } else { dy.YARD = reader["YARD"].ToString(); } if (reader["CARRIERID"] == DBNull.Value) { dy.CARRIERID = ""; } else { dy.CARRIERID = reader["CARRIERID"].ToString(); } dy.WX = ToInt32(reader["WX"]); dy.EMAIL = ToInt32(reader["EMAIL"]); list.Add(dy); } } return list; } public static List GetMBLNOForYARD() { string sql = @"select t0.* from op_seae_billtrack_dy t0 left join op_seae_billtrack t1 on t0.MBLNO = t1.MBLNO where ( DATEDIFF(day,t0.CreateTime,getDate()) < 15) and (t1.FanChangShiJian <>'已返场' or t1.FanChangShiJian is null) and t0.MBLNO <> '' order by t1.TrackTimesCZ "; List list = new List(); using (SqlDataReader reader = DSSQLHelper.GetReader(sql)) { while (reader.Read()) { op_seae_billtrack_dy dy = new op_seae_billtrack_dy(); dy.GID = reader["GID"].ToString(); dy.UserID = reader["UserID"].ToString(); dy.CreateTime = ToDateTime(reader["CreateTime"].ToString()); dy.MBLNO = reader["MBLNO"].ToString(); if (reader["YARD"] == DBNull.Value) { dy.YARD = ""; } else { dy.YARD = reader["YARD"].ToString(); } if (reader["CARRIERID"] == DBNull.Value) { dy.CARRIERID = ""; } else { dy.CARRIERID = reader["CARRIERID"].ToString(); } dy.WX = ToInt32(reader["WX"]); dy.EMAIL = ToInt32(reader["EMAIL"]); list.Add(dy); } } return list; } public static List GetMBLNOForFX() { string sql = @"select t0.* from op_seae_billtrack_dy t0 left join op_seae_billtrack t1 on t0.MBLNO = t1.MBLNO where t1.ata is null and t0.MBLNO <> '' order by t1.TrackTimesCZ "; List list = new List(); using (SqlDataReader reader = DSSQLHelper.GetReader(sql)) { while (reader.Read()) { op_seae_billtrack_dy dy = new op_seae_billtrack_dy(); dy.GID = reader["GID"].ToString(); dy.UserID = reader["UserID"].ToString(); dy.CreateTime = ToDateTime(reader["CreateTime"].ToString()); dy.MBLNO = reader["MBLNO"].ToString(); if (reader["YARD"] == DBNull.Value) { dy.YARD = ""; } else { dy.YARD = reader["YARD"].ToString(); } if (reader["CARRIERID"] == DBNull.Value) { dy.CARRIERID = ""; } else { dy.CARRIERID = reader["CARRIERID"].ToString(); } dy.WX = ToInt32(reader["WX"]); dy.EMAIL = ToInt32(reader["EMAIL"]); list.Add(dy); } } return list; } public static List GetMBLNOForYGT() { string sql = @"select t0.* from op_seae_billtrack_dy t0 left join op_seae_billtrack t1 on t0.MBLNO = t1.MBLNO where ( DATEDIFF(day,t0.CreateTime,getDate()) < 15) and t1.KaiChuanShiJian is null order by t1.TrackTimesQDPORT"; List list = new List(); using (SqlDataReader reader = DSSQLHelper.GetReader(sql)) { while (reader.Read()) { op_seae_billtrack_dy dy = new op_seae_billtrack_dy(); dy.GID = reader["GID"].ToString(); dy.UserID = reader["UserID"].ToString(); dy.MBLNO = reader["MBLNO"].ToString(); dy.CreateTime = ToDateTime(reader["CreateTime"].ToString()); if (reader["YARD"] == DBNull.Value) { dy.YARD = ""; } else { dy.YARD = reader["YARD"].ToString(); } if (reader["CARRIERID"] == DBNull.Value) { dy.CARRIERID = ""; } else { dy.CARRIERID = reader["CARRIERID"].ToString(); } dy.WX = ToInt32(reader["WX"]); dy.EMAIL = ToInt32(reader["EMAIL"]); list.Add(dy); } } return list; } public static List GetFXCarrierIDList() { string sql = "select CODENAME,EDICODE2 from info_client where ISCARRIER = 1"; List list = new List(); using (SqlDataReader reader = DSSQLHelper.GetReader(sql)) { while (reader.Read()) { FXCarrier fc = new FXCarrier(); fc.CarrierID = reader["CODENAME"].ToString(); fc.FXCID = reader["EDICODE2"].ToString(); list.Add(fc); } } return list; } public static DateTime? ConvertToUnixDateTime(string v) { try { if (v==null||v=="null") { return null; } string UnixTime = v.Substring(0, v.Length - 3); //时间戳 System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); DateTime TranslateDate = startTime.AddSeconds(double.Parse(UnixTime)); return TranslateDate; } catch (Exception) { return null; } } public static string GetCustMail(string uid) { string sql = "select top 1 EMAIL1 from user_baseinfo where USERID = '"+uid+"' "; string mail = ""; try { mail = DSSQLHelper.ExcuteScalarSQL(sql).ToString(); } catch (Exception) { mail = ""; } return mail; } } }