CNCEC_SUBQHSE_WUHUAN/SGGL/BLL/OpenService/FileInsertService.cs

349 lines
15 KiB
C#
Raw Normal View History

using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
2025-09-15 19:12:00 +08:00
using System.Text.RegularExpressions;
using System.Web;
namespace BLL
{
public static class FileInsertService
{
/// <summary>
/// 保存其他单位附件同步处理
/// </summary>
/// <param name="domain">单位域名地址</param>
/// <param name="attachFileId">附件id</param>
/// <param name="tokeyId">附件keyid</param>
/// <param name="attachSource">附件json信息</param>
/// <param name="attachUrl">附件地址</param>
public static void SaveAttachFileRecords(string domain, string attachFileId, string tokeyId, string attachSource, string attachUrl)
{
if (!string.IsNullOrEmpty(attachFileId) && !string.IsNullOrEmpty(attachSource) && !string.IsNullOrEmpty(attachUrl))
{
List<byte[]> FileContext = new List<byte[]>();
if (!string.IsNullOrWhiteSpace(domain) && !string.IsNullOrWhiteSpace(attachUrl))
{
FileContext = FilePathTransStream(domain, attachUrl);
}
InsertAttachFilesRecord(attachFileId, tokeyId, attachSource, attachUrl, FileContext);
}
}
2025-09-18 11:26:22 +08:00
/// <summary>
/// 获取服务器图片转byte
/// </summary>
/// <param name="fileHost"></param>
/// <param name="attachUrl"></param>
/// <returns></returns>
public static List<byte[]> FilePathTransStream(string fileHost, string attachUrl)
{
List<byte[]> bytes = new List<byte[]>();
var strs = attachUrl.Trim().Split(',');
try
{
foreach (var fileUrl in strs)
{
string path = ConfigurationManager.AppSettings["localRoot"] + fileUrl;
if (!File.Exists(path))
2025-09-18 11:26:22 +08:00
{
//BLL.ErrLogInfo.WriteLog($"不存在则新增,附件地址:{path}");
string filepath = $"{fileHost}{fileUrl}";
//BLL.ErrLogInfo.WriteLog($"不存在则新增,责任单位附件地址:{filepath}");
// 创建WebClient实例
using (WebClient webClient = new WebClient())
2025-09-18 11:26:22 +08:00
{
// 下载图片并保存到内存流
using (MemoryStream ms = new MemoryStream(webClient.DownloadData(filepath)))
{
// 将MemoryStream转换为byte数组
byte[] imageBytes = ms.ToArray();
bytes.Add(imageBytes);
//// 使用byte数组例如保存到文件或进行其他处理
//File.WriteAllBytes("localImage.jpg", imageBytes);
}
2025-09-18 11:26:22 +08:00
}
}
//else
//{
// BLL.ErrLogInfo.WriteLog($"已存在附件地址:{path}");
//}
2025-09-18 11:26:22 +08:00
}
}
catch (Exception ex)
{
BLL.ErrLogInfo.WriteLog($"服务器图片转换异常:{fileHost};图片地址:{attachUrl}", ex.Message);
2025-09-18 11:26:22 +08:00
}
return bytes;
}
/// <summary>
/// 获取附件数据流类
/// </summary>
/// <param name="attachUrl">附件路径</param>
/// <returns></returns>
public static void FileInsert(List<byte[]> fileContextList, string attachUrl)
{
if (fileContextList != null && fileContextList.Count > 0)
{
string physicalpath = Funs.RootPath;
//HttpContext.Current.Request.PhysicalApplicationPath;
string fullPath = physicalpath + attachUrl;
if (!File.Exists(fullPath))
{
byte[] fileContext = fileContextList[0];
int index = fullPath.LastIndexOf("\\");
string filePath = fullPath.Substring(0, index);
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
//string savePath = fullPath + fileName;
//文件读写模式
System.IO.FileMode fileMode = System.IO.FileMode.Create;
//写入文件
using (System.IO.FileStream fs = new System.IO.FileStream(fullPath, fileMode, System.IO.FileAccess.Write))
{
fs.Write(fileContext, 0, fileContext.Length);
}
}
}
}
/// <summary>
/// 获取多附件数据流类
/// </summary>
/// <param name="attachUrl">附件路径</param>
/// <returns></returns>
public static void FileMoreInsert(List<byte[]> fileContextList, string attachUrl)
2026-02-28 16:09:10 +08:00
{
if (fileContextList.Count > 0)
{
string[] strs = attachUrl.Trim().Split(',');
int i = 0;
string physicalpath = ConfigurationManager.AppSettings["localRoot"];
foreach (var item in fileContextList)
{
//HttpContext.Current.Request.PhysicalApplicationPath;
string fullPath = (physicalpath + strs[i]).Replace('/', '\\');
if (!File.Exists(fullPath))
{
byte[] fileContext = item;
int index = fullPath.LastIndexOf("\\");
string filePath = fullPath.Substring(0, index);
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
//string savePath = fullPath + fileName;
//文件读写模式
System.IO.FileMode fileMode = System.IO.FileMode.Create;
//写入文件
using (System.IO.FileStream fs = new System.IO.FileStream(fullPath, fileMode, System.IO.FileAccess.Write))
{
fs.Write(fileContext, 0, fileContext.Length);
}
}
i++;
}
}
}
/// <summary>
/// 获取多附件数据流类
/// </summary>
/// <param name="attachUrl">附件路径</param>
/// <returns></returns>
public static void FileMoreInsertOld(List<byte[]> fileContextList, string attachUrl)
{
if (fileContextList != null && fileContextList.Count() > 0)
{
if (fileContextList.Count > 0)
{
string[] strs = attachUrl.Trim().Split(',');
int i = 0;
foreach (var item in fileContextList)
{
if (strs.Count() > i)
{
string physicalpath = Funs.RootPath;
2025-09-15 19:12:00 +08:00
string fpath = strs[i];
string fullPath = physicalpath + fpath;
if (!File.Exists(fullPath))
{
byte[] fileContext = item;
2025-09-15 19:12:00 +08:00
string fileName = Regex.Match(fullPath, @"[^/\\?]+(\?.*)?$").Value;
string filePath = fullPath.Replace(fileName, "");
//int index = fullPath.LastIndexOf("\\");
//string filePath = fullPath.Substring(0, index) ;
//string filePath = index > 0 ? fullPath.Substring(0, index) : fullPath;
2025-09-15 19:12:00 +08:00
try
{
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
}
catch (Exception ex)
{
2025-09-15 19:12:00 +08:00
ErrLogInfo.WriteLog($"附件【{filePath}】获取异常!");
//continue;
}
//string savePath = fullPath + fileName;
//文件读写模式
System.IO.FileMode fileMode = System.IO.FileMode.Create;
//写入文件
using (System.IO.FileStream fs = new System.IO.FileStream(fullPath, fileMode, System.IO.FileAccess.Write))
{
fs.Write(fileContext, 0, fileContext.Length);
}
}
i++;
}
}
}
}
}
/// <summary>
/// 数据和附件插入到多附件表
/// </summary>
public static void InsertAttachFile(string attachFileId, string dataId, string attachSource, string attachUrl, List<byte[]> fileContext)
{
var getAtt = Funs.DB.AttachFile.FirstOrDefault(x => x.AttachFileId == attachFileId);
if (getAtt != null)
{
Funs.DB.AttachFile.DeleteOnSubmit(getAtt);
Funs.DB.SubmitChanges();
}
//多附件
var attachFile = Funs.DB.AttachFile.FirstOrDefault(x => x.ToKeyId == dataId);
if (attachFile == null && !string.IsNullOrEmpty(attachSource))
{
Model.AttachFile newAttachFile = new Model.AttachFile
{
AttachFileId = attachFileId,
ToKeyId = dataId,
AttachSource = attachSource,
AttachUrl = attachUrl
};
Funs.DB.AttachFile.InsertOnSubmit(newAttachFile);
Funs.DB.SubmitChanges();
////插入附件文件
BLL.FileInsertService.FileMoreInsert(fileContext, attachUrl);
}
else
{
if (attachFile.AttachUrl != attachUrl)
{
///删除附件文件
BLL.UploadAttachmentService.DeleteFile(Funs.RootPath, attachFile.AttachUrl);
////插入附件文件
BLL.FileInsertService.FileMoreInsert(fileContext, attachUrl);
attachFile.AttachSource = attachSource;
attachFile.AttachUrl = attachUrl;
Funs.DB.SubmitChanges();
}
}
}
2025-09-18 11:26:22 +08:00
/// <summary>
/// 数据和附件插入到多附件表
/// </summary>
public static void InsertAttachFilesRecord(string attachFileId, string dataId, string attachSource, string attachUrl, List<byte[]> fileContext)
{
2026-02-28 16:09:10 +08:00
BLL.ErrLogInfo.WriteLog($"数据和附件插入到多附件表1");
if (!string.IsNullOrEmpty(attachFileId))
{
//多附件
var attachFile = Funs.DB.AttachFile.FirstOrDefault(x => x.ToKeyId == dataId);
if (attachFile == null)
{
2026-02-28 16:09:10 +08:00
BLL.ErrLogInfo.WriteLog($"数据和附件插入到多附件表2attachFile == null");
////插入附件文件
BLL.FileInsertService.FileMoreInsert(fileContext, attachUrl);
Model.AttachFile newAttachFile = new Model.AttachFile
{
AttachFileId = attachFileId,
ToKeyId = dataId,
AttachSource = attachSource,
AttachUrl = attachUrl
};
Funs.DB.AttachFile.InsertOnSubmit(newAttachFile);
Funs.DB.SubmitChanges();
}
else
{
2026-02-28 16:09:10 +08:00
BLL.ErrLogInfo.WriteLog($"数据和附件插入到多附件表2attachFile != null");
if (attachFile.AttachUrl != attachUrl)
{
2026-02-28 16:09:10 +08:00
BLL.ErrLogInfo.WriteLog($"数据和附件插入到多附件表2attachFile != null attachFile.AttachUrl != attachUrl");
/////删除附件文件
//BLL.UploadAttachmentService.DeleteFile(ConfigurationManager.AppSettings["localRoot"], attachFile.AttachUrl);
////插入附件文件
BLL.FileInsertService.FileMoreInsert(fileContext, attachUrl);
attachFile.AttachSource = attachSource;
attachFile.AttachUrl = attachUrl;
Funs.DB.SubmitChanges();
}
}
}
}
2025-09-18 11:26:22 +08:00
/// <summary>
/// 数据和附件插入到多附件表【不存实际文件,只存地址】
/// </summary>
public static void InsertAttachFileRecord(string attachFileId, string dataId, string attachSource, string attachUrl)
{
var getAtt = Funs.DB.AttachFile.FirstOrDefault(x => x.AttachFileId == attachFileId);
if (getAtt != null)
{
Funs.DB.AttachFile.DeleteOnSubmit(getAtt);
Funs.DB.SubmitChanges();
}
//多附件
var attachFile = Funs.DB.AttachFile.FirstOrDefault(x => x.ToKeyId == dataId);
if (attachFile == null && !string.IsNullOrEmpty(attachSource))
{
Model.AttachFile newAttachFile = new Model.AttachFile
{
AttachFileId = attachFileId,
ToKeyId = dataId,
AttachSource = attachSource,
AttachUrl = attachUrl
};
Funs.DB.AttachFile.InsertOnSubmit(newAttachFile);
Funs.DB.SubmitChanges();
}
else
{
if (attachFile.AttachUrl != attachUrl)
{
///删除附件文件
BLL.UploadAttachmentService.DeleteFile(Funs.RootPath, attachFile.AttachUrl);
attachFile.AttachSource = attachSource;
attachFile.AttachUrl = attachUrl;
Funs.DB.SubmitChanges();
}
}
}
}
}