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.

464 lines
19 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.IO;
using dongshengFtpTools.common;
using MailAnalyzeTools.Common;
using System.Windows.Forms;
using MailAnalyzeTools.Logic.HongAn;
using MailAnalyzeTools.Models;
using System.Data;
/*
* 处理共享文件夹里的edi提单,处理完成移动到其他文件夹
*
*
*
*/
namespace MailAnalyzeTools
{
class dealHA_Edi_tool
{
//状态栏标签
public ToolStripStatusLabel TSLabl_Info;
private static string m_HA_Edi_Path;
private static string m_HA_Edi_TargetPath;
private static string GetHA_Edi_Path()
{
if (m_HA_Edi_Path == null)
{
m_HA_Edi_Path = ConfigurationManager.AppSettings["HA_Edi_Path"];
}
return m_HA_Edi_Path;
}
private static string GetHA_Edi_TargetPath()
{
if (m_HA_Edi_TargetPath == null)
{
m_HA_Edi_TargetPath = ConfigurationManager.AppSettings["HA_Edi_TargetPath"];
if (string.IsNullOrEmpty(m_HA_Edi_TargetPath))
{
string basepath = System.AppDomain.CurrentDomain.BaseDirectory;
m_HA_Edi_TargetPath = Path.Combine(Directory.GetParent(basepath).Parent.FullName, "data");
}
}
return m_HA_Edi_TargetPath;
}
public static bool IsBusy;
//获得要处理的文件private bool analyzeFilelist(
public bool analyzeFilel()
{
bool isok = true;
string pth = GetHA_Edi_Path();
try
{
const string TDNAME = "提单确认";
IsBusy = true;
//文件数
int iindex = 0;
int iFailure = 0;//失败文件数
//按修改日期升序
string sql_pre = "INSERT INTO [t_op_seae_td_file]([ftp_filename],[lastmodi_date] ,[filesize],[download_ok],[remark],local_path,download_date,ReportID,fileType) "
+ " VALUES ('{0}','{1}','{2}',{3},'{4}','{5}',getdate(),'{6}','{7}')";
string sql;
string sql_Analyze;
DateTime dtmodi = DateTime.Today;
string localfileName = "";
DirectoryInfo folder = new DirectoryInfo(pth);
FileInfo[] files = folder.GetFiles("*.*");
int ncount = files.Length;
//TODO:严重问题列表,需要发邮件通知!!!!!!!
List<string> fileDealErrList = new List<string>();
foreach (FileInfo file in files)
{
iindex++;
bool isokOp = true;
fileprop fp = new fileprop();
fp.fdate = file.LastWriteTime.ToString("yyy-MM-dd HH:mm:ss");
fp.fname = file.Name;
fp.fsize = file.Length.ToString();
string fname = file.FullName;
//
iindex++;
if (TSLabl_Info != null)
{
try
{
TSLabl_Info.Text = string.Format("{0}/{1}:{2}", iindex, ncount, fp.fname);
}
catch (Exception)
{
;
}
}
Application.DoEvents();
DateTime taskdatetime = file.LastWriteTime;
//localpath要更新地址
string localpath;
string newreportid = NewGUID();
string reporttype = "未知";
string errorinfo = "";
localpath = GetHA_Edi_TargetPath();
if (!Directory.Exists(localpath))
{
Directory.CreateDirectory(localpath);
}
// 目标路径+"ediTD"+"yyyymm";
localpath = Path.Combine(localpath, string.Format("ediTD\\{0}", DateTime.Today.ToString("yyyyMM")));
if (!Directory.Exists(localpath))
{
Directory.CreateDirectory(localpath);
}
if (file.Extension != null && file.Extension.ToLower() == ".xml")
{
reporttype = TDNAME;
}
//string localfullpath ="1.xml";
string localfullpath = localpath + "\\" + Path.GetFileNameWithoutExtension(file.Name) + DateTime.Now.ToString("_yyyyMMddHHmmss") + file.Extension;
StringBuilder blderrorinfo = new StringBuilder();
try
{
File.Move(fname, localfullpath);
}
catch (Exception ex)
{
LogHelper.WriteLog("ediTD", ex.Message);
continue;
}
//任务对象
TOP_Task optsk = new TOP_Task();
//任务编号
optsk.F_01RWBH = newreportid;
optsk.F_02SJRWH = null;
optsk.F_03RWLX = reporttype;
optsk.F_04RWLY = "FTP";
optsk.F_05RWZT = "待导入确认";
optsk.F_06RWSM = "鸿安货运Edi提单";
optsk.F_07FQR = "DEMO-SA";
//任务开始时间,初始化值-->用实际时间替换
optsk.F_09RWKSSJ = taskdatetime.ToString("yyyy-MM-dd HH:mm:ss");
optsk.F_08LRRQ = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
optsk.F_10WCFS = "手动";
optsk.F_14WJBH = newreportid;
//电子档案路径-->服务器路径-->待用共享路径替换
optsk.F_16DZDALJ = localfullpath;
optsk.F_17SFGG = true;
//插入文件接收记录
sql = string.Format(sql_pre, fp.fname, taskdatetime.ToString("yyyy-MM-dd HH:mm:ss"), fp.fsize, 1, "", localfullpath, newreportid, reporttype);
int iresult = DbHelperSQL.ExecuteSql(sql);
if (iresult < 1)
{
//问题很严重
string tmp = "ediTD 插入记录表出错";
LogHelper.WriteLog(tmp, localfullpath);
optsk.F_22Remark = "ediTD 插入记录表出错@!@";
}
//错误的文件名称
if (reporttype != TDNAME)
{
optsk.F_03RWLX = TDNAME;
optsk.F_05RWZT = "报文文件错误";
iFailure++;
errorinfo = "未知类型的鸿安edi提单";
LogHelper.WriteLog(errorinfo, localfullpath);
if (iresult > 0)
{
UpdateEdiFileDealStatus(newreportid, false, errorinfo);
}
//插入失败任务
InsertHAHYediTask(optsk, null, null,blderrorinfo);
continue;
}
//分析
try
{
TOp_seae_edi_rp_iftmbc edi_rp_iftmbc = new TOp_seae_edi_rp_iftmbc();
List<TOP_EDI_CTN_HACS> ctnlist = new List<TOP_EDI_CTN_HACS>();
//HAEdiAnalyzeTool.ImportMCSEdiList(localfullpath,edi_rp_iftmbc, ctnlist);
//局部变量还是成员变量?????-->TODO:成员
HAEdiXmlAnalyzeTool hntool = new HAEdiXmlAnalyzeTool();
edi_rp_iftmbc.F_Base_ReportID = newreportid;
//分析
isokOp = hntool.AnalyzeHAEdi(localfullpath, edi_rp_iftmbc, ctnlist, blderrorinfo);
//提单号查询 sea编号及 对ctnlist 赋值
initialTaskSeaRelatedInfo(optsk, edi_rp_iftmbc, ctnlist);
//todo 插入任务
InsertHAHYediTask(optsk,edi_rp_iftmbc, ctnlist, blderrorinfo);
}
catch (Exception ex)
{
errorinfo = "分析HAHY edi 发生异常: " + ex.Message;
blderrorinfo.AppendLine(errorinfo);
LogHelper.WriteLog(errorinfo, " reportid为" + newreportid);
}
UpdateEdiFileDealStatus(newreportid, isokOp, errorinfo);
//先移动,如果成功做后续处理:插入记录,日期和文件名作为关键字
Console.WriteLine(file.FullName);
Console.WriteLine(file.LastWriteTime);
}
}
catch (Exception ex)
{
isok = false;
LogHelper.WriteLog( typeof(dealHA_Edi_tool) ,ex);
}
finally
{
IsBusy = false;
}
return isok;
}
/// <summary>
///
/// </summary>
/// <param name="optsk"></param>
/// <param name="edibc"></param>
/// <param name="ctnlist"></param>
/// <returns></returns>
private bool InsertHAHYediTask(TOP_Task optsk, TOp_seae_edi_rp_iftmbc edibc, List<TOP_EDI_CTN_HACS> ctnlist, StringBuilder errorinfo)
{
string sql;
bool isok = true;
if (edibc != null)
{
try
{
sql = edibc.GetHAHY_EdiTD_OpSeaInsertSql();
int i = DbHelperSQL.ExecuteSql(sql);
if (i < 1)
{
errorinfo.AppendLine("插入业务信息失败");
}
}
catch (Exception ex)
{
string info = "插入业务信息失败:" + ex.Message;
errorinfo.AppendLine(info);
LogHelper.WriteLog(info, optsk.F_16DZDALJ);
isok = false;
}
}
if (ctnlist != null)
{
foreach (var item in ctnlist)
{
item.base_reportid = edibc.F_Base_ReportID;
try
{
sql = item.GetHAHY_EdiTD_OpctnInsertSql();
int i = DbHelperSQL.ExecuteSql(sql);
if (i < 1)
{
errorinfo.AppendLine("插入业务明细信息失败");
}
}
catch (Exception ex)
{
isok = false;
string info = "插入业务明细信息失败:" + ex.Message;
errorinfo.AppendLine(info);
LogHelper.WriteLog(info, optsk.F_16DZDALJ);
}
}
}
if (!isok)
{
optsk.F_22Remark = (string.IsNullOrEmpty(optsk.F_22Remark) ? "" : optsk.F_22Remark) + errorinfo.ToString();
}
string sql_pre = @"INSERT INTO [t_op_task]
([任务编号] ,[任务类型],[任务来源],[任务状态],[任务说明]
,[发起人] ,[录入日期] ,[任务开始时间] ,[完成方式] ,[完成时间]
,[提单号],[文件编号],[邮件编号],[电子档案路径],
[是否公共],[任务相关人员],[SEA编号],[备注])
values({0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13},{14},{15},{16},{17}) ";
sql = string.Format(sql_pre,
StrUtill.GetValueString(optsk.F_01RWBH), // 0[任务编号] o
StrUtill.GetValueString(optsk.F_03RWLX), // 1任务类型 o
StrUtill.GetValueString(optsk.F_04RWLY), // 2任务来源 o
StrUtill.GetValueString(optsk.F_05RWZT), // 3任务状态 o
StrUtill.GetValueString(optsk.F_06RWSM), // 4任务说明 o
StrUtill.GetValueString(optsk.F_07FQR), // 5发起人 o
StrUtill.GetValueString(optsk.F_08LRRQ), //6 录入日期 o
StrUtill.GetValueString(optsk.F_09RWKSSJ), //7 任务开始时间 o-->to
StrUtill.GetValueString(optsk.F_10WCFS), // 8完成方式 o
StrUtill.GetValueString(optsk.F_11WCSJ), // 9完成时间 h
StrUtill.GetValueString(optsk.F_13TDH), //10 提单号 to
StrUtill.GetValueString(optsk.F_14WJBH), //11 文件编号 o
StrUtill.GetValueString(optsk.F_15YJBH), //12 邮件编号 h
StrUtill.GetValueString(optsk.F_16DZDALJ,180), //13 电子档案路径 o->to
optsk.F_17SFGG?"1":"0", // 是否公共 o--> to
StrUtill.GetValueString(optsk.F_18RWXGRY), // 任务相关人员 to
StrUtill.GetValueString(optsk.F_19SEABH), // SEA编号 to
StrUtill.GetValueString(optsk.F_22Remark,100) //备注 to
);
try
{
int i= DbHelperSQL.ExecuteSql(sql);
}
catch (Exception ex )
{
LogHelper.WriteLog("插入H鸿安货运Edi任务发生异常:", ex.Message);
UpdateEdiFileDealStatus(edibc.F_Base_ReportID, false, ex.Message);
}
//pdfName
//邮件附件上传到nass服务器
return true;
}
/// <summary>
/// 根据提单号进行任务分配
/// </summary>
/// <param name="optsk"></param>
/// <param name="edibc"></param>
/// <param name="ctnlist"></param>
private bool initialTaskSeaRelatedInfo(TOP_Task optsk, TOp_seae_edi_rp_iftmbc edibc, List<TOP_EDI_CTN_HACS> ctnlist)
{
bool isfound = false;
if (edibc != null)
{
//提单号,如果提单号为空则取订舱号
optsk.F_13TDH = string.IsNullOrEmpty(edibc.F_RFF_BM) ? edibc.F_RFF_BN:edibc.F_RFF_BM ;
}
if (!string.IsNullOrEmpty(optsk.F_13TDH))
{
string tdh = optsk.F_13TDH;
//查看主题单号是否存在业务系统中,以便进行任务分配
string sql = @"select s.编号,s.客服员 from t_op_seae s where s.业务类型='普通货' and s.主提单号='{0}';";
DataTable tbltmp = DbHelperSQL.QueryTable(string.Format(sql, optsk.F_13TDH));
if (tbltmp != null && tbltmp.Rows.Count > 0)
{
isfound = true;
//业务编号
string seano = tbltmp.Rows[0]["编号"].ToString();
optsk.F_19SEABH = seano;
//任务人
optsk.F_18RWXGRY = tbltmp.Rows[0]["客服员"].ToString();
optsk.F_17SFGG = false;
edibc.F_EXT_SeaNo = seano;
//从明细实体类中获得:
//危险品分类
//危险品编号
//冷藏通风量
//温度单位
//设置温度
//最低温度 <==设置温度
//最高温度 <==设置温度
foreach (var item in ctnlist)
{
item.F_SEANO = seano;
//危险品分类
if (!string.IsNullOrEmpty(item.ExtDGIMOClassCode))
{
edibc.F_WP_DGS_Type = item.ExtDGIMOClassCode;
}
//危险品编号
if (!string.IsNullOrEmpty(item.ExtDGUNDGNumber))
{
edibc.F_WP_DGS_No = item.ExtDGUNDGNumber;
}
//风流量
if (!string.IsNullOrEmpty(item.ExtEquipmentAirFlow))
{
edibc.F_EXT_EquipmentAirFlow = item.ExtEquipmentAirFlow;
}
//设置温度
if (!string.IsNullOrEmpty(item.ExtEquipmentTemperature))
{
edibc.F_WP_TMP = item.ExtEquipmentTemperature;
}
//温度单位
if (!string.IsNullOrEmpty(item.ExtEquipmentTemperatureUOM))
{
edibc.F_EXT_EquipmentTemperatureUOM = item.ExtEquipmentTemperatureUOM;
}
}
}
}
return isfound;
}
/// <summary>
/// 更新HAHY edi 处理状态
/// </summary>
/// <param name="reportid"></param>
/// <param name="isdealok"></param>
/// <param name="info"></param>
/// <returns></returns>
private bool UpdateEdiFileDealStatus(string reportid, bool isdealok, string info)
{
bool isok = true;
try
{
string sql = "UPDATE t_op_seae_td_file SET deal_ok = {1}, remark= isnull(remark,'')+{2} WHERE [ReportID]='{0}'";
{
info=(string.IsNullOrEmpty(info)?"''": StrUtill.GetValueString(info, 100));
}
int i = DbHelperSQL.ExecuteSql(string.Format(sql, reportid, isdealok ? 1 : 0,info ));
if (i < 1)
{
LogHelper.WriteLog("更新HAHY edi 处理状态未成功:reportid为", reportid);
isok = false;
}
}
catch (Exception)
{
isok = false;
LogHelper.WriteLog("更新HAHY edi 处理状态出错:reportid为", reportid);
}
return isok;
}
private string NewGUID()
{
return System.Guid.NewGuid().ToString("N");
}
}
}