123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362 |
- using ProjectBase.Data.BaseDAL;
- using ProjectBase.Data.Logs;
- using ProjectBase.Data.Redis;
- using SIMDP.BLL;
- using SIMDP.Util;
- using System;
- using System.Collections;
- using System.Data;
- using System.Data.SqlClient;
- using System.IO;
- using System.Net;
- using System.Net.Sockets;
- using System.Text;
- using static SIMDP.Project.WNet;
- namespace SIMDP.Project
- {
- public static class ShareVar
- {
- // 上一次读取的压机文件名
- public static string LastFile1 = "LastFile1";
- }
- public class Tool
- {
- public static string filespath = System.Configuration.ConfigurationManager.AppSettings["files_path"];
- public static string exportpath = System.Configuration.ConfigurationManager.AppSettings["export_path"];
- public static int expireDay_excel = int.Parse(System.Configuration.ConfigurationManager.AppSettings["expireDay_excel"]);//30
- public static int expireDay_bmp = int.Parse(System.Configuration.ConfigurationManager.AppSettings["expireDay_bmp"]);//90
- public static string localpath = "X:";
- public static string remotepath1 = System.Configuration.ConfigurationManager.AppSettings["remote_path1"];
- public static string remotepath2 = System.Configuration.ConfigurationManager.AppSettings["remote_path2"];
- public static string testpath = System.Configuration.ConfigurationManager.AppSettings["test_path"];
- public static short ReadCsv(out string csv_bmp_path, out string barcode, out string qrcode)
- {
- csv_bmp_path = "";
- barcode = "";
- qrcode = "";
- //远程访问
- int status = NetworkConnection.Connect(testpath, localpath, "FVAdmin", "FVAdmin");
- if (status != 0)
- {
- LogHelper.log.Debug($"未能访问到共享文件路径:{testpath},错误码:{(ERROR_ID)status}");
- LogAndNotice("共享访问异常", 3, $"未能访问到共享文件路径:{testpath},错误码:{(ERROR_ID)status}");
- return 2;
- }
- NetworkConnection.Disconnect(localpath);
- string currentTime = (DateTime.Now).ToString("yyyyMMdd");
- string folderPath = filespath + "\\" + currentTime;
- string[] folders;
- if (Directory.Exists(folderPath))
- folders = Directory.GetFileSystemEntries(folderPath);
- else
- {
- string yesterdayTime = (DateTime.Now.AddDays(-1)).ToString("yyyyMMdd");
- folderPath = filespath + "\\" + yesterdayTime;
- if (Directory.Exists(folderPath))
- folders = Directory.GetFileSystemEntries(folderPath);
- else
- {
- LogHelper.log.Debug("ReadCsv:未找到当日或昨日文件夹");
- LogAndNotice("读取CSV文件异常", 3, "未找到当日或昨日文件夹");
- return 2;
- }
- }
- string folderBigger = "";
- foreach (var file in folders)
- {
- if (folderBigger.CompareTo(file) < 0)
- folderBigger = file;
- }
- RedisHelper redisHelper = new RedisHelper();
- string sfolderpath = redisHelper.GetString(ShareVar.LastFile1);
- if (sfolderpath == folderBigger)
- {
- LogHelper.log.Debug("ReadCsv:找到的文件夹与上一次相同");
- LogAndNotice("读取CSV文件异常", 3, "找到的文件夹与上一次相同");
- return 2;
- }
- //文件夹存在,判断CSV文件
- string[] csvFile = Directory.GetFiles(folderBigger, "*.csv");
- if (csvFile.Length != 1)
- {
- LogHelper.log.Debug("ReadCsv:文件夹内的CSV文件数不为1");
- LogAndNotice("读取CSV文件异常", 3, "文件夹内的CSV文件数不为1");
- return 2;
- }
- csv_bmp_path = folderBigger;//此处易出错
- bool bRes = AnalysisCsv(csvFile[0], out barcode, out qrcode);
- if (!bRes) return 2;
- return 1;
- }
- public static short ReadBmp(string csv_bmppath, int pictureNum)
- {
- string[] bmpFiles = Directory.GetFiles(csv_bmppath, "*.bmp");
- if (bmpFiles.Length == 0)
- {
- LogHelper.log.Debug("ReadBmp:文件夹内无BMP文件");
- LogAndNotice("读取照片异常", 3, "文件夹内无BMP文件");
- return 2;
- }
- if (bmpFiles.Length != pictureNum)
- {
- LogHelper.log.Debug($"ReadBmp:找到的BMP文件个数不为{pictureNum}");
- LogAndNotice("读取照片异常", 3, $"找到的BMP文件个数不为{pictureNum}");
- return 2;
- }
- RedisHelper redis = new RedisHelper();
- redis.SetString(ShareVar.LastFile1, csv_bmppath);
- return 1;
- }
- public static bool AnalysisCsv(string csv_bmppath, out string barcode, out string qrcode)
- {
- barcode = "";
- qrcode = "";
- using (StreamReader sr = new StreamReader(csv_bmppath))
- {
- string firstLine = sr.ReadLine();
- if (string.IsNullOrEmpty(firstLine))
- {
- LogHelper.log.Debug("AnalysisCsv:第一行无数据");
- LogAndNotice("解析CSV文件异常", 3, "第一行无数据");
- return false;
- }
- barcode = firstLine;
- string secondLine = sr.ReadLine();
- if (string.IsNullOrEmpty(secondLine))
- {
- LogHelper.log.Debug("AnalysisCsv:第二行无数据");
- LogAndNotice("解析CSV文件异常", 3, "第二行无数据");
- return false;
- }
- qrcode = secondLine;//带实际情况其修改
- return true;
- }
- }
- public static short ReadCsv2(out string csv_bmp_path, out string barcode, out string qrcode)
- {
- csv_bmp_path = "";
- barcode = "";
- qrcode = "";
- string currentTime = (DateTime.Now).ToString("yyyyMMdd");
- string folderPath = filespath + "\\" + currentTime;
- string[] folders;
- if (Directory.Exists(folderPath))
- folders = Directory.GetFileSystemEntries(folderPath);
- else
- {
- string yesterdayTime = (DateTime.Now.AddDays(-1)).ToString("yyyyMMdd");
- folderPath = filespath + "\\" + yesterdayTime;
- if (Directory.Exists(folderPath))
- folders = Directory.GetFileSystemEntries(folderPath);
- else
- {
- LogHelper.log.Debug("ReadCsv:未找到当日或昨日文件夹");
- LogAndNotice("读取CSV文件异常", 3, "未找到当日或昨日文件夹");
- return 2;
- }
- }
- string folderBigger = "";
- foreach (var file in folders)
- {
- if (folderBigger.CompareTo(file) < 0)
- folderBigger = file;
- }
- RedisHelper redisHelper = new RedisHelper();
- string sfolderpath = redisHelper.GetString(ShareVar.LastFile1);
- if (sfolderpath == folderBigger)
- {
- LogHelper.log.Debug("ReadCsv:找到的文件夹与上一次相同");
- LogAndNotice("读取CSV文件异常", 3, "找到的文件夹与上一次相同");
- return 2;
- }
- //文件夹存在,判断CSV文件
- string[] csvFile = Directory.GetFiles(folderBigger, "*.csv");
- if (csvFile.Length != 1)
- {
- LogHelper.log.Debug("ReadCsv:文件夹内的CSV文件数不为1");
- LogAndNotice("读取CSV文件异常", 3, "文件夹内的CSV文件数不为1");
- return 2;
- }
- csv_bmp_path = folderBigger;//此处易出错
- bool bRes = AnalysisCsv(csvFile[0], out barcode, out qrcode);
- if (!bRes) return 2;
- return 1;
- }
- public static short ReadBmp2(string csv_bmppath, int pictureNum)
- {
- string[] bmpFiles = Directory.GetFiles(csv_bmppath, "*.bmp");
- if (bmpFiles.Length == 0)
- {
- LogHelper.log.Debug("ReadBmp:文件夹内无BMP文件");
- LogAndNotice("读取照片异常", 3, "文件夹内无BMP文件");
- return 2;
- }
- if (bmpFiles.Length != pictureNum)
- {
- LogHelper.log.Debug($"ReadBmp:找到的BMP文件个数不为{pictureNum}");
- LogAndNotice("读取照片异常", 3, $"找到的BMP文件个数不为{pictureNum}");
- return 2;
- }
- RedisHelper redis = new RedisHelper();
- redis.SetString(ShareVar.LastFile1, csv_bmppath);
- return 1;
- }
- public static bool AnalysisCsv2(string csv_bmppath, out string barcode, out string qrcode)
- {
- barcode = "";
- qrcode = "";
- using (StreamReader sr = new StreamReader(csv_bmppath))
- {
- string firstLine = sr.ReadLine();
- if (string.IsNullOrEmpty(firstLine))
- {
- LogHelper.log.Debug("AnalysisCsv:第一行无数据");
- LogAndNotice("解析CSV文件异常", 3, "第一行无数据");
- return false;
- }
- barcode = firstLine;
- string secondLine = sr.ReadLine();
- if (string.IsNullOrEmpty(secondLine))
- {
- LogHelper.log.Debug("AnalysisCsv:第二行无数据");
- LogAndNotice("解析CSV文件异常", 3, "第二行无数据");
- return false;
- }
- qrcode = secondLine;//带实际情况其修改
- return true;
- }
- }
- public static void DeleteTask()
- {
- int deleteFiles = 0;
- int deleteFolders = 0;
- if (Directory.Exists(exportpath))
- {
- string[] files = Directory.GetFiles(exportpath);
- string cutoffDate = DateTime.Now.AddDays(-expireDay_excel).ToString("yyyyMMdd");
- string cutPath = exportpath + "\\" + cutoffDate + ".xlsx";
- foreach (var file in files)
- {
- if (file.CompareTo(cutPath) < 0)
- {
- try
- {
- File.Delete(file);
- deleteFiles++;
- }
- catch (Exception ex)
- {
- LogHelper.log.Debug($"DeleteTask:执行定期删除任务出错--" + ex.Message);
- }
- }
- }
- }
- if (Directory.Exists(filespath))
- {
- string[] files = Directory.GetFileSystemEntries(filespath);
- string cutoffDate = DateTime.Now.AddDays(-expireDay_bmp).ToString("yyyyMMdd");
- string cutPath = filespath + "\\" + cutoffDate;
- foreach (var file in files)
- {
- if (file.CompareTo(cutPath) < 0)
- {
- try
- {
- Directory.Delete(file, true);
- deleteFolders++;
- }
- catch (Exception ex)
- {
- LogHelper.log.Debug($"DeleteTask:执行定期删除任务出错--" + ex.Message);
- }
- }
- }
- }
- LogHelper.log.Debug($"DeleteTask:定期删除任务已执行,已删除文件: {deleteFiles}个,已删除文件夹: {deleteFolders}个");
- }
- public static void LogAndNotice(string title, int type, string content)
- {
- if (type == 3)
- LogHelper.log.Error($"{title}:{content}");
- else if (type == 2)
- LogHelper.log.Warn($"{title}:{content}");
- else if (type == 1)
- LogHelper.log.Info($"{title}:{content}");
- ProjectBase.Data.BaseDAL.BLLFactory<BLL.BlSystemNotice>.Instance.PublishSysMessage(title, type, content);
- }
- public static void TestNetWorkConnection()
- {
- string localpath = "X:";
- int status = NetworkConnection.Connect("\\\\192.168.43.51\\Log", localpath, "test", "test");
- if (status == (int)ERROR_ID.ERROR_SUCCESS)
- {
- FileStream fs = new FileStream(localpath + @"\123.txt", FileMode.OpenOrCreate);
- using (StreamWriter stream = new StreamWriter(fs))
- {
- stream.WriteLine("你好呀,成功了");
- stream.Flush();
- stream.Close();
- }
- fs.Close();
- }
- else
- {
- Console.WriteLine(status);
- }
- NetworkConnection.Disconnect(localpath);
- }
- }
- }
|