|
|
using Newtonsoft.Json;using NFine.Code;using NFine.Data.Extensions;using NFine.Domain._03_Entity.SRM;using NFine.Repository;using System;using System.Collections.Generic;using System.Data;using System.Linq;using System.Text;using System.Net;using System.IO;using System.Reflection;using System.Data.SqlClient;using UpdateServiceLibrary.Helper;using System.Net.NetworkInformation;using System.Net.Sockets;using System.Web;
namespace NFine.Application.WMS{ public class SystemUpdateApp : RepositoryFactory<ICSVendor> { //public string DownloadFileFolder = @"C:\path\folder";
//public string UpdateFileFolder = @"C:\path\folder2";
public static DataTable Invmes = new DataTable();
PickMaterialApp App = new PickMaterialApp(); DownLoadFileHelper downLoadFileHelper = new DownLoadFileHelper(); FileHelper helper = new FileHelper();
#region 获取更新文件信息
/// <summary>
///获取更新文件信息
/// </summary>
/// <returns></returns>
public string GetFileList() { string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location.TrimEnd(','); string result = String.Empty; try { result = JsonConvert.SerializeObject(GetUpdateFilesDetail().FaBuList); } catch (Exception ex) { result = ex.Message;
} return result; } #endregion
#region 获取详情信息
/// <summary>
/// 获取详情信息
/// </summary>
/// <returns></returns>
public string GetFilesDetail(string faBuNum) { string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location.TrimEnd(','); string result = String.Empty; try { result = JsonConvert.SerializeObject(GetUpdateFilesDetail(faBuNum).FaBuList[0].Files); } catch (Exception ex) { result = ex.Message;
} return result; } #endregion
#region 获取文件信息
/// <summary>
/// 获取文件信息
/// </summary>
/// <returns></returns>
public JsonData GetUpdateFilesDetail(string faBuNum="") { JsonData result; try { string sql = @"SELECT F_FullName,F_Description FROM [dbo].[Sys_SRM_Items] WHERE F_EnCode = 'SystemUpdate'"; var dt = SqlHelper.CmdExecuteDataTable(sql); if (dt.Rows.Count <= 0) { throw new Exception("请先在自定义档案维护客户信息"); } TypeModel model = new TypeModel { CustomerName = dt.Rows[0]["F_FullName"].ToString(), XTType = "WMS", FaBuNum = faBuNum
}; string Inputstr = JsonConvert.SerializeObject(model);
string APIURL = dt.Rows[0]["F_Description"].ToString()+ "APIGetFaBuFile"; string jsonString = HttpPost(APIURL, Inputstr); result = JsonConvert.DeserializeObject<JsonData>(jsonString); } catch (Exception ex) { throw new Exception(ex.Message);
} return result; } #endregion
#region 接口api解析
/// <summary>
/// 接口api解析
/// </summary>
/// <param name="url">地址</param>
/// <param name="body">参数</param>
/// <returns></returns>
public static string HttpPost(string url, string body)
{ try { Encoding encoding = Encoding.UTF8; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.Accept = "application/json, text/javascript, */*"; //"text/html, application/xhtml+xml, */*";
request.ContentType = "application/json; charset=utf-8";
byte[] buffer = encoding.GetBytes(body); request.ContentLength = buffer.Length; request.GetRequestStream().Write(buffer, 0, buffer.Length); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding)) { return reader.ReadToEnd(); } } catch (WebException ex) { throw new Exception(ex.Message); } } #endregion
#region 调用更新接口
/// <summary>
/// 调用更新接口
/// </summary>
/// <returns></returns>
public ResultModel ConfirmDown(string faBuNum) { ResultModel result; try { string userName = NFine.Code.OperatorProvider.Provider.GetCurrent().UserName; string sql = @"SELECT F_FullName,F_Description FROM [dbo].[Sys_SRM_Items] WHERE F_EnCode = 'SystemUpdate'"; string ip1=string.Empty; var dt = SqlHelper.CmdExecuteDataTable(sql); if (dt.Rows.Count<=0) { throw new Exception("请先在自定义档案维护客户信息"); } // 获取所有的网络接口
NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
// 遍历所有网络接口,寻找当前活动的接口(即已连接的网络接口)
foreach (NetworkInterface networkInterface in networkInterfaces) { // 如果网络接口是活动的(即已连接)
if (networkInterface.OperationalStatus == OperationalStatus.Up) { // 获取该接口的所有IP地址信息
UnicastIPAddressInformationCollection ipAddressInfo = networkInterface.GetIPProperties().UnicastAddresses;
// 遍历IP地址信息,寻找IPv4地址
foreach (UnicastIPAddressInformation ip in ipAddressInfo) { if (ip.Address.AddressFamily == AddressFamily.InterNetwork) { // 输出IPv4地址
ip1= ip.Address.ToString();
// 如果只需要第一个IPv4地址,可以在找到后退出循环
break; } } } } ConfirmDownModel model = new ConfirmDownModel { CustomerName = dt.Rows[0]["F_FullName"].ToString(), PersonName = userName, FaBuNum = faBuNum, IP1= ip1
}; string Inputstr = JsonConvert.SerializeObject(model);
string APIURL = dt.Rows[0]["F_Description"].ToString()+ "APIConfirmDownFaBuFile"; string jsonString = HttpPost(APIURL, Inputstr); result = JsonConvert.DeserializeObject<ResultModel>(jsonString); } catch (Exception ex) { throw new Exception(ex.Message);
} return result; }
#endregion
/// <summary>
/// 更新文件
/// </summary>
/// <param name="savePath"></param>
/// <param name="Year"></param>
/// <returns></returns>
public string UpdateFiles(String keyValue) { string msg = string.Empty; //数据获取
try { string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location; string MUSER = NFine.Code.OperatorProvider.Provider.GetCurrent().UserCode; string MUSERNAME = NFine.Code.OperatorProvider.Provider.GetCurrent().UserName; SqlConnection conn = SqlHelper.GetDataCenterConn(); //获取文件版本号
List<string> filesArr = keyValue.Split(',').Where(o=>!o.IsEmpty()).ToList();
string publishPath = System.AppDomain.CurrentDomain.BaseDirectory; string name = new DirectoryInfo(publishPath).Name + "-WMSBSBackup"; string DownloadFileFolder = Path.Combine(new DirectoryInfo(publishPath).Parent.FullName, name); if (!Directory.Exists(DownloadFileFolder)) Directory.CreateDirectory(DownloadFileFolder); // 获取文件夹的DirectoryInfo对象
DirectoryInfo directoryInfo = new DirectoryInfo(DownloadFileFolder);
string folderPrefix = DateTime.Now.ToString("yyyyMMdd"); // 你想要匹配的文件夹前缀
//string newFolderName=string.Empty; // 新文件夹的名称
string newFolderPath = string.Empty;
// 获取所有以指定前缀开头的文件夹
var directories = Directory.GetDirectories(DownloadFileFolder, folderPrefix + "*") .Select(dir => new DirectoryInfo(dir)) .ToList();
// 如果没有找到任何文件夹,则直接创建新文件夹
if (!directories.Any()) { newFolderPath = Path.Combine(DownloadFileFolder, folderPrefix + "01"); Directory.CreateDirectory(newFolderPath); } else { // 解析文件夹名称以获取流水号,并找出最大的流水号
int maxSerialNumber = directories .Select(dir => int.Parse(dir.Name.Substring(folderPrefix.Length))) .Max();
// 流水号加1,并创建新文件夹
int newSerialNumber = maxSerialNumber + 1; newFolderPath = Path.Combine(DownloadFileFolder, $"{folderPrefix}{newSerialNumber:D2}"); // D2表示格式化为两位数
}
Exception exception = null; Directory.CreateDirectory(newFolderPath); filesArr.ForEach(faBuNum => { var files = GetUpdateFilesDetail(faBuNum).FaBuList[0].Files; //List<Files> files = new List<Files>();
//Files f1 = new Files {
// FilePath = "http://119.3.29.177:8013/ReportFile/ProjectFaBuFile/发布文件202403060331541test.sql"
//};
//Files f2 = new Files
//{
// FilePath = "http://119.3.29.177:8013/ReportFile/ProjectFaBuFile/发布文件202403060329051系统更新压缩补丁包.zip"
//};
//files.Add(f1);
//files.Add(f2);
var downloadFolder = Path.Combine(newFolderPath, faBuNum); if (!Directory.Exists(downloadFolder)) Directory.CreateDirectory(downloadFolder); var UpdateFileFolder = Path.Combine(downloadFolder, "UnZip"); if (!Directory.Exists(UpdateFileFolder)) Directory.CreateDirectory(UpdateFileFolder); //备份需要替换的文件
var backUpfolder = Path.Combine(newFolderPath, "Backup"); if (!Directory.Exists(backUpfolder)) Directory.CreateDirectory(backUpfolder); files.ForEach(info => { var downloadFilePath = Path.Combine(downloadFolder, Path.GetFileName(info.FilePath)); //下载文件
var downLoad = downLoadFileHelper.DownloadFile(info.FilePath, downloadFilePath); if (!downLoad.IsSuccess) { throw new Exception(downLoad.Data.Message); } //获取文件的名称
string fileName = Path.GetFileNameWithoutExtension(downloadFilePath); //获取后缀名
string fileNameExtension = Path.GetExtension(downloadFilePath); //判断文件名称,进行解压缩操作
if (fileName.EndsWith("系统更新压缩补丁包")) { //if (Directory.Exists(UpdateFileFolder))
// Directory.Delete(UpdateFileFolder, true);
var unzip = ZipHelper.Unzip(new FileInfo(downloadFilePath), new DirectoryInfo(UpdateFileFolder)); if (!unzip.IsSuccess) { throw new Exception(unzip.Message); } //备份需要替换的文件
//var backUpfolder = Path.Combine(new DirectoryInfo(publishPath).Parent.FullName, "Backup");
//if (!Directory.Exists(backUpfolder))
// Directory.CreateDirectory(backUpfolder);
var backupPath = Path.Combine(backUpfolder, DateTime.Now.ToString("yyyyMMddHHmmss")+".zip"); ZipHelper.ZipFile(new DirectoryInfo(publishPath), new FileInfo(backupPath)); //移动替换文件
FileController.MoveFolder(UpdateFileFolder, publishPath); Directory.Delete(UpdateFileFolder, true);
} else if (fileNameExtension == ".sql") { try { //读取文件中的内容,并执行sql语句
string sqlText = File.ReadAllText(downloadFilePath); //执行sql语句
SqlHelper.CmdExecuteNonQueryLi(sqlText); } catch(Exception ex) { msg = fileName+ "脚本执行失败,请手动操作\n" + ex; exception = ex; }
} }); //替换成功,调接口
var returnModel = ConfirmDown(faBuNum);
});
if (exception != null) throw exception;
return msg;
} catch (Exception ex) { throw new Exception(msg+ex.Message);
}
} #region 更新文件
/// <summary>
/// 更新文件
/// </summary>
/// <param name="uploadedFileName">上传的文件名</param>
/// <param name="fileStream">上传的文件流</param>
/// <param name="savePath">上传文件目标文件夹</param>
/// <returns></returns>
public string UpdateFiles(string uploadedFileName, Stream fileStream,string savePath) { string msg = string.Empty; //数据获取
try { SqlConnection conn = SqlHelper.GetDataCenterConn(); string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location; string MUSER = NFine.Code.OperatorProvider.Provider.GetCurrent().UserCode; string MUSERNAME = NFine.Code.OperatorProvider.Provider.GetCurrent().UserName;
#region 校验文件名规则
// 压缩包命名规则 发布客户~系统类型~发布单据号
int index = uploadedFileName.LastIndexOf('.'); var informationArr = Path.GetFileName(uploadedFileName.Substring(0, index)).Split('~'); if (informationArr.Length <= 0) { msg = "文件命名规则必须是:发布客户~系统类型~发布单据号,请确认"; return msg; } string sql = @"SELECT F_FullName,F_Description FROM [dbo].[Sys_SRM_Items] WHERE F_EnCode = 'SystemUpdate'"; var dt = SqlHelper.CmdExecuteDataTable(sql); if (dt.Rows.Count <= 0) { throw new Exception("请先在自定义档案维护客户信息"); } if (dt.Rows[0]["F_FullName"].ToString() != informationArr[0]) { msg = $"更新文件客户名称:{informationArr[0]}和系统客户名称:{dt.Rows[0]["F_FullName"]}不一致"; return msg; } if (informationArr[1] != "WMS") { msg = $"请确保更新文件系统为WMS"; return msg; } #endregion
var uploadfile = Path.Combine(savePath, uploadedFileName); #region 保存文件
if (!Directory.Exists(savePath)) Directory.CreateDirectory(savePath);
var buff = new byte[1024 * 1024]; int len = 0; using (var nf = new FileStream(uploadfile, FileMode.OpenOrCreate)) { while ((len = fileStream.Read(buff, 0, buff.Length)) > 0) { nf.Write(buff, 0, len); nf.Flush(); } } #endregion
#region 生成文件解压路径
//获取文件版本号
string faBuNum = informationArr[2]; string publishPath = System.AppDomain.CurrentDomain.BaseDirectory; string name = new DirectoryInfo(publishPath).Name + "-WMSBSBackup"; string DownloadFileFolder = Path.Combine(new DirectoryInfo(publishPath).Parent.FullName, name); if (!Directory.Exists(DownloadFileFolder)) Directory.CreateDirectory(DownloadFileFolder); // 获取文件夹的DirectoryInfo对象
DirectoryInfo directoryInfo = new DirectoryInfo(DownloadFileFolder);
string folderPrefix = DateTime.Now.ToString("yyyyMMdd"); string newFolderPath = string.Empty;
// 获取所有以指定前缀开头的文件夹
var directories = Directory.GetDirectories(DownloadFileFolder, folderPrefix + "*"); // 如果没有找到任何文件夹,则直接创建新文件夹
if (!directories.Any()) { newFolderPath = Path.Combine(DownloadFileFolder, folderPrefix + "01"); } else { // 解析文件夹名称以获取流水号,并找出最大的流水号
int maxSerialNumber = directories .Select(dir => int.Parse(Path.GetFileName(dir).Substring(folderPrefix.Length))) .Max();
// 流水号加1,并创建新文件夹
int newSerialNumber = maxSerialNumber + 1; newFolderPath = Path.Combine(DownloadFileFolder, $"{folderPrefix}{newSerialNumber:D2}"); // D2表示格式化为两位数
} Directory.CreateDirectory(newFolderPath);
var downloadFolder = Path.Combine(newFolderPath, faBuNum); if (!Directory.Exists(downloadFolder)) Directory.CreateDirectory(downloadFolder); //获取解压下载文件夹
var UnZipFileFolder = Path.Combine(downloadFolder, "UnZip"+ informationArr[0]); if (!Directory.Exists(UnZipFileFolder)) Directory.CreateDirectory(UnZipFileFolder); #endregion
var unzipSource = ZipHelper.Unzip(new FileInfo(uploadfile), new DirectoryInfo(UnZipFileFolder)); if (!unzipSource.IsSuccess) { throw new Exception(unzipSource.Message); }
var files = Directory.GetFiles(UnZipFileFolder).ToList(); var UpdateFileFolder = Path.Combine(downloadFolder, "UnZip"); if (!Directory.Exists(UpdateFileFolder)) Directory.CreateDirectory(UpdateFileFolder);
#region 创建备份文件夹
var backUpfolder = Path.Combine(newFolderPath, "Backup"); if (!Directory.Exists(backUpfolder)) Directory.CreateDirectory(backUpfolder); #endregion
Exception exception = null; files.ForEach(info => {
//获取文件的名称
string fileName = Path.GetFileNameWithoutExtension(info); //获取后缀名
string fileNameExtension = Path.GetExtension(info); //判断文件名称,进行解压缩操作
if (fileName.EndsWith("系统更新压缩补丁包")) { //if (Directory.Exists(UpdateFileFolder))
// Directory.Delete(UpdateFileFolder, true);
var unzip = ZipHelper.Unzip(new FileInfo(info), new DirectoryInfo(UpdateFileFolder)); if (!unzip.IsSuccess) { throw new Exception(unzip.Message); } //备份需要替换的文件
var backupPath = Path.Combine(backUpfolder, DateTime.Now.ToString("yyyyMMddHHmmss") + ".zip"); ZipHelper.ZipFile(new DirectoryInfo(publishPath), new FileInfo(backupPath)); //移动替换文件
FileController.MoveFolder(UpdateFileFolder, publishPath); Directory.Delete(UpdateFileFolder, true);
} else if (fileNameExtension == ".sql") { try { //读取文件中的内容,并执行sql语句
string sqlText = File.ReadAllText(info); //执行sql语句
SqlHelper.CmdExecuteNonQueryLi(sqlText); } catch (Exception ex) { msg = fileName + "脚本执行失败,请手动操作\n" + ex; exception = ex; }
} //替换成功,调接口
var returnModel = ConfirmDown(faBuNum);
});
if (exception != null) throw exception; return msg;
} catch (Exception ex) { throw new Exception(msg + ex.Message);
}
} #endregion
//深拷贝
#region
public static T DeepCopyByReflect<T>(T obj) { if (obj == null) return obj;
var type = obj.GetType(); //如果是字符串或值类型则直接返回
if (obj is string || type.IsValueType) return obj; if(type.IsArray) { var elementType = Type.GetType(type.FullName.Replace("[]", null)); var array = obj as Array; var copied = Array.CreateInstance(elementType, array.Length); for (int idx = 0; idx < copied.Length; idx++) { copied.SetValue(DeepCopyByReflect(array.GetValue(idx)), idx); } return (T)Convert.ChangeType(copied,type); }
object retval = Activator.CreateInstance(obj.GetType()); FieldInfo[] fields = obj.GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic |BindingFlags.Instance | BindingFlags.Static); foreach (FieldInfo field in fields) { try { field.SetValue(retval, DeepCopyByReflect(field.GetValue(obj))); } catch { } } return (T)retval; } #endregion
// 定义一个类来表示 JSON 数据的结构
public class JsonData { public string IsSuccess { get; set; } public string Message { get; set; } public List<JsonDetail> FaBuList { get; set; } }
public class JsonDetail { public string FaBuNum { get; set; } public string FaBuVersion { get; set; } public string FaBuContent { get; set; } public string FaBuRemark { get; set; } public string ProjectCode { get; set; } public string FaBuTime { get; set; } public string ConfirmPerson { get; set; } public string IsDownloaded { get; set; } public string DownTime { get; set; } public string DownPersonName { get; set; } public List<Files> Files { get; set; } } /// <summary>
/// 文件详情
/// </summary>
public class Files { public string FileName { get; set; } public string FilePath { get; set; } public string FileVersion { get; set; } }
public class TypeModel { /// <summary>
/// 客户名称
/// </summary>
public string CustomerName { get; set; } /// <summary>
/// 系统名
/// </summary>
public string XTType { get; set; } /// <summary>
/// 系统名
/// </summary>
public string FaBuNum { get; set; }
}
/// <summary>
/// 回传对象
/// </summary>
public class ConfirmDownModel { /// <summary>
/// 客户名称
/// </summary>
public string CustomerName { get; set; } /// <summary>
/// 发布单号
/// </summary>
public string FaBuNum { get; set; } /// <summary>
/// 调用电脑接口IP1
/// </summary>
public string IP1 { get; set; } /// <summary>
///调用电脑接口IP2
/// </summary>
public string IP2 { get; set; } /// <summary>
/// 下载人员名称
/// </summary>
public string PersonName { get; set; } } /// <summary>
/// 返回对象
/// </summary>
public class ResultModel { /// <summary>
/// 是否成功
/// </summary>
public bool IsSuccess { get; set; } /// <summary>
/// 返回消息
/// </summary>
public string Message { get; set; }
}
}}
|