schangxiang@126.com
2024-08-18 d4443cb0dfa7c7b6719fae621d7cc6d32a502dff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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();
        /// <summary>
        /// 解析工件号二维码
        /// </summary>
        /// <param name="log"></param>
        /// <returns></returns>
        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;
        }
        /// <summary>
        /// 解析pdf文件
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="getPdfValue"></param>
        /// <returns></returns>
        public static List<string> ReadPdfConntent(string filePath, string getPdfValue)
        {
            List<string> lst = new List<string>();
            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;
            }
        }
    }
}