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 JobAutoCreateFee.Model; namespace JobCreateFee { public class JobGetMhD7Data : IJob { private ILog log = LogManager.GetLogger(typeof(JobGetMhD7Data)); public void Execute(IJobExecutionContext context) { try { string connStr = context.JobDetail.JobDataMap.GetString("ConnectString"); string MhconnStr = context.JobDetail.JobDataMap.GetString("MhConnectString"); string D6querySql = context.JobDetail.JobDataMap.GetString("D6QuerySql"); log.Debug($"开始获取数据"); using (SqlConnection dbcon = new SqlConnection(connStr)) { var strSql = new StringBuilder(); strSql.Append(D6querySql); SqlCommand cmd = new SqlCommand(); cmd.CommandText = strSql.ToString(); cmd.CommandTimeout = 120000; //要加这一句 cmd.Connection = dbcon; SqlDataAdapter adapter = new SqlDataAdapter(cmd); DataTable table = new DataTable(); adapter.Fill(table); var CtnCodeList = GetCtnCodeList(dbcon); var ServiceCodeList = GetServiceCodeList(dbcon); var FrtCodeList = GetFrtCodeList(dbcon); if (table.Rows.Count > 0) { log.Debug($"数据{table.Rows.Count}条"); foreach (DataRow row in table.Rows) { var MBLNO = row["MBLNO"].ToString(); var BSNO = row["BSNO"].ToString(); var BSTYPE = row["BSTYPE"].ToString(); if (!string.IsNullOrEmpty(MBLNO)) { log.Debug($"提单号:{MBLNO}"); while (BSNO.Length < 10) { BSNO = " " + BSNO; } using (SqlConnection dbmh = new SqlConnection(MhconnStr)) { var strSqlMh = new StringBuilder(); strSqlMh.Append("exec spGetBookingId '"+ MBLNO + "' "); SqlCommand cmdMh = new SqlCommand(); cmdMh.CommandText = strSqlMh.ToString(); cmdMh.CommandTimeout = 120000; //要加这一句 cmdMh.Connection = dbmh; SqlDataAdapter adapterMh = new SqlDataAdapter(cmdMh); DataTable tableMh = new DataTable(); adapterMh.Fill(tableMh); var BOOKID = ""; if (tableMh.Rows.Count > 0) { foreach (DataRow rowmh in tableMh.Rows) { BOOKID=rowmh["BookingId"].ToString(); } } if (!string.IsNullOrEmpty(BOOKID)) { log.Debug($"BOOKID:{BOOKID}"); var strSqlMhData = new StringBuilder(); strSqlMhData.Append("exec sp_Rpt_GetBooking " + BOOKID + " "); SqlCommand cmdMhData = new SqlCommand(); cmdMhData.CommandText = strSqlMhData.ToString(); cmdMhData.CommandTimeout = 120000; //要加这一句 cmdMhData.Connection = dbmh; SqlDataAdapter adapterMhData = new SqlDataAdapter(cmdMhData); DataTable tableMhData = new DataTable(); adapterMhData.Fill(tableMhData); if (tableMhData.Rows.Count > 0) { var i = 1; var cargoid = "S"; Int32 pkgs = 0; decimal kgs = 0; decimal cbm = 0; string ServiceContract = ""; string Comment = ""; string Vessel = ""; string Voyage = ""; string POL = ""; string POD = ""; string TermsCode = ""; string PaymentTypeDescription = ""; string ShipperDescription = ""; string MarksNumbers = ""; string HarmonizedCode = ""; string PackageType = ""; string SHIPPER = ""; string CONSIGNEE = ""; string NOTIFYPARTY = ""; string FinalDest = ""; string FinalDestCountry = ""; string ETD = ""; string MhLane = ""; string POLID = ""; string PODID = ""; string FinalDestID = ""; var CtnList = new List(); var CtnSumList = new List(); foreach (DataRow rowmhdata in tableMhData.Rows) { if (i == 1) { var isnotref = rowmhdata["IsNonOperReefer"].ToString(); if (isnotref == "0") cargoid = "R"; ServiceContract = rowmhdata["ServiceContract"].ToString(); Comment = rowmhdata["Comment"].ToString(); Vessel = rowmhdata["Vessel"].ToString(); Voyage = rowmhdata["Voyage"].ToString(); POL = rowmhdata["POL"].ToString(); POD = rowmhdata["POD"].ToString(); TermsCode = rowmhdata["TermsCode"].ToString(); var ServiceCode = ServiceCodeList.Find(x => x.EDICODE == TermsCode); if (ServiceCode != null) TermsCode = ServiceCode.SERVICE; FinalDest = rowmhdata["FinalDest"].ToString(); FinalDestCountry = rowmhdata["FinalDestCountry"].ToString(); if (FinalDestCountry == "United States" || FinalDestCountry == "Canada") { var FinalDestCountryStateCode = rowmhdata["FinalDestCountryStateCode"].ToString(); FinalDest = FinalDest + "," + FinalDestCountryStateCode; } log.Debug($"提单号:{MBLNO} ,目的港:{FinalDest}"); PaymentTypeDescription = rowmhdata["PaymentTypeDescription"].ToString(); var FrtCode = FrtCodeList.Find(x => x.EDICODE == PaymentTypeDescription); if (FrtCode != null) PaymentTypeDescription = FrtCode.FRT; ShipperDescription = rowmhdata["ShipperDescription"].ToString(); MarksNumbers = rowmhdata["MarksNumbers"].ToString(); HarmonizedCode = rowmhdata["HarmonizedCode"].ToString(); PackageType = rowmhdata["PackageType"].ToString(); SHIPPER = rowmhdata["BLShipper"].ToString() + Environment.NewLine + rowmhdata["BLShipperAddress"].ToString(); if (string.IsNullOrEmpty(rowmhdata["BLShipperContact"].ToString())) SHIPPER = SHIPPER + Environment.NewLine + rowmhdata["BLShipperContact"].ToString(); CONSIGNEE = rowmhdata["BLConsignee"].ToString() + Environment.NewLine + rowmhdata["BLConsigneeAddress"].ToString(); if (string.IsNullOrEmpty(rowmhdata["BLConsigneeContact"].ToString())) CONSIGNEE = CONSIGNEE + Environment.NewLine + rowmhdata["BLConsigneeContact"].ToString(); NOTIFYPARTY = rowmhdata["BLNotify1"].ToString(); if (string.IsNullOrEmpty(rowmhdata["BLNotify1Address"].ToString())) NOTIFYPARTY = NOTIFYPARTY + Environment.NewLine + rowmhdata["BLNotify1Address"].ToString(); if (string.IsNullOrEmpty(rowmhdata["BLNotify1Contact"].ToString())) NOTIFYPARTY = NOTIFYPARTY + Environment.NewLine + rowmhdata["BLNotify1Contact"].ToString(); //ETD = GETETD(Vessel, Voyage, dbcon); log.Debug($"提单号:{MBLNO} ,获取数据完成"); MhLane = GetMHLane(BOOKID,dbmh); POLID = GetMHPortid(POL, dbmh); PODID = GetMHPortid(POD, dbmh); FinalDestID = GetMHPortid(FinalDest, dbmh); log.Debug($"提单号:{MBLNO} ,获取港口代码"); } pkgs = pkgs + Convert.ToInt32(rowmhdata["Quantity"]); cbm = cbm + Convert.ToDecimal(rowmhdata["Measurement"]); kgs = kgs + Convert.ToDecimal(rowmhdata["Weight"]); var ContainerNumber = rowmhdata["ContainerNumber"].ToString(); var SealNumber = rowmhdata["SealNumber"].ToString(); var ContainerTypeCode = rowmhdata["ContainerTypeCode"].ToString(); var CtnsCount = rowmhdata["CtnsCount"].ToString(); var CtnCode = CtnCodeList.Find(x => x.EDICODE == ContainerTypeCode); var Ctnid = rowmhdata["ContainerId"].ToString(); var Ctn = CtnList.Find(x => x.Ctnid == Ctnid); if (Ctn == null) { var NewCtn = new Ctn(); NewCtn.Ctnid = Ctnid; if (CtnCode != null) { NewCtn.CTNALL = CtnCode.CTNALL; NewCtn.CTNSIZE = CtnCode.CTNSIZE; NewCtn.CTNTYPE = CtnCode.CTNTYPE; NewCtn.CTNCODE = CtnCode.CTNCODE; } else { NewCtn.CTNALL = ContainerTypeCode; NewCtn.CTNCODE = ContainerTypeCode; } NewCtn.EdiCode = ContainerTypeCode; NewCtn.CtnNum = 1; NewCtn.Pkgs = Convert.ToInt32(rowmhdata["Quantity"]); NewCtn.Cbm = Convert.ToDecimal(rowmhdata["Measurement"]); NewCtn.Kgs = Convert.ToDecimal(rowmhdata["Weight"]); var TMPTEU = 1; if (ContainerTypeCode.IndexOf("40") > 0) { TMPTEU = 2; } NewCtn.Teu = TMPTEU; var CtnSum = CtnSumList.Find(x => x.CTNALL == NewCtn.CTNALL); if (CtnSum == null) { var NewCtnSum = new Ctn(); NewCtnSum.CTNALL = CtnCode.CTNALL; NewCtnSum.CtnNum = 1; NewCtnSum.Teu = NewCtn.Teu; CtnSumList.Add(NewCtnSum); } else { CtnSum.CtnNum = CtnSum.CtnNum + 1; CtnSum.Teu = CtnSum.Teu + NewCtn.Teu; } CtnList.Add(NewCtn); } else { Ctn.Pkgs = Ctn.Pkgs+ Convert.ToInt32(rowmhdata["Quantity"]); Ctn.Cbm = Ctn.Cbm+Convert.ToDecimal(rowmhdata["Measurement"]); Ctn.Kgs = Ctn.Kgs+ Convert.ToDecimal(rowmhdata["Weight"]); } i = i + 1; } log.Debug($"提单号:{MBLNO} ,集装箱处理"); if (BSTYPE == "分单") { dbcon.Open(); var cmdDeleteCtnSQL = "delete from op_ctn where BSNO='" + BSNO + "' "; SqlCommand cmdDeleteCtn = new SqlCommand(cmdDeleteCtnSQL, dbcon); cmdDeleteCtn.ExecuteNonQuery(); dbcon.Close(); } else { dbcon.Open(); var cmdDeleteCtnSQL = "delete from op_ctn where BSNO='" + BSNO + "' "; SqlCommand cmdDeleteCtn = new SqlCommand(cmdDeleteCtnSQL, dbcon); cmdDeleteCtn.ExecuteNonQuery(); dbcon.Close(); } log.Debug($"提单号:{MBLNO} ,清空集装箱"); var ctnstr = ""; var ctnUpdatestr = ""; int TEU = 0; int CTN20 = 0; int CTN40= 0; int CTN40HC= 0; int CTN45 = 0; foreach (var items in CtnSumList) { if (!string.IsNullOrEmpty(ctnstr)) ctnstr = ctnstr + " " + items.CTNALL + "*" + items.CtnNum.ToString(); else ctnstr = items.CTNALL + "*" + items.CtnNum.ToString(); TEU = TEU + items.Teu; if (items.CTNALL == "20GP") { ctnUpdatestr = ctnUpdatestr + ",CNTR1=" + items.CtnNum.ToString(); CTN20 = CTN20 + items.CtnNum; } if (items.CTNALL == "20RF") { ctnUpdatestr = ctnUpdatestr + ",CNTR5=" + items.CtnNum.ToString(); CTN20 = CTN20 + items.CtnNum; } if (items.CTNALL == "40GP") { ctnUpdatestr = ctnUpdatestr + ",CNTR2=" + items.CtnNum.ToString(); CTN40 = CTN40 + items.CtnNum; } if (items.CTNALL == "40RF") { ctnUpdatestr = ctnUpdatestr + ",CNTR6=" + items.CtnNum.ToString(); CTN40 = CTN40 + items.CtnNum; } if (items.CTNALL == "40RH") { ctnUpdatestr = ctnUpdatestr + ",CNTR7=" + items.CtnNum.ToString(); CTN40HC = CTN40HC + items.CtnNum; } if (items.CTNALL == "45HC") { ctnUpdatestr = ctnUpdatestr + ",CNTR9=" + items.CtnNum.ToString(); CTN45 = CTN45 + items.CtnNum; } if (items.CTNALL == "40HC") { ctnUpdatestr = ctnUpdatestr + ",CNTR4=" + items.CtnNum.ToString(); CTN40HC = CTN40HC + items.CtnNum; } if (items.CTNALL == "45HQ") { ctnUpdatestr = ctnUpdatestr + ",CNTR9=" + items.CtnNum.ToString(); CTN45 = CTN45 + items.CtnNum; } } ctnUpdatestr = ctnUpdatestr + ",TEU=" + TEU.ToString(); foreach (var items in CtnList) { if (BSTYPE == "分单") { dbcon.Open(); var cmdInsertCtnSQL = "insert into op_ctn (CTN_ID,BSNO,CTNCODE,SIZE,CTN,CTNNUM,TEU,CTNALL,CNTRNO,SEALNO,PKGS,KINDPKGS,KGS,CBM) values (" + "'"+ Guid.NewGuid().ToString() + "','" + BSNO + "','" + items.CTNCODE + "','" + items.CTNSIZE + "','" + items.CTNTYPE + "',1," + items.Teu.ToString() + ",'" + items.CTNALL.Replace("'", "''") + "','" + items.Cntrno + "','" + items.Sealno + "','" + Convert.ToString(items.Pkgs) + "','" + PackageType + "'," + Convert.ToString(items.Kgs) + "," + Convert.ToString(items.Cbm) + " )"; SqlCommand cmdInsertCtn = new SqlCommand(cmdInsertCtnSQL, dbcon); cmdInsertCtn.ExecuteNonQuery(); dbcon.Close(); } else { dbcon.Open(); var cmdInsertCtnSQL = "insert into op_ctn (CTN_ID,BSNO,CTNCODE,SIZE,CTN,CTNNUM,TEU,CTNALL,CNTRNO,SEALNO,PKGS,KINDPKGS,KGS,CBM) values (" + "'" + Guid.NewGuid().ToString() + "','" + BSNO + "','" + items.CTNCODE + "','" + items.CTNSIZE + "','" + items.CTNTYPE + "',1," + items.Teu.ToString() + ",'" + items.CTNALL.Replace("'", "''") + "','" + items.Cntrno + "','" + items.Sealno + "','" + Convert.ToString(items.Pkgs) + "','" + PackageType + "'," + Convert.ToString(items.Kgs) + "," + Convert.ToString(items.Cbm) + " )"; SqlCommand cmdInsertCtn = new SqlCommand(cmdInsertCtnSQL, dbcon); cmdInsertCtn.ExecuteNonQuery(); dbcon.Close(); } } log.Debug($"提单号:{MBLNO} ,插入集装箱"); var strSqlUpdateD6 = new StringBuilder(); if (BSTYPE == "分单") { strSqlUpdateD6.Append("update op_seae_billmanage set REMARK=@Comment,VESSEL=@Vessel,VOYNO=@Voyage,PORTLOAD=@POL"); strSqlUpdateD6.Append(",PORTDISCHARGE=@POD,DESTINATION=@DEST,SERVICE=@TermsCode,BLFRT=@PaymentTypeDescription,DESCRIPTION=@ShipperDescription,MARKS=@MarksNumbers"); strSqlUpdateD6.Append(",PKGS=@PKGS,KGS=@KGS,CBM=@CBM,KINDPKGS=@PackageType,NOPKGS=@PKGSKIND,GROSSWEIGHT=@GOODKGS,MEASUREMENT=@GOODCBM,CNTRTOTAL=@CTNSTR"); strSqlUpdateD6.Append(",PORTLOADID=@POLID,PORTDISCHARGEID=@PODID,DESTINATIONID=@FinalDestID"); strSqlUpdateD6.Append(",SHIPPER=@SHIPPER,CONSIGNEE=@CONSIGNEE,NOTIFYPARTY=@NOTIFYPARTY " + ctnUpdatestr + " where BSNO='" + BSNO + "'"); } else { strSqlUpdateD6.Append("update op_seae set SERVICECONTRACTNO=@ServiceContract,REMARK=@Comment,VESSEL=@Vessel,VOYNO=@Voyage,PORTLOAD=@POL"); strSqlUpdateD6.Append(",PORTDISCHARGE=@POD,DESTINATION=@DEST,SERVICE=@TermsCode,BLFRT=@PaymentTypeDescription,DESCRIPTION=@ShipperDescription,MARKS=@MarksNumbers"); strSqlUpdateD6.Append(",PKGS=@PKGS,KGS=@KGS,CBM=@CBM,HSCODE=@HarmonizedCode,KINDPKGS=@PackageType,NOPKGS=@PKGSKIND,GROSSWEIGHT=@GOODKGS,MEASUREMENT=@GOODCBM,CNTRTOTAL=@CTNSTR"); strSqlUpdateD6.Append(",PORTLOADID=@POLID,PORTDISCHARGEID=@PODID,DESTINATIONID=@FinalDestID"); strSqlUpdateD6.Append(",SHIPPER=@SHIPPER,CONSIGNEE=@CONSIGNEE,NOTIFYPARTY=@NOTIFYPARTY,LANE=@MHLANE "+ ctnUpdatestr + " where BSNO='" + BSNO + "'"); } dbcon.Open(); log.Debug($"更新业务信息{strSqlUpdateD6.ToString()}"); SqlCommand cmdUpdateD6 = new SqlCommand(strSqlUpdateD6.ToString(), dbcon); cmdUpdateD6.Parameters.AddWithValue("@ServiceContract", ServiceContract); cmdUpdateD6.Parameters.AddWithValue("@Comment", Comment); cmdUpdateD6.Parameters.AddWithValue("@Vessel", Vessel); cmdUpdateD6.Parameters.AddWithValue("@Voyage", Voyage); cmdUpdateD6.Parameters.AddWithValue("@POL", POL); cmdUpdateD6.Parameters.AddWithValue("@POD", POD); cmdUpdateD6.Parameters.AddWithValue("@DEST", FinalDest); cmdUpdateD6.Parameters.AddWithValue("@TermsCode", TermsCode); cmdUpdateD6.Parameters.AddWithValue("@PaymentTypeDescription", PaymentTypeDescription); cmdUpdateD6.Parameters.AddWithValue("@ShipperDescription", ShipperDescription); cmdUpdateD6.Parameters.AddWithValue("@MarksNumbers", MarksNumbers); cmdUpdateD6.Parameters.AddWithValue("@PKGS", pkgs); cmdUpdateD6.Parameters.AddWithValue("@KGS", kgs); cmdUpdateD6.Parameters.AddWithValue("@CBM", cbm); cmdUpdateD6.Parameters.AddWithValue("@HarmonizedCode", HarmonizedCode); cmdUpdateD6.Parameters.AddWithValue("@PackageType", PackageType); cmdUpdateD6.Parameters.AddWithValue("@PKGSKIND", pkgs.ToString() + PackageType); cmdUpdateD6.Parameters.AddWithValue("@GOODKGS", kgs.ToString() + "KGS"); cmdUpdateD6.Parameters.AddWithValue("@GOODCBM", cbm.ToString() + "CBM"); cmdUpdateD6.Parameters.AddWithValue("@CTNSTR", ctnstr); cmdUpdateD6.Parameters.AddWithValue("@POLID", POLID); cmdUpdateD6.Parameters.AddWithValue("@PODID", PODID); cmdUpdateD6.Parameters.AddWithValue("@FinalDestID", FinalDestID); cmdUpdateD6.Parameters.AddWithValue("@SHIPPER", SHIPPER); cmdUpdateD6.Parameters.AddWithValue("@CONSIGNEE", CONSIGNEE); cmdUpdateD6.Parameters.AddWithValue("@NOTIFYPARTY", NOTIFYPARTY); cmdUpdateD6.Parameters.AddWithValue("@ETD", ETD); cmdUpdateD6.Parameters.AddWithValue("@MHLANE", MhLane); cmdUpdateD6.ExecuteNonQuery(); dbcon.Close(); } else { log.Debug($"提单号:{MBLNO}未获取MH数据"); } } else { log.Debug($"提单号:{MBLNO}未获取MH BOOKINGID"); } } } } } } } catch (Exception ex) { log.Error(ex.Message); log.Error(ex.StackTrace); } } public static List GetCtnCodeList(SqlConnection dbcon) { var strSql = new StringBuilder(); strSql.Append("Select CTNID,CTNTYPE,CTNSIZE,CTN,(SELECT TOP 1 EDICODE FROM code_ctn_edi WHERE EDINAME='MHEDI' AND CTN=CODE_CTN.CTN) MHEDI "); strSql.Append(" from code_ctn "); var evList = new List(); SqlDataAdapter adapter = new SqlDataAdapter(strSql.ToString(), dbcon); DataTable table = new DataTable(); adapter.Fill(table); if (table.Rows.Count > 0) { foreach (DataRow row in table.Rows) { var evData = new CtnTypeCode(); evData.CTNCODE = Convert.ToString(row["CTNID"]); evData.CTNTYPE = Convert.ToString(row["CTNTYPE"]); evData.CTNSIZE= Convert.ToString(row["CTNSIZE"]); evData.CTNALL= Convert.ToString(row["CTN"]); evData.EDICODE = Convert.ToString(row["MHEDI"]); evList.Add(evData); } } return evList; } //static public string GETETD(string VESSEL, string VOYNO, SqlConnection dbcon) //{ // var result = ""; // var strSql = new StringBuilder(); // strSql.Append("SELECT top 1 开船日期 from t_code_voyage v left join t_code_vessel l on (l.VS_ID=v.VS_ID) where l.英文船名='"+ VESSEL + "' and v.航次='"+VOYNO+"' "); // SqlDataAdapter adapter = new SqlDataAdapter(strSql.ToString(), dbcon); // DataTable table = new DataTable(); // adapter.Fill(table); // if (table.Rows.Count > 0) // { // foreach (DataRow row in table.Rows) // { // result = row["开船日期"].ToString(); // } // } // return result; //} public static List GetServiceCodeList(SqlConnection dbcon) { var strSql = new StringBuilder(); strSql.Append("Select SERVICE,EDICODE "); strSql.Append(" from code_service_edi where EDINAME='MHEDI'"); var evList = new List(); SqlDataAdapter adapter = new SqlDataAdapter(strSql.ToString(), dbcon); DataTable table = new DataTable(); adapter.Fill(table); if (table.Rows.Count > 0) { foreach (DataRow row in table.Rows) { var evData = new ServiceCode(); evData.SERVICE = Convert.ToString(row["SERVICE"]); evData.EDICODE = Convert.ToString(row["EDICODE"]); evList.Add(evData); } } return evList; } public static List GetFrtCodeList(SqlConnection dbcon) { var strSql = new StringBuilder(); strSql.Append("Select FRT,MHEDI EDICODE "); strSql.Append(" from code_frt "); var evList = new List(); SqlDataAdapter adapter = new SqlDataAdapter(strSql.ToString(), dbcon); DataTable table = new DataTable(); adapter.Fill(table); if (table.Rows.Count > 0) { foreach (DataRow row in table.Rows) { var evData = new FrtCode(); evData.FRT = Convert.ToString(row["FRT"]); evData.EDICODE = Convert.ToString(row["EDICODE"]); evList.Add(evData); } } return evList; } static public string GetMHLane(string Bookid,SqlConnection dbcon) { var result = ""; var strSql = new StringBuilder(); strSql.Append("select Service from uService where ServiceId in (select ServiceId from uBooking where BookingId ="+ Bookid + ")"); SqlDataAdapter adapter = new SqlDataAdapter(strSql.ToString(), dbcon); DataTable table = new DataTable(); adapter.Fill(table); if (table.Rows.Count > 0) { foreach (DataRow row in table.Rows) { result = row["Service"].ToString(); } } return result; } static public string GetMHPortid(string port, SqlConnection dbcon) { var result = ""; if (string.IsNullOrEmpty(port)) return result; var strSql = new StringBuilder(); strSql.Append("select top 1 LocationCode from uLocation where Location ='" + port + "'"); SqlDataAdapter adapter = new SqlDataAdapter(strSql.ToString(), dbcon); DataTable table = new DataTable(); adapter.Fill(table); if (table.Rows.Count > 0) { foreach (DataRow row in table.Rows) { result = row["LocationCode"].ToString(); } } return result; } } }