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.

429 lines
17 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 System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//
using OpenPop.Pop3;
using OpenPop.Mime;
using System.IO;
using MailAnalyzeTools.Models;
using System.Configuration;
using System.Threading;
using MailAnalyzeTools.Common;
namespace MailAnalyzeTools
{
public partial class frmAnalyzeMail
{
private bool dealFtpKodaMail(TOp_seae_edi_mail edimai, StringBuilder subbody)
{
bool result = true;
string subtext = edimai.F_03_YJZT;
//ftp_kodai--Transmission Received: Accepted File: MGAE7700010020170724094917643.XML
const string cntHG00 = "AFR-接受";
//ftp_kodai--Transmission Received: Rejected File: HASLNREH77000P0020170724112424688.XML
const string cntHG01 = "AFR-拒绝";
string fv1_guid;
string fv2_sendtime = edimai.F_04_YJFSRQ.ToString("yyyy-MM-dd HH:mm:ss");
short fv13_ftpmatched = 0;
string hblno = "";//分提单号
string hblstatus = "";//状态
//string errorInfo = "";//错误信息
List<string> m_ftpbodyLines;
bool accItem = false;
bool rejItem = false;
List<string> accList = new List<string>();
List<string> rejList = new List<string>();
m_ftpbodyLines = subbody.ToString().Replace("\r\n", "\r").Split('\r').ToList();
////存在多票和部分拒绝
int i;
int iaccStart = 0;
int irejStart = 0;
//定位及判定 接受/拒绝
for (i = 0; i < m_ftpbodyLines.Count; i++)
{
if (m_ftpbodyLines[i].IndexOf("Number of Documents Accepted") >= 0)
{
var strarr = m_ftpbodyLines[i].Trim().Split(':');
if (strarr.Length == 2 && strarr[1].Trim() != "0")
{
accItem = true;
iaccStart = i;
}
}
if (m_ftpbodyLines[i].IndexOf("Number of Documents Rejected") >= 0)
{
var strarr = m_ftpbodyLines[i].Trim().Split(':');
if (strarr.Length == 2 && strarr[1].Trim() != "0")
{
rejItem = true;
irejStart = i;
}
break;
}
}
//接受
bool start = false;
if (accItem)
{
for (i = iaccStart; i < m_ftpbodyLines.Count; i++)
{
if (m_ftpbodyLines[i].Trim().IndexOf("Documents Accepted") == 0)
{
var strarr = m_ftpbodyLines[i].Trim().Split(':');
if (strarr.Length == 2 && strarr[1].Trim() != "<None>")
{
start = true;
accList = strarr[1].Trim().Replace(" ", " ").Split(' ').ToList();
continue;
}
}
if (m_ftpbodyLines[i].Trim().IndexOf("Documents Rejected") == 0 || m_ftpbodyLines[i].IndexOf("=") >= 0
|| m_ftpbodyLines[i].IndexOf("Document:") >= 0)
break;//查到拒绝则结束
if (start)
{
var aa = m_ftpbodyLines[i].Trim().Replace(" ", " ").Split(' ').ToList();
accList.InsertRange(accList.Count, aa);
}
}
}
//拒绝
start = false;
if (rejItem)
{
for (i = irejStart; i < m_ftpbodyLines.Count; i++)
{
if (m_ftpbodyLines[i].Trim().IndexOf("Documents Rejected") == 0)
{
var strarr = m_ftpbodyLines[i].Trim().Split(':');
if (strarr.Length == 2 && strarr[1].Trim() != "<None>")
{
start = true;
rejList = strarr[1].Trim().Replace(" ", " ").Split(' ').ToList();
continue;
}
}
if (m_ftpbodyLines[i].IndexOf("=") >= 0 || m_ftpbodyLines[i].IndexOf("Document:") >= 0)
break;//查到其他内容则结束
if (start)
{
var aa = m_ftpbodyLines[i].Trim().Replace(" ", " ").Split(' ').ToList();
rejList.InsertRange(rejList.Count, aa);
}
}
}
var arr = subtext.Split(':');
switch (arr.Length)
{
case 3:
if (arr[1].Trim() == "Accepted File" || arr[1].IndexOf("Accepted") >= 0)
{
hblstatus = cntHG00;
}
else if (arr[1].Trim() == "Rejected File" || arr[1].IndexOf("Rejected") >= 0)
{
hblstatus = cntHG01;
}
if (arr[2].Length > 21)
{
//YYYYMMDDhhiissmmm.xml
hblno = arr[2].Substring(0, arr[2].Length - "20170724104404343.XML".Length).Trim();
}
break;
default:
break;
}
string sql_update = " update op_seae_edi set MANIFESTSTATUS='{0}' where HBLNO='{1}'";
//插入状态:guid,状态,邮件发送时间,分提单号
string sql_status = @" INSERT INTO [op_status]
(ST_ID,BSNO,[STATUS],ISCOMP,COMPTIME,INPUTTIME,STTYPE)
select distinct '{0}' AS ST_ID, BSNO,'{1}' as [STATUS],1 AS [ISCOMP],'{2}' as [COMPTIME],GETDATE() as inputtime,9 as STTYP
from op_seae_edi where HBLNO='{3}' ;";
if (accList.Count + rejList.Count < 1)
{
m_errlist.Add("未找到匹配标记");
fv13_ftpmatched = 0;
}
string infoDetail = "";
int imatchtimes1 = 0;
int imatchtimes2 = 0;
//接受
if (accList.Count > 0)
{
if (m_errlist == null)
m_errlist = new List<string>();
infoDetail = "AFR-接受:";
for (i = 0; i < accList.Count; i++)
{
try
{
infoDetail += " " + accList[i];
//edi表状态和时间在一个字段
int itmp = DbHelperSQL.ExecuteSql(string.Format(sql_update, cntHG00 + "_" + fv2_sendtime, accList[i]));
if (itmp < 1)
{
m_errlist.Add("未能更新舱单状态,没有匹配的分提单号" + accList[i]);
fv13_ftpmatched = 0;
}
else
{
imatchtimes1++;
}
fv1_guid = System.Guid.NewGuid().ToString("N").ToUpper();
itmp = DbHelperSQL.ExecuteSql(string.Format(sql_status, fv1_guid, cntHG00, fv2_sendtime, accList[i]));
if (itmp < 1)
{
m_errlist.Add("未能正确插入提单状态,分提单号" + accList[i]);
fv13_ftpmatched = 0;
}
}
catch (Exception ex)
{
LogHelper.WriteLog(this.GetType(), ex);
m_errlist.Add("更新状态时发生异常,请查看日志!");
result = false;
}
}
}
//拒绝
if (rejList.Count > 0)
{
if (m_errlist == null)
m_errlist = new List<string>();
infoDetail = (infoDetail == "" ? "" : infoDetail + ";") + "AFR-拒绝:";
for (i = 0; i < rejList.Count; i++)
{
try
{
infoDetail += " " + rejList[i];
//edi表 状态和时间在一个字段
int itmp = DbHelperSQL.ExecuteSql(string.Format(sql_update, cntHG01 + "_" + fv2_sendtime, rejList[i]));
if (itmp < 1)
{
m_errlist.Add("未能更新舱单状态,没有匹配的分提单号" + rejList[i]);
}
else
{
imatchtimes2++;
}
fv1_guid = System.Guid.NewGuid().ToString("N").ToUpper();
itmp = DbHelperSQL.ExecuteSql(string.Format(sql_status, fv1_guid, cntHG01, fv2_sendtime, rejList[i]));
if (itmp < 1)
{
m_errlist.Add("未能正确插入提单状态,分提单号" + rejList[i]);
}
}
catch (Exception ex)
{
LogHelper.WriteLog(this.GetType(), ex);
m_errlist.Add("更新状态时发生异常,请查看日志!");
result = false;
}
}
}
edimai.F_10_HBLNO = hblno;
edimai.F_11_edistatus = hblstatus;
edimai.F_12_ftpstatus = infoDetail;
short mathresult = 0;
if ((imatchtimes1 + imatchtimes2) == (accList.Count + rejList.Count))
{
//全匹配
mathresult = 2;
}
else if ((imatchtimes1 + imatchtimes2) > 0)
{
//部分匹配
mathresult = 1;
}
else
{
//无匹配
mathresult = 0;
}
edimai.F_13_ismatched = mathresult;
return result;
}
private bool dealKodaMail(TOp_seae_edi_mail edimai)
{
bool result = true;
string subtext = edimai.F_03_YJZT;
string fv1_guid = System.Guid.NewGuid().ToString("N").ToUpper();
string fv2_sendtime = edimai.F_04_YJFSRQ.ToString("yyyy-MM-dd HH:mm:ss");
string hblstatus;//状态
string hblno;//分提单号
string errorInfo;//错误信息
string tmp;
int itmp;
//KODA HBL J0R7PASU5123948530: JAPAN Ocean AFR Filing: Received LNK HBL Linked With Carrier BL//已关联
const string cntHG02 = "AFR-已关联";
//KODA HBL J0R7NAEC77000400: JAPAN Ocean AFR Filing: Received NL2 Carrier BL Not On File//船公司单未发送
const string cntHG03 = "AFR-船公司单未发送";
//KODA HBL J0R7PASU5137295802: JAPAN Ocean AFR Filing Rejected
const string cntHG04 = "AFR-海关拒绝";
//KODA HBL J0R7SNL7QDJL900787: JAPAN Ocean AFR Filing: Received NL1 Carrier BL Filed As Regular Type
const string cntHG05 = "AFR-提单不一致";
const string unknown1_status = "AFR-未知主题";
const string unknown2_hblno = "AFR-未能获取HBLNO";
hblno = "-1";
subtext = subtext.Trim();
hblstatus = "";
errorInfo = "";
if (subtext.IndexOf("KODA") >= 0)
{
//hengda
var arr = subtext.Split(':');
switch (arr.Length)
{
case 2:
if (arr[1].Trim() == "JAPAN Ocean AFR Filing Rejected" || arr[1].IndexOf("Rejected") >= 0)
{ //状态
hblstatus = cntHG04;// "海关拒绝"
//分提单号
tmp = arr[0].Trim();
itmp = tmp.IndexOf("KODA HBL J0R7");
if (itmp >= 0)
{
hblno = tmp.Substring(itmp + ("KODA HBL J0R7").Length, tmp.Length - itmp - ("KODA HBL J0R7").Length);
}
else
{
itmp = tmp.IndexOf("J0R7");
if (itmp >= 0)
{
hblno = tmp.Substring(itmp + ("J0R7").Length, tmp.Length - itmp - ("J0R7").Length);
}
}
}
break;
case 3:
//状态
if (arr[2].Trim() == "Received LNK HBL Linked With Carrier BL" || arr[1].IndexOf("HBL Linked") >= 0)
{
hblstatus = cntHG02;// "已关联"
}
else if (arr[2].Trim() == "Received NL2 Carrier BL Not On File" || arr[1].IndexOf("BL Not") >= 0)
{
hblstatus = cntHG03;// "船公司单未发送"
}
else if (arr[2].Trim() == "Received NL1 Carrier BL Filed As Regular Type" || arr[1].IndexOf("Regular Type") >= 0)
{
hblstatus = cntHG05;// "提单不一致"
}
//分提单号
tmp = arr[0].Trim();
itmp = tmp.IndexOf("KODA HBL J0R7");
if (itmp >= 0)
{
hblno = tmp.Substring(itmp + ("KODA JHBL0R7").Length, tmp.Length - itmp - ("KODA HBL J0R7").Length);
}
else
{
itmp = tmp.IndexOf("J0R7");
if (itmp >= 0)
{
hblno = tmp.Substring(itmp + ("J0R7").Length, tmp.Length - itmp - ("J0R7").Length);
}
}
break;
default:
errorInfo = unknown1_status;
break;
}
}//end "KODA"
if (hblno == "-1")
{
errorInfo += unknown2_hblno;
}
edimai.F_10_HBLNO = hblno;
edimai.F_11_edistatus = hblstatus;
//textBox2.Text = hblno;
//textBox3.Text = hblstatus;
//textBox4.Text = errorInfo;
//更新状态:
string sql_update = " update op_seae_edi set MANIFESTSTATUS='{0}' where HBLNO='{1}'";
//插入状态:guid,状态,邮件发送时间,分提单号
string sql_status = @" INSERT INTO [op_status]
(ST_ID,BSNO,[STATUS],ISCOMP,COMPTIME,INPUTTIME,STTYPE)
select distinct '{0}' AS ST_ID, BSNO,'{1}' as [STATUS],1 AS [ISCOMP],'{2}' as [COMPTIME],GETDATE() as inputtime,9 as STTYP
from op_seae_edi where HBLNO='{3}' ;";
short mathresult = 0;
try
{
if (m_errlist == null)
m_errlist = new List<string>();
//edi表状态和时间在一个字段
itmp = DbHelperSQL.ExecuteSql(string.Format(sql_update, hblstatus + "_" + fv2_sendtime, hblno));
if (itmp < 1)
{
m_errlist.Add("未能更新舱单状态,没有匹配的分提单号" + hblno);
}
else
{
mathresult = 2;
}
itmp = DbHelperSQL.ExecuteSql(string.Format(sql_status, fv1_guid, hblstatus, fv2_sendtime, hblno));
if (itmp < 1)
{
m_errlist.Add("未能正确插入提单状态,分提单号" + hblno);
}
}
catch (Exception ex)
{
LogHelper.WriteLog(this.GetType(), ex);
m_errlist.Add("更新状态时发生异常,请查看日志!");
result = false;
}
edimai.F_13_ismatched = mathresult;
return result;
}
}
}