You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
DS7/JobAutoCreateFee/JobD7DjyFee.cs

674 lines
31 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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;
namespace JobCreateFee
{
public class JobD7DjyFee : IJob
{
private ILog log = LogManager.GetLogger(typeof(JobD7DjyFee));
public void Execute(IJobExecutionContext context)
{
log.Debug($"Execute开始");
try
{
string connStr = context.JobDetail.JobDataMap.GetString("ConnectString");
string reqUrl = context.JobDetail.JobDataMap.GetString("DjyReqFeeUrl");
string ENTEROPERATOR = context.JobDetail.JobDataMap.GetString("ENTEROPERATOR");
string companyCode = context.JobDetail.JobDataMap.GetString("DjyFeeCompanyCode");
string returnUrl = context.JobDetail.JobDataMap.GetString("DjyReturnFeeUrl");
string BsSql = context.JobDetail.JobDataMap.GetString("BsSql");
int reqTimeout = Convert.ToInt32(context.JobDetail.JobDataMap.GetString("RequestTimeout"));
long ts = DataTableToJsonHelper.ConvertDateTimeToInt(DateTime.Now);
string KFCUSTCODE = context.JobDetail.JobDataMap.GetString("KFCUSTCODE");
string KFCUSTKEY = context.JobDetail.JobDataMap.GetString("KFCUSTKEY");
string KFUSERID = context.JobDetail.JobDataMap.GetString("KFUSERID");
string KFURL = context.JobDetail.JobDataMap.GetString("KFURL");
string DJYKF = context.JobDetail.JobDataMap.GetString("DJYKF");
string FEESTATUS = context.JobDetail.JobDataMap.GetString("FEESTATUS");
string DJYKFCUSTNAME = context.JobDetail.JobDataMap.GetString("DJYKFCUSTNAME");
string DJYKFFEENAME = context.JobDetail.JobDataMap.GetString("DJYKFFEENAME");
string NOADDFEE = context.JobDetail.JobDataMap.GetString("NOADDFEE");
Dictionary<string, string> dicData = new Dictionary<string, string>();
dicData.Add("compid", companyCode);
log.Debug($"获取数据");
var htmlFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, NOADDFEE);
string[] nofeenamelist = new string[0];
if (System.IO.File.Exists(htmlFile))
{
var Shipping = File.ReadAllText(htmlFile);
Shipping = Shipping.Replace("\n", "\\");
Shipping = Shipping.Replace("\r", " ");
nofeenamelist = Shipping.Split('\\');
}
//发送请求数据
string rtn = WebRequestHelper.DoPost(reqUrl, dicData, reqTimeout * 1000);
//var objRtn = JsonConvert.DeserializeAnonymousType(rtn, new { status = "", message = "" });
log.Debug($"数据返回:{rtn}");
var parseJsonObj = JsonConvert.DeserializeObject<List<JsonFeeResponse>>(rtn);
if (parseJsonObj != null && parseJsonObj.Count > 0)
{
var returnlist = parseJsonObj;
var feebacklist = new List<feeback>();
string sql = "";
try
{
using (SqlConnection dbcon = new SqlConnection(connStr))
{
dbcon.Open();
foreach (var returndata in returnlist)
{
var BS = GETBSNO(returndata.MBLNO, dbcon, BsSql);
if (BS.BSNO != "" && !BS.FEESTATUS)
{
//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(" DELIVERYNO='" + returndata.CANCEL_NO + "'", dbcon);
foreach (var fee in feelist)
{
sql = "DELETE FROM ch_fee where GID=" + 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)
{
var isadd = true;
foreach (var nofee in nofeenamelist) {
log.Debug($"费用代码:{nofee},大简云{feedata.FEECODE}");
if (nofee.Trim() == feedata.FEECODE.Trim()) {
isadd = false;
}
}
if (isadd&&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,BS.ETD);
}
price = price.Replace(",", "");
var CUTOMERNAME = GETCUSTNAME(returndata.CARRIER_CODE, dbcon,"");
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()+ "','" + BS.BSNO + "',2,'" + FEENAME.Replace(",", "") + "','船公司','"+ CUTOMERNAME + "','" + GETUNIT(feedata.UNIT.Replace(",", ""), feedata.CTNALL.Replace(",", ""), dbcon) + "'";
sql = sql + "," + price + "," + COUNT.Replace(",", "") + "," + AMOUNT.Replace(",", "") + ",'" + curr + "'," + exrate.ToString() + ",'','',0";
sql = sql + ",'"+ ENTEROPERATOR + "',GETDATE(),0,0,10,"+ FEESTATUS + ",'',''";
sql = sql + ",0,"+ AMOUNT.Replace(",", "") + ",0,0," + price + ",'"+ ENTEROPERATOR + "',GETDATE(),'',0,0,'大简云单票账单','RMB','',0,'" + feedata.FEECODE.Replace(",", "").Replace(".", "") + "'";
sql = sql + ",'" + returndata.INVOICE_NO + "',0,0,0,0,0,0)";
// 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);
string guid = Guid.NewGuid().ToString();
if (DJYKF == "1")
{
var kf = GETKF(returndata.MBLNO,dbcon);
if (!kf)
{
try
{
var djyreturn = DeductionFee(guid, returndata.MBLNO, KFCUSTCODE, KFCUSTKEY, KFUSERID, KFURL);
if (djyreturn.code == "200" || djyreturn.code == "450")
{
if (!string.IsNullOrEmpty(DJYKFCUSTNAME))
{
var djyfee = djyreturn.data.payInfo;
//foreach (var djyfee in feelist)
//{
if (djyfee.total != "0")
{
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() + "','" + BS.BSNO + "',2,'" + DJYKFFEENAME + "','船公司','" + DJYKFCUSTNAME + "','票'";
sql = sql + "," + djyfee.price.ToString() + "," + djyfee.count.ToString() + "," + djyfee.total.ToString() + ",'RMB',1,'','',0";
sql = sql + ",'" + ENTEROPERATOR + "',GETDATE(),0,0,10," + FEESTATUS + ",'',''";
sql = sql + ",0," + djyfee.total.ToString() + ",0,0," + djyfee.price.ToString() + ",'" + ENTEROPERATOR + "',GETDATE(),'',0,0,'大简云单票账单','RMB','',0,''";
sql = sql + ",'" + returndata.INVOICE_NO + "',0,0,0,0,0,0)";
// log.Debug($"更新语句:{sql}");
SqlCommand cmd = new SqlCommand(sql, dbcon);
cmd.ExecuteNonQuery();
sql = "INSERT INTO op_seae_kf(BSNO, BSTYPE)";
sql = sql + "VALUES ";
sql = sql + "('" + returndata.MBLNO + "','7')";
// log.Debug($"更新语句:{sql}");
SqlCommand cmd2 = new SqlCommand(sql, dbcon);
cmd2.ExecuteNonQuery();
}
//}
}
}
else
{
log.Debug($"提单号:{returndata.MBLNO},大简云扣费失败:" + djyreturn.message);
}
}
catch (Exception E)
{
log.Debug($"提单号:{returndata.MBLNO},大简云扣费出现错误" + E.Message);
}
}
}
}
}
//}
}
else {
var feeback = new feeback();
feeback.ReceiveId = returndata.ReceiveId;
feeback.Success = false;
feeback.Reason = "费用状态已锁定 不允许插入费用!";
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 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 MBLNO='" + 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 GETCARRIERFEEUSD(string BSNO, SqlConnection dbcon)
//{
// var result = false;
// var strSql = new StringBuilder();
// strSql.Append("SELECT COUNT(1) CT from ch_fee where 费用状态<>'录入状态' AND 类型='付' and 结算单位='船公司' and 费用名称<>'提箱小票费' 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 ch_fee where BSNO='" + BSNO + "' AND DELIVERYNO='" + 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 GETKF(string MBLNO,SqlConnection dbcon)
{
var result = false;
var strSql = new StringBuilder();
strSql.Append("SELECT COUNT(1) CT from op_seae_kf where BSNO='" + MBLNO + "' AND BSTYPE='7'");
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 ch_fee where DELIVERYNO='" + INVNO + "' ");
strSql.Append(" AND (ISNULL(SETTLEMENT,0)<>0 OR ISNULL(INVOICE,0)<>0 OR ISNULL(ORDERAMOUNT,0)<>0 OR ISNULL(ORDERINVOICE,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, SqlConnection dbcon)
{
var BSNO = UNIT;
if (BSNO == "FIX") BSNO = "单票";
if (BSNO == "UNI") BSNO = CTNALL;
var strSql = new StringBuilder();
strSql.Append("select top 1 [CTN] from code_ctn_edi where [EDICODE]='" + BSNO + "'");
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["CTN"].ToString();
}
}
if (string.IsNullOrEmpty(FEENAME))
{
//if (curr == "USD")
//{
// FEENAME = "海运费";
//} else
FEENAME = BSNO;
}
return FEENAME;
//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 = FEECODE;
}
return FEENAME;
}
static public string GETCUSTNAME(string CUSTCODE, SqlConnection dbcon, string curr)
{
var strSql = new StringBuilder();
strSql.Append("select top 1 [CUST] from code_cust_edi where [EDICODE]='" + CUSTCODE + "' and [EDINAME]='DJYFEE'");
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["CUST"].ToString();
}
}
if (string.IsNullOrEmpty(FEENAME))
{
//if (curr == "USD")
//{
// FEENAME = "海运费";
//} else
FEENAME = CUSTCODE;
}
return FEENAME;
}
static public List<jffee> GetFeeList(string constr, SqlConnection dbcon) {
var feelist = new List<jffee>();
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 JsonFeeReturnResponse DeductionFee(string _Guid, string _MBLNO, string custname, string custkey, string userid, string djyurl, string bsType = "7")
{
var seconds = DateTime.Now.ToTimeStamp();
var runId = Guid.NewGuid().ToString();
var srcBeforMD5 = $"{runId}{custname}expend{bsType}0{_Guid}{_MBLNO}{seconds}{custkey}";
//调用扣费接口
var postObj = new
{
runId,
userId = custname,
module = "expend",//固定
bsType = bsType,
sendType = "0",
timestamp = seconds,//秒级时间戳
md5 = srcBeforMD5.ToMd5(),// 加密字符串小写 RunId + UserId + Module + BsType + SendType+Timestamp+ Key
Data = new
{
BSNO = _Guid,
MBLNO = _MBLNO,
CtnrInfo = "",
CtnrCount = 1,
IsCredit = 1,//是否是信用支付 1 信用支付 0不允许信用支付默认值为0
LURURENID = userid,///当前登录人id
SENDUSERID = userid
}
};
var apiJson = JsonConvert.SerializeObject(postObj);
log.Debug($"扣费上传数据:{apiJson}");
var apiRtn = WebRequestHelper.DoPost(djyurl, apiJson);
log.Debug($"扣费返回数据:{apiRtn}");
return JsonConvert.DeserializeObject<JsonFeeReturnResponse>(apiRtn);
//var jobjApiRtn = JObject.Parse(apiRtn);
//var code = Convert.ToInt32(jobjApiRtn.GetValue("code").ToString());
//if (code == 200 || code == 450)
//{
// return true;
//}
//else
//{
// log.Debug($"扣费失败提醒,执行扣费失败!");
//}
//return false;
}
}
//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<DJYFEE> 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 D7OPSEAE
{
public string BSNO { get; set; }
public bool FEESTATUS { get; set; }
public bool BSSTATUS { get; set; }
public string ETD { 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; }
//}
}