You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

707 lines
23 KiB

using System;
using System.Collections.Generic;
using System.Text;
using Stone.Common;
using System.IO;
using Gm_WMS.DataAccess.DataService;
using Stone.Entity;
using System.Data;
using System.Threading;
using System.Data.SqlClient;
namespace Stone.WinBiz.JISAdmin
{
public class F_EDI_JIS5000Sequence
{
public static bool IsRun = false;
public static string Path_Receive = string.Empty;
public static string Path_Error = string.Empty;
public static string Path_Bak = string.Empty;
static F_EDI_JIS5000Sequence()
{
Path_Receive = MyAppconfig.ReadValue("接收路径");
Path_Error = MyAppconfig.ReadValue("错误路径");
Path_Bak = MyAppconfig.ReadValue("备份路径");
}
public static void Run()
{
int UpdateTime = Convert.ToInt32(MyAppconfig.ReadValue("解析刷新时间(秒)")) * 1000;
while (IsRun)
{
try
{
ProcessData();
}
catch (Exception ex)
{
MyLogger.Write(ex.Message);
}
System.Threading.Thread.Sleep(UpdateTime);
}
}
public static void ProcessData()
{
DirectoryInfo dir = new DirectoryInfo(Path_Receive);
FileInfo[] files = dir.GetFiles("*.*", SearchOption.TopDirectoryOnly);
//Array.Sort(files, new MyFileSort(FileOrder.LastWriteTime)); //按修改日期升序排列
Array.Sort(files, new MyFileSort(FileOrder.Name)); //按文件名升序排列
foreach (FileInfo file in files)
{
Entity_t_JIS_Log t_JIS_Log = new Entity_t_JIS_Log();
DataRow drJIS_Log = t_JIS_Log.Table.NewRow();
StreamReader sr = null;
try
{
drJIS_Log["FileType"] = GetFileType(file.Name);
drJIS_Log["FileName"] = file.Name;
drJIS_Log["StartTime"] = MyDateTime.GetServerDateTime();
drJIS_Log = t_JIS_Log.Add(drJIS_Log);
sr = file.OpenText();
string text = sr.ReadToEnd();
sr.Close();
string fileType = "";
if (file.Name.ToUpper().IndexOf("SY78") >= 0)
{
fileType = "SY78";
}
if (file.Name.ToUpper().IndexOf("SY88") >= 0)
{
fileType = "SY88";
}
if (fileType == "")
{
throw new Exception(file.Name + " 中不包含 SY78 或 SY88");
}
//判断文件格式是否正式
IsFileOK(text, file.Name);
DateTime StartTime = DateTime.Now;
if (file.Name.ToUpper().IndexOf("DEL") >= 0)
{
ProcessDataLab(text, fileType, file.Name); //解析LAB数据
}
TimeSpan TimeOut = DateTime.Now - StartTime;
MyLogger.Write(file.Name + " 处理成功,用时[" + TimeOut.TotalSeconds + "]秒");
string Path_Bak_New = Path_Bak + "\\" + MyDateTime.Format(MyDateTimeType.Date);
if (!Directory.Exists(Path_Bak_New))
{
Directory.CreateDirectory(Path_Bak_New);
}
MoveFile(file, Path_Bak_New);
drJIS_Log["EndTime"] = MyDateTime.GetServerDateTime();
drJIS_Log["ProcessState"] = "Success";
t_JIS_Log.Edit(drJIS_Log);
}
catch (Exception ex)
{
MyLogger.Write(file.Name + " 处理失败,原因为:" + ex.Message);
if (sr != null) sr.Close();
drJIS_Log["EndTime"] = MyDateTime.GetServerDateTime();
drJIS_Log["ProcessState"] = "Failed";
drJIS_Log["Memo"] = MyStrings.GetString(ex.Message);
t_JIS_Log.Edit(drJIS_Log);
MoveFile(file, Path_Error);
//F_SMS.SendSMS(file.Name + " 处理失败,原因为:" + ex.Message);
F_Mail.Send(file.Name + " 处理失败,原因为:" + ex.Message);
//如果文件有问题,不移动文件,等待一分钟后再处理
Thread.Sleep(60 * 1000);
}
}
}
private static void ProcessDataLab(string text, string fileType, string filename)
{
Entity_t_JIS_Lab t_JIS_Lab = new Entity_t_JIS_Lab();
Entity_t_Product t_Product = new Entity_t_Product();
LocalDBService db = new LocalDBService();
text = text.Replace("\r\n", "").Replace("\r", "").Replace("\n", "");
string[] sp_text = text.Split('\'');
string version = string.Empty;
string releaseId = string.Empty;
string partNumber = string.Empty;
string orderNumber = string.Empty;
string status = string.Empty;
string startDate = string.Empty;
string endDate = string.Empty;
string quantity = string.Empty;
string cumulativeQuantity = string.Empty;
string location = string.Empty;
string LocationCode = string.Empty;
string time = "";
StringBuilder sqls = new StringBuilder();
string sql = "";
List<string> items = new List<string>();
foreach (string line_text in sp_text)
{
if (line_text.Trim() == "") continue;
string[] sp_line = line_text.Split('+');
if (sp_line[0] == "ARD")
{
partNumber = sp_line[1].Split(':')[0];
sql = $"delete from t_JIS_Lab where [fileType]='{fileType}' and [partNumber]='{partNumber}'";
sqls.AppendLine(sql);
if (sqls.Length > 20000)
{
db.Exec_NonQuery(sqls.ToString());
sqls = new StringBuilder();
}
}
}
var strPartMsg = "";
foreach (string line_text in sp_text)
{
if (line_text.Trim() == "") continue;
string[] sp_line = line_text.Split('+');
if (sp_line[0] == "UNB")
{
version = sp_line[5];
time = "20" + sp_line[4].Split(':')[0] + sp_line[4].Split(':')[1];
}
//if (sp_line[0] == "MID")
//{
// releaseId = sp_line[1];
// releaseIdNew = releaseId.Replace("S", "").Replace("/", "").Replace("-", "").Replace(" ", "0");
//}
if (sp_line[0] == "PDI")
{
releaseId = sp_line[1].Trim();
}
if (sp_line[0] == "SAD")
{
status = sp_line[1];
}
if (sp_line[0] == "ARD")
{
partNumber = sp_line[1].Split(':')[0];
orderNumber = sp_line[3];
//校验零件号是否失效
var strsql = "";
strsql = "select top 1 * from t_Product where Code = '" + partNumber + "' order by id desc";
var dsProd = db.Exec_DataSet(strsql);
if (dsProd.Tables[0].Rows.Count > 0)
{
var strState = dsProd.Tables[0].Rows[0]["State"].ToString();
if (strState == "0" || string.IsNullOrEmpty(strState))
strPartMsg += "零件号 " + partNumber + " 已失效!" + Environment.NewLine;
}
}
if (sp_line[0] == "CSG")
{
LocationCode = sp_line[1].Split(':')[0];
location = sp_line[3];
//校验卸货点是否失效
var strsql = "";
strsql = "select top 1 * from t_Location where Code = '" + LocationCode + "' order by id desc";
var dsLoc = db.Exec_DataSet(strsql);
if (dsLoc.Tables[0].Rows.Count > 0)
{
var strState = dsLoc.Tables[0].Rows[0]["State"].ToString();
if (strState == "0" || string.IsNullOrEmpty(strState))
strPartMsg += "卸货点 " + partNumber + " 已失效!" + Environment.NewLine;
}
}
if(sp_line[0] == "DST")
{
cumulativeQuantity = sp_line[2].Split(':')[1];
}
if (sp_line[0] == "DEL")
{
startDate = sp_line[1].Split(':')[0];
endDate = sp_line[1].Split(':')[2];
quantity = sp_line[2].Split(':')[0];
//cumulativeQuantity = sp_line[2].Split(':')[4];
startDate = GetDateTimeStyle5(startDate, MyDateTimeType.Date);
endDate = GetDateTimeStyle5(endDate, MyDateTimeType.Date);
object[] obj = new object[14];
obj[0] = partNumber;
obj[1] = startDate;
obj[2] = endDate;
obj[3] = GetDateTimeStyle1(time, MyDateTimeType.DateTime);
obj[4] = releaseId;
obj[5] = location;
obj[6] = orderNumber;
obj[7] = cumulativeQuantity;
obj[8] = quantity;
obj[9] = status;
obj[10] = fileType;
obj[11] = version;
obj[12] = LocationCode;
obj[13] = filename;
//sql = "delete from t_JIS_Lab where partNumber='{0}' and startDate<='{1}' and endDate>='{2}'";
//sql = string.Format(sql, obj);
//sqls.AppendLine(sql);
//插入新的数据(当前数据)
sql = @"
insert into t_JIS_Lab(partNumber, startDate, endDate, [time], releaseId, location, orderNumber, cumulativeQuantity, quantity, [status], [fileType], [version], [LocationCode], [filename])
values('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', {7}, {8}, {9}, '{10}', '{11}', '{12}', '{13}')
";
sql = string.Format(sql, obj);
sqls.AppendLine(sql);
//插入新的数据(版本对比)
sql = @"
insert into t_JIS_Lab_All(partNumber, startDate, endDate, [time], releaseId, location, orderNumber, cumulativeQuantity, quantity, [status], [fileType], [version], [LocationCode], [filename])
values('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', {7}, {8}, {9}, '{10}', '{11}', '{12}', '{13}')
";
sql = string.Format(sql, obj);
sqls.AppendLine(sql);
//更新采购订单号
//sql = $"update t_Product set [StandardNo]='{orderNumber}' where [Code]='{partNumber}'";
//sqls.AppendLine(sql);
if (sqls.Length > 20000)
{
db.Exec_NonQuery(sqls.ToString());
sqls = new StringBuilder();
}
}
}
if (sqls.ToString() != "")
{
db.Exec_NonQuery(sqls.ToString());
sqls = new StringBuilder();
}
if (strPartMsg != "")
{
F_Mail.Send(strPartMsg);
}
}
private static void MoveFile(FileInfo file, string MovePath)
{
string filename1 = Path_Receive + "\\" + file.Name;
string filename2 = MovePath + "\\" + file.Name;
try
{
if (File.Exists(filename2)) File.Delete(filename2);
File.Move(filename1, filename2);
}
catch (Exception ex)
{
MyLogger.Write("移动文件错误 " + filename1 + "->" + filename2 + ",原因为:" + ex.Message);
}
}
private static string GetFileType(string filename)
{
string ret = "";
if (filename.ToUpper().IndexOf("ASA") >= 0)
{
ret = "ASA";
}
if (filename.ToUpper().IndexOf("ATC") >= 0)
{
ret = "ATC";
}
if (filename.ToUpper().IndexOf("ATP") >= 0)
{
ret = "ATP";
}
if (filename.ToUpper().IndexOf("DEL") >= 0)
{
ret = "解析LAB文件";
}
if (filename.ToUpper().IndexOf("ANB") >= 0)
{
ret = "ANB";
}
return ret;
}
private static void IsFileOK(string text, string filename)
{
text = text.Replace("\r\n", "").Replace("\r", "").Replace("\n", "");
string[] sp_text = text.Split('\'');
if (sp_text[0].Substring(0, 3) != "UNB")
{
throw new Exception(filename + " 不以UNB开头");
}
if (sp_text[sp_text.Length - 2].Substring(0, 3) != "UNZ")
{
throw new Exception(filename + " 不以UNZ结尾");
}
if (sp_text[sp_text.Length - 3].Substring(0, 3) != "UNT")
{
throw new Exception(filename + " 未找到UNT");
}
int segment_count = Convert.ToInt32(sp_text[sp_text.Length - 3].Split('+')[1]);
if (segment_count != sp_text.Length - 3)
{
throw new Exception(filename + " Segment 数量不正确");
}
}
#region 日期时间转换
/// <summary>
/// 201401160939 和 20140116093911 将这种字符串转换为日期
/// </summary>
/// <param name="sDateTime"></param>
/// <param name="dType"></param>
/// <returns></returns>
public static string GetDateTimeStyle1(string sDateTime, MyDateTimeType dType)
{
if (sDateTime.Length != 12 && sDateTime.Length != 14)
{
throw new Exception("[" + sDateTime + "]日期时间格式错误!");
}
string Result = "";
string year = sDateTime.Substring(0, 4);
string month = sDateTime.Substring(4, 2);
string day = sDateTime.Substring(6, 2);
string h = sDateTime.Substring(8, 2);
string m = sDateTime.Substring(10, 2);
string s = sDateTime.Length == 14 ? sDateTime.Substring(12, 2) : "0";
string mi = "0";
if (month.Length == 1) month = "0" + month;
if (day.Length == 1) day = "0" + day;
if (h.Length == 1) h = "0" + h;
if (m.Length == 1) m = "0" + m;
if (s.Length == 1) s = "0" + s;
if (mi.Length == 1) mi = "00" + mi;
if (mi.Length == 2) mi = "0" + mi;
if (dType == MyDateTimeType.DateTime)
{
Result = year + "-" + month + "-" + day + " " + h + ":" + m + ":" + s;
}
if (dType == MyDateTimeType.Date)
{
Result = year + "-" + month + "-" + day;
}
if (dType == MyDateTimeType.Time)
{
Result = h + ":" + m + ":" + s;
}
if (dType == MyDateTimeType.Batch)
{
Result = year.Substring(2, 2) + month + day;
}
if (dType == MyDateTimeType.BillNo)
{
Result = year + month + day + h + m + s + mi;
}
return Result;
}
//12.04.2014 09:07:00
public static string GetDateTimeStyle2(string sDateTime, MyDateTimeType dType)
{
if (sDateTime.Length != 19) throw new Exception("[" + sDateTime + "]日期时间格式错误!");
string Result = "";
string year = sDateTime.Substring(6, 4);
string month = sDateTime.Substring(3, 2);
string day = sDateTime.Substring(0, 2);
string h = sDateTime.Substring(11, 2);
string m = sDateTime.Substring(14, 2);
string s = sDateTime.Substring(17, 2);
string mi = "0";
if (month.Length == 1) month = "0" + month;
if (day.Length == 1) day = "0" + day;
if (h.Length == 1) h = "0" + h;
if (m.Length == 1) m = "0" + m;
if (s.Length == 1) s = "0" + s;
if (mi.Length == 1) mi = "00" + mi;
if (mi.Length == 2) mi = "0" + mi;
if (dType == MyDateTimeType.DateTime)
{
Result = year + "-" + month + "-" + day + " " + h + ":" + m + ":" + s;
}
if (dType == MyDateTimeType.Date)
{
Result = year + "-" + month + "-" + day;
}
if (dType == MyDateTimeType.Time)
{
Result = h + ":" + m + ":" + s;
}
if (dType == MyDateTimeType.Batch)
{
Result = year.Substring(2, 2) + month + day;
}
if (dType == MyDateTimeType.BillNo)
{
Result = year + month + day + h + m + s + mi;
}
return Result;
}
/// <summary>
/// 20140116093900 将这种字符串转换为日期
/// </summary>
/// <param name="sDateTime"></param>
/// <param name="dType"></param>
/// <returns></returns>
public static string GetDateTimeStyle3(string sDateTime, MyDateTimeType dType)
{
if (sDateTime.Length != 12 && sDateTime.Length != 14) throw new Exception("[" + sDateTime + "]日期时间格式错误!");
string Result = "";
string year = sDateTime.Substring(0, 4);
string month = sDateTime.Substring(4, 2);
string day = sDateTime.Substring(6, 2);
string h = sDateTime.Substring(8, 2);
string m = sDateTime.Substring(10, 2);
string s = sDateTime.Length == 14 ? sDateTime.Substring(12, 2) : "0";
string mi = "0";
if (month.Length == 1) month = "0" + month;
if (day.Length == 1) day = "0" + day;
if (h.Length == 1) h = "0" + h;
if (m.Length == 1) m = "0" + m;
if (s.Length == 1) s = "0" + s;
if (mi.Length == 1) mi = "00" + mi;
if (mi.Length == 2) mi = "0" + mi;
if (dType == MyDateTimeType.DateTime)
{
Result = year + "-" + month + "-" + day + " " + h + ":" + m + ":" + s;
}
if (dType == MyDateTimeType.Date)
{
Result = year + "-" + month + "-" + day;
}
if (dType == MyDateTimeType.Time)
{
Result = h + ":" + m + ":" + s;
}
if (dType == MyDateTimeType.Batch)
{
Result = year.Substring(2, 2) + month + day;
}
if (dType == MyDateTimeType.BillNo)
{
Result = year + month + day + h + m + s + mi;
}
return Result;
}
/// <summary>
/// 20140808 将这种字符串转换为日期
/// </summary>
/// <param name="sDateTime"></param>
/// <param name="dType"></param>
/// <returns></returns>
public static string GetDateTimeStyle4(string sDateTime, MyDateTimeType dType)
{
if (sDateTime.Length != 8) throw new Exception("[" + sDateTime + "]日期时间格式错误!");
string Result = "";
string year = sDateTime.Substring(0, 4);
string month = sDateTime.Substring(4, 2);
string day = sDateTime.Substring(6, 2);
string h = "0";
string m = "0";
string s = "0";
string mi = "0";
if (month.Length == 1) month = "0" + month;
if (day.Length == 1) day = "0" + day;
if (h.Length == 1) h = "0" + h;
if (m.Length == 1) m = "0" + m;
if (s.Length == 1) s = "0" + s;
if (mi.Length == 1) mi = "00" + mi;
if (mi.Length == 2) mi = "0" + mi;
if (dType == MyDateTimeType.DateTime)
{
Result = year + "-" + month + "-" + day + " " + h + ":" + m + ":" + s;
}
if (dType == MyDateTimeType.Date)
{
Result = year + "-" + month + "-" + day;
}
if (dType == MyDateTimeType.Time)
{
Result = h + ":" + m + ":" + s;
}
if (dType == MyDateTimeType.Batch)
{
Result = year.Substring(2, 2) + month + day;
}
if (dType == MyDateTimeType.BillNo)
{
Result = year + month + day + h + m + s + mi;
}
return Result;
}
/// <summary>
/// 140808 将这种字符串转换为日期
/// </summary>
/// <param name="sDateTime"></param>
/// <param name="dType"></param>
/// <returns></returns>
public static string GetDateTimeStyle5(string sDateTime, MyDateTimeType dType)
{
if (sDateTime.Length != 6) throw new Exception("[" + sDateTime + "]日期时间格式错误!");
string Result = "";
string year = "20" + sDateTime.Substring(0, 2);
string month = sDateTime.Substring(2, 2);
string day = sDateTime.Substring(4, 2);
string h = "0";
string m = "0";
string s = "0";
string mi = "0";
if (month.Length == 1) month = "0" + month;
if (day.Length == 1) day = "0" + day;
if (h.Length == 1) h = "0" + h;
if (m.Length == 1) m = "0" + m;
if (s.Length == 1) s = "0" + s;
if (mi.Length == 1) mi = "00" + mi;
if (mi.Length == 2) mi = "0" + mi;
if (dType == MyDateTimeType.DateTime)
{
Result = year + "-" + month + "-" + day + " " + h + ":" + m + ":" + s;
}
if (dType == MyDateTimeType.Date)
{
Result = year + "-" + month + "-" + day;
}
if (dType == MyDateTimeType.Time)
{
Result = h + ":" + m + ":" + s;
}
if (dType == MyDateTimeType.Batch)
{
Result = year.Substring(2, 2) + month + day;
}
if (dType == MyDateTimeType.BillNo)
{
Result = year + month + day + h + m + s + mi;
}
return Result;
}
#endregion
}
}