/// <summary> /// 获取一条冷链数据上报进度 /// </summary> /// <param name="senderCodeList"></param> /// <returns></returns> private static UploadDataProgress GetNextWaitUploadDataProgress(List <string> senderCodeList) { UploadDataProgress progress = null; string sql = "select * from (select p.*,o.ShipmentCode from uploadDataProgress p left join huadong_tms_order o on p.relationId = o.SHIPDETAILID or p.relationId=o.LEGCODE where p.handleFlag = -1 and endnodeTime is not null and 【SecretKey】 order by p.nodetime limit 1)a left JOIN coldStorage b on a.StorageId = b.id"; string secretkeySql = "SecretKey ='" + Utility._SecretKey + "' and SHIPMENTCODE<>''"; if (senderCodeList != null) { secretkeySql = string.Format("SecretKey in ('{0}')", string.Join("','", senderCodeList)); } sql = sql.Replace("【SecretKey】", secretkeySql); using (MySqlDataReader reader = DbHelperMySQL.ExecuteReader(sql)) { if (reader.Read()) { progress = new UploadDataProgress(); progress.Id = Convert.ToInt32(reader["Id"]); progress.relationId = reader["relationId"].ToString(); progress.storageId = Convert.ToInt32(reader["storageId"]); progress.storageName = reader["storageName"].ToString(); progress.nodeTime = Convert.ToDateTime(reader["nodeTime"]); progress.endNodeTime = Convert.ToDateTime(reader["endNodeTime"]); progress.uploadProgress = Convert.ToDateTime(reader["uploadProgress"]); progress.shipmentCode = reader["shipmentCode"].ToString(); progress.storageType = Convert.ToInt32(reader["storageType"]); } } if (progress == null) { int count = 0; try { //本轮检测完毕,重置标记,开始下一轮检测 sql = "update uploadDataProgress p inner join (select relationId from huadong_tms_order where 【SecretKey】) as o set handleflag = -1 where handleflag=0 and p.relationId in(o.relationId)"; sql = sql.Replace("【SecretKey】", secretkeySql); count = DbHelperMySQL.ExecuteSql(sql); Console.WriteLine("共" + count + "条处理进度已处理完一轮,已重置进度标记成功."); } catch (Exception ex) { Console.WriteLine("共" + count + "条处理进度已处理完一轮,重置进度标记时失败:" + ex.Message); } } return(progress); }
/// <summary> /// 更新冷链数据上报处理进度 /// </summary> /// <param name="progress"></param> /// <returns></returns> public static bool UpdateUploadProgress(UploadDataProgress progress, bool haveData = true) { int handleFlag = 0; string sql = string.Empty; try { if (haveData && progress.endNodeTime > progress.uploadProgress) { sql = string.Format("update uploadDataProgress set lastHandleTime='{0}',uploadProgress='{1}' where Id = {2}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), progress.uploadProgress.ToString("yyyy-MM-dd HH:mm:ss"), progress.Id); DbHelperMySQL.ExecuteSql(sql); return(true);//如果还有数据则不更新标记 } handleFlag = progress.endNodeTime <= progress.uploadProgress ? 1 : 0; sql = string.Format("update uploadDataProgress set handleFlag='{0}',lastHandleTime='{1}',uploadProgress='{3}' where Id = {2}", handleFlag, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), progress.Id, progress.uploadProgress.ToString("yyyy-MM-dd HH:mm:ss")); return(DbHelperMySQL.ExecuteSql(sql) == 1); } finally { try { if (Utility._LinkType == "2" && handleFlag == 1) { //检查是否所有节点与冷链数据都已经上报完成 sql = string.Format("select count(*)-p.c as count from waybill_node n INNER JOIN (select Count(*) c from uploaddataprogress where relationId='{0}' and handleFlag=1) p where n.scannumber='{0}';", progress.relationId); int count = Convert.ToInt32(DbHelperMySQL.GetSingle(sql)); if (count == 1) { //数据已上报完成 M_TMSEnd end = new M_TMSEnd() { Arrived = true, OrderNo = progress.relationId, JcOrderNo = GetJcOrderNoForProgress(progress.relationId) }; string xmlStr = Utility.ParseXMLToString(end); string receiverCode = TMSOrderServer.GetAllLoginkSenderCode("2")[0]; LoginkHelp.Send(Utility._SecurityURL, Utility._TransportURL, Utility._MyCode, Utility._MyPwd, receiverCode, xmlStr, ActionType.JTWL_ENTRUST_TRANS_BookingNoteStatus); } } } catch (Exception ex) { Console.WriteLine("上报TMSEnd失败:" + ex.Message); } } }
private void _bw_DoWork(object sender, DoWorkEventArgs e) { DateTime dtNow = DateTime.Now; string msg = dtNow.ToString() + ":【订单温湿度上报】 "; string xmlResult = string.Empty; UploadDataProgress relation = new UploadDataProgress(); try { List <string> senderCodeList = null; List <M_TMSData> loginkData = null; if (Utility._LinkType == "2") { senderCodeList = TMSOrderServer.GetAllLoginkSenderCode(); if (senderCodeList.Count == 0) { msg += "没有配置通过运管平台交换数据的上游发货单位"; return; } } XML details = DataUploadServer.GetNextWaitUploadDatasProgress(senderCodeList, ref loginkData, out relation); if (relation != null && relation.Id != 0) { msg += string.Format("R【{0}】 O【{1}】 T【{2}】", relation.Id, relation.relationId, relation.uploadProgress); } if (details == null) { if (relation != null && relation.Id != 0) // && !_ignoreTempRelationList.Contains(relation.Id)) // DataUploadServer.UpdateHandleTHTime(relation.Id, false); //_ignoreTempRelationList.Add(relation.Id); { msg += "没有新的节点数据[relationId:" + relation.Id + "]"; } return; } //运管平台上报 if (senderCodeList != null) { string dataXml = Utility.ParseXMLToString(loginkData); string receiverCode = TMSOrderServer.GetAllLoginkSenderCode()[0]; LoginkHelp.Send(Utility._SecurityURL, Utility._TransportURL, Utility._MyCode, Utility._MyPwd, receiverCode, dataXml, ActionType.LOGINK_CN_TRANSPORT_PREBOOKING); msg += "上报成功 Receiver[" + receiverCode + "]"; bool isUpdate = DataUploadServer.UpdateUploadProgress(relation);//, ref _ignoreTempRelationList); Utility.SaveXMLRequestAndRespond(dataXml, string.Empty, "Temperature", relation.relationId); msg += isUpdate ? "更新进度成功" : "更新进度失败"; return; } _UploadXML.MESSAGEHEAD.SENDTIME = dtNow.ToString("yyyy-MM-dd HH:mm:ss"); _UploadXML.MESSAGEHEAD.FILENAME = dtNow.ToString("yyyyMMddHHmmss"); _UploadXML.MESSAGEDETAIL = "<![CDATA[" + Utility.ParseXMLToString <Model.NodeDataUpload.XML>(details) + "]]>"; string xmlRequest = Utility.ParseXMLToString <Model.NodeUpload.XML>(_UploadXML).Replace("<", "<").Replace(">", ">"); StringBuilder param = new StringBuilder(); param.Append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://service.webservice.vtradex.com\" > "); param.Append("<soapenv:Header/>"); param.Append("<soapenv:Body>"); param.Append("<ser:temperatureXml1>" + xmlRequest + "</ser:temperatureXml1>"); param.Append("</soapenv:Body>"); param.Append("</soapenv:Envelope>"); xmlResult = HTTPHelper.SendHTTPRequest(param.ToString(), "getTmsShipmentTemperature", "temperatureXml1"); Model.NodeUpload.XML uploadResult = Utility.ParseXMLToObjec <Model.NodeUpload.XML>(xmlResult, param.ToString()); if (uploadResult.MSGCODE == "0" || string.IsNullOrEmpty(uploadResult.MSGCODE)) { msg += "上报失败 " + uploadResult.MSGCONTENT; } else if (uploadResult.MSGCODE == "1") { msg += "上报成功 "; try { msg += DataUploadServer.UpdateUploadProgress(relation) ? "更新进度成功." : "更新进度失败."; } catch (Exception ex1) { msg += ex1.Message; } } Utility.SaveXMLRequestAndRespond(param.ToString(), xmlResult, "Temperature", relation.relationId); } catch (Exception ex) { msg += ex.Message; Utility.SaveErrLog(ex.Message, "Temperature"); //if (relation.Id != 0 )//&& _ignoreTempRelationList.Contains(relation.Id) == false) // DataUploadServer.UpdateHandleTHTime(relation.Id, false); //_ignoreTempRelationList.Add(relation.Id); if (relation != null) { if (!ex.Message.Contains("重新开始") && !ex.Message.Contains("成功") && !ex.Message.Contains("没有新的载体温湿度")) { //DataUploadServer.UpdateHandleTHTime(relation.Id, false); SendEmail(new Exception(msg)); } } } finally { Console.WriteLine(msg.Replace("\n", "")); } }
public static Model.NodeDataUpload.XML GetNextWaitUploadDatasProgress(List <string> senderCodeList, ref List <M_TMSData> loginkData, out UploadDataProgress progress) { Model.NodeDataUpload.XML result = null; progress = null; try { progress = GetNextWaitUploadDataProgress(senderCodeList); if (progress != null) { DataTable dt = GetStorageData(0, progress.uploadProgress, progress.endNodeTime.AddDays(int.Parse(Utility._StorageDataTimeOut)), progress.storageId); if (dt == null) { string info = string.Format("PId【{0}】 RId【{1}】 Time【{2}】", progress.Id, progress.relationId, progress.uploadProgress.ToString("yyyy-MM-dd HH:mm:ss")); if ((DateTime.Now - progress.uploadProgress).TotalDays > int.Parse(Utility._StorageDataTimeOut)) { progress.uploadProgress = DateTime.Now; //progress.endNodeTime; UpdateUploadProgress(progress, false); throw new Exception(info + "放弃等待当前载体温湿度,已过" + int.Parse(Utility._StorageDataTimeOut) + "*24小时"); } else { UpdateUploadProgress(progress, false); throw new Exception(info + "没有新的载体温湿度."); } } result = new Model.NodeDataUpload.XML(); result.CONTENTLIST = new List <CONTENT>(); result.CONTENTLIST.Add(new CONTENT()); result.CONTENTLIST[0].DETAILLIST = new List <DETAIL>(); DETAIL dModel = new DETAIL(); dModel.ECNO = progress.shipmentCode; dModel.LEGNO = progress.relationId; Utility.AddLogText(string.Format("dModel:ECNO:{0},LEGNO:{1},RelationId:{2}", dModel.ECNO, dModel.LEGNO, progress.relationId)); if (progress.storageType == 1) { dModel.WAREHOUSECODE = progress.storageName; } else if (progress.storageType == 2) { dModel.LICENSENO = progress.storageName; } //GetStorageByNodeId((int)relation.CurrentUploadDataNodeId, ref dModel); loginkData = new List <M_TMSData>(); foreach (DataRow row in dt.Rows) { string temp = row["t"] is DBNull ? string.Empty : row["t"].ToString(); string hump = "-300.0"; try { hump = row["h"] is DBNull ? string.Empty : row["h"].ToString(); } catch { } if (temp == "System.Byte[]") { temp = System.Text.Encoding.Default.GetString(row["t"] as byte[]); } if (hump == "System.Byte[]") { hump = System.Text.Encoding.Default.GetString(row["h"] as byte[]); } DETAIL detail = new DETAIL(); detail.ECNO = dModel.ECNO; detail.LEGNO = dModel.LEGNO; detail.LICENSENO = dModel.LICENSENO; detail.WAREHOUSECODE = dModel.WAREHOUSECODE; detail.TRACKTIME = Convert.ToDateTime(row["datatime"]).ToString("yyyy-MM-dd HH:mm:ss"); detail.TEMPREATURE = temp; //.Replace("-300.0", "").Replace("-300", ""); detail.HUMIDITY = hump; //.Replace("-300.0", "").Replace("-300", ""); try { detail.LONGITUDE = row["lo"] is DBNull ? string.Empty : row["lo"].ToString(); } catch { } try { detail.LATITUDE = row["la"] is DBNull ? string.Empty : row["la"].ToString(); } catch { } Utility.AddLogText(string.Format("detail:ECNO:{0},LEGNO:{1}", detail.ECNO, detail.LEGNO)); //result.CONTENTLIST[0].DETAILLIST.Add(detail); M_TMSData data = new M_TMSData(); data.Latitude = detail.LATITUDE == string.Empty ? null : detail.LATITUDE.ToString(); data.Longitude = detail.LONGITUDE == string.Empty ? null : detail.LONGITUDE.ToString(); data.RecordTime = Convert.ToDateTime(row["datatime"]); data.OrderNo = progress.relationId; data.JcOrderNo = GetJcOrderNoForProgress(progress.relationId); data.JcNodeId = GetJcNodeIdForProgress(progress.relationId, progress.storageId, progress.nodeTime.ToString("yyyy-MM-dd HH:mm:ss")); Type t = data.GetType(); for (int i = 0; i < temp.Split('|').Count(); i++) { t.GetProperty("T" + (i + 1)).SetValue(data, temp.Split('|')[i].ToString(), null); } for (int i = 0; i < hump.Split('|').Count(); i++) { t.GetProperty("RH" + (i + 1)).SetValue(data, hump.Split('|')[i].ToString(), null); } //loginkData.Add(data); progress.uploadProgress = DateTime.Parse(detail.TRACKTIME); if (DateTime.Parse(detail.TRACKTIME) > progress.endNodeTime) { progress.uploadProgress = progress.endNodeTime; if (loginkData.Count == 0) { bool isUpdate = DataUploadServer.UpdateUploadProgress(progress); throw new Exception("仅一条末尾数据且大于节点结束时间,直接更新此进度:" + (isUpdate ? "成功" : "失败")); } break; } else { loginkData.Add(data); result.CONTENTLIST[0].DETAILLIST.Add(detail); } } } } catch (Exception ex) { throw new Exception("获取冷链数据失败:" + ex.Message); } return(result); }