using iTextSharp.text.pdf;
using iWare_SCADA_BusinessLogical.Utils;
using iWare_SCADA_Model;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static log4net.Appender.RollingFileAppender;
namespace iWare_SCADA_BusinessLogical.BLL
{
public class CommonManager
{
public static readonly CommonManager Instance = new CommonManager();
///
/// 解析工件号二维码
///
///
///
public WorkPieceLog GetWorkPieceID(WorkPieceLog log,LogType type)
{
try
{
if(log==null|| log.WorkPieceID.Trim().Length!=22)
{
Log4NetHelper.WriteErrorLog(type, $"解析工件号{CheckNULLForString(log.WorkPieceID)} {log.Id}二维码时出错,二维码格式不对");
return log;
}
log.WorkPieceIDTo1 = log.WorkPieceID.Substring(0, 2);
log.WorkPieceIDTo2 = log.WorkPieceID.Substring(2, 4);
log.WorkPieceIDTo3 = log.WorkPieceID.Substring(6, 6);
log.WorkPieceIDTo4 = log.WorkPieceID.Substring(12, 2);
log.WorkPieceIDTo5 = log.WorkPieceID.Substring(14, 4);
log.WorkPieceIDTo6 = log.WorkPieceID.Substring(18, 2);
log.WorkPieceIDTo7 = log.WorkPieceID.Substring(20, 2);
}
catch(Exception ex)
{
Log4NetHelper.WriteErrorLog(type, $"解析工件号{CheckNULLForString(log.WorkPieceID)} {log.Id}二维码时异常:", ex);
}
return log;
}
public string CheckNULLForString(string str)
{
return string.IsNullOrEmpty(str) ? "" : str;
}
public string CheackPath(string path, int type, int time)
{
var directory = Path.GetDirectoryName(path);
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
if (!File.Exists(path))
{
File.Create(path).Close();
}
string content = "";
// 同步来源数据
using (StreamReader reader = new StreamReader(path))
{
content = reader.ReadToEnd();
Console.WriteLine("读取的内容是:" + content);
if (content == null || content == "")
{
switch (type)
{
case 1: content = DateTimeHelper.GetDateTime().AddYears(time).ToString(); break;
case 2: content = DateTimeHelper.GetDateTime().AddMonths(time).ToString(); break;
case 3: content = DateTimeHelper.GetDateTime().AddDays(time).ToString(); break;
case 4: content = DateTimeHelper.GetDateTime().AddHours(time).ToString(); break;
case 5: content = DateTimeHelper.GetDateTime().AddMinutes(time).ToString(); break;
case 6: content = DateTimeHelper.GetDateTime().AddSeconds(time).ToString(); break;
default:
content = DateTimeHelper.GetDateTime().ToString(); break;
}
//content = DateTimeHelper.GetDateTime().AddMinutes(-10).ToString();
}
}
return content;
}
///
/// 解析pdf文件
///
///
///
///
public static List ReadPdfConntent(string filePath, string getPdfValue)
{
List lst = new List();
try
{
string pdffilename = filePath;
PdfReader pdfreader = new PdfReader(pdffilename);
int numberOfPages = pdfreader.NumberOfPages;
//for (int i = 1; i <= numberOfPages; ++i)
//{
// lst.Add(iTextSharp.text.pdf.parser.PdfTextExtractor.GetTextFromPage(pdfreader,i));
// text.AppendLine();
//}
string text = iTextSharp.text.pdf.parser.PdfTextExtractor.GetTextFromPage(pdfreader, 1);
string[] words = text.Split('\n');
foreach (var item in words)
{
string value = Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(item));
if (value.Contains(getPdfValue))
{
string[] splitValue = value.Split(':');
lst.Add(splitValue[1].Trim());
}
}
pdfreader.Close();
return lst;
}
catch (Exception ex)
{
throw ex;
}
}
public static DateTime? GetOP60Time(string datetime)
{
try
{
string regex = @"^(\d{1,2})\.(\d{1,2})\.(\d{4})\/(\d{1,2}):(\d{1,2}):(\d{1,2})";
var macth = RegexExtension.Match(datetime, regex, 20000);
if (macth.Success)
{
string date = $"{macth.Groups[3].Value}/{macth.Groups[2].Value}/{macth.Groups[1].Value} {macth.Groups[4].Value}:{macth.Groups[5].Value}:{macth.Groups[6].Value}";
DateTime Time;
if (DateTime.TryParseExact(date, "yyyy/MM/dd HH:mm:ss", CultureInfo.CurrentCulture, DateTimeStyles.None, out Time))
{
return Time;
}
}
return null;
}
catch (Exception ex)
{
return null;
}
}
}
}