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 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 TermsCode = "" ;
string PaymentTypeDescription = "" ;
string ShipperDescription = "" ;
string MarksNumbers = "" ;
string HarmonizedCode = "" ;
string PackageType = "" ;
string SHIPPER = "" ;
string CONSIGNEE = "" ;
string NOTIFYPARTY = "" ;
string FinalDest = "" ;
string ETD = "" ;
var CtnList = new List < Ctn > ( ) ;
foreach ( DataRow rowmhdata in tableMhData . Rows )
{
if ( i = = 1 )
{
var isnotref = rowmhdata [ "IsNonOperReefer" ] . ToString ( ) ;
if ( isnotref = = "0" ) cargoid = "R" ;
pkgs = pkgs + Convert . ToInt32 ( rowmhdata [ "Quantity" ] ) ;
cbm = cbm + Convert . ToDecimal ( rowmhdata [ "Measurement" ] ) ;
kgs = kgs + Convert . ToDecimal ( rowmhdata [ "Weight" ] ) ;
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 ( ) ;
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 ) ;
}
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 TEU = 0 ;
if ( ContainerTypeCode . IndexOf ( "20" ) > 0 )
{
TEU = 2 * Convert . ToInt32 ( CtnsCount ) ;
}
if ( CtnCode ! = null )
{
var Ctn = CtnList . Find ( x = > x . CtnType = = CtnCode . CTNALL ) ;
if ( Ctn = = null )
{
var NewCtn = new Ctn ( ) ;
NewCtn . CtnType = CtnCode . CTNALL ;
NewCtn . CtnNum = 1 ;
CtnList . Add ( NewCtn ) ;
}
else {
Ctn . CtnNum = Ctn . CtnNum + 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 ( ) ;
var cmdInsertCtnSQL = "insert into t_op_ctn_fendan (AS_ID,代码,尺寸,箱型,数量,TEU,表现形式,箱号,封号,件数,包装,重量,尺码) values ("
+ "'" + BSNO + "','" + CtnCode . CTNCODE + "','" + CtnCode . CTNSIZE + "','" + CtnCode . CTNTYPE + "',1," + TEU . ToString ( ) + ",'" + CtnCode . CTNALL . Replace ( "'" , "''" ) + "','" + ContainerNumber + "','" + SealNumber + "','" + Convert . ToString ( rowmhdata [ "Quantity" ] ) + "','" + PackageType + "'," + Convert . ToString ( rowmhdata [ "Weight" ] ) + "," + Convert . ToString ( rowmhdata [ "Measurement" ] ) + " )" ;
SqlCommand cmdInsertCtn = new SqlCommand ( cmdInsertCtnSQL , dbcon ) ;
cmdInsertCtn . ExecuteNonQuery ( ) ;
dbcon . Close ( ) ;
}
else
{
dbcon . Open ( ) ;
var cmdDeleteCtnSQL = "delete from t_op_ctn where 编号='" + BSNO + "' " ;
SqlCommand cmdDeleteCtn = new SqlCommand ( cmdDeleteCtnSQL , dbcon ) ;
cmdDeleteCtn . ExecuteNonQuery ( ) ;
var cmdInsertCtnSQL = "insert into t_op_ctn (编号,代码,尺寸,箱型,数量,TEU,表现形式,箱号,封号,件数,包装,重量,尺码) values ("
+ "'" + BSNO + "','" + CtnCode . CTNCODE + "','" + CtnCode . CTNSIZE + "','" + CtnCode . CTNTYPE + "',1," + TEU . ToString ( ) + ",'" + CtnCode . CTNALL . Replace ( "'" , "''" ) + "','" + ContainerNumber + "','" + SealNumber + "','" + Convert . ToString ( rowmhdata [ "Quantity" ] ) + "','" + PackageType + "'," + Convert . ToString ( rowmhdata [ "Weight" ] ) + "," + Convert . ToString ( rowmhdata [ "Measurement" ] ) + " )" ;
SqlCommand cmdInsertCtn = new SqlCommand ( cmdInsertCtnSQL , dbcon ) ;
cmdInsertCtn . ExecuteNonQuery ( ) ;
dbcon . Close ( ) ;
}
log . Debug ( $"已更新业务信息{MBLNO}" ) ;
}
i = i + 1 ;
}
var ctnstr = "" ;
foreach ( var items in CtnList ) {
if ( ! string . IsNullOrEmpty ( ctnstr ) )
ctnstr = ctnstr + " " + items . CtnType + "*" + items . CtnNum . ToString ( ) ;
else ctnstr = items . CtnType + "*" + items . CtnNum . ToString ( ) ;
}
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 ( ",发货人代码=@SHIPPER,收货人代码=@CONSIGNEE,通知人代码=@NOTIFYPARTY,开船日期=@ETD 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 ( ",发货人代码=@SHIPPER,收货人代码=@CONSIGNEE,通知人代码=@NOTIFYPARTY,开船日期=@ETD 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 ( "@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 ( "@SHIPPER" , SHIPPER ) ;
cmdUpdateD6 . Parameters . AddWithValue ( "@CONSIGNEE" , CONSIGNEE ) ;
cmdUpdateD6 . Parameters . AddWithValue ( "@NOTIFYPARTY" , NOTIFYPARTY ) ;
cmdUpdateD6 . Parameters . AddWithValue ( "@ETD" , ETD ) ;
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 < CtnTypeCode > GetCtnCodeList ( SqlConnection dbcon )
{
var strSql = new StringBuilder ( ) ;
strSql . Append ( "Select [代码],[箱型],[尺寸],[表现形式],MHEDI代码 " ) ;
strSql . Append ( " from t_code_ctn " ) ;
var evList = new List < CtnTypeCode > ( ) ;
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 < ServiceCode > GetServiceCodeList ( SqlConnection dbcon )
{
var strSql = new StringBuilder ( ) ;
strSql . Append ( "Select 运输条款,MHEDI代码 " ) ;
strSql . Append ( " from t_code_items " ) ;
var evList = new List < ServiceCode > ( ) ;
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 < FrtCode > GetFrtCodeList ( SqlConnection dbcon )
{
var strSql = new StringBuilder ( ) ;
strSql . Append ( "Select 付费方式,MHEDI代码 " ) ;
strSql . Append ( " from t_code_free " ) ;
var evList = new List < FrtCode > ( ) ;
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 ;
}
}
}