using log4net; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Quartz; using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.IO; using System.Linq; using System.Text; using DSWeb.Common; using JobAutoCreateFee.Model; using System.Security.Policy; using System.Xml.Linq; using System.Diagnostics.Eventing.Reader; namespace JobCreateFee { public class JobD6CoscoFee : IJob { private ILog log = LogManager.GetLogger(typeof(JobD6CoscoFee)); public void Execute(IJobExecutionContext context) { log.Debug($"Execute开始"); try { string connStr = context.JobDetail.JobDataMap.GetString("ConnectString"); string reqUrl = context.JobDetail.JobDataMap.GetString("CoscoReqFeeUrl"); string UserKey = context.JobDetail.JobDataMap.GetString("UserKey"); string UserSecret = context.JobDetail.JobDataMap.GetString("UserSecret"); string WebAccount = context.JobDetail.JobDataMap.GetString("WebAccount"); string WebPassword = context.JobDetail.JobDataMap.GetString("WebPassword"); string CompanyCode = context.JobDetail.JobDataMap.GetString("CompanyCode"); string BsSql = context.JobDetail.JobDataMap.GetString("BsSql"); string FEECUSTNAME = context.JobDetail.JobDataMap.GetString("FEECUSTNAME"); string ENTEROPERATOR = context.JobDetail.JobDataMap.GetString("ENTEROPERATOR"); string FEESTATUS = context.JobDetail.JobDataMap.GetString("FEESTATUS"); int reqTimeout = Convert.ToInt32(context.JobDetail.JobDataMap.GetString("RequestTimeout")); long ts = DataTableToJsonHelper.ConvertDateTimeToInt(DateTime.Now); log.Debug($"获取数据"); using (SqlConnection dbcon = new SqlConnection(connStr)) { var strSql = new StringBuilder(); strSql.Append(BsSql); 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 feebacklist = new List(); if (table.Rows.Count > 0) { log.Debug($"数据{table.Rows.Count}条"); //var FeeTemplateDetailList = GetFeeTemplate(FEECUSTNAME, dbcon); foreach (DataRow row in table.Rows) { var MBLNO = row["MBLNO"].ToString(); var BSNO = row["BSNO"].ToString(); var ETD = row["ETD"].ToString(); if (!string.IsNullOrEmpty(MBLNO) && MBLNO.Length > 4) { MBLNO = MBLNO.Substring(4); } var JsonCoscoFeeSend = new JsonCoscoFeeSend(); JsonCoscoFeeSend.userKey = UserKey; JsonCoscoFeeSend.userSecret = UserSecret; JsonCoscoFeeSend.companyCode = CompanyCode; JsonCoscoFeeSend.webAccount = WebAccount; JsonCoscoFeeSend.webPassword = WebPassword; JsonCoscoFeeSend.bno = MBLNO; JsonCoscoFeeSend.summaryFlag = false; var sendstr = JsonConvert.SerializeObject(JsonCoscoFeeSend); log.Debug($"发送包:{sendstr}"); //发送请求数据 string rtn = WebRequestHelper.DoPost(reqUrl, sendstr, reqTimeout * 1000); log.Debug($"数据返回:{rtn}"); var objRtn = JsonConvert.DeserializeAnonymousType(rtn, new { code = "", msg = "" }); if (objRtn.code=="200") { var objreturn= JsonConvert.DeserializeObject(rtn); var returnlist = objreturn.data; if (returnlist != null && returnlist.Count != 0) { var feelist = new List(); var unitlist = GetFeeUnit(BSNO, dbcon); var isctnnum = 1; var findunitlist = unitlist.FindAll(x=>x.ISCTN); if (findunitlist != null && findunitlist.Count >= 2) { isctnnum = 2; } string sql = ""; try { foreach (var returndata in returnlist) { if (!GETINVNOISUSE(BSNO, returndata.itemUuid, dbcon)) { if (Convert.ToDecimal(returndata.totalAmount.Trim())!=0) { var price = returndata.totalAmount; if (price == "") price = "0"; var curr = returndata.currencyUnit; if (curr == "CNY") curr = "RMB"; var FEENAME = GETFEENAME(returndata.feeName.Replace("'", "").Replace(",", "").Replace(".", ""), dbcon, curr); decimal exrate = 1; var AMOUNT = returndata.totalAmount; var COUNT = "1"; if (curr != "RMB") { exrate = GetUsdExrate(dbcon, curr, ETD); } price = price.Replace(",", ""); if (string.IsNullOrEmpty(FEENAME)) { var feeback = new feeback(); feeback.ReceiveId = returndata.bno; feeback.Success = false; feeback.Reason = $"提单号:{MBLNO},编号:{BSNO},费用名称:{returndata.feeName} 未匹配"; feebacklist.Add(feeback); log.Debug($"提单号:{MBLNO},编号:{BSNO},费用名称:{returndata.feeName} 未匹配"); } else { bool isadd = false; var newfee = new MsChFee(); newfee.FA_ID = returndata.itemUuid; newfee.Currency = curr; newfee.CustomerName = FEECUSTNAME; newfee.BSNO = BSNO; newfee.ExChangerate = exrate; newfee.FeeName = FEENAME; newfee.Quantity = 1; newfee.Unit = "票"; newfee.UnitPrice =Convert.ToDecimal(returndata.totalAmount); newfee.Amount= Convert.ToDecimal(returndata.totalAmount); newfee.FeeEName = returndata.feeName.Replace("'", "").Replace(",", "").Replace(".", ""); feelist.Add(newfee); isadd = true; if (!isadd) { var feeback = new feeback(); feeback.ReceiveId = returndata.bno; feeback.Success = false; feeback.Reason = $"提单号:{MBLNO},编号:{BSNO},费用名称:{returndata.feeName} 金额:{returndata.totalAmount}未匹配添加"; feebacklist.Add(feeback); log.Debug($"提单号:{MBLNO},编号:{BSNO},费用名称:{returndata.feeName} 金额:{returndata.totalAmount} 未匹配添加"); } } } } } } catch (Exception e) { log.Debug($"记录插入错误:{e.Message}" + sql); } try { if (feelist != null && feelist.Count != 0) { dbcon.Open(); foreach (var fee in feelist) { sql = "INSERT INTO ch_fee(GID, BSNO, FEETYPE, FEENAME,CUSTOMERTYPE, CUSTOMERNAME, UNIT, UNITPRICE, QUANTITY, AMOUNT, CURRENCY,"; sql = sql + "EXCHANGERATE, REASON, REMARK, COMMISSIONRATE,ENTEROPERATOR, ENTERDATE,ISOPEN, ISADVANCEDPAY, SORT, FEESTATUS, FEEFRT,CARGO_GID,"; sql = sql + "TAXRATE,NOTAXAMOUNT,ACCTAXRATE,LINENUM,TAXUNITPRICE,MODIFIEDUSER,MODIFIEDTIME,WMSOUTBSNO,PREAMOUNT,ISINVOICE,INPUTMODE,LOCALCURR,MANAGER,ISACC,FEEDESCRIPTION,DELIVERYNO,SETTLEMENT,INVOICE,ORDERAMOUNT,ORDERINVOICE,ORDERSETTLEMENT,ORDERINVSETTLEMENT)"; sql = sql + "VALUES "; sql = sql + "('" + Guid.NewGuid().ToString() + "','" + fee.BSNO + "',2,'" + fee.FeeName + "','船公司','" + fee.CustomerName + "','" + fee.Unit + "'"; sql = sql + "," + fee.UnitPrice + "," + fee.Quantity + "," + fee.Amount + ",'" + fee.Currency + "'," + fee.ExChangerate + ",'','',0"; sql = sql + ",'" + ENTEROPERATOR + "',GETDATE(),0,0,10," + FEESTATUS + ",'',''"; sql = sql + ",0," + fee.Amount + ",0,0," + fee.UnitPrice + ",'" + ENTEROPERATOR + "',GETDATE(),'',0,0,'COSCO账单回写','RMB','',0,'" + fee.FeeEName + "'"; sql = sql + ",'" + fee.FA_ID + "',0,0,0,0,0,0)"; // log.Debug($"更新语句:{sql}"); SqlCommand cmd2 = new SqlCommand(sql, dbcon); cmd2.ExecuteNonQuery(); } dbcon.Close(); } } catch (Exception ex) { log.Debug($"插入费用出错:{ex.Message+sql}"); } } } else { log.Debug($"数据返回错误:{rtn}"); } } } } } catch (Exception ex) { log.Error(ex.Message); log.Error(ex.StackTrace); } } static public D7OPSEAE GETBSNO(string MBLNO, SqlConnection dbcon,string BsSql) { var OPSEAE = new D7OPSEAE(); var strSql = new StringBuilder(); strSql.Append("SELECT BSNO,BSSTATUS,FEESTATUS,ETD from op_seae where BSNO='" + MBLNO + "' and ISNULL(ISCANCEL,'')<>'1'"); if (!string.IsNullOrEmpty(BsSql)) strSql.Append(" and "+BsSql); SqlDataAdapter adapter = new SqlDataAdapter(strSql.ToString(), dbcon); DataTable table = new DataTable(); adapter.Fill(table); var BSNO = ""; if (table.Rows.Count > 0) { foreach (DataRow row in table.Rows) { OPSEAE.BSNO = row["BSNO"].ToString(); OPSEAE.FEESTATUS = Convert.ToBoolean(row["FEESTATUS"].ToString()); OPSEAE.BSSTATUS = Convert.ToBoolean(row["BSSTATUS"].ToString()); OPSEAE.ETD = Convert.ToString(row["ETD"].ToString()); } } return OPSEAE; } static public bool GETINVNOISUSE(string BSNO,string INVNO, SqlConnection dbcon) { var result = false; var strSql = new StringBuilder(); strSql.Append("SELECT COUNT(1) CT from t_ch_fee where 编号='" + BSNO + "' AND 船公司发票号='" + INVNO + "'"); 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) { if (Convert.ToInt32(row["CT"].ToString()) != 0) result = true; } } return result; } static public List GetFeeTemplate(string FA_ID, SqlConnection dbcon) { var headList = new List(); var strSql = new StringBuilder(); strSql.Append("SELECT GID,TEMPLATEID,FEENAME,CUSTOMERNAME,CLIENT,UNIT"); strSql.Append(",CURRENCY,UNITPRICE,REMARK,SORT,CREATEUSER,CREATETIME,EXCHANGERATE,ISCTN,TAXRATE,TAXUNITPRICE,TAX,ACCTAXRATE"); strSql.Append(",(select EnumValueName from tSysEnumValue where LangId=0 and EnumTypeID=96201 and EnumValueID=code_fee_templatedetail_auto.CLIENT) as CLIENTREF"); strSql.Append(",(case ISCTN when 1 then '是' else '否' end) as ISCTNREF,ISINVOICE,ISADVANCEDPAY,FEEFRT,SALECORP,FEEDESCRIPTION,UNITPRICEDESCRPTION "); strSql.Append(" from code_fee_templatedetail_auto WHERE CUSTOMERNAME='" + FA_ID + "'"); strSql.Append(" order by SORT "); 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 data = new CodeFeeTemplateDetailAuto(); data.GID = Convert.ToString(row["GID"]); data.TEMPLATEID = Convert.ToString(row["TEMPLATEID"]); if (row["UNITPRICE"] != DBNull.Value) data.UNITPRICE = Convert.ToDecimal(row["UNITPRICE"]); if (row["EXCHANGERATE"] != DBNull.Value) data.EXCHANGERATE = Convert.ToDecimal(row["EXCHANGERATE"]); if (row["TAXRATE"] != DBNull.Value) data.TAXRATE = Convert.ToDecimal(row["TAXRATE"]); if (row["TAX"] != DBNull.Value) data.TAX = Convert.ToDecimal(row["TAX"]); data.FEENAME = Convert.ToString(row["FEENAME"]); data.FEEDESCRIPTION = Convert.ToString(row["FEEDESCRIPTION"]); data.CUSTOMERNAME = Convert.ToString(row["CUSTOMERNAME"]); if (row["CLIENT"] != DBNull.Value) data.CLIENT = Convert.ToString(row["CLIENT"]); data.CLIENTREF = Convert.ToString(row["CLIENTREF"]); data.UNIT = Convert.ToString(row["UNIT"]); data.CURRENCY = Convert.ToString(row["CURRENCY"]); data.REMARK = Convert.ToString(row["REMARK"]); if (row["SORT"] != DBNull.Value) data.SORT = Convert.ToInt16(row["SORT"]); data.FEEFRT = Convert.ToString(row["FEEFRT"]); data.CREATEUSER = Convert.ToString(row["CREATEUSER"]); data.CREATETIME = Convert.ToString(row["CREATETIME"]); if (row["ISCTN"] != DBNull.Value) data.ISCTN = Convert.ToBoolean(row["ISCTN"]); data.ISCTNREF = Convert.ToString(row["ISCTNREF"]); if (row["ISINVOICE"] != DBNull.Value) data.ISINVOICE = Convert.ToBoolean(row["ISINVOICE"]); if (row["ISADVANCEDPAY"] != DBNull.Value) data.ISADVANCEDPAY = Convert.ToBoolean(row["ISADVANCEDPAY"]); data.SALECORP = Convert.ToString(row["SALECORP"]); if (row["TAXUNITPRICE"] != DBNull.Value) data.TAXUNITPRICE = Convert.ToDecimal(row["TAXUNITPRICE"]); if (row["ACCTAXRATE"] != DBNull.Value) data.ACCTAXRATE = Convert.ToDecimal(row["ACCTAXRATE"]); data.UNITPRICEDESCRPTION = Convert.ToString(row["UNITPRICEDESCRPTION"]); headList.Add(data); } } return headList; } static public List GetFeeUnit(string BSNO, SqlConnection dbcon) { decimal TEUNUM; TEUNUM = 0; decimal PKGS; PKGS = 0; decimal KGS; KGS = 0; decimal CBM; CBM = 0; decimal NETWEIGHT = 0; decimal CTNNUM; CTNNUM = 0; var headList = new List(); var strSql = new StringBuilder(); strSql.Append("SELECT NO,PKGS,KGS,CBM,NETWEIGHT,CDP,FCDP,CTNALL,CTNNUM,TEU,0 提箱小票数"); strSql.Append(",0 超额小票数"); strSql.Append(",0 不超额小票数 FROM ("); strSql.Append("SELECT '1' as NO,件数 PKGS,重量 KGS,尺码 CBM,重量 AS NETWEIGHT,0 CDP,"); strSql.Append("0 FCDP,"); strSql.Append("'' as CTNALL,0 AS CTNNUM,0 AS TEU,0 提箱小票数 "); strSql.Append(",isnull((select sum(数量) from t_op_ctn where 编号=t_op_seae.编号),0) 总箱数 "); // strSql.Append(",isnull((select sum(ctnrCount) from t_op_txxp_log where 编号=t_op_seae.编号),0)-isnull((select sum(数量) from t_op_ctn where 编号=t_op_seae.编号),0) 超额小票数 "); strSql.Append(" from t_op_seae "); strSql.Append(" where 编号='" + BSNO + "'"); strSql.Append(") AS T "); strSql.Append(" union "); strSql.Append("SELECT '2' AS NO,0 AS PKGS,0 AS KGS,0 AS CBM,0 AS NETWEIGHT,0 AS CDP,0 AS FCDP,"); strSql.Append("P.表现形式 CTNALL,SUM(P.数量) AS CTNNUM,SUM(P.TEU) AS TEU,0 提箱小票数,0 超额小票数,0 不超额小票数"); strSql.Append(" from t_op_ctn P "); strSql.Append(" where p.编号='" + BSNO + "'"); strSql.Append(" GROUP BY P.表现形式 order by PKGS desc"); SqlDataAdapter adapter = new SqlDataAdapter(strSql.ToString(), dbcon); DataTable table = new DataTable(); adapter.Fill(table); var isfirst = 0; if (table.Rows.Count > 0) { foreach (DataRow row in table.Rows) { if (row["NO"].ToString() == "1") { var data = new MsFeeUnit(); data.FEEUNIT = "单票"; data.FEEQUANTITY = 1; data.ISCTN = false; headList.Add(data); if (!string.IsNullOrEmpty(row["PKGS"].ToString())) PKGS = Convert.ToDecimal(row["PKGS"].ToString()); if (!string.IsNullOrEmpty(row["KGS"].ToString())) KGS = Convert.ToDecimal(row["KGS"].ToString()); if (!string.IsNullOrEmpty(row["CBM"].ToString())) CBM = Convert.ToDecimal(row["CBM"].ToString()); if (!string.IsNullOrEmpty(row["NETWEIGHT"].ToString())) NETWEIGHT = Convert.ToDecimal(row["NETWEIGHT"].ToString()); var data2 = new MsFeeUnit(); data2.FEEUNIT = "重量"; data2.FEEQUANTITY = KGS; data2.ISCTN = false; headList.Add(data2); var data3 = new MsFeeUnit(); data3.FEEUNIT = "尺码"; data3.FEEQUANTITY = CBM; data3.ISCTN = false; headList.Add(data3); var data4 = new MsFeeUnit(); data4.FEEUNIT = "件数"; data4.FEEQUANTITY = PKGS; data4.ISCTN = false; headList.Add(data4); var data5 = new MsFeeUnit(); data5.FEEUNIT = "舱单票数"; data5.FEEQUANTITY = Convert.ToInt32(row["CDP"].ToString()); data5.ISCTN = false; headList.Add(data5); var data6 = new MsFeeUnit(); data6.FEEUNIT = "舱单分票"; data6.FEEQUANTITY = Convert.ToInt32(row["FCDP"].ToString()); data6.ISCTN = false; headList.Add(data6); var txxpdata = new MsFeeUnit(); txxpdata.FEEUNIT = "提箱小票数"; txxpdata.FEEQUANTITY = Convert.ToInt32(row["提箱小票数"].ToString()); txxpdata.ISCTN = false; headList.Add(txxpdata); var txxpdata2 = new MsFeeUnit(); txxpdata2.FEEUNIT = "超额小票数"; txxpdata2.FEEQUANTITY = Convert.ToInt32(row["超额小票数"].ToString()); txxpdata2.ISCTN = false; headList.Add(txxpdata2); if (Convert.ToInt32(row["不超额小票数"].ToString()) > 0) { var txxpdata3 = new MsFeeUnit(); txxpdata3.FEEUNIT = "不超额小票数"; txxpdata3.FEEQUANTITY = Convert.ToInt32(row["不超额小票数"].ToString()); txxpdata3.ISCTN = false; headList.Add(txxpdata3); } else { //var txxpdata3 = new MsFeeUnit(); //txxpdata3.FEEUNIT = "不超额小票数"; //txxpdata3.FEEQUANTITY = Convert.ToInt32(row["不超额小票数"].ToString()); //headList.Add(txxpdata3); } } if (row["CTNALL"].ToString() != "") { var ctndata = new MsFeeUnit(); ctndata.FEEUNIT = row["CTNALL"].ToString(); if (!string.IsNullOrEmpty(row["CTNNUM"].ToString())) ctndata.FEEQUANTITY = Convert.ToDecimal(row["CTNNUM"].ToString()); CTNNUM = CTNNUM + ctndata.FEEQUANTITY; if (!string.IsNullOrEmpty(row["TEU"].ToString())) TEUNUM = TEUNUM + Convert.ToDecimal(row["TEU"].ToString()); ctndata.ISCTN = true; headList.Add(ctndata); } isfirst = 1; } var teudata = new MsFeeUnit(); teudata.FEEUNIT = "TEU"; teudata.FEEQUANTITY = TEUNUM; teudata.ISCTN = false; headList.Add(teudata); var ctnalldata = new MsFeeUnit(); ctnalldata.FEEUNIT = "箱数"; ctnalldata.FEEQUANTITY = CTNNUM; ctnalldata.FEEQUANTITY = CTNNUM; //ctnalldata.ISCTN = false; headList.Add(ctnalldata); } return headList; } static public string GETUNIT(string UNIT,string CTNALL) { var BSNO = UNIT; if (BSNO == "FIX") BSNO = "单票"; if (BSNO == "UNI") BSNO = CTNALL; return BSNO.Replace("'", "''"); } static public string GETFEENAME(string FEECODE, SqlConnection dbcon, string curr) { var strSql = new StringBuilder(); strSql.Append("select top 1 [FEENAME] from code_fee_edi where [FEEENAME]='" + FEECODE + "'"); SqlDataAdapter adapter = new SqlDataAdapter(strSql.ToString(), dbcon); DataTable table = new DataTable(); adapter.Fill(table); var FEENAME = ""; if (table.Rows.Count > 0) { foreach (DataRow row in table.Rows) { FEENAME = row["FEENAME"].ToString(); } } if (string.IsNullOrEmpty(FEENAME)) { //if (curr == "USD") //{ // FEENAME = "海运费"; //} else FEENAME =""; } return FEENAME; } static public List GetFeeList(string constr, SqlConnection dbcon) { var feelist = new List(); var strSql = new StringBuilder(); strSql.Append("select GID,BSNO from ch_fee where " + constr); 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 fee = new jffee(); fee.ch_id = row["GID"].ToString(); fee.bsno = row["BSNO"].ToString(); feelist.Add(fee); } } return feelist; } static public decimal GetUsdExrate(SqlConnection dbcon,string curr,string ETD) { decimal usdexrate = 0; var startdate = ETD; if (string.IsNullOrEmpty(startdate)) startdate = DateTime.Now.ToString("yyyy-MM-dd"); if (curr == "USD") { var strSql = new StringBuilder(); strSql.Append("select VALUE from currency_exchange where STARTTIME<='"+ startdate + "' and ENDTIME>='"+ startdate + "'"); strSql.Append(" and CURRENCYID in (select GID from code_currency where CODENAME='"+curr+"') "); 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) { usdexrate = Convert.ToDecimal(row["VALUE"].ToString()); } } } if (usdexrate == 0) { SqlDataAdapter adapter2 = new SqlDataAdapter("select DEFAULTRATE from code_currency where CODENAME='" + curr + "'", dbcon); DataTable table2 = new DataTable(); adapter2.Fill(table2); if (table2.Rows.Count > 0) { foreach (DataRow row in table2.Rows) { usdexrate = Convert.ToDecimal(row["DEFAULTRATE"].ToString()); } } } return usdexrate; } } //public class JsonFeeResponse //{ // public string ReceiveId { get; set; } // public string MBLNO { get; set; } // public string INVOICE_NO { get; set; } // public string CANCEL_NO { get; set; } // public string TOTAL_AMOUNT { get; set; } // public List DATA { get; set; } //} //public class DJYFEE //{ // public string FEECODE { get; set; } // public string AMOUNT { get; set; } // public string AMOUNT_USD { get; set; } // public string CURRENCY { get; set; } // public string CTNALL { get; set; } // public string TAX { get; set; } // public string COUNT { get; set; } // public string UNIT { get; set; } // public string RATE { get; set; } //} //public class feeback //{ // public string ReceiveId { get; set; } // public bool Success { get; set; } // public string Reason { get; set; } //} //public class jffee //{ // public string ch_id { get; set; } // public string bsno { get; set; } //} }