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.

364 lines
18 KiB
C#

10 months ago
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 HtmlAgilityPack;
using HcUtility.Core;
using HcUtility.Comm;
using System.Data.Entity;
using System.Text.RegularExpressions;
namespace Job_JieFeng_FTP
{
public class Job_JFFTP : IJob
{
private ILog log = LogManager.GetLogger(typeof(Job_JFFTP));
private string connStr { get; set; }
private string FTPADDRESS { get; set; }
private string FTPPATH { get; set; }
private string FTPUSERNAME { get; set; }
private string FTPPWD { get; set; }
private string carrierStr { get; set; }
private List<CARRIER> carrierList { get; set; }
private { get; set; }
//错误文件记录
//List<string> ERRORFILEList = new List<string>();
/// <summary>
/// 当前正在处理的xml文件在ftp端的文件名
/// </summary>
private string currXMLFile { get; set; }
private string ErrorFilePackName { get { return "ERROR"; } }
private string ErrorFileHead { get { return "ERROR_"; } }
public void Execute(IJobExecutionContext context)
{
log.Debug($"Execute开始");
connStr = context.JobDetail.JobDataMap.GetString("ConnectString");
FTPADDRESS = context.JobDetail.JobDataMap.GetString("FTPADDRESS");
FTPPATH = context.JobDetail.JobDataMap.GetString("FTPPATH");
FTPUSERNAME = context.JobDetail.JobDataMap.GetString("FTPUSERNAME");
FTPPWD = context.JobDetail.JobDataMap.GetString("FTPPWD");
carrierStr = context.JobDetail.JobDataMap.GetString("CARRIER");
//1:获得FTP的文件列表
//var ftp = new FtpWeb("ddlucky.vicp.net:3721", "", "dsuser", "dsuser");
var ftpmsg = "";
var ftp = new FtpWeb(FTPADDRESS, FTPPATH, FTPUSERNAME, FTPPWD);
try
{
//log.Debug($"读取任务信息完成");
while (carrierStr.Substring(carrierStr.Length - 1, 1) == ";")
{
carrierStr = carrierStr.Substring(0, carrierStr.Length - 1);
}
carrierList = new List<CARRIER>();
var carrierarray = Regex.Split(carrierStr, ";;");
foreach (string carrierstr in carrierarray)
{
var temparray = Regex.Split(carrierstr, ";");
CARRIER carrier = new CARRIER(temparray[0], temparray[1]);
carrierList.Add(carrier);
}
//log.Debug($"船公司读取完成");
var PaymentTermStr = context.JobDetail.JobDataMap.GetString("PaymentTerm");
= new (PaymentTermStr);
//if (!ftp.DirectoryExist(ErrorFilePackName))
//{
// log.Debug($"在FTP中建立错误文件文件夹{ErrorFilePackName}");
// ftp.MakeDir(ErrorFilePackName);
//}
//获得FTP的文件列表
var filearray = ftp.GetFileList("",out ftpmsg);
log.Debug($"FTP连接:{FTPADDRESS},{FTPPATH},{FTPUSERNAME},{FTPPWD},ftpmsg={ftpmsg}");
if (filearray == null || filearray.Count() == 0)
{
log.Debug($"没有可读取的文件,进程结束");
moveErrorFileOut(ftp);
return;
}
log.Debug($"发现文件数:{filearray.Count()}");
//如果文件列表没有文件 结束
//if ( filearray.Count() == 0 ) return;
var filepath = AppDomain.CurrentDomain.BaseDirectory + @"\XMLFILE\" + DateTime.Now.ToString("yyyy-MM-dd") + @"\";
if (Directory.Exists(filepath) == false)
{
Directory.CreateDirectory(filepath);
log.Debug($"服务器本地建立文件夹:{filepath}");
}
//var pkgslist = DBWork.GetPKGSList(connStr);
//2:遍历文件列表 将文件解析成数据对象
var clcount = 0;
foreach (var filename in filearray) {
try
{
ftp = new FtpWeb(FTPADDRESS, FTPPATH, FTPUSERNAME, FTPPWD);
//只处理末尾是.xml的文件 不分大小写;
var filenamelast = filename.Substring(filename.Length - 4, 4).ToUpper();
if (filenamelast != ".XML")
continue;
if (filename.Contains(ErrorFileHead))
continue;
clcount++;
//2.1:保存数据对象 记录结果
if (Directory.Exists(filepath + filename) == false)
{
log.Debug($"保存文件:{filename}");
ftp.Download(filepath, filename);
currXMLFile = filename;
}
var xmlstr = ftp.getFileStr(filename);
//XMLParser xmlParser = new XMLParser();
//XMLNode xn = xmlParser.Parse(xmlstr);
log.Debug($"开始解析文档{clcount.ToString()}:{filename}");
var xmlroot = XmlReader.GetXmlDocByXmlContent(xmlstr);
log.Debug($"已解析至xml对象开始整理数据");
var _r = 0;//影响行数
var msgout = "";
var pkgslist = DBWork.GetPKGSList(connStr);
var ctncodelist= DBWork.GetCtnCodeList(connStr);
var head = new FTP_SeaeOrderMC(xmlroot, carrierList, , pkgslist, ctncodelist, out msgout);
if (msgout != "over")
{
log.Debug($"提取数据错误:{msgout}");
movetoerror(ftp);
//GC.Collect();
}
else
{
log.Debug($"解析完成,保存数据:{filename}");
var sqlstr = DBWork.getSaveSql(connStr, head);
log.Debug($"Save语句:{sqlstr}");
_r = DBWork.Save(connStr, head);
log.Debug($"保存完成");
////2.2 将保存正确的文件删除。
if (_r > 0)
{
log.Debug($"开始删除:{filename}");
ftp.Delete(filename);
log.Debug($"结束删除");
}
if (head.BLTYPE != "Booking") {
var htmlFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "JFMailTemplate.html");
var content = File.ReadAllText(htmlFile);
content = content.Replace("$SHIPPER$", head.SHIPPER.Replace("\r\n", "<br />"));
content = content.Replace("$CONSIGNEE$", head.CONSIGNEE.Replace("\r\n", "<br />"));
content = content.Replace("$NOTIFYPARTY$", head.NOTIFYPARTY.Replace("\r\n", "<br />"));
content = content.Replace("$CUSTNO$", head.CUSTNO);
content = content.Replace("$MBLNO$", head.MBLNO);
content = content.Replace("$ETD$", head.ETD);
content = content.Replace("$SERVICE$", head.SERVICE);
content = content.Replace("$BLFRT$", head.BLFRT);
content = content.Replace("$PREPARDAT$", head.PREPARDAT);
content = content.Replace("$PAYABLEAT$", head.PAYABLEAT);
content = content.Replace("$VESSEL$", head.VESSEL);
content = content.Replace("$VOYNO$", head.VOYNO);
content = content.Replace("$PORTLOAD$", head.PORTLOAD);
content = content.Replace("$NOBILL$", head.NOBILL);
content = content.Replace("$ISSUEPLACE$", head.ISSUEPLACE);
content = content.Replace("$PORTDISCHARGE$", head.PORTDISCHARGE);
content = content.Replace("$PLACEDELIVERY$", head.PLACEDELIVERY);
content = content.Replace("$MARKS$", head.MARKS.Replace("\r\n", "<br />"));
content = content.Replace("$PKGS$", head.PKGS);
content = content.Replace("$KINDPKGS$", head.KINDPKGS);
content = content.Replace("$DESCRIPTION$", head.DESCRIPTION.Replace("\r\n", "<br />"));
content = content.Replace("$KGS$", head.KGS);
content = content.Replace("$CBM$", head.CBM);
content = content.Replace("$CARRIER$", head.CARRIER);
content = content.Replace("$CUSTATTN$", head.CUSTATTN);
content = content.Replace("$CUSTTEL$", head.CUSTTEL);
content = content.Replace("$DCLASS$", head.DCLASS);
content = content.Replace("$DUNNO$", head.DUNNO);
content = content.Replace("$SERVICECONTRACTNO$", head.SERVICECONTRACTNO);
content = content.Replace("$ISSUETYPE$", head.ISSUETYPE);
content = content.Replace("$TEMPMIN$", head.TEMPMIN);
content = content.Replace("$TEMPMAX$", head.TEMPMAX);
content = content.Replace("$REMARK$", head.REMARK.Replace("\r\n", "<br />"));
HtmlDocument htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(content);
var nodeTbList = htmlDoc.GetElementbyId("tbCtnDataList");
nodeTbList.RemoveAllChildren();
foreach (var itemsctn in head.CtnList) {
var tr = htmlDoc.CreateElement("tr");
nodeTbList.AppendChild(tr);
var CNTRNO = htmlDoc.CreateElement("td");
tr.AppendChild(CNTRNO);
CNTRNO.SetAttributeValue("style", "padding:10px;border-left:1px solid #333;border-bottom:1px solid #333;text-align:center;font-size:14px;");
CNTRNO.InnerHtml = $"{itemsctn.CNTRNO}";
var SEALNO = htmlDoc.CreateElement("td");
tr.AppendChild(SEALNO);
SEALNO.SetAttributeValue("style", "padding:10px;border-bottom:1px solid #333;text-align:center;font-size:14px;");
SEALNO.InnerHtml = $"{itemsctn.SEALNO}";
var CTNALL = htmlDoc.CreateElement("td");
tr.AppendChild(CTNALL);
CTNALL.SetAttributeValue("style", "padding:10px;border-bottom:1px solid #333;text-align:center;font-size:14px;");
CTNALL.InnerHtml = $"{itemsctn.CTNALL}";
var PKGS = htmlDoc.CreateElement("td");
tr.AppendChild(PKGS);
PKGS.SetAttributeValue("style", "padding:10px;border-bottom:1px solid #333;text-align:center;font-size:14px;");
PKGS.InnerHtml = $"{itemsctn.PKGS}{itemsctn.KINDPKGS}";
var KGS = htmlDoc.CreateElement("td");
tr.AppendChild(KGS);
KGS.SetAttributeValue("style", "padding:10px;border-bottom:1px solid #333;text-align:center;font-size:14px;");
KGS.InnerHtml = $"{itemsctn.KGS}";
var CBM = htmlDoc.CreateElement("td");
tr.AppendChild(CBM);
CBM.SetAttributeValue("style", "padding:10px;border-bottom:1px solid #333;text-align:center;font-size:14px;");
CBM.InnerHtml = $"{itemsctn.CBM}";
var HSCODE = htmlDoc.CreateElement("td");
tr.AppendChild(HSCODE);
HSCODE.SetAttributeValue("style", "padding:10px;border-bottom:1px solid #333;text-align:center;font-size:14px;");
HSCODE.InnerHtml = $"{itemsctn.HSCODE}";
var GOODSNAME = htmlDoc.CreateElement("td");
tr.AppendChild(GOODSNAME);
GOODSNAME.SetAttributeValue("style", "padding:10px;border-bottom:1px solid #333;border-right:1px solid #333;text-align:center;font-size:14px;");
GOODSNAME.InnerHtml = $"{itemsctn.GOODSNAME.Replace("\r\n", "<br />")}";
}
StringWriter writer = new StringWriter();
htmlDoc.Save(writer);
string str = writer.ToString();
var stmpid = DBWork.GetMailStmp(connStr);
var maillist = DBWork.Get(connStr,head);
DBWork.SaveMail(connStr, maillist,"辛克截单:"+head.MBLNO+" "+head.CUSTNO, str, stmpid);
}
//}
//GC.Collect();
}
}
catch (Exception ex)
{
log.Debug($"错误foreach{ex.Message}");
//GC.Collect();
//将该文件写入error文件夹 并删除原文件
movetoerror(ftp);
//GC.Collect();
//log.Error(ex.StackTrace);
}
}
moveErrorFileOut(ftp);
}
catch (Exception ex)
{
log.Debug($"错误:{ex.Message}");
//GC.Collect();
//将该文件写入error文件夹 并删除原文件
//movetoerror(ftp);
//GC.Collect();
//log.Error(ex.StackTrace);
}
moveErrorFileOut(ftp);
}
private void DoSendMessage(string mblno) {
}
private void movetoerror(FtpWeb ftp) {
try
{
//ftp.MoveFile(currXMLFile, ErrorFilePackName + "/" + currXMLFile);
//log.Debug($"文件移动:" + ErrorFilePackName + "/" + currXMLFile + " 完成");
ftp.ReName(currXMLFile, ErrorFileHead + currXMLFile);
log.Debug($"文件重命名:" + ErrorFileHead + currXMLFile + " 完成");
}
catch (Exception ex3)
{
log.Debug($"文件移动错误_movetoerror" + ex3.Message);
}
}
private void moveErrorFileOut(FtpWeb ftp)
{
log.Debug($"错误文件移动回根目录:开始");
try
{
var ftpmsg = "";
//var ftp = new FtpWeb(FTPADDRESS, FTPPATH, FTPUSERNAME, FTPPWD);
//ftp.GotoDirectory("ERROR", false);
//var errorfilearray = ftp.GetFileList("", out ftpmsg);
//log.Debug($"错误文件数量:{errorfilearray.Length.ToString()}");
//if(errorfilearray.Length>0)
// log.Debug($"错误文件1:{errorfilearray[0].ToString()}");
//if (errorfilearray!=null && errorfilearray.Length>0)
// foreach (var errorFile in errorfilearray)
// {
// log.Debug($"错误文件{errorFile}移动:开始 ");
// ftp.MoveFile( errorFile,"../"+errorFile);
// log.Debug($"错误文件{errorFile}移动:完成");
// }
//ftp = new FtpWeb(FTPADDRESS, FTPPATH, FTPUSERNAME, FTPPWD);
//20200825 改为将ERROR五个字母放置在文件名前面
//var ftp = new FtpWeb(FTPADDRESS, FTPPATH, FTPUSERNAME, FTPPWD);
var errorfilearray = ftp.GetFileList("", out ftpmsg);
if (errorfilearray != null) {
foreach (var efn in errorfilearray) {
if(efn.Contains(ErrorFileHead))
ftp.ReName(efn, efn.Replace(ErrorFileHead, ""));
}
}
}
catch (Exception ex3)
{
log.Debug($"文件移动错误_moveErrorFileOut" + ex3.Message);
}
}
}
}