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 JobDjyD7Xf : IJob { private ILog log = LogManager.GetLogger(typeof(JobDjyD7Xf)); private const string CfgFileName = "starttime.cfg"; private static string CfgFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, CfgFileName); public void Execute(IJobExecutionContext context) { log.Debug($"Execute开始"); try { string connStr = context.JobDetail.JobDataMap.GetString("ConnectString"); string reqUrl = context.JobDetail.JobDataMap.GetString("DjyReqUrl"); string companyCode = context.JobDetail.JobDataMap.GetString("DjyCompanyCode"); string companyName = context.JobDetail.JobDataMap.GetString("DjyCompanyName"); string FeeName = context.JobDetail.JobDataMap.GetString("FeeName"); string CustName = context.JobDetail.JobDataMap.GetString("CustName"); string companySecret = context.JobDetail.JobDataMap.GetString("DjyCompanySecret"); string bsType = context.JobDetail.JobDataMap.GetString("DjybsType"); string COSCOAUTOCREATEFEE = context.JobDetail.JobDataMap.GetString("COSCOAUTOCREATEFEE"); string CoscoCustName = context.JobDetail.JobDataMap.GetString("COSCOCustName"); string ENTEROPERATOR = context.JobDetail.JobDataMap.GetString("ENTEROPERATOR"); int reqTimeout = Convert.ToInt32(context.JobDetail.JobDataMap.GetString("RequestTimeout")); long ts = DataTableToJsonHelper.ConvertDateTimeToInt(DateTime.Now); JsonD6XFSend reqObj = new JsonD6XFSend(); var runid = Guid.NewGuid().ToString(); reqObj.runid = runid; reqObj.userId = companyCode; reqObj.module = "expendlist"; reqObj.bsType = bsType; reqObj.sendType = "0"; reqObj.timestamp = ts; var md5str = DataTableToJsonHelper.MD5Encrypt32(runid + companyCode + "expendlist" + bsType + "0" + ts.ToString() + companySecret); reqObj.md5 = md5str; //JArray jarrTemps = new JArray(); JsonD6XFData jobjTemp = new JsonD6XFData(); jobjTemp.page = 1; jobjTemp.limit = 10000; jobjTemp.BsType =Convert.ToInt16(bsType); jobjTemp.Name = companyName; var starttimestr = File.ReadAllText(CfgFilePath); if (!string.IsNullOrEmpty(starttimestr)) { long startts = DataTableToJsonHelper.ConvertDateTimeToInt(Convert.ToDateTime(starttimestr)); jobjTemp.starttime = startts; } var endtime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); long endts = DataTableToJsonHelper.ConvertDateTimeToInt(Convert.ToDateTime(endtime)); jobjTemp.endtime = endts; reqObj.data = jobjTemp; var sendstr = JsonConvert.SerializeObject(reqObj); log.Debug($"发送包:{sendstr}"); //发送请求数据 string rtn = WebRequestHelper.DoPost(reqUrl, sendstr, reqTimeout * 1000); //var objRtn = JsonConvert.DeserializeAnonymousType(rtn, new { status = "", message = "" }); log.Debug($"数据返回:{rtn}"); var parseJsonObj = JsonConvert.DeserializeObject(rtn); if (parseJsonObj.status) { var returnlist = parseJsonObj.data; try { using (SqlConnection dbcon = new SqlConnection(connStr)) { dbcon.Open(); foreach (var returndata in returnlist) { var BS = GETBSNO(returndata.mblno, dbcon); if (returndata.price == "" || returndata.price == null) returndata.price = "0"; if (returndata.ctnrCount == "" || returndata.ctnrCount == null) returndata.ctnrCount = "0"; var feecustname = CustName; if (COSCOAUTOCREATEFEE == "1"&& returndata.forwarder=="中远海运"&& (Convert.ToDecimal(returndata.price) == 0)) { if (Convert.ToDecimal(returndata.price) == 0) returndata.price = "30"; feecustname = CoscoCustName; } string sql = " if (select GID from t_op_txxp_log where GID='" + returndata.gid + "') is null INSERT INTO t_op_txxp_log([编号],[GID],[beizhu],[mblno],[bsstatus],[bstype],[createtime]"; sql = sql + ",[price],[sendtype],[ctnrCount],[ctnrInfo]) values ('" + BS.BSNO + "','" + returndata.gid + "'"; sql = sql + ",'" + returndata.beizhu + "','" + returndata.mblno + "','" + returndata.bsstatus + "','" + returndata.bstype + "','" + returndata.createtime + "'"; sql = sql + "," + returndata.price + ",'" + returndata.sendtype + "'," + returndata.ctnrCount + ",'" + returndata.ctnrInfo + "')"; SqlCommand cmd = new SqlCommand(sql, dbcon); cmd.ExecuteNonQuery(); if (!string.IsNullOrEmpty(BS.BSNO) && BS.FEESTATUS != "1" && BS.BSSTATUS != "1") { if (Convert.ToDecimal(returndata.price) != 0 && !string.IsNullOrEmpty(feecustname) && !string.IsNullOrEmpty(FeeName)) { 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 + "','','" + feecustname + "','票'"; sql = sql + "," + returndata.price + ",1," + returndata.price + ",'RMB',1,'','',0"; sql = sql + ",'" + ENTEROPERATOR + "',GETDATE(),0,0,10,1,'',''"; sql = sql + ",0," + returndata.price + ",0,0," + returndata.price + ",'" + ENTEROPERATOR + "',GETDATE(),'',0,0,'大简云消费记录','RMB','',0,''"; sql = sql + ",'"+ returndata.gid + "',0,0,0,0,0,0)"; log.Debug($"{returndata.mblno},-费用语句-{sql}"); //dbcon.Open(); SqlCommand cmd2 = new SqlCommand(sql, dbcon); cmd2.ExecuteNonQuery(); //dbcon.Close(); } } } dbcon.Close(); } } catch (Exception e) { log.Debug($"记录插入错误:{e.Message}"); } File.WriteAllText(CfgFilePath, endtime); } else { log.Debug($"数据返回错误:{parseJsonObj.message}"); } } catch (Exception ex) { log.Error(ex.Message); log.Error(ex.StackTrace); } } public static int SaveMail(SqlConnection dbcon, string sendto, string title, string body, string smtpid) { var sqlstr = $" INSERT INTO [Mail_Send] ([GID],[SendTo],[Title],[Body],[SendStatus],[TryCount],[CreateTime],[SmtpConfig],[Sender],[ShowName]) select newid(),'{sendto}','{title}','{body.Replace("'", "''")}','Create',0,GETDATE(),'{smtpid}','','' "; SqlCommand cmd = new SqlCommand(sqlstr, dbcon); return cmd.ExecuteNonQuery(); } static public OPSEAE GETBSNO(string MBLNO, SqlConnection dbcon) { var OPSEAE = new OPSEAE(); var strSql = new StringBuilder(); strSql.Append("SELECT BSNO,FEESTATUS,BSSTATUS from op_seae where MBLNO='" + 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["BSNO"].ToString(); OPSEAE.FEESTATUS = row["FEESTATUS"].ToString(); OPSEAE.BSSTATUS = row["BSSTATUS"].ToString(); } } return OPSEAE; } } }