using log4net; 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 JobGetMhData : IJob { private ILog log = LogManager.GetLogger(typeof(JobGetMhData)); 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["主提单号"].ToString(); var BSNO = row["编号"].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 POLID = ""; string PODID = ""; string TermsCode = ""; string PaymentTypeDescription = ""; string ShipperDescription = ""; string MarksNumbers = ""; string HarmonizedCode = ""; string PackageType = ""; string SHIPPER = ""; string CONSIGNEE = ""; string NOTIFYPARTY = ""; string FinalDest = ""; string FinalDestID = ""; string FinalDestCountry = ""; string ETD = ""; string MhLane = ""; 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); MhLane = GetMHLane(BOOKID,dbmh); POLID = GetMHPortid(POL, dbmh); PODID = GetMHPortid(POD, dbmh); FinalDestID = GetMHPortid(FinalDest, dbmh); } 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; } if (BSTYPE == "分单") { dbcon.Open(); var cmdDeleteCtnSQL = "delete from t_op_ctn_fendan where AS_ID='" + BSNO + "' "; SqlCommand cmdDeleteCtn = new SqlCommand(cmdDeleteCtnSQL, dbcon); cmdDeleteCtn.ExecuteNonQuery(); dbcon.Close(); } else { dbcon.Open(); var cmdDeleteCtnSQL = "delete from t_op_ctn where 编号='" + BSNO + "' "; SqlCommand cmdDeleteCtn = new SqlCommand(cmdDeleteCtnSQL, dbcon); cmdDeleteCtn.ExecuteNonQuery(); dbcon.Close(); } 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 == "20'GP") { ctnUpdatestr = ctnUpdatestr + ",箱型1=" + items.CtnNum.ToString(); CTN20 = CTN20 + items.CtnNum; } if (items.CTNALL == "20'RF") { ctnUpdatestr = ctnUpdatestr + ",箱型2=" + items.CtnNum.ToString(); CTN20 = CTN20 + items.CtnNum; } if (items.CTNALL == "40'GP") { ctnUpdatestr = ctnUpdatestr + ",箱型3=" + items.CtnNum.ToString(); CTN40 = CTN40 + items.CtnNum; } if (items.CTNALL == "40'RF") { ctnUpdatestr = ctnUpdatestr + ",箱型4=" + items.CtnNum.ToString(); CTN40 = CTN40 + items.CtnNum; } if (items.CTNALL == "40'RH") { ctnUpdatestr = ctnUpdatestr + ",箱型5=" + items.CtnNum.ToString(); CTN40HC = CTN40HC + items.CtnNum; } if (items.CTNALL == "45'HC") { ctnUpdatestr = ctnUpdatestr + ",箱型6=" + items.CtnNum.ToString(); CTN45 = CTN45 + items.CtnNum; } if (items.CTNALL == "40'HC") { ctnUpdatestr = ctnUpdatestr + ",箱型7=" + items.CtnNum.ToString(); CTN40HC = CTN40HC + items.CtnNum; } if (items.CTNALL == "45'HQ") { ctnUpdatestr = ctnUpdatestr + ",箱型8=" + items.CtnNum.ToString(); CTN45 = CTN45 + items.CtnNum; } if (items.CTNALL == "40'FR") { ctnUpdatestr = ctnUpdatestr + ",箱型9=" + items.CtnNum.ToString(); CTN40 = CTN40 + items.CtnNum; } } ctnUpdatestr = ctnUpdatestr + ",箱TEU=" + TEU.ToString(); ctnUpdatestr = ctnUpdatestr + ",合计20=" + CTN20.ToString(); ctnUpdatestr = ctnUpdatestr + ",合计40=" + CTN40.ToString(); ctnUpdatestr = ctnUpdatestr + ",合计40H=" + CTN40HC.ToString(); ctnUpdatestr = ctnUpdatestr + ",合计45=" + CTN45.ToString(); foreach (var items in CtnList) { if (BSTYPE == "分单") { dbcon.Open(); var cmdInsertCtnSQL = "insert into t_op_ctn_fendan (AS_ID,代码,尺寸,箱型,数量,TEU,表现形式,箱号,封号,件数,包装,重量,尺码) values (" + "'" + 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 t_op_ctn (编号,代码,尺寸,箱型,数量,TEU,表现形式,箱号,封号,件数,包装,重量,尺码) values (" + "'" + 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(); } } var strSqlUpdateD6 = new StringBuilder(); if (BSTYPE == "分单") { strSqlUpdateD6.Append("update t_op_seae_assistant set 运费协议号=@ServiceContract,备注=@Comment,船名=@Vessel,航次=@Voyage,装货港=@POL"); strSqlUpdateD6.Append(",卸货港=@POD,目的地=@DEST,运输条款=@TermsCode,付费方式=@PaymentTypeDescription,货物描述=@ShipperDescription,唛头=@MarksNumbers"); strSqlUpdateD6.Append(",件数=@PKGS,重量=@KGS,尺码=@CBM,运输方式=@HarmonizedCode,包装=@PackageType,件数包装=@PKGSKIND,货物重量=@GOODKGS,货物尺码=@GOODCBM,集装箱=@CTNSTR"); strSqlUpdateD6.Append(",装港代码=@POLID,卸货代码=@PODID,目的地代码=@FinalDestID"); strSqlUpdateD6.Append(",发货人代码=@SHIPPER,收货人代码=@CONSIGNEE,通知人代码=@NOTIFYPARTY,开船日期=@ETD,船公司航线=@MHLANE where AS_ID='" + BSNO + "'"); } else { strSqlUpdateD6.Append("update t_op_seae set 运费协议号=@ServiceContract,备注=@Comment,船名=@Vessel,航次=@Voyage,装货港=@POL"); strSqlUpdateD6.Append(",卸货港=@POD,目的地=@DEST,运输条款=@TermsCode,付费方式=@PaymentTypeDescription,货物描述=@ShipperDescription,唛头=@MarksNumbers"); strSqlUpdateD6.Append(",件数=@PKGS,重量=@KGS,尺码=@CBM,运输方式=@HarmonizedCode,包装=@PackageType,件数包装=@PKGSKIND,货物重量=@GOODKGS,货物尺码=@GOODCBM,集装箱=@CTNSTR"); strSqlUpdateD6.Append(",装港代码=@POLID,卸货代码=@PODID,目的地代码=@FinalDestID"); strSqlUpdateD6.Append(",发货人代码=@SHIPPER,收货人代码=@CONSIGNEE,通知人代码=@NOTIFYPARTY,开船日期=@ETD,船公司航线=@MHLANE "+ ctnUpdatestr + " where 编号='" + 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("@POLID", POLID); cmdUpdateD6.Parameters.AddWithValue("@PODID", PODID); cmdUpdateD6.Parameters.AddWithValue("@FinalDestID", FinalDestID); 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", "PACKAGE"); cmdUpdateD6.Parameters.AddWithValue("@PKGSKIND", pkgs.ToString() + "PACKAGES"); cmdUpdateD6.Parameters.AddWithValue("@GOODKGS", kgs.ToString() + "KGS"); cmdUpdateD6.Parameters.AddWithValue("@GOODCBM", cbm.ToString() + "CBM"); cmdUpdateD6.Parameters.AddWithValue("@CTNSTR", ctnstr); 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 [代码],[箱型],[尺寸],[表现形式],MHEDI代码 "); strSql.Append(" from t_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["代码"]); evData.CTNTYPE = Convert.ToString(row["箱型"]); evData.CTNSIZE= Convert.ToString(row["尺寸"]); evData.CTNALL= Convert.ToString(row["表现形式"]); 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 运输条款,MHEDI代码 "); strSql.Append(" from t_code_items "); 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["运输条款"]); evData.EDICODE = Convert.ToString(row["MHEDI代码"]); evList.Add(evData); } } return evList; } public static List GetFrtCodeList(SqlConnection dbcon) { var strSql = new StringBuilder(); strSql.Append("Select 付费方式,MHEDI代码 "); strSql.Append(" from t_code_free "); 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["付费方式"]); evData.EDICODE = Convert.ToString(row["MHEDI代码"]); 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; } } }