Tool.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. using ProjectBase.Data.BaseDAL;
  2. using ProjectBase.Data.Logs;
  3. using ProjectBase.Data.Redis;
  4. using SIMDP.BLL;
  5. using SIMDP.Util;
  6. using System;
  7. using System.Collections;
  8. using System.Data;
  9. using System.Data.SqlClient;
  10. using System.IO;
  11. using System.Net;
  12. using System.Net.Sockets;
  13. using System.Text;
  14. using static SIMDP.Project.WNet;
  15. namespace SIMDP.Project
  16. {
  17. public static class ShareVar
  18. {
  19. // 上一次读取的压机文件名
  20. public static string LastFile1 = "LastFile1";
  21. }
  22. public class Tool
  23. {
  24. public static string filespath = System.Configuration.ConfigurationManager.AppSettings["files_path"];
  25. public static string exportpath = System.Configuration.ConfigurationManager.AppSettings["export_path"];
  26. public static int expireDay_excel = int.Parse(System.Configuration.ConfigurationManager.AppSettings["expireDay_excel"]);//30
  27. public static int expireDay_bmp = int.Parse(System.Configuration.ConfigurationManager.AppSettings["expireDay_bmp"]);//90
  28. public static string localpath = "X:";
  29. public static string remotepath1 = System.Configuration.ConfigurationManager.AppSettings["remote_path1"];
  30. public static string remotepath2 = System.Configuration.ConfigurationManager.AppSettings["remote_path2"];
  31. public static string testpath = System.Configuration.ConfigurationManager.AppSettings["test_path"];
  32. public static short ReadCsv(out string csv_bmp_path, out string barcode, out string qrcode)
  33. {
  34. csv_bmp_path = "";
  35. barcode = "";
  36. qrcode = "";
  37. //远程访问
  38. int status = NetworkConnection.Connect(testpath, localpath, "FVAdmin", "FVAdmin");
  39. if (status != 0)
  40. {
  41. LogHelper.log.Debug($"未能访问到共享文件路径:{testpath},错误码:{(ERROR_ID)status}");
  42. LogAndNotice("共享访问异常", 3, $"未能访问到共享文件路径:{testpath},错误码:{(ERROR_ID)status}");
  43. return 2;
  44. }
  45. NetworkConnection.Disconnect(localpath);
  46. string currentTime = (DateTime.Now).ToString("yyyyMMdd");
  47. string folderPath = filespath + "\\" + currentTime;
  48. string[] folders;
  49. if (Directory.Exists(folderPath))
  50. folders = Directory.GetFileSystemEntries(folderPath);
  51. else
  52. {
  53. string yesterdayTime = (DateTime.Now.AddDays(-1)).ToString("yyyyMMdd");
  54. folderPath = filespath + "\\" + yesterdayTime;
  55. if (Directory.Exists(folderPath))
  56. folders = Directory.GetFileSystemEntries(folderPath);
  57. else
  58. {
  59. LogHelper.log.Debug("ReadCsv:未找到当日或昨日文件夹");
  60. LogAndNotice("读取CSV文件异常", 3, "未找到当日或昨日文件夹");
  61. return 2;
  62. }
  63. }
  64. string folderBigger = "";
  65. foreach (var file in folders)
  66. {
  67. if (folderBigger.CompareTo(file) < 0)
  68. folderBigger = file;
  69. }
  70. RedisHelper redisHelper = new RedisHelper();
  71. string sfolderpath = redisHelper.GetString(ShareVar.LastFile1);
  72. if (sfolderpath == folderBigger)
  73. {
  74. LogHelper.log.Debug("ReadCsv:找到的文件夹与上一次相同");
  75. LogAndNotice("读取CSV文件异常", 3, "找到的文件夹与上一次相同");
  76. return 2;
  77. }
  78. //文件夹存在,判断CSV文件
  79. string[] csvFile = Directory.GetFiles(folderBigger, "*.csv");
  80. if (csvFile.Length != 1)
  81. {
  82. LogHelper.log.Debug("ReadCsv:文件夹内的CSV文件数不为1");
  83. LogAndNotice("读取CSV文件异常", 3, "文件夹内的CSV文件数不为1");
  84. return 2;
  85. }
  86. csv_bmp_path = folderBigger;//此处易出错
  87. bool bRes = AnalysisCsv(csvFile[0], out barcode, out qrcode);
  88. if (!bRes) return 2;
  89. return 1;
  90. }
  91. public static short ReadBmp(string csv_bmppath, int pictureNum)
  92. {
  93. string[] bmpFiles = Directory.GetFiles(csv_bmppath, "*.bmp");
  94. if (bmpFiles.Length == 0)
  95. {
  96. LogHelper.log.Debug("ReadBmp:文件夹内无BMP文件");
  97. LogAndNotice("读取照片异常", 3, "文件夹内无BMP文件");
  98. return 2;
  99. }
  100. if (bmpFiles.Length != pictureNum)
  101. {
  102. LogHelper.log.Debug($"ReadBmp:找到的BMP文件个数不为{pictureNum}");
  103. LogAndNotice("读取照片异常", 3, $"找到的BMP文件个数不为{pictureNum}");
  104. return 2;
  105. }
  106. RedisHelper redis = new RedisHelper();
  107. redis.SetString(ShareVar.LastFile1, csv_bmppath);
  108. return 1;
  109. }
  110. public static bool AnalysisCsv(string csv_bmppath, out string barcode, out string qrcode)
  111. {
  112. barcode = "";
  113. qrcode = "";
  114. using (StreamReader sr = new StreamReader(csv_bmppath))
  115. {
  116. string firstLine = sr.ReadLine();
  117. if (string.IsNullOrEmpty(firstLine))
  118. {
  119. LogHelper.log.Debug("AnalysisCsv:第一行无数据");
  120. LogAndNotice("解析CSV文件异常", 3, "第一行无数据");
  121. return false;
  122. }
  123. barcode = firstLine;
  124. string secondLine = sr.ReadLine();
  125. if (string.IsNullOrEmpty(secondLine))
  126. {
  127. LogHelper.log.Debug("AnalysisCsv:第二行无数据");
  128. LogAndNotice("解析CSV文件异常", 3, "第二行无数据");
  129. return false;
  130. }
  131. qrcode = secondLine;//带实际情况其修改
  132. return true;
  133. }
  134. }
  135. public static short ReadCsv2(out string csv_bmp_path, out string barcode, out string qrcode)
  136. {
  137. csv_bmp_path = "";
  138. barcode = "";
  139. qrcode = "";
  140. string currentTime = (DateTime.Now).ToString("yyyyMMdd");
  141. string folderPath = filespath + "\\" + currentTime;
  142. string[] folders;
  143. if (Directory.Exists(folderPath))
  144. folders = Directory.GetFileSystemEntries(folderPath);
  145. else
  146. {
  147. string yesterdayTime = (DateTime.Now.AddDays(-1)).ToString("yyyyMMdd");
  148. folderPath = filespath + "\\" + yesterdayTime;
  149. if (Directory.Exists(folderPath))
  150. folders = Directory.GetFileSystemEntries(folderPath);
  151. else
  152. {
  153. LogHelper.log.Debug("ReadCsv:未找到当日或昨日文件夹");
  154. LogAndNotice("读取CSV文件异常", 3, "未找到当日或昨日文件夹");
  155. return 2;
  156. }
  157. }
  158. string folderBigger = "";
  159. foreach (var file in folders)
  160. {
  161. if (folderBigger.CompareTo(file) < 0)
  162. folderBigger = file;
  163. }
  164. RedisHelper redisHelper = new RedisHelper();
  165. string sfolderpath = redisHelper.GetString(ShareVar.LastFile1);
  166. if (sfolderpath == folderBigger)
  167. {
  168. LogHelper.log.Debug("ReadCsv:找到的文件夹与上一次相同");
  169. LogAndNotice("读取CSV文件异常", 3, "找到的文件夹与上一次相同");
  170. return 2;
  171. }
  172. //文件夹存在,判断CSV文件
  173. string[] csvFile = Directory.GetFiles(folderBigger, "*.csv");
  174. if (csvFile.Length != 1)
  175. {
  176. LogHelper.log.Debug("ReadCsv:文件夹内的CSV文件数不为1");
  177. LogAndNotice("读取CSV文件异常", 3, "文件夹内的CSV文件数不为1");
  178. return 2;
  179. }
  180. csv_bmp_path = folderBigger;//此处易出错
  181. bool bRes = AnalysisCsv(csvFile[0], out barcode, out qrcode);
  182. if (!bRes) return 2;
  183. return 1;
  184. }
  185. public static short ReadBmp2(string csv_bmppath, int pictureNum)
  186. {
  187. string[] bmpFiles = Directory.GetFiles(csv_bmppath, "*.bmp");
  188. if (bmpFiles.Length == 0)
  189. {
  190. LogHelper.log.Debug("ReadBmp:文件夹内无BMP文件");
  191. LogAndNotice("读取照片异常", 3, "文件夹内无BMP文件");
  192. return 2;
  193. }
  194. if (bmpFiles.Length != pictureNum)
  195. {
  196. LogHelper.log.Debug($"ReadBmp:找到的BMP文件个数不为{pictureNum}");
  197. LogAndNotice("读取照片异常", 3, $"找到的BMP文件个数不为{pictureNum}");
  198. return 2;
  199. }
  200. RedisHelper redis = new RedisHelper();
  201. redis.SetString(ShareVar.LastFile1, csv_bmppath);
  202. return 1;
  203. }
  204. public static bool AnalysisCsv2(string csv_bmppath, out string barcode, out string qrcode)
  205. {
  206. barcode = "";
  207. qrcode = "";
  208. using (StreamReader sr = new StreamReader(csv_bmppath))
  209. {
  210. string firstLine = sr.ReadLine();
  211. if (string.IsNullOrEmpty(firstLine))
  212. {
  213. LogHelper.log.Debug("AnalysisCsv:第一行无数据");
  214. LogAndNotice("解析CSV文件异常", 3, "第一行无数据");
  215. return false;
  216. }
  217. barcode = firstLine;
  218. string secondLine = sr.ReadLine();
  219. if (string.IsNullOrEmpty(secondLine))
  220. {
  221. LogHelper.log.Debug("AnalysisCsv:第二行无数据");
  222. LogAndNotice("解析CSV文件异常", 3, "第二行无数据");
  223. return false;
  224. }
  225. qrcode = secondLine;//带实际情况其修改
  226. return true;
  227. }
  228. }
  229. public static void DeleteTask()
  230. {
  231. int deleteFiles = 0;
  232. int deleteFolders = 0;
  233. if (Directory.Exists(exportpath))
  234. {
  235. string[] files = Directory.GetFiles(exportpath);
  236. string cutoffDate = DateTime.Now.AddDays(-expireDay_excel).ToString("yyyyMMdd");
  237. string cutPath = exportpath + "\\" + cutoffDate + ".xlsx";
  238. foreach (var file in files)
  239. {
  240. if (file.CompareTo(cutPath) < 0)
  241. {
  242. try
  243. {
  244. File.Delete(file);
  245. deleteFiles++;
  246. }
  247. catch (Exception ex)
  248. {
  249. LogHelper.log.Debug($"DeleteTask:执行定期删除任务出错--" + ex.Message);
  250. }
  251. }
  252. }
  253. }
  254. if (Directory.Exists(filespath))
  255. {
  256. string[] files = Directory.GetFileSystemEntries(filespath);
  257. string cutoffDate = DateTime.Now.AddDays(-expireDay_bmp).ToString("yyyyMMdd");
  258. string cutPath = filespath + "\\" + cutoffDate;
  259. foreach (var file in files)
  260. {
  261. if (file.CompareTo(cutPath) < 0)
  262. {
  263. try
  264. {
  265. Directory.Delete(file, true);
  266. deleteFolders++;
  267. }
  268. catch (Exception ex)
  269. {
  270. LogHelper.log.Debug($"DeleteTask:执行定期删除任务出错--" + ex.Message);
  271. }
  272. }
  273. }
  274. }
  275. LogHelper.log.Debug($"DeleteTask:定期删除任务已执行,已删除文件: {deleteFiles}个,已删除文件夹: {deleteFolders}个");
  276. }
  277. public static void LogAndNotice(string title, int type, string content)
  278. {
  279. if (type == 3)
  280. LogHelper.log.Error($"{title}:{content}");
  281. else if (type == 2)
  282. LogHelper.log.Warn($"{title}:{content}");
  283. else if (type == 1)
  284. LogHelper.log.Info($"{title}:{content}");
  285. ProjectBase.Data.BaseDAL.BLLFactory<BLL.BlSystemNotice>.Instance.PublishSysMessage(title, type, content);
  286. }
  287. public static void TestNetWorkConnection()
  288. {
  289. string localpath = "X:";
  290. int status = NetworkConnection.Connect("\\\\192.168.43.51\\Log", localpath, "test", "test");
  291. if (status == (int)ERROR_ID.ERROR_SUCCESS)
  292. {
  293. FileStream fs = new FileStream(localpath + @"\123.txt", FileMode.OpenOrCreate);
  294. using (StreamWriter stream = new StreamWriter(fs))
  295. {
  296. stream.WriteLine("你好呀,成功了");
  297. stream.Flush();
  298. stream.Close();
  299. }
  300. fs.Close();
  301. }
  302. else
  303. {
  304. Console.WriteLine(status);
  305. }
  306. NetworkConnection.Disconnect(localpath);
  307. }
  308. }
  309. }