using log4net; using Newtonsoft.Json; using Quartz; using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Text; using JobAutoCreateFee.Model; using System.IO; namespace JobCreateFee { public class JobVgmEdiReply : IJob { private ILog log = LogManager.GetLogger(typeof(JobVgmEdiReply)); public void Execute(IJobExecutionContext context) { log.Debug($"Execute开始"); try { string connStr = context.JobDetail.JobDataMap.GetString("ConnectString"); string FtpAddr = context.JobDetail.JobDataMap.GetString("FtpAddr"); string FtpPath = context.JobDetail.JobDataMap.GetString("FtpPath"); string FtpUserName = context.JobDetail.JobDataMap.GetString("FtpUserName"); string FtpPsw = context.JobDetail.JobDataMap.GetString("FtpPsw"); //string BcPath = context.JobDetail.JobDataMap.GetString("VgmPath"); string tempPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "VgmTmp"); string BcPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "VgmPath"); if (!Directory.Exists(tempPath)) { Directory.CreateDirectory(tempPath); } if (!Directory.Exists(BcPath)) { Directory.CreateDirectory(BcPath); } var ftp = new FtpWeb(FtpAddr, FtpPath, FtpUserName, FtpPsw); try { var ftpmsg = ""; var filearray = ftp.GetFileList("", out ftpmsg); log.Debug($"FTP连接:{FtpAddr},{FtpPath},{FtpUserName},{FtpPsw},ftpmsg={ftpmsg}"); if (filearray == null || filearray.Count() == 0) { log.Debug($"没有可读取的文件,进程结束"); return; } log.Debug($"发现文件数:{filearray.Count()}"); foreach (var filename in filearray) { try { ftp = new FtpWeb(FtpAddr, FtpPath, FtpUserName, FtpPsw); ftp.Download(BcPath, filename); log.Debug($"下载文件:{filename}"); log.Debug($"开始删除:{filename}"); ftp.Delete(filename); log.Debug($"结束删除"); } catch (Exception ex) { log.Debug($"错误:{ex.Message}"); } } } catch (Exception ex) { log.Debug($"错误:{ex.Message}"); } string[] files = Directory.GetFiles(BcPath); if (files.Length > 0) { foreach (string file in files) { FileInfo fileInfo = new FileInfo(file); string pathtxt = File.ReadAllText(file); var opList = pathtxt.Split('\''); var MBLNO = ""; var CNTRNO = ""; var EDISTATUS = ""; log.Debug($"文件名"+ fileInfo.Name); foreach (var opstr in opList) { if (opstr.Length >= 10) { log.Debug($"" + opstr); var head = opstr.TrimStart().Substring(0, 3); if (head == "RFF") { log.Debug($"RFF" + opstr); var headlist = opstr.Split('+'); var fieldcount = 0; foreach (var str in headlist) { log.Debug($"RFFstr" + str); if (fieldcount == 1) { if (str.IndexOf("BM:") > 0) { MBLNO = str.Replace("BM:","").Trim() ; log.Debug($"业务编号:" + MBLNO); } if (str.IndexOf("EQ:") > 0) { CNTRNO = str.Replace("EQ:", "").Trim(); log.Debug($"箱号:" + CNTRNO); } } fieldcount = fieldcount + 1; } } } } if (!string.IsNullOrEmpty(MBLNO) && !string.IsNullOrEmpty(CNTRNO)) { log.Debug($"业务编号: " + MBLNO+" 箱号:"+ CNTRNO); using (SqlConnection dbcon = new SqlConnection(connStr)) { dbcon.Open(); string sql = " update t_op_ctn set VGM状态='接收成功' where 箱号='"+CNTRNO+"' and 编号 in (select 编号 from t_op_seae where 主提单号='" + MBLNO+"')"; SqlCommand cmd = new SqlCommand(sql, dbcon); cmd.ExecuteNonQuery(); sql = " update t_op_seae set VGM状态='接收成功' from t_op_seae t left join (select 编号,sum(CASE WHEN VGM状态='接收成功' then 1 else 0 end) VCTNNUM,sum(1) CTNNUM from t_op_ctn group by 编号) c on (c.编号=t.编号) where t.主提单号='" + MBLNO + "' and c.CTNNUM=c.VCTNNUM "; cmd = new SqlCommand(sql, dbcon); cmd.ExecuteNonQuery(); dbcon.Close(); } File.Copy(file, Path.Combine(tempPath, fileInfo.Name)); File.Delete(file); } } } } catch (Exception ex) { log.Error(ex.Message); log.Error(ex.StackTrace); } } } }