using JobYunDang.Model; using log4net; using Newtonsoft.Json; using Quartz; using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Text; using YDN; namespace JobYunDang { public class JobDownloadData : IJob { private ILog log = LogManager.GetLogger(typeof(JobDownloadData)); public void Execute(IJobExecutionContext context) { string connStr = context.JobDetail.JobDataMap.GetString("ConnectString"); string reqUrl = context.JobDetail.JobDataMap.GetString("ReqUrl"); string companyCode = context.JobDetail.JobDataMap.GetString("CompanyCode"); string companySecret = context.JobDetail.JobDataMap.GetString("CompanySecret"); string querySql = context.JobDetail.JobDataMap.GetString("QuerySql"); using (SqlConnection dbcon = new SqlConnection(connStr)) { SqlDataAdapter adapter = new SqlDataAdapter(querySql, dbcon); DataTable table = new DataTable(); adapter.Fill(table); if (table.Columns.Contains("mblno") && table.Columns.Contains("carrierid")) { if (table.Rows.Count > 0) { List listOjb = new List(); foreach (DataRow row in table.Rows) { if (row["mblno"].ToString()!=""&& row["carrierid"].ToString() !=""&& row["portloadid"].ToString() != "") { var obj = new { referenceno = row["mblno"].ToString(), carriercd = row["carrierid"].ToString(), pol = row["portloadid"].ToString() }; listOjb.Add(obj); log.Debug($"下载数据请求,主提单号:{row["mblno"].ToString()}"); } } var map = new Dictionary(); map.Add("data", JsonConvert.SerializeObject(listOjb)); var urlMap = new Dictionary(); urlMap.Add("companyid", companyCode); var html = RequestApi.HttpPost(reqUrl, companySecret, urlMap, map).Replace("'", "''"); if (html.IndexOf("exceptioncode") >= 0) { } else { var list = JsonConvert.DeserializeObject>(html); dbcon.Open(); StringBuilder sbSql = new StringBuilder(); foreach (var track in list) { try { if (dbcon.State==0) dbcon.Open(); if (track.success) { using (var trans = dbcon.BeginTransaction()) { sbSql.Clear(); #region ydw_linertracking sbSql.AppendLine($"delete ydw_linertracking where keyid='{track.objlinertracking.keyid}';"); string sql_linertracking = $@"INSERT INTO [ydw_linertracking] (keyid, blprefix, referenceno, blno, bkgno, trfsno, ctnrno, isctnr, bkgvolumn, carriercd, carrier, currentnodeplace, currentnodetime, currentnode, updatetime, createtime, endtime, pol, dtp, terminalcd, terminal, dptloadid, vslname, voy, declarationno, agentcd, currentvslname, currentvoy, longtitude, latitude) VALUES ( '{track.objlinertracking.keyid}',--keyid '{track.objlinertracking.blprefix}',--blprefix '{track.objlinertracking.referenceno}',--referenceno '{track.objlinertracking.blno}',--blno '{track.objlinertracking.bkgno}',--bkgno '{track.objlinertracking.trfsno}',--trfsno '{track.objlinertracking.ctnrno}',--ctnrno '{track.objlinertracking.isctnr}',--isctnr '{track.objlinertracking.bkgvolumn}',--bkgvolumn '{track.objlinertracking.carriercd}',--carriercd '{track.objlinertracking.carrier}',--carrier '{track.objlinertracking.currentnodeplace}',--currentnodeplace {(track.objlinertracking.currentnodetime.HasValue ? "'" + track.objlinertracking.currentnodetime.Value.ToString() + "'" : "null")},--currentnodetime '{track.objlinertracking.currentnode}',--currentnode {(track.objlinertracking.updatetime.HasValue ? "'" + track.objlinertracking.updatetime.Value.ToString() + "'" : "null")},--updatetime {(track.objlinertracking.createtime.HasValue ? "'" + track.objlinertracking.createtime.Value.ToString() + "'" : "null")},--createtime {(track.objlinertracking.endtime.HasValue ? "'" + track.objlinertracking.endtime.Value.ToString() + "'" : "null")},--endtime '{track.objlinertracking.pol}',--pol '{track.objlinertracking.dtp}',--dtp '{track.objlinertracking.terminalcd}',--terminalcd '{track.objlinertracking.terminal}',--terminal '{track.objlinertracking.dptloadid}',--dptloadid '{track.objlinertracking.vslname}',--vslname '{track.objlinertracking.voy}',--voy '{track.objlinertracking.declarationno}',--declarationno '{track.objlinertracking.agentcd}',--agentcd '{track.objlinertracking.currentvslname}',--currentvslname '{track.objlinertracking.currentvoy}',--currentvoy {(track.objlinertracking.longtitude.HasValue ? track.objlinertracking.longtitude.Value.ToString() : "null")},--longtitude {(track.objlinertracking.latitude.HasValue ? track.objlinertracking.latitude.Value.ToString() : "null")}--latitude );"; sbSql.AppendLine(sql_linertracking); #endregion #region ydw_linertrackingstatus sbSql.AppendLine($"delete ydw_linertrackingstatus where fkeyid='{track.objlinertracking.keyid}';"); if (track.objlinertracking.lstlinertrackingstatus != null && track.objlinertracking.lstlinertrackingstatus.Count > 0) { foreach (var objsta in track.objlinertracking.lstlinertrackingstatus) { string sql_linertrackingstatus = $@"INSERT INTO [ydw_linertrackingstatus] ( keyid, fkeyid, blno, statuscd, statedescription, statustime, statusplace, vslname, voy, isest, cancelid, updatetime, createtime ) VALUES ( '{objsta.keyid}',--keyid '{objsta.fkeyid}',--fkeyid '{objsta.blno}',--blno '{objsta.statuscd}',--statuscd '{objsta.statedescription}',--statedescription {(objsta.statustime.HasValue ? "'" + objsta.statustime.Value.ToString() + "'" : "null")},--statustime '{objsta.statusplace}',--statusplace '{objsta.vslname}',--vslname '{objsta.voy}',--voy '{objsta.isest}',--isest '{objsta.cancelid}',--cancelid {(objsta.updatetime.HasValue ? "'" + objsta.updatetime.Value.ToString() + "'" : "null")},--updatetime {(objsta.createtime.HasValue ? "'" + objsta.createtime.Value.ToString() + "'" : "null")}--createtime );"; sbSql.AppendLine(sql_linertrackingstatus); } } #endregion #region ydw_billinfo if (track.objlinertracking.objbillinfo != null) { log.Debug($"下载数据,主提单号:{track.objlinertracking.objbillinfo.blprefix + track.objlinertracking.objbillinfo.blno+"-"+ track.objlinertracking.objbillinfo.bkgno}"); #region ydw_billinfo主表 sbSql.AppendLine($"update op_seae set YDWUpdate=GETDATE() where mblno='{track.objlinertracking.objbillinfo.blprefix + track.objlinertracking.objbillinfo.blno}' or mblno='{track.objlinertracking.objbillinfo.bkgno}';"); sbSql.AppendLine($"delete ydw_billinfo where keyid='{track.objlinertracking.objbillinfo.keyid}';"); string sql_billinfo = $@"INSERT INTO [ydw_billinfo] ( keyid, blprefix, bkgno, blno, carriercd, carrier, plrcd, plr, polcd, pol, dtpcd, dtp, pldcd, pld, bkgvolumn, vslname, voy, updatetime, createtime, blid, currentnode, currentnodetime, currentnodeplace ) VALUES ( '{track.objlinertracking.objbillinfo.keyid}',--keyid '{track.objlinertracking.objbillinfo.blprefix}',--blprefix '{track.objlinertracking.objbillinfo.bkgno}',--bkgno '{track.objlinertracking.objbillinfo.blno}',--blno '{track.objlinertracking.objbillinfo.carriercd}',--carriercd '{track.objlinertracking.objbillinfo.carrier}',--carrier '{track.objlinertracking.objbillinfo.plrcd}',--plrcd '{track.objlinertracking.objbillinfo.plr}',--plr '{track.objlinertracking.objbillinfo.polcd}',--polcd '{track.objlinertracking.objbillinfo.pol}',--pol '{track.objlinertracking.objbillinfo.dtpcd}',--dtpcd '{track.objlinertracking.objbillinfo.dtp}',--dtp '{track.objlinertracking.objbillinfo.pldcd}',--pldcd '{track.objlinertracking.objbillinfo.pld}',--pld '{track.objlinertracking.objbillinfo.bkgvolumn}',--bkgvolumn '{track.objlinertracking.objbillinfo.vslname}',--vslname '{track.objlinertracking.objbillinfo.voy}',--voy {(track.objlinertracking.objbillinfo.updatetime.HasValue ? "'" + track.objlinertracking.objbillinfo.updatetime.Value.ToString() + "'" : "null")},--updatetime {(track.objlinertracking.objbillinfo.createtime.HasValue ? "'" + track.objlinertracking.objbillinfo.createtime.Value.ToString() + "'" : "null")},--createtime '{track.objlinertracking.objbillinfo.blid}',--blid '{track.objlinertracking.objbillinfo.currentnode}',--currentnode {(track.objlinertracking.objbillinfo.currentnodetime.HasValue ? "'" + track.objlinertracking.objbillinfo.currentnodetime.Value.ToString() + "'" : "null")},--currentnodetime '{track.objlinertracking.objbillinfo.currentnodeplace}'--currentnodeplace );"; sbSql.AppendLine(sql_billinfo); #endregion #region ydw_carriage if (track.objlinertracking.objbillinfo.lstcarriages != null) { sbSql.AppendLine($"delete ydw_carriage where bkeyid='{track.objlinertracking.objbillinfo.keyid}';"); foreach (var objcarr in track.objlinertracking.objbillinfo.lstcarriages) { string sql_carriage = $@"INSERT INTO [ydw_carriage] ( keyid, bkeyid, sno, vslname, voy, blno, pol, pod, etd, eta, atd, ata ) VALUES ( '{objcarr.keyid}',--keyid '{objcarr.bkeyid}',--bkeyid {(objcarr.sno.HasValue ? objcarr.sno.Value.ToString() : "null")},--sno '{objcarr.vslname}',--vslname '{objcarr.voy}',--voy '{objcarr.blno}',--blno '{objcarr.pol}',--pol '{objcarr.pod}',--pod {(objcarr.etd.HasValue ? "'" + objcarr.etd.Value.ToString() + "'" : "null")},--etd {(objcarr.eta.HasValue ? "'" + objcarr.eta.Value.ToString() + "'" : "null")},--eta {(objcarr.atd.HasValue ? "'" + objcarr.atd.Value.ToString() + "'" : "null")},--atd {(objcarr.ata.HasValue ? "'" + objcarr.ata.Value.ToString() + "'" : "null")}--ata );"; sbSql.AppendLine(sql_carriage); } } #endregion #region ydw_ctnrinfo if (track.objlinertracking.objbillinfo.lstctnrinfos != null) { sbSql.AppendLine($"delete ydw_ctnrinfo where bkeyid='{track.objlinertracking.objbillinfo.keyid}';"); foreach (var objctnr in track.objlinertracking.objbillinfo.lstctnrinfos) { string sql_ctnrinfo = $@"INSERT INTO [ydw_ctnrinfo] ( keyid, bkeyid, vkeyid, conno, sealno, csize, ctype, ctnrtype, pkgs, gwgt, vgm, currentstatus, currenttime, currentstation, updatetime ) VALUES ( '{objctnr.keyid}',--keyid '{objctnr.bkeyid}',--bkeyid '{objctnr.vkeyid}',--vkeyid '{objctnr.conno}',--conno '{objctnr.sealno}',--sealno '{objctnr.csize}',--csize '{objctnr.ctype}',--ctype '{objctnr.ctnrtype}',--ctnrtype '{objctnr.pkgs}',--pkgs '{objctnr.gwgt}',--gwgt '{objctnr.vgm}',--vgm '{objctnr.currentstatus}',--currentstatus {(objctnr.currenttime.HasValue ? "'" + objctnr.currenttime.Value.ToString() + "'" : "null")},--currenttime '{objctnr.currentstation}',--currentstation {(objctnr.updatetime.HasValue ? "'" + objctnr.updatetime.Value.ToString() + "'" : "null")}--updatetime );"; sbSql.AppendLine(sql_ctnrinfo); #region ydw_ctnrstatus if (objctnr.lstctnrstatus != null) { sbSql.AppendLine($"delete ydw_ctnrstatus where ckeyid='{objctnr.vkeyid}';"); foreach (var objctnrstatus in objctnr.lstctnrstatus) { string sql_ctnrstatus = $@"INSERT INTO [ydw_ctnrstatus] ( keyid, ckeyid, status, statedescription, vslvoy, vslname, voy, eventtime, station, transportation, iscode, isest, updatetime ) VALUES ( '{objctnrstatus.keyid}',--keyid '{objctnrstatus.ckeyid}',--ckeyid '{objctnrstatus.status}',--status '{objctnrstatus.statedescription}',--statedescription '{objctnrstatus.vslvoy}',--vslvoy '{objctnrstatus.vslname}',--vslname '{objctnrstatus.voy}',--voy {(objctnrstatus.eventtime.HasValue ? "'" + objctnrstatus.eventtime.Value.ToString() + "'" : "null")},--eventtime '{objctnrstatus.station}',--station '{objctnrstatus.transportation}',--transportation '{objctnrstatus.iscode}',--iscode '{objctnrstatus.isest}',--isest {(objctnrstatus.updatetime.HasValue ? "'" + objctnrstatus.updatetime.Value.ToString() + "'" : "null")}--updatetime );"; sbSql.AppendLine(sql_ctnrstatus); } } #endregion } } #endregion } #endregion #region ydw_terminaloptime // sbSql.AppendLine($"delete ydw_terminaloptime where keyid='{track.objlinertracking.keyid}';"); // if (track.objlinertracking.objterminalop != null) // { // //测试发现track.objlinertracking.objterminalop.keyid返回null // if (track.objlinertracking.objterminalop.keyid == null) // { // track.objlinertracking.objterminalop.keyid = Guid.NewGuid().ToString(); // } // string sql_terminaloptime = $@"INSERT INTO [ydw_terminaloptime] // ( //keyid, //fkeyid, //vslcd, //vslname, //vslnamecn, //voyin, //voyout, //servicecd, //servicename, //carriercd, //carrier, //terminalcd, //terminal, //eta, //ata, //atd, //etd, //cyopentime, //fumigationtime, //cyclosetime, //customcutofftime, //cycutofftime, //carriagecutofftime, //cutofftime, //remarks, //createtime, //updatetime, //berthno // ) // VALUES // ( //'{track.objlinertracking.objterminalop.keyid}',--keyid //'{track.objlinertracking.objterminalop.fkeyid}',--fkeyid //'{track.objlinertracking.objterminalop.vslcd}',--vslcd //'{track.objlinertracking.objterminalop.vslname}',--vslname //'{track.objlinertracking.objterminalop.vslnamecn}',--vslnamecn //'{track.objlinertracking.objterminalop.voyin}',--voyin //'{track.objlinertracking.objterminalop.voyout}',--voyout //'{track.objlinertracking.objterminalop.servicecd}',--servicecd //'{track.objlinertracking.objterminalop.servicename}',--servicename //'{track.objlinertracking.objterminalop.carriercd}',--carriercd //'{track.objlinertracking.objterminalop.carrier}',--carrier //'{track.objlinertracking.objterminalop.terminalcd}',--terminalcd //'{track.objlinertracking.objterminalop.terminal}',--terminal //{(track.objlinertracking.objterminalop.eta.HasValue ? "'" + track.objlinertracking.objterminalop.eta.Value.ToString() + "'" : "null")},--eta //{(track.objlinertracking.objterminalop.ata.HasValue ? "'" + track.objlinertracking.objterminalop.ata.Value.ToString() + "'" : "null")},--ata //{(track.objlinertracking.objterminalop.atd.HasValue ? "'" + track.objlinertracking.objterminalop.atd.Value.ToString() + "'" : "null")},--atd //{(track.objlinertracking.objterminalop.etd.HasValue ? "'" + track.objlinertracking.objterminalop.etd.Value.ToString() + "'" : "null")},--etd //{(track.objlinertracking.objterminalop.cyopentime.HasValue ? "'" + track.objlinertracking.objterminalop.cyopentime.Value.ToString() + "'" : "null")},--cyopentime //{(track.objlinertracking.objterminalop.fumigationtime.HasValue ? "'" + track.objlinertracking.objterminalop.fumigationtime.Value.ToString() + "'" : "null")},--fumigationtime //{(track.objlinertracking.objterminalop.cyclosetime.HasValue ? "'" + track.objlinertracking.objterminalop.cyclosetime.Value.ToString() + "'" : "null")},--cyclosetime //{(track.objlinertracking.objterminalop.customcutofftime.HasValue ? "'" + track.objlinertracking.objterminalop.customcutofftime.Value.ToString() + "'" : "null")},--customcutofftime //{(track.objlinertracking.objterminalop.cycutofftime.HasValue ? "'" + track.objlinertracking.objterminalop.cycutofftime.Value.ToString() + "'" : "null")},--cycutofftime //{(track.objlinertracking.objterminalop.carriagecutofftime.HasValue ? "'" + track.objlinertracking.objterminalop.carriagecutofftime.Value.ToString() + "'" : "null")},--carriagecutofftime //{(track.objlinertracking.objterminalop.cutofftime.HasValue ? "'" + track.objlinertracking.objterminalop.cutofftime.Value.ToString() + "'" : "null")},--cutofftime //'{track.objlinertracking.objterminalop.remarks}',--remarks //{(track.objlinertracking.objterminalop.createtime.HasValue ? "'" + track.objlinertracking.objterminalop.createtime.Value.ToString() + "'" : "null")},--createtime //{(track.objlinertracking.objterminalop.updatetime.HasValue ? "'" + track.objlinertracking.objterminalop.updatetime.Value.ToString() + "'" : "null")},--updatetime //'{track.objlinertracking.objterminalop.berthno}'--berthno // );"; // sbSql.AppendLine(sql_terminaloptime); // } #endregion SqlCommand cmdIns = new SqlCommand(sbSql.ToString(), dbcon, trans); cmdIns.ExecuteNonQuery(); trans.Commit(); } } } catch (Exception ex) { log.Error(ex.Message+ sbSql.ToString()); log.Error(ex.StackTrace); dbcon.Close(); } finally { dbcon.Close(); // 关闭数据库连接 } } dbcon.Close(); } } else { log.Debug($"未查询到数据,SQL语句:{querySql}"); } } else { log.Error($"未包含所需的列(mblno,carrierid),SQL语句:{querySql}"); } } } } }