using IBKLinker_Minio.Entity;
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using Minio;
using System.Net.PeerToPeer;
using System.IO;
using System.Reactive.Linq;
using Minio.DataModel;
using System.Security.AccessControl;
using System.IO.Pipes;
using System.Net.Mime;
using IBKLinker_Minio.Entity.MinioController;
using SOA.Objects;
using SOA.Persistent;
using System.Web.Http.Results;
using System.Runtime.InteropServices.ComTypes;
using System.Xml;
using Swashbuckle.Swagger;
using System.Runtime.ConstrainedExecution;
using System.Net.Http;
using System.Web.UI.WebControls;
using System.Runtime.Remoting.Contexts;
using System.Runtime.InteropServices;
using System.Net;
using NPOI.XSSF.UserModel;
using NPOI.SS.UserModel;
using System.Net.Http.Headers;
using NPOI.SS.Formula.Functions;
using System.Xml.Linq;
using NPOI.HPSF;
using System.Configuration;
using NPOI.SS.Formula.Atp;
namespace IBKLinker_Minio.Common
{
///
/// 作者:孙亮
/// 编写时间:2023-10-13
/// 编写内容:minio业务逻辑
///
public class MinioBusinessLogic
{
private static readonly ILog logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
#region 异步处理取minio文件流数据方法
///
/// 异步处理取minio文件流数据方法
///
public async Task Download(string BuckeName, string path)
{
MemoryStream memoryStream = new MemoryStream();
try
{
var minio = ConnectMinIO();//调用连接minio数据库方法
BuckeName = GetBuckName();//调用取桶名称方法
await minio.GetObjectAsync(BuckeName, path, (stream) =>
{
stream.CopyTo(memoryStream);//将文件去除复制到memoryStream
});
memoryStream.Position = 0;//清除
string base64String = Convert.ToBase64String(memoryStream.ToArray());//转换base64
return base64String;//返回值
}
catch (Exception ex)
{
logger.InfoFormat("MinioBusinessLogic/Download方法错误:" + ex.ToString());
throw;
}
}
#endregion
#region 文件上传操作日志记录
///
/// 文件上传操作日志记录
///
public void MinioOperationLog(string OperateUser, string FileName, string Operate, string FilePk, string FileUPk, IDbContext db) //传参为操作人、文件名、操作、数据库连接上下文对象
{
try
{
MinioOperationlog MOdata = new MinioOperationlog();
DateTime time = DateTime.Now;//时间
MOdata.Pk = Convert.ToInt32(db.QueryScalar("select S_MINIO_OPERATIONLOG.nextval from dual"));///拿到表自增长的值赋值给自增长主键pk;
MOdata.OperateUser = OperateUser;//操作者
MOdata.OperateTime = time;//操作时间
MOdata.FilePk = FilePk.ToString();//文件pk
MOdata.FileUPk = FileUPk.ToString();//文件父级pk
MOdata.FileName = FileName;//文件名
MOdata.Operater = OperateUser + Operate + FileName;//操作(操作人,加操作,加文件名称)
MOdata.CreationTime = time;//创建时间
db.Insert(MOdata);//写入数据库
}
catch (Exception ex)
{
logger.InfoFormat("MinioBusinessLogic/MinioOperationLog方法错误:" + ex.ToString());
throw;
}
}
#endregion
#region 文件夹递归查询
///
/// 文件夹递归查询
///
public List GetSubDirectories(int parentPk)
{
List subDirs = new List();
using (IDbContext db = ObjectContainer.GetObject("db"))
{
List subDirectories = db.Query("select * from MINIO_STORAGE_PATH where UPK ='" + parentPk + "' and CLEAR_FLAG='0' and FILE_TYPE='0' ORDER BY PK").ToList();//查询子级中是否还有子级
foreach (var subDir in subDirectories)
{
RequstStoragePath subDirModel = new RequstStoragePath();
subDirModel.Pk = subDir.Pk;//主键ID
subDirModel.Upk = subDir.Upk;//父级文件夹pk
subDirModel.FileName = subDir.FileName;//文件名称
subDirModel.FilePath = subDir.FilePath;//文件路径
subDirModel.FileType = subDir.FileType;//文件类型
subDirModel.FileLevel = subDir.FileLevel;//层级
subDirModel.CreatorTime = subDir.CreatorTime;//创建时间
subDirModel.CreatorUser = subDir.CreatorUser;//创建者
subDirModel.DownloadNum = subDir.DownloadNum;//下载次数
subDirModel.ModifyUsers = subDir.ModifyUsers;//修改用户
subDirModel.ModifyTime = subDir.ModifyTime;//修改时间
subDirModel.ClearUsers = subDir.ClearUsers;//删除用户
subDirModel.ClearTime = subDir.ClearTime;//删除时间
subDirModel.ClearFlag = subDir.ClearFlag;//删除标记 0未删除 1删除
subDirModel.DocumentType = subDir.DocumentType;//文件种类:1、技术协议 2、质量要点 3、工艺卡 4、其他
subDirModel.requstStoragePaths = GetSubDirectories(subDir.Pk); // 递归调用获取子文件夹
subDirs.Add(subDirModel);
}
}
return subDirs;
}
#endregion
#region 连接minio数据库公用方法
///
/// 连接minio数据库公用方法
///
public MinioClient ConnectMinIO()
{
string MinIOIp = ConfigurationManager.AppSettings["MinIOIp"];//minio数据库连接地址
string MinIOName = ConfigurationManager.AppSettings["MinIOName"];//minio数据库连接用户名
string MinIoPass = ConfigurationManager.AppSettings["MinIoPass"];//minio数据库连接密码
MinioClient minio = new MinioClient(MinIOIp, MinIOName, MinIoPass);//连接所需minio所在地址、登录名、密码
return minio;
}
#endregion
#region 取minio数据库中桶名称
///
/// 取minio数据库中桶名称
///
public string GetBuckName()
{
string BuckName = ConfigurationManager.AppSettings["BuckName"];//minio数据库连接地址
return BuckName;
}
#endregion
#region 公用方法
///
/// 矫正pk值
///
///
///
///
public Int32 getPk(String sequenceName, String tableName)
{
///创建数据库连接上下文对象
using (IDbContext db = ObjectContainer.GetObject("db"))
{
///拿到表自增长序列最大pk值
int pk_SequenceCurrentMax = Convert.ToInt32(db.QueryScalar("select " + sequenceName + ".nextval from dual"));
///拿到表目前最大pk值
Object pk_TableCurrentMaxObj = db.QueryScalar("select t.pk from " + tableName + " t where rownum <=1 order by t.pk desc");
int pk_TableCurrentMax = pk_SequenceCurrentMax;
//如果表最大值查询结果不为空进行对比,
if (pk_TableCurrentMaxObj != null)
{
pk_TableCurrentMax = Convert.ToInt32(pk_TableCurrentMaxObj);
//拿到表中最大值和序列最大值之间的差值
int num_BetweenTableSequence = pk_TableCurrentMax - pk_SequenceCurrentMax + 1;
//更改序列递增规则,使下一次拿到的序列号比表中最大pk大1
db.QueryScalar("alter sequence " + sequenceName + " increment by " + num_BetweenTableSequence.ToString());
//执行nexval拿到下一个序列号
pk_TableCurrentMax = Convert.ToInt32(db.QueryScalar("select " + sequenceName + ".nextval from dual"));
//更改序列递增规则为每次递增一个。
db.QueryScalar("alter sequence " + sequenceName + " increment by 1");
}
return pk_TableCurrentMax;
}
}
///
/// 编写内容:下载文件转换流数据方法
///
public string Base64(string FilePath)
{
string data = "";
try
{
using (MemoryStream msReader = new MemoryStream())
{
using (FileStream fs = new FileStream(FilePath, FileMode.Open))
{
byte[] buffer = new byte[1024];
int readLen = 0;
while ((readLen = fs.Read(buffer, 0, buffer.Length)) > 0)
{
msReader.Write(buffer, 0, readLen);
}
}
data = Convert.ToBase64String(msReader.ToArray());//转换base64
}
return data;//返回值
}
catch (Exception ex)
{
logger.InfoFormat(ex.ToString());
throw;
}
}
#endregion
}
}