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.Net; using System.Runtime.Remoting; using System.Text; namespace JobCreateFee { public class JobBankData : IJob { private ILog log = LogManager.GetLogger(typeof(JobBankData)); public void Execute(IJobExecutionContext context) { log.Debug($"Execute开始"); try { string connStr = context.JobDetail.JobDataMap.GetString("ConnectString"); string reqUrl = context.JobDetail.JobDataMap.GetString("ReqUrl"); string companyName = context.JobDetail.JobDataMap.GetString("CompanyName"); string companyId = context.JobDetail.JobDataMap.GetString("CompanyId"); string CustId = context.JobDetail.JobDataMap.GetString("CustId"); string userId = context.JobDetail.JobDataMap.GetString("UserId"); string D7Url = context.JobDetail.JobDataMap.GetString("D7Url"); int reqTimeout = Convert.ToInt32(context.JobDetail.JobDataMap.GetString("RequestTimeout")); var JsonGetBankData = new GetBankData(); JsonGetBankData.AccountName = companyName; JsonGetBankData.AccountKey = CustId; JsonGetBankData.BeginDate = DateTime.Now.AddDays(-3).ToString("yyyyMMdd"); JsonGetBankData.EndDate = DateTime.Now.ToString("yyyyMMdd"); var PCNO = DateTime.Now.ToString("yyyyMMddhhmmss"); var sendstr = JsonConvert.SerializeObject(JsonGetBankData); log.Debug($"发送包:{sendstr}"); //发送请求数据 string rtn = WebRequestHelper.DoPost(reqUrl, sendstr, reqTimeout * 1000); log.Debug($"数据返回:{rtn}"); var parseJsonObj = JsonConvert.DeserializeObject(rtn); if (parseJsonObj.result!=null&& parseJsonObj.result.Count!=0) { using (SqlConnection dbcon = new SqlConnection(connStr)) { dbcon.Open(); foreach (var bankdata in parseJsonObj.result) { if (!GETDISADD(bankdata.RecordID, dbcon)) { string sql = " INSERT INTO ch_fee_bankdata([GID],[PCNO],[SFNO],[TRANSTYPE],[DRAWEE_BANK],[DRAWEE_ACCOUNT],[DRAWEE_NAME],[PAYEE_BANK],[PAYEE_ACCOUNT]"; sql = sql + ",[PAYEE_NAME],[TRANSDATE],[CURRENCY],[AMOUNT],BALANCE,[TRANSNO],[IMPORTDATE],[IMPORTER],[ATTITIONAL],[REMARK],[SUMMARY],[CREATEUSER],[CREATETIME],[ACCOUNT],[BANK],[BANKGID],CORPID) values (NEWID(),'" + PCNO + "'"; sql = sql + ",'1','" + bankdata.TransactionType + "','" + bankdata.PayerAccountBank + "','" + bankdata.PayerAccountNumber + "','" + bankdata.PayerName + "'"; sql = sql + ",'" + bankdata.PayeeAccountBank+ "','" + bankdata.PayeeAccountNumber+ "','" + bankdata.PayeeName + "','" + bankdata.TransactionDate.Substring(0,4)+"-"+ bankdata.TransactionDate.Substring(4, 2)+"-"+ bankdata.TransactionDate.Substring(6, 2)+" "+ bankdata.TransactionTime + "','" + bankdata.TradeCurrency + "'," + bankdata.TradeAmount + "," + bankdata.AfterTransactionBalance + ",'" + bankdata.TransactionReferenceNumber + "',GETDATE(),'系统自动'" +",'" + bankdata.Reference + "','" + bankdata.Purpose + "','" + bankdata.Remark+ bankdata.Remarks + "','系统自动',GETDATE(),'" + bankdata.AccountId + "'" +",'" + bankdata.AccountName+ "','" + bankdata.RecordID + "','"+ companyId + "')"; log.Debug($"插入语句:{sql}"); SqlCommand cmd = new SqlCommand(sql, dbcon); cmd.ExecuteNonQuery(); } } //hp.makefee(); dbcon.Close(); } Dictionary dicData = new Dictionary(); dicData.Add("PCNO", PCNO); dicData.Add("CORPID", companyId); dicData.Add("USERID", userId); // log.Debug($"发送包(自动结算):{dicData.ToString()}"); var autostlrtn = DoPostHead(D7Url, dicData,""); log.Debug($"数据返回:{autostlrtn}"); } else { log.Debug($"数据返回为空"); } } 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(); } public static void SaveImageToSql(string connectionString, byte[] imageData) { using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); // 使用参数化的SQL查询来避免SQL注入 string sql = "INSERT INTO Images (Image) VALUES (@Image)"; using (SqlCommand command = new SqlCommand(sql, connection)) { // 将图像数据作为参数传递 command.Parameters.Add(new SqlParameter("@Image", SqlDbType.Image) { Value = imageData }); command.ExecuteNonQuery(); } } } public static byte[] ReadImageFromFile(string filePath) { // 确保文件存在 if (!File.Exists(filePath)) throw new FileNotFoundException("The file could not be found.", filePath); // 读取图像文件并转换为字节数组 return File.ReadAllBytes(filePath); } static public bool GETDISADD(string RECOUNTID, SqlConnection dbcon) { var strSql = new StringBuilder(); strSql.Append("SELECT COUNT(1) CT from ch_fee_bankdata where BANKGID='" + RECOUNTID + "'"); SqlDataAdapter adapter = new SqlDataAdapter(strSql.ToString(), dbcon); DataTable table = new DataTable(); adapter.Fill(table); var result =false; if (table.Rows.Count > 0) { foreach (DataRow row in table.Rows) { if (Convert.ToInt32(row["CT"].ToString()) != 0) result = true; } } return result; } public static string DoPostHead(string url, Dictionary headdic, string json) { string responseString = "";//post返回的结果 ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, err) => { return true; }; ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url); req.Method = "POST"; if (headdic.Count > 0) { foreach (var item in headdic) { req.Headers.Add(item.Key, item.Value); } } if (!string.IsNullOrWhiteSpace(json)) { byte[] postBytes = Encoding.UTF8.GetBytes(json); req.ContentType = "application/json; charset=utf-8"; req.ContentLength = Encoding.UTF8.GetByteCount(json); Stream stream = req.GetRequestStream(); stream.Write(postBytes, 0, postBytes.Length); req.Timeout = 100000; stream.Close(); } else { req.ContentLength = 0; } var response = req.GetResponse(); Stream streamResponse = response.GetResponseStream(); StreamReader streamRead = new StreamReader(streamResponse); responseString = streamRead.ReadToEnd(); response.Close(); streamRead.Close(); return responseString; } } public class BankData { /// /// 青岛东胜伟业软件有限公司 /// public string Custom { get; set; } /// /// 中国银行 /// public string BankName { get; set; } /// /// 青岛大简云物流科技有限公司 /// public string AccountName { get; set; } /// /// /// public string AccountId { get; set; } /// /// 来账 /// public string TransactionType { get; set; } /// /// 小额普通 /// public string BusinessType { get; set; } /// /// /// public string AccountHoldingBankNumberOfPayer { get; set; } /// /// 平安银行青岛经济技术开发区支行 /// public string PayerAccountBank { get; set; } /// /// /// public string PayerAccountNumber { get; set; } /// /// 青岛海泰优升国际物流有限公司 /// public string PayerName { get; set; } /// /// /// public string AccountHoldingBankNumberOfPayee { get; set; } /// /// 中国银行青岛香港西路支行 /// public string PayeeAccountBank { get; set; } /// /// /// public string PayeeAccountNumber { get; set; } /// /// 青岛大简云物流科技有限公司 /// public string PayeeName { get; set; } /// /// /// public string TransactionDate { get; set; } /// /// /// public string TransactionTime { get; set; } /// /// /// public string TradeCurrency { get; set; } /// /// /// public string TradeAmount { get; set; } /// /// /// public string AfterTransactionBalance { get; set; } /// /// /// public string ValueDate { get; set; } /// /// /// public string ExchangeRate { get; set; } /// /// /// public string TransactionReferenceNumber { get; set; } /// /// /// public string OnlineBankingTransactionRef { get; set; } /// /// /// public string CustomerTransactionRef { get; set; } /// /// /// public string VoucherType { get; set; } /// /// /// public string VoucherNumber { get; set; } /// /// /// public string RecordID { get; set; } /// /// /// public string Reference { get; set; } /// /// /// public string Purpose { get; set; } /// /// 舱单 /// public string Remark { get; set; } /// /// beps.121.001.01 A100 R104100000004 中国银行总行 /// public string Remarks { get; set; } /// /// /// public string Reserve1 { get; set; } /// /// /// public string Reserve2 { get; set; } /// /// /// public string Reserve3 { get; set; } /// /// /// public string OpeningBankNumberOfNominalPayer { get; set; } /// /// /// public string OpeningBankNameOfNominalPayer { get; set; } /// /// /// public string AccountNumberOfNominalPayer { get; set; } /// /// /// public string NameOfNominalPayer { get; set; } /// /// /// public string OpeningBankNumberOfNominalPayee { get; set; } /// /// /// public string OpeningBankNameOfNominalPayee { get; set; } /// /// /// public string AccountNumberOfNominalPayee { get; set; } /// /// /// public string NameOfNominalPayee { get; set; } /// /// /// public string CreateTime { get; set; } /// /// /// public string FilePaths { get; set; } } public class BankResult { /// /// /// public List result { get; set; } } public class GetBankData { public string AccountName { get; set; } public string BeginDate { get; set; } public string EndDate { get; set; } public string AccountKey { get; set; } } }