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; namespace JobCreateFee { public class JobDjyFee : IJob { private ILog log = LogManager.GetLogger(typeof(JobDjyFee)); public void Execute(IJobExecutionContext context) { log.Debug($"Execute开始"); try { string connStr = context.JobDetail.JobDataMap.GetString("ConnectString"); string reqUrl = context.JobDetail.JobDataMap.GetString("DjyReqFeeUrl"); string companyCode = context.JobDetail.JobDataMap.GetString("DjyFeeCompanyCode"); string returnUrl = context.JobDetail.JobDataMap.GetString("DjyReturnFeeUrl"); int reqTimeout = Convert.ToInt32(context.JobDetail.JobDataMap.GetString("RequestTimeout")); long ts = DataTableToJsonHelper.ConvertDateTimeToInt(DateTime.Now); Dictionary dicData = new Dictionary(); dicData.Add("compid", companyCode); log.Debug($"获取数据"); //发送请求数据 string rtn = WebRequestHelper.DoPost(reqUrl, dicData, reqTimeout * 1000); //var objRtn = JsonConvert.DeserializeAnonymousType(rtn, new { status = "", message = "" }); log.Debug($"数据返回:{rtn}"); var parseJsonObj = JsonConvert.DeserializeObject>(rtn); if (parseJsonObj != null && parseJsonObj.Count > 0) { var returnlist = parseJsonObj; var feebacklist = new List(); string sql = ""; try { using (SqlConnection dbcon = new SqlConnection(connStr)) { dbcon.Open(); foreach (var returndata in returnlist) { var BS = GETBSNO(returndata.MBLNO, dbcon); if (BS.BSNO != "" && BS.FEESTATUS != "提交财务" && BS.FEESTATUS != "费用封帐" && BS.BSSTATUS != "应付完毕") { if (GETCARRIERFEEUSD(BS.BSNO, dbcon)) { var feeback = new feeback(); feeback.ReceiveId = returndata.ReceiveId; feeback.Success = false; feeback.Reason = "船公司费用已提交"; feebacklist.Add(feeback); } else { if (!string.IsNullOrEmpty(returndata.CANCEL_NO)) { if (GETFEEISUSE(returndata.CANCEL_NO, dbcon)) { var feeback = new feeback(); feeback.ReceiveId = returndata.ReceiveId; feeback.Success = false; feeback.Reason = "费用已结算或申请"; feebacklist.Add(feeback); } else { var feelist = GetFeeList(" MSK发票号码='" + returndata.CANCEL_NO + "' and DE_ID='大简云帐单'", dbcon); foreach (var fee in feelist) { sql = "DELETE FROM t_ch_fee where CH_ID=" + fee.ch_id; SqlCommand cmd = new SqlCommand(sql, dbcon); cmd.ExecuteNonQuery(); } var feeback = new feeback(); feeback.ReceiveId = returndata.ReceiveId; feeback.Success = true; feeback.Reason = ""; feebacklist.Add(feeback); } } else { if (!GETINVNOISUSE(BS.BSNO, returndata.INVOICE_NO, dbcon)) { var FeeList = returndata.DATA; foreach (var feedata in FeeList) { if (Convert.ToDecimal(feedata.AMOUNT.Trim()) > 0) { var price = feedata.RATE; if (price == "") price = "0"; var curr = feedata.CURRENCY; if (curr == "CNY") curr = "RMB"; var FEENAME = GETFEENAME(feedata.FEECODE.Replace(",", "").Replace(".", "").Replace("'", ""), dbcon, curr); decimal exrate = 1; var AMOUNT = feedata.AMOUNT; var COUNT = feedata.COUNT; if (curr != "RMB") { if (curr != "USD") { if (!string.IsNullOrEmpty(feedata.AMOUNT_USD) && feedata.AMOUNT_USD != "0") { AMOUNT = feedata.AMOUNT_USD; curr = "USD"; COUNT = "1"; price = AMOUNT; } } exrate = GetUsdExrate(dbcon, curr); } price = price.Replace(",", ""); sql = "INSERT INTO [t_ch_fee] ([类型],[费用状态],[编号],[费用名称],[结算单位],[客户名称],[标准]"; sql = sql + ",[单价],[数量],[金额],[币别],[汇率],[备注],[结算金额],[开票金额]"; sql = sql + ",[录入人],[录入日期],[是否对帐],[顺序],[冲抵金额]"; sql = sql + ",[机密],[是否垫付],[申请金额],[不开发票],[费用英文名称]"; sql = sql + ",[申请开票],[提交],[不含税价],[税率],[税额],MSK发票号码,DE_ID) VALUES "; sql = sql + "('付','录入状态','" + BS.BSNO + "','" + FEENAME.Replace(",", "").Replace("'", "''") + "','船公司','CMA','" + GETUNIT(feedata.UNIT.Replace(",", ""), feedata.CTNALL.Replace(",", "")) + "'"; sql = sql + "," + price + "," + COUNT.Replace(",", "") + "," + AMOUNT.Replace(",", "") + ",'" + curr + "'," + exrate.ToString() + ",'',0,0"; sql = sql + ",'SA',GETDATE(),0,10,0"; sql = sql + ",0,0,0,0,'" + feedata.FEECODE.Replace(",", "").Replace(".", "") + "'"; sql = sql + ",0,0," + price + ",0,0,'" + returndata.INVOICE_NO + "','大简云帐单')"; // log.Debug($"更新语句:{sql}"); SqlCommand cmd = new SqlCommand(sql, dbcon); cmd.ExecuteNonQuery(); } } var feeback = new feeback(); feeback.ReceiveId = returndata.ReceiveId; feeback.Success = true; feeback.Reason = ""; feebacklist.Add(feeback); } } } } else { var feeback = new feeback(); feeback.ReceiveId = returndata.ReceiveId; feeback.Success = false; if (BS.BSSTATUS == "应付完毕") feeback.Reason = "业务状态为:" + BS.BSSTATUS + " 不允许插入费用!"; else feeback.Reason = "费用状态为:" + BS.FEESTATUS + " 不允许插入费用!"; feebacklist.Add(feeback); log.Debug($"提单号:{returndata.MBLNO},编号:{BS.BSNO},费用状态:{BS.FEESTATUS} 未插入"); } } dbcon.Close(); } if (feebacklist.Count != 0) { log.Debug($"上传数据:{JsonConvert.SerializeObject(feebacklist.ToList())}"); string rtn2 = WebRequestHelper.DoPost(returnUrl, JsonConvert.SerializeObject(feebacklist.ToList()), reqTimeout * 1000); log.Debug($"数据返回2:{rtn2}"); } } catch (Exception e) { log.Debug($"记录插入错误:{e.Message}" + sql); } } else { log.Debug($"数据返回错误:{parseJsonObj}"); } } catch (Exception ex) { log.Error(ex.Message); log.Error(ex.StackTrace); } } static public OPSEAE GETBSNO(string MBLNO, SqlConnection dbcon) { var OPSEAE = new OPSEAE(); var strSql = new StringBuilder(); strSql.Append("SELECT 编号,费用状态,业务状态 from t_op_seae where 主提单号='" + MBLNO + "'"); 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["编号"].ToString(); OPSEAE.FEESTATUS = row["费用状态"].ToString(); OPSEAE.BSSTATUS = row["业务状态"].ToString(); } } return OPSEAE; } static public bool GETCARRIERFEEUSD(string BSNO, SqlConnection dbcon) { var result = false; var strSql = new StringBuilder(); strSql.Append("SELECT COUNT(1) CT from t_ch_fee where 费用状态<>'录入状态' AND 类型='付' and 结算单位='船公司' and (费用名称<>'提箱小票费' and 费用名称<>'EDI信息服务费') and 编号='" + BSNO + "'"); 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 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 MSK发票号码='" + 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 bool GETFEEISUSE(string INVNO, SqlConnection dbcon) { var result =false; var strSql = new StringBuilder(); strSql.Append("SELECT COUNT(1) CT from t_ch_fee where MSK发票号码='" + INVNO + "' and DE_ID='大简云帐单' "); strSql.Append(" AND (ISNULL(结算金额,0)<>0 OR ISNULL(开票金额,0)<>0 OR ISNULL(冲抵金额,0)<>0 OR ISNULL(申请金额,0)<>0 OR ISNULL(申请开票,0)<>0) "); 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 = Convert.ToInt16(row["CT"].ToString())>0; } } return result; } 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 [费用名称] from t_code_fee_mskedi where [费用英文名称]='" + 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["费用名称"].ToString(); } } if (string.IsNullOrEmpty(FEENAME)) { //if (curr == "USD") //{ // FEENAME = "海运费"; //} else FEENAME = FEECODE; } return FEENAME; } static public List GetFeeList(string constr, SqlConnection dbcon) { var feelist = new List(); var strSql = new StringBuilder(); strSql.Append("select CH_ID,编号 from t_ch_fee where " + constr); 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) { var fee = new jffee(); fee.ch_id = row["CH_ID"].ToString(); fee.bsno = row["编号"].ToString(); feelist.Add(fee); } } return feelist; } static public decimal GetUsdExrate(SqlConnection dbcon,string curr) { decimal usdexrate = 0; if (curr == "USD") { var strSql = new StringBuilder(); strSql.Append("select 汇率 from t_code_usdexrate where 开始日期<=GETDATE() and 结束日期>=GETDATE()"); 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["汇率"].ToString()); } } } if (usdexrate == 0) { SqlDataAdapter adapter2 = new SqlDataAdapter("select 汇率 from t_code_currency where 币别='"+ curr + "'", dbcon); DataTable table2 = new DataTable(); adapter2.Fill(table2); if (table2.Rows.Count > 0) { foreach (DataRow row in table2.Rows) { usdexrate = Convert.ToDecimal(row["汇率"].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 CARRIER_CODE { get; set; } public string TOTAL_AMOUNT { get; set; } public List DATA { get; set; } } public class JsonFeeReturnResponse { public string code { get; set; } public string status { get; set; } public string message { get; set; } public JsonFeeReturnDataResponse data { get; set; } } public class JsonFeeReturnDataResponse { public string runCount { get; set; } public JsonFeeReturnDataFeeResponse payInfo { get; set; } } public class JsonFeeReturnDataFeeResponse { public string price { get; set; } public string count { get; set; } public string total { 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 OPSEAE { public string BSNO { get; set; } public string FEESTATUS { get; set; } public string BSSTATUS { 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; } } }