using IBKLinker_Minio.Entity;
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Web.Http;
using Minio;
using System.IO;
using System.Reactive.Linq;
using IBKLinker_Minio.Entity.MinioController;
using SOA.Objects;
using SOA.Persistent;
using System.Net.Http;
using System.Net;
using NPOI.XSSF.UserModel;
using System.Net.Http.Headers;
using IBKLinker_Minio.Common;
using System.Windows.Forms;
using System.Data;
namespace IBKLinker_Minio.Controller.MinioController
{
///
/// 作者:孙亮
/// 编写时间:2023-08-15
/// 编写内容:mino上传接口
///
[RoutePrefix("minio/minioapi")]
public class MinioApiController : ApiController
{
///
/// 调用log日志文件
///
private static readonly ILog logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
public static List SliceUploadData = new List();
public static List rootDirectories = new List();
#region minio权限表
///
/// 添加minio权限表
///
/// 权限表
[HttpPost, Route("AddAuthority")]
public RequestEntity AddAuthority(MinioAuthority minioAuthority)
{
RequestEntity request = new RequestEntity();
MinioBusinessLogic bb = new MinioBusinessLogic();//定义业务逻辑方法
try
{
///创建数据库连接上下文对象
using (IDbContext db = ObjectContainer.GetObject("db"))
{
///拿到表自增长的值赋值给自增长主键pk
minioAuthority.Id = bb.getPk("S_MINIO_AUTHORITY", "MINIO_AUTHORITY");
///给创建时间赋值
minioAuthority.CreatorTime = DateTime.Now;
///把需要添加的数据同步到数据库
db.Insert(minioAuthority);
request.code = "0";
}
}
catch (Exception ex)
{
request.code = "1";
logger.ErrorFormat("主表:MINIO_AUTHORITY,minio权限表添加数据失败,错误原因:{0}", ex);
}
return request;
}
///
/// 修改minio权限表
///
/// 修改数据
[HttpPost, Route("UpdateAuthority")]
public RequestEntity UpdateAuthority(List data)
{
RequestEntity request = new RequestEntity();
try
{
///创建数据库连接上下文对象
using (IDbContext db = ObjectContainer.GetObject("db"))
{
try
{
///启动事务
db.BeginTransaction();
///给修改时间赋值
DateTime time = DateTime.Now;
db.Update(data);
db.Commit();
request.code = "0";
}
catch (Exception ex)
{
request.code = "1";
db.Rollback();
///把错误日志写到日志文件
logger.ErrorFormat("主表:MINIO_AUTHORITY,修改minio权限表失败,错误原因:{0}", ex);
}
}
}
catch (Exception ex)
{
request.code = "1";
logger.ErrorFormat("主表:MINIO_AUTHORITY,创建数据库连接对象,错误原因:{0}", ex);
}
return request;
}
///
/// 删除minio权限表
///
/// 需要删除的数据
[HttpPost, Route("RemoveAuthority")]
public RequestEntity RemoveAuthority(List master)
{
RequestEntity request = new RequestEntity();
try
{
using (IDbContext db = ObjectContainer.GetObject("db"))
{
db.BeginTransaction();
db.Delete(master);
db.Commit();
request.code = "0";
}
}
catch (Exception ex)
{
request.code = "1";
logger.ErrorFormat("主表:MINIO_AUTHORITY,创建数据库连接对象失败,错误原因:{0}", ex);
}
return request;
}
///
/// 查询minio权限表
///
///
[HttpPost, Route("GetAuthority")]
public RequestEntity GetAuthority(MinioAuthority minioAuthority)
{
RequestEntity request = new RequestEntity();
try
{
using (IDbContext db = ObjectContainer.GetObject("db"))
{
string sql = string.Empty;
//IList result = new List();
if (!string.IsNullOrEmpty(minioAuthority.LimitsAuthorityId))
{
sql += " and a.LIMITS_AUTHORITY_ID ='" + minioAuthority.LimitsAuthorityId + "'";
}
request.data = db.Query("select a.*,b.Role_Name as RoleName from MINIO_AUTHORITY a left join MINIO_ROLE_PERMISSION b on a.LIMITS_AUTHORITY_ID=b.ID where 1=1 " + sql + " ORDER BY PK");
request.code = "0";
}
}
catch (Exception ex)
{
request.code = "1";
logger.ErrorFormat("主表:MINIO_AUTHORITY,查询minio权限表失败,错误原因:{0}", ex);
}
return request;
}
#endregion
#region minio用户表
///
/// 添加minio角色表
///
/// 角色表
[HttpPost, Route("AddUSER")]
public RequestEntity AddUSER(MinioUser minioUser)
{
RequestEntity request = new RequestEntity();
try
{
///创建数据库连接上下文对象
using (IDbContext db = ObjectContainer.GetObject("db"))
{
///拿到表自增长的值赋值给自增长主键pk
//minioUser.UserId = getPk("S_MINIO_USER", "MINIO_USER");
///给创建时间赋值
minioUser.CreatorTime = DateTime.Now;
///把需要添加的数据同步到数据库
db.Insert(minioUser);
request.code = "0";
}
}
catch (Exception ex)
{
request.code = "1";
logger.ErrorFormat("主表:MINIO_USER,minio角色表添加数据失败,错误原因:{0}", ex);
}
return request;
}
///
/// 修改minio用户表
///
/// 修改数据
[HttpPost, Route("UpdateUSER")]
public RequestEntity UpdateUSER(List data)
{
RequestEntity request = new RequestEntity();
try
{
///创建数据库连接上下文对象
using (IDbContext db = ObjectContainer.GetObject("db"))
{
try
{
///启动事务
db.BeginTransaction();
///给修改时间赋值
DateTime time = DateTime.Now;
db.Update(data);
db.Commit();
request.code = "0";
}
catch (Exception ex)
{
request.code = "1";
db.Rollback();
///把错误日志写到日志文件
logger.ErrorFormat("主表:MINIO_USER,修改minio角色表失败,错误原因:{0}", ex);
}
}
}
catch (Exception ex)
{
request.code = "1";
logger.ErrorFormat("主表:MINIO_USER,创建数据库连接对象,错误原因:{0}", ex);
}
return request;
}
///
/// 删除minio角色表
///
/// 需要删除的数据
[HttpPost, Route("RemoveUSER")]
public RequestEntity RemoveUSER(List master)
{
RequestEntity request = new RequestEntity();
try
{
using (IDbContext db = ObjectContainer.GetObject("db"))
{
db.BeginTransaction();
db.Delete(master);
db.Commit();
request.code = "0";
}
}
catch (Exception ex)
{
request.code = "1";
logger.ErrorFormat("主表:MINIO_USER,创建数据库连接对象失败,错误原因:{0}", ex);
}
return request;
}
///
/// 查询minio用户表
///
///
[HttpPost, Route("GetUSER")]
public RequestEntity GetUSER(MinioUser minioUser)
{
RequestEntity request = new RequestEntity();
try
{
using (IDbContext db = ObjectContainer.GetObject("db"))
{
string sql = string.Empty;
//IList result = new List();
if (!string.IsNullOrEmpty(minioUser.UserName))
{
sql += " and a.USER_NAME like'%" + minioUser.UserName + "%'";
}
if (!string.IsNullOrEmpty(minioUser.UserId))
{
sql += " and a.USER_ID ='" + minioUser.UserId + "'";
}
if (!string.IsNullOrEmpty(minioUser.State.ToString()))
{
sql += " and a.STATE =" + minioUser.State + "";
}
if (!string.IsNullOrEmpty(minioUser.RoleId))
{
sql += " and a.ROLE_ID ='" + minioUser.RoleId + "'";
}
request.data = db.Query("select a.*,b.Role_Name as RoleName from MINIO_USER a left join MINIO_ROLE_PERMISSION b on a. ROLE_ID=b.ID where 1=1 " + sql + " ORDER BY USER_ID");
request.code = "0";
}
}
catch (Exception ex)
{
request.code = "1";
logger.ErrorFormat("主表:MINIO_USER,查询minio角色表失败,错误原因:{0}", ex);
}
return request;
}
#endregion
#region minio文件关联表
///
/// 添加minio文件关联表
///
/// 文件关联表
[HttpPost, Route("AddFileassociation")]
public RequestEntity AddFileassociation(IList minioFileassociation)
{
RequestEntity request = new RequestEntity();
///创建数据库连接上下文对象
using (IDbContext db = ObjectContainer.GetObject("db"))
{
try
{
foreach (var data in minioFileassociation)
{
bool Asstion = true;//定义是否执行关联
IList SelMFdata = new List();
SelMFdata = db.Query("select * from MINIO_FILEASSOCIATION where 1=1 and (FILE_PK='" + data.FilePk + "' or ASSOCIATION_FILE_PK='" + data.FilePk + "')");
//查询当前被关联文件是否已经被关联过
if (SelMFdata.Count > 0)
{
foreach (var item in SelMFdata)
{
if (item.FilePk == data.FilePk && item.AssociationFilePk == data.AssociationFilePk) //如果当前要关联的pk 等于已有的关联pk 被关联pk 等于已有的被关联pk
{
Asstion = false;
}
if (item.FilePk == data.AssociationFilePk && item.AssociationFilePk == data.FilePk) //如果当前要关联的pk 等于已有的被关联pk 被关联pk 等于已有的关联pk
{
Asstion = false;
}
}
}
if (Asstion)
{
db.BeginTransaction();//启动事务
///拿到表自增长的值赋值给自增长主键pk
data.Pk = Convert.ToInt32(db.QueryScalar("select S_MINIO_FILEASSOCIATION.nextval from dual"));
data.CreatorTime = DateTime.Now;
///把需要添加的数据同步到数据库
db.Insert(data);
db.Commit();//提交事务
request.code = "0";
request.msg = "关联成功!";
}
else
{
request.code = "1";
request.msg = "当前文件已经被关联过,请勿重复关联!";
}
}
}
catch (Exception ex)
{
db.Rollback();//回滚
request.code = "1";
request.msg = "关联失败!";
logger.ErrorFormat("主表:MINIO_FILEASSOCIATION,minio文件关联表表添加数据失败,错误原因:{0}", ex);
}
}
return request;
}
///
/// 修改minio文件关联表
///
/// 修改数据
[HttpPost, Route("UpdateFileassociation")]
public RequestEntity UpdateFileassociation(List data)
{
RequestEntity request = new RequestEntity();
///创建数据库连接上下文对象
using (IDbContext db = ObjectContainer.GetObject("db"))
{
try
{
try
{
///启动事务
db.BeginTransaction();
///给修改时间赋值
DateTime time = DateTime.Now;
db.Update(data);
db.Commit();
request.code = "0";
request.msg = "修改成功!";
}
catch (Exception ex)
{
request.code = "1";
db.Rollback();//回滚
///把错误日志写到日志文件
logger.ErrorFormat("主表:MINIO_FILEASSOCIATION,修改minio文件关联表表失败,错误原因:{0}", ex);
}
}
catch (Exception ex)
{
db.Rollback();//回滚
request.code = "1";
logger.ErrorFormat("主表:MINIO_FILEASSOCIATION,创建数据库连接对象,错误原因:{0}", ex);
}
}
return request;
}
///
/// 删除minio文件关联表
///
/// 需要删除的数据
[HttpPost, Route("RemoveFileassociation")]
public RequestEntity RemoveFileassociation(MinioFileassociation master)
{
RequestEntity request = new RequestEntity();
///创建数据库连接上下文对象
using (IDbContext db = ObjectContainer.GetObject("db"))
{
try
{
if (master != null)
{
bool Asstion = true;
List Deldata = new List();
IList SelMFdata = new List();
SelMFdata = db.Query("select * from MINIO_FILEASSOCIATION where 1=1 and (FILE_PK='" + master.FilePk + "' or ASSOCIATION_FILE_PK='" + master.FilePk + "')");
//查询当前被关联文件是否已经被关联过
if (SelMFdata.Count > 0)
{
foreach (var item in SelMFdata)
{
if (item.FilePk == master.FilePk && item.AssociationFilePk == master.AssociationFilePk) //如果当前要关联的pk 等于已有的关联pk 被关联pk 等于已有的被关联pk
{
Deldata.Add(item);//向需要删除的list中追加数据
}
if (item.FilePk == master.AssociationFilePk && item.AssociationFilePk == master.FilePk) //如果当前要关联的pk 等于已有的被关联pk 被关联pk 等于已有的关联pk
{
Deldata.Add(item);//向需要删除的list中追加数据
}
}
}
db.BeginTransaction();//启动事务
db.Delete(Deldata);
db.Commit();//提交事务
request.code = "0";
request.msg = "删除成功!";
}
else
{
request.code = "1";
request.msg = "删除失败!";
}
}
catch (Exception ex)
{
db.Rollback();//回滚
request.code = "1";
logger.ErrorFormat("主表:MINIO_FILEASSOCIATION,创建数据库连接对象失败,错误原因:{0}", ex);
}
}
return request;
}
///
/// 查询minio文件关联表
///
///
[HttpPost, Route("GetFileassociation")]
public RequestEntity GetFileassociation(string FilePk)
{
RequestEntity request = new RequestEntity();
try
{
using (IDbContext db = ObjectContainer.GetObject("db"))
{
if (FilePk != null)
{
IList SelMFdata = new List();
IList result = new List();
SelMFdata = db.Query("select * from MINIO_FILEASSOCIATION where 1=1 and (FILE_PK='" + int.Parse(FilePk) + "' or ASSOCIATION_FILE_PK='" + int.Parse(FilePk) + "') ORDER BY PK");
if (SelMFdata.Count > 0) //如果有数据
{
foreach (var item in SelMFdata)
{
MinioFileassociation date = new MinioFileassociation();//存储单条数据
if (item.FilePk.ToString() == FilePk)//判断当前查询结果文件pk是否等于选中的文件pk
{
#region 数据赋值
date.Pk = item.Pk;//主键
date.FileType = item.FileType;//文件类型
date.FilePk = item.FilePk;//文件PK
date.FilePath = item.FilePath;//文件路径
date.FileLevel = item.FileLevel;//文件层级
date.FileName = item.FileName;//文件名称
date.DownloadNum = item.DownloadNum;//文件下载次数
date.Version = item.Version;//文件版本
date.CreatorUser = item.CreatorUser;//创建者
date.CreatorTime = item.CreatorTime;//创建时间
date.AssociationFilePk = item.AssociationFilePk;//关联文件PK
date.AssociationFileType = item.AssociationFileType;//关联文件类型
date.AssociationFilePath = item.AssociationFilePath;//关联文件路径
date.AssociationFileName = item.AssociationFileName;//关联文件名称
date.AssociationFileLevel = item.AssociationFileLevel;//关联文件层级
date.AssociationDownloadNum = item.AssociationDownloadNum;//关联文件下载次数
date.AssociationVersion = item.AssociationVersion;//关联文件版本
#endregion
}
else //如果不等与的话将关联文件与被关联文件信息调换位置返回前端
{
#region 数据赋值
date.Pk = item.Pk;//主键
date.FileType = item.AssociationFileType;//文件类型
date.FilePk = item.AssociationFilePk;//文件PK
date.FilePath = item.AssociationFilePath;//文件路径
date.FileLevel = item.AssociationFileLevel;//文件层级
date.FileName = item.AssociationFileName;//文件名称
date.DownloadNum = item.AssociationDownloadNum;//文件下载次数
date.Version = item.AssociationVersion;//文件版本
date.CreatorUser = item.CreatorUser;//创建者
date.CreatorTime = item.CreatorTime;//创建时间
date.AssociationFilePk = item.FilePk;//关联文件PK
date.AssociationFileType = item.FileType;//关联文件类型
date.AssociationFilePath = item.FilePath;//关联文件路径
date.AssociationFileName = item.FileName;//关联文件名称
date.AssociationFileLevel = item.FileLevel;//关联文件层级
date.AssociationDownloadNum = item.DownloadNum;//关联文件下载次数
date.AssociationVersion = item.Version;//关联文件版本
#endregion
}
result.Add(date);//添加数据到list
}
request.data = result;
request.code = "0";
}
else
{
request.code = "0";
request.msg = "暂无数据!";
request.data = result;
}
}
}
}
catch (Exception ex)
{
request.code = "1";
logger.ErrorFormat("主表:MINIO_FILEASSOCIATION,查询minio文件关联表表失败,错误原因:{0}", ex);
}
return request;
}
#endregion
#region minio角色权限表
///
/// 添加minio角色权限表
///
/// minio角色权限表
[HttpPost, Route("AddRolePermission")]
public RequestEntity AddRolePermission(RolePermissionModel minioRolePermission)
{
MinioBusinessLogic bb = new MinioBusinessLogic();//定义业务逻辑方法
RequestEntity request = new RequestEntity();
try
{
List AuthorityList = new List();
///创建数据库连接上下文对象
using (IDbContext db = ObjectContainer.GetObject("db"))
{
minioRolePermission.minioRolePermission.Id = Guid.NewGuid().ToString();
minioRolePermission.minioRolePermission.LimitsAuthorityId = Guid.NewGuid().ToString();
foreach (var item in minioRolePermission.ds.Split(','))
{
MinioAuthority ds = new MinioAuthority();
ds.LimitsAuthorityId = minioRolePermission.minioRolePermission.LimitsAuthorityId;
ds.CreatorTime = DateTime.Now;
ds.CreatorUser = minioRolePermission.minioRolePermission.CreatorUser;
ds.Id = Convert.ToInt32(db.QueryScalar("select S_MINIO_STORAGE_PATH.nextval from dual"));
ds.FileId = int.Parse(item);
AuthorityList.Add(ds);
}
db.BeginTransaction();
///拿到表自增长的值赋值给自增长主键pk
minioRolePermission.minioRolePermission.Pk = bb.getPk("S_MINIO_ROLE_PERMISSION", "MINIO_ROLE_PERMISSION");
///给创建时间赋值
minioRolePermission.minioRolePermission.CreatorTime = DateTime.Now;
///把需要添加的数据同步到数据库
db.Insert(minioRolePermission.minioRolePermission);
db.Insert(AuthorityList);
db.Commit();
request.code = "0";
}
}
catch (Exception ex)
{
request.code = "1";
logger.ErrorFormat("主表:MINIO_ROLE_PERMISSION,minio角色权限表添加数据失败,错误原因:{0}", ex);
}
return request;
}
///
/// 修改minio角色表
///
/// 修改数据
[HttpPost, Route("UpdateRolePermission")]
public RequestEntity UpdateRolePermission(RolePermissionModel minioRolePermission)
{
RequestEntity request = new RequestEntity();
try
{
List AuthorityList = new List();
///创建数据库连接上下文对象
using (IDbContext db = ObjectContainer.GetObject("db"))
{
//data.Id=Guid.NewGuid().ToString();
if (string.IsNullOrEmpty(minioRolePermission.minioRolePermission.LimitsAuthorityId))
{
minioRolePermission.minioRolePermission.LimitsAuthorityId = Guid.NewGuid().ToString();
}
foreach (var item in minioRolePermission.ds.Split(','))
{
MinioAuthority ds = new MinioAuthority();
ds.LimitsAuthorityId = minioRolePermission.minioRolePermission.LimitsAuthorityId;
ds.CreatorTime = DateTime.Now;
ds.CreatorUser = minioRolePermission.minioRolePermission.CreatorUser;
ds.FileId = int.Parse(item);
ds.Id = Convert.ToInt32(db.QueryScalar("select S_MINIO_STORAGE_PATH.nextval from dual"));
AuthorityList.Add(ds);
}
try
{
///启动事务
db.BeginTransaction();
///给修改时间赋值
//DateTime time = DateTime.Now;
db.Update(minioRolePermission.minioRolePermission);
//删除历史记录加入新的记录
db.Execute("delete from MINIO_AUTHORITY where LIMITS_AUTHORITY_ID='" + minioRolePermission.minioRolePermission.LimitsAuthorityId + "' ");
db.Insert(AuthorityList);
db.Commit();
request.code = "0";
}
catch (Exception ex)
{
request.code = "1";
db.Rollback();
///把错误日志写到日志文件
logger.ErrorFormat("主表:MINIO_ROLE_PERMISSION,修改minio角色权限表表失败,错误原因:{0}", ex);
}
}
}
catch (Exception ex)
{
request.code = "1";
logger.ErrorFormat("主表:MINIO_ROLE_PERMISSION,创建数据库连接对象,错误原因:{0}", ex);
}
return request;
}
///
/// 删除minio角色表
///
/// 需要删除的数据
[HttpPost, Route("RemoveRolePermission")]
public RequestEntity RemoveRolePermission(MinioRolePermission data)
{
RequestEntity request = new RequestEntity();
IList MUData = new List();
IList UdpData = new List();
string sql = "begin ";
using (IDbContext db = ObjectContainer.GetObject("db"))
{
try
{
if (data != null)
{
MUData = db.Query("select * from MINIO_USER where 1=1 and ROLE_ID='" + data.Id + "'");//查询用户表中使用要被删除的角色
db.BeginTransaction();//启动事务
if (MUData.Count > 0)//如果有数据需要同步修改用户表中的角色
{
foreach (var item in MUData)
{
MinioUser date = new MinioUser();
date.UserId = item.UserId;//用户ID
date.UserName = item.UserName;//用户姓名
date.RoleId = "";//角色ID(将角色id改成0)
date.CreatorUser = item.CreatorUser;//创建人
date.State = item.State;//状态
date.CreatorTime = item.CreatorTime;//创建时间
UdpData.Add(date);//将数据加入list中
}
db.Update(UdpData);
}
sql += string.Format("delete from MINIO_AUTHORITY where 1=1 and LIMITS_AUTHORITY_ID='{0}';", data.LimitsAuthorityId);
sql += string.Format("delete from MINIO_ROLE_PERMISSION where 1=1 and PK={0};", data.Pk);
sql += "end;";
db.Execute(sql);
db.Commit();//提交事务
request.code = "0";
request.msg = "删除成功";
}
}
catch (Exception ex)
{
db.Rollback();//回滚
request.code = "1";
logger.ErrorFormat("主表:MINIO_ROLE_PERMISSION,创建数据库连接对象失败,错误原因:{0}", ex);
}
}
return request;
}
///
/// 查询minio角色表
///
///
[HttpPost, Route("GetRolePermission")]
public RequestEntity GetRolePermission(MinioRolePermission minioRolePermission)
{
RequestEntity request = new RequestEntity();
try
{
using (IDbContext db = ObjectContainer.GetObject("db"))
{
string sql = string.Empty;
IList result = new List();
if (!string.IsNullOrEmpty(minioRolePermission.Id))
{
sql += " and ID ='" + minioRolePermission.Id + "'";
}
if (!string.IsNullOrEmpty(minioRolePermission.RoleName))
{
sql += " and ROLE_NAME like'%" + minioRolePermission.RoleName + "%'";
}
request.data = db.Query("select * from MINIO_ROLE_PERMISSION where 1=1 " + sql + " ORDER BY PK");
request.code = "0";
}
}
catch (Exception ex)
{
request.code = "1";
logger.ErrorFormat("主表:MINIO_ROLE_PERMISSION,查询minio角色权限表失败,错误原因:{0}", ex);
}
return request;
}
#endregion
#region minio文件文件夹路径表
///
/// 添加minio文件文件夹路径表
///
/// minio文件文件夹路径表
[HttpPost, Route("AddStoragePath")]
public RequestEntity AddStoragePath(List minioStoragePath)
{
RequestEntity request = new RequestEntity();
MinioBusinessLogic bb = new MinioBusinessLogic();//定义业务逻辑方法
using (IDbContext db = ObjectContainer.GetObject("db"))
{
try
{
MinioClient minio = bb.ConnectMinIO();//调用连接minio数据库方法
string BuckName = bb.GetBuckName();//调用取桶名称方法
var bucket = minio.BucketExistsAsync(BuckName);//判断桶是否存在
if (bucket.Result == false) //不存在就创建
{
minio.MakeBucketAsync(BuckName);//创建桶
}
string Operate = "";//操作
List date = new List();
List checkdatelist = new List();
string RecordFileName = "";//重复文件名称记录
///创建数据库连接上下文对象
db.BeginTransaction();//启动事务
if (minioStoragePath != null)
{
foreach (var items in minioStoragePath) //先循环接收数据
{
checkdatelist = db.Query("select t.* from MINIO_STORAGE_PATH t where 1=1 and t.FILE_NAME='" + items.FileName + "'").ToList();//查询上传的文件或新建的文件夹在当前文件夹下是否有重复存在
if (checkdatelist.Count > 0 && items.FileType !=0) //如果有
{
RecordFileName += items.FileName + ",";//记录名称
}
}
if (RecordFileName == "")//如果正常上传
{
foreach (var item in minioStoragePath)
{
MinioStoragePath data = new MinioStoragePath();
date = db.Query("select * from MINIO_STORAGE_PATH where pk='" + minioStoragePath[0].Upk + "' ORDER BY PK").ToList();
if (item.FileType == 0) //如果是文件夹
{
if (item.Upk > 0)//判断如果创建的是根目录文件夹前端不需要对父级id传值
{
if (date.Count > 0)
{
#region 文件文件夹路径表字段赋值
data.Upk = minioStoragePath[0].Upk;//子文件夹的父级id
data.FileName = minioStoragePath[0].FileName;//文件名称
data.FileType = minioStoragePath[0].FileType;//文件类型
data.FilePath = minioStoragePath[0].FilePath;//文件路径
data.CreatorUser = minioStoragePath[0].CreatorUser;//创建者
data.ClearFlag = "0";//删除状态为未删除
data.CreatorTime = DateTime.Now;//创建时间
data.FileLevel = date[0].FileLevel + 1;//层级
data.Pk = Convert.ToInt32(db.QueryScalar("select S_MINIO_STORAGE_PATH.nextval from dual"));///拿到表自增长的值赋值给自增长主键pk
#endregion
db.Insert(data);
#region 记录日志
Operate = " 创建了一个新的文件夹,";
bb.MinioOperationLog(data.CreatorUser, data.FileName, Operate, data.Pk.ToString(), data.Upk.ToString(), db);
#endregion
}
}
else
{
#region 文件文件夹路径表字段赋值
data.Pk = Convert.ToInt32(db.QueryScalar("select S_MINIO_STORAGE_PATH.nextval from dual"));///拿到表自增长的值赋值给自增长主键pk
data.Upk = 0;//子文件夹的父级id(如果是根目录默认父级id为0)
data.FileName = minioStoragePath[0].FileName;//文件名称
data.FileType = minioStoragePath[0].FileType;//文件类型
data.FilePath = minioStoragePath[0].FilePath;//文件路径
data.CreatorUser = minioStoragePath[0].CreatorUser;//创建者
data.CreatorTime = DateTime.Now;//创建时间
data.ClearFlag = "0";//删除状态为未删除
data.FileLevel = 1;//层级
#endregion
db.Insert(data);
#region 记录日志
Operate = " 创建了一个新的文件夹,";
bb.MinioOperationLog(data.CreatorUser, data.FileName, Operate, data.Pk.ToString(), data.Upk.ToString(), db);
#endregion
}
#region minio创建文件夹
//var folderName = minioStoragePath[0].FilePath + "/";//取前端发送路径(或单一文件夹名称)
//minio.PutObjectAsync(buckname, folderName, new MemoryStream(), 0, "application/octet-stream");
#endregion
request.msg = "文件夹创建成功!";
request.code = "0";
}
if (item.FileType == 1)//如果是文件
{
var contentType = item.FileCategory;
string strBase64 = item.Bate64;//取文件base64流数据
byte[] bt = Convert.FromBase64String(strBase64);//获取html base64
var uploadTask = minio.PutObjectAsync(BuckName, "/" + item.FileName, new MemoryStream(bt), bt.Length, contentType);//上传文件到minio数据库
uploadTask.Wait();
if (uploadTask.Status == TaskStatus.RanToCompletion && !uploadTask.IsFaulted)//判断是否上传成功
{
#region 文件文件夹路径表字段赋值
data.Pk = Convert.ToInt32(db.QueryScalar("select S_MINIO_STORAGE_PATH.nextval from dual"));///拿到表自增长的值赋值给自增长主键pk
data.Upk = item.Upk;//子文件夹的父级id(如果是根目录默认父级id为0)
data.FileName = item.FileName;//文件名称
data.FileType = item.FileType;//文件类型
data.FilePath = item.FilePath;//文件路径
data.CreatorUser = item.CreatorUser;//创建者
data.CreatorTime = DateTime.Now;//创建时间
data.ClearFlag = "0";//删除状态为未删除
data.FileLevel = date[0].FileLevel + 1;//层级
data.DocumentType = item.DocumentType;//文件种类
#endregion
db.Insert(data);
request.msg = "文件上传成功!";
request.code = "0";
#region 记录日志
Operate = " 上传了一个新的文档,";
bb.MinioOperationLog(item.CreatorUser, item.FileName, Operate, data.Pk.ToString(), data.Upk.ToString(), db);
#endregion
}
else
{
request.msg = "文件上传失败,请检查文件是否正确!";
request.code = "1";
}
}
}
}
else
{
request.msg = "文件上传失败,其中" + RecordFileName + "在此文件夹已经存在!";
request.code = "1";
}
db.Commit();
}
}
catch (Exception ex)
{
db.Rollback();
request.code = "1";
logger.ErrorFormat("主表:MINIO_STORAGE_PATH,minio文件文件夹路径表添加数据失败,错误原因:{0}", ex);
}
}
return request;
}
///
/// 修改minio文件文件夹路径
///
/// 修改数据
[HttpPost, Route("UpdateStoragePath")]
public RequestEntity UpdateStoragePath(List data)
{
RequestEntity request = new RequestEntity();
MinioBusinessLogic bb = new MinioBusinessLogic();//定义业务逻辑方法
try
{
MinioClient minio = bb.ConnectMinIO();//调用连接minio数据库方法
string BuckName = bb.GetBuckName();//调用取桶名称方法
var bucket = minio.BucketExistsAsync(BuckName);//判断桶是否存在
if (bucket.Result == false) //不存在就创建
{
minio.MakeBucketAsync(BuckName);//创建桶
}
///创建数据库连接上下文对象
using (IDbContext db = ObjectContainer.GetObject("db"))
{
try
{
///启动事务
db.BeginTransaction();
///给修改时间赋值
DateTime time = DateTime.Now;//取当前时间
MinioStoragePath date = new MinioStoragePath();
IList UdpMFdata = new List();
List datelist = new List();
string Operate = "";//操作
if (data.Count > 0)
{
foreach (var item in data)
{
date = new MinioStoragePath();
date.Pk = item.Pk;//主键ID
date.Upk = item.Upk;//子文件夹的父ID
date.FileName = item.FileName;//文件名称
date.FileType = item.FileType;//文件类型
date.FilePath = item.FilePath;//文件路径
date.CreatorUser = item.CreatorUser;//创建者
date.CreatorTime = item.CreatorTime;//创建时间
date.DownloadNum = item.DownloadNum;//下载次数
date.FileLevel = item.FileLevel;//层级
date.ModifyUsers = item.ModifyUsers;//修改用户
date.ModifyTime = time;//修改时间
date.ClearFlag = "0";//删除标记 0未删除 1删除
date.DocumentType = item.DocumentType;//文件种类
date.Version = item.Version;//版本
#region 判断修改关联信息
IList SelMFdata = new List();
SelMFdata = db.Query("select * from MINIO_FILEASSOCIATION where 1=1 and (FILE_PK='" +item.Pk + "' or ASSOCIATION_FILE_PK='" + item.Pk + "')");//查询修改路径的文件关联关系
if (SelMFdata.Count >0) //如果有数据
{
foreach (var itemmf in SelMFdata)
{
if (itemmf.FilePk == item.Pk) //判断是关联的文件
{
itemmf.FilePath = item.FilePath;//替换文件路径
itemmf.FileLevel = item.FileLevel;//替换文件层级
}
if (itemmf.AssociationFilePk == item.Pk) //判断是被关联的文件
{
itemmf.AssociationFilePath = item.FilePath;//替换文件路径
itemmf.AssociationFileLevel = item.FileLevel;//替换文件层级
}
UdpMFdata.Add(itemmf);//写入list
}
}
#endregion
if (item.Bate64 != null)//如果是修改文件
{
var contentType = item.FileCategory;
string strBase64 = item.Bate64;//取文件base64流数据
byte[] bt = Convert.FromBase64String(strBase64);//获取html base64
var uploadTask = minio.PutObjectAsync(BuckName, "/" + item.FileName, new MemoryStream(bt), bt.Length, contentType);//上传文件到minio数据库
uploadTask.Wait();
if (uploadTask.Status == TaskStatus.RanToCompletion && !uploadTask.IsFaulted)//判断是否上传成功
{
datelist.Add(date);
#region 记录日志
Operate = " 更新了这个文档, ";
bb.MinioOperationLog(item.CreatorUser, item.FileName, Operate, item.Pk.ToString(), item.Upk.ToString(), db);
#endregion
}
}
else //正常修改文件/文件夹名称或移动
{
datelist.Add(date);
}
}
}
if (datelist != null)
{
if (UdpMFdata.Count > 0)
{
db.Update(UdpMFdata);
}
db.Update(datelist);
db.Commit();//提交事务
request.code = "0";
request.msg = "修改成功!";
}
else
{
request.code = "1";
request.msg = "请检查是否符合修改规范!";
}
}
catch (Exception ex)
{
request.code = "1";
db.Rollback();//回滚
///把错误日志写到日志文件
logger.ErrorFormat("主表:MINIO_STORAGE_PATH,修改minio文件文件夹路径表失败,错误原因:{0}", ex);
}
}
}
catch (Exception ex)
{
request.code = "1";
logger.ErrorFormat("主表:MINIO_STORAGE_PATH,创建数据库连接对象,错误原因:{0}", ex);
}
return request;
}
///
/// 删除minio文件文件夹路径表
///
/// 需要删除的数据
[HttpPost, Route("RemoveStoragePath")]
public RequestEntity RemoveStoragePath(List master)
{
RequestEntity request = new RequestEntity();
MinioBusinessLogic bb = new MinioBusinessLogic();//定义业务逻辑方法
using (IDbContext db = ObjectContainer.GetObject("db"))
{
try
{
DateTime time = DateTime.Now;//取当前时间
MinioClient minio = bb.ConnectMinIO();//调用连接minio数据库方法
string BuckName = bb.GetBuckName();//调用取桶名称方法
string Operate = "";//操作
string FilePk = "";//文件pk
string fileUPk = "";//文件父级pk
db.BeginTransaction();
string sql = "";
//db.Delete(master);
foreach (var item in master)
{
if (item.FileType == 1) //如果类型是文件
{
var DelTask = minio.RemoveObjectAsync(BuckName, item.FileName);//minio操作删除文件
DelTask.Wait();
if (DelTask.Status == TaskStatus.RanToCompletion && !DelTask.IsFaulted)//判断是否删除成功
{
sql = "begin ";
sql += string.Format("delete from MINIO_STORAGE_PATH where 1=1 and PK='{2}';", item.ClearUsers, time, item.Pk);//更新文件路径表中的删除状态
sql += string.Format(" delete from MINIO_FILEASSOCIATION where FILE_PK like'%{0}%' or ASSOCIATION_FILE_PK like'%{0}%';", item.Pk);//删除关联表中的关联关系
sql += "end;";
db.Execute(sql);
#region 记录日志
Operate = " 删除了这个文档, ";
bb.MinioOperationLog(item.ClearUsers, item.FileName, Operate, FilePk, fileUPk, db);
#endregion
}
}
else //如果类型是文件夹
{
sql = string.Format("delete from MINIO_STORAGE_PATH where 1=1 and PK='{2}' ", item.ClearUsers, time, item.Pk);//更新文件路径表中的删除状态
db.Execute(sql);
#region 记录日志
Operate = " 删除了这个文件夹, ";
bb.MinioOperationLog(item.ClearUsers, item.FileName, Operate, FilePk, fileUPk, db);
#endregion
}
}
request.code = "0";
request.msg = "删除成功";
db.Commit();
}
catch (Exception ex)
{
db.Rollback();
request.code = "1";
logger.ErrorFormat("主表:MINIO_ROLE_PERMISSION,创建数据库连接对象失败,错误原因:{0}", ex);
}
}
return request;
}
///
/// 查询minio文件文件夹路径表
///
///
[HttpPost, Route("GetStoragePath")]
public RequestEntity GetStoragePath(GetStoragePathModel PathData)
{
RequestEntity request = new RequestEntity();//定义返回集合
try
{
using (IDbContext db = ObjectContainer.GetObject("db"))
{
string sql = string.Empty;
IList result = new List();//定义查询到的list
if (PathData != null)//传输数据不为空
{
if (!string.IsNullOrEmpty(PathData.FileName.ToString()))//如果有名称模糊查询
{
sql += " and FILE_NAME like'%" + PathData.FileName + "%'";
}
if (PathData.Pk > 0) //如果在子文件夹内
{
sql += " and UPK = '" + PathData.Pk + "'";
}
if (!string.IsNullOrEmpty(PathData.BeginTime))//如果有开始时间
{
sql += string.Format(" and CREATE_TIME >= to_date('{0}', 'yyyy/mm/dd hh24:mi:ss')", PathData.BeginTime);//开始时间
}
if (!string.IsNullOrEmpty(PathData.EndTime))//如果有结束时间
{
sql += string.Format(" and CREATE_TIME <= to_date('{0}', 'yyyy/mm/dd hh24:mi:ss')", PathData.EndTime);//结束时间
}
request.data = db.Query("select * from MINIO_STORAGE_PATH where 1=1 " + sql + " and CLEAR_FLAG='0' ORDER BY PK");
request.code = "0";
}
else
{
request.code = "1";
request.msg = "查询失败,请检查查询条件!";
}
}
}
catch (Exception ex)
{
request.code = "1";
logger.ErrorFormat("主表:MINIO_STORAGE_PATH,查询minio文件文件夹路径表失败,错误原因:{0}", ex);
}
return request;
}
#region 根据pk查询文件信息
///
/// 根据PK查询minio文件文件夹路径表
///
///
[HttpPost, Route("GetStoragePathPk")]
public RequestEntity GetStoragePathPk(string Pk)
{
RequestEntity request = new RequestEntity();//定义返回集合
try
{
using (IDbContext db = ObjectContainer.GetObject("db"))
{
string sql = string.Empty;
IList result = new List();//定义查询到的list
if (Pk != null)//传输数据不为空
{
request.data = db.Query("select * from MINIO_STORAGE_PATH where 1=1 and PK='"+Pk+"' and CLEAR_FLAG='0' ORDER BY PK");
request.code = "0";
}
else
{
request.code = "1";
request.msg = "查询失败,请检查查询条件!";
}
}
}
catch (Exception ex)
{
request.code = "1";
logger.ErrorFormat("主表:MINIO_STORAGE_PATH,查询minio文件文件夹路径表失败,错误原因:{0}", ex);
}
return request;
}
#endregion
#endregion
#region minio文件菜单递归和权限判断
///
/// 根据ID查询子菜单
///
///
///
[HttpPost, Route("GetMenuTree")]
public List GetMenuTree(string PK = "")
{
List date = new List();
using (IDbContext db = ObjectContainer.GetObject("db"))
{
string sql = string.Empty;
if (!string.IsNullOrEmpty(PK))
{
sql += " and UPK =" + int.Parse(PK);
}
date = db.Query("select * from MINIO_STORAGE_PATH where 1=1 " + sql + "and CLEAR_FLAG='0' ORDER BY CREATOR_TIME desc").ToList();
}
return date;
}
///
/// 获取所有文件夹
///
///
[HttpPost, Route("GetRecursion")]
public RequestEntity GetRecursion()
{
MinioBusinessLogic bb = new MinioBusinessLogic();//定义业务逻辑方法
RequestEntity request = new RequestEntity();
try
{
List requstStoragePaths = new List();
using (IDbContext db = ObjectContainer.GetObject("db"))
{
rootDirectories = db.Query("select * from MINIO_STORAGE_PATH where CLEAR_FLAG='0' and FILE_TYPE='0' ORDER BY PK").ToList();//查询所有文件夹
foreach (var rootDir in rootDirectories)
{
if (rootDir.Upk == 0 && rootDir.FileLevel == 1)//只要根目录文件夹
{
RequstStoragePath rootModel = new RequstStoragePath();
rootModel.Pk = rootDir.Pk;//主键ID
rootModel.Upk = rootDir.Upk;//父级文件夹pk
rootModel.FileName = rootDir.FileName;//文件名称
rootModel.FilePath = rootDir.FilePath;//文件路径
rootModel.FileType = rootDir.FileType;//文件类型
rootModel.FileLevel = rootDir.FileLevel;//层级
rootModel.CreatorTime = rootDir.CreatorTime;//创建时间
rootModel.CreatorUser = rootDir.CreatorUser;//创建者
rootModel.DownloadNum = rootDir.DownloadNum;//下载次数
rootModel.ModifyUsers = rootDir.ModifyUsers;//修改用户
rootModel.ModifyTime = rootDir.ModifyTime;//修改时间
rootModel.ClearUsers = rootDir.ClearUsers;//删除用户
rootModel.ClearTime = rootDir.ClearTime;//删除时间
rootModel.ClearFlag = rootDir.ClearFlag;//删除标记 0未删除 1删除
rootModel.requstStoragePaths = bb.GetSubDirectories(rootDir.Pk);//子级存进list
requstStoragePaths.Add(rootModel);//添加list
}
}
}
request.data = requstStoragePaths;
request.code = "0";
}
catch (Exception)
{
request.code = "1";
throw;
}
return request;
}
///
/// 权限查询
///
///
///
///
///
[HttpGet, Route("GetEmpower")]
public RequestEntity GetEmpower(string UserID, string FileName, string EmpowerType)
{
RequestEntity request = new RequestEntity();
try
{
//用户ID,文件ID,文件操作类型(查看/移动)
using (IDbContext db = ObjectContainer.GetObject("db"))
{
string sql = string.Empty;
if (!string.IsNullOrEmpty(UserID))
{
sql += "and a.USER_ID='" + UserID + "'";
}
if (!string.IsNullOrEmpty(FileName))
{
sql += "and d.File_Name='" + FileName + "'";
}
if (!string.IsNullOrEmpty(EmpowerType))
{
if (EmpowerType == "下载/打印")
{
sql += "and b.PRINT_FLAG=1";
}
if (EmpowerType == "查看")
{
sql += "and b.VIEW_FLAG=1";
}
if (EmpowerType == "上传")
{
sql += "and b.UPLOAD_FLAG=1";
}
if (EmpowerType == "编辑")
{
sql += "and b.EDIT_FLAG=1";
}
if (EmpowerType == "移动")
{
sql += "and b.MOVE_FLAG=1";
}
if (EmpowerType == "删除")
{
sql += "and b.DELETE_FLAG=1";
}
if (EmpowerType == "文件夹查看")
{
sql += "and b.FILE_VIEW_PERMISSIONS=1";
}
if (EmpowerType == "文件夹控制")
{
sql += "and b.FILE_CONTROL_PERMISSIONS=1";
}
}
request.data = db.Query("select b.*,d.FILE_NAME,d.FILE_PATH from minio_user a left join minio_role_permission b on a.role_id=b.id left join minio_authority c on b.limits_authority_id=c.limits_authority_id left join minio_storage_path d on c.file_id=d.pk where 1=1 " + sql + " ORDER BY d.PK").ToList();
request.code = "0";
}
}
catch (Exception)
{
request.code = "1";
//throw;
}
//通过用户ID查询用户得角色ID,
//判断当前角色是否有操作权限
//通过角色ID查询用户有权限得文件夹
return request;
}
#endregion
#region minio文件下载及预览
///
/// 文件下载及预览接口
///
[HttpPost, Route("FileDownload")]
public RequestEntity FileDownload(List minioStoragePath)
{
MinioBusinessLogic bb = new MinioBusinessLogic();//定义业务逻辑方法
RequestEntity request = new RequestEntity();//定义返回集合
List data = new List();//定义输出下载的list
string NewFileName = "";
using (IDbContext db = ObjectContainer.GetObject("db"))
{
try
{
if (minioStoragePath != null)
{
foreach (var item in minioStoragePath)
{
MinioStoragePath date = new MinioStoragePath();//定义单条返回数据
string BuckName = bb.GetBuckName();//调用取桶名称方法
NewFileName = item.FileName;
if (item.Version != null && item.FileName.IndexOf(".pdf") > 0)//判断是否是pdf文件并且有版本做处理
{
NewFileName = item.FileName.Replace(".pdf", "_") + item.Version + ".pdf";
}
if (item.Version != null && item.FileName.IndexOf(".PDF") > 0)//判断是否是pdf文件并且有版本做处理
{
NewFileName = item.FileName.Replace(".PDF", "_") + item.Version + ".PDF";
}
var uploadTask = bb.Download(BuckName, NewFileName);//异步取base64
uploadTask.Wait();//等待取值
if (uploadTask.Status == TaskStatus.RanToCompletion && !uploadTask.IsFaulted) //判读是否有数值
{
date.Bate64 = uploadTask.Result;//赋值流文件
date.FileName = item.FileName;//赋值文件名称
data.Add(date);
if (item.OperationType == "0") //操作类型为下载
{
int DownloadNum = item.DownloadNum + 1;//将下载次数加一
string sql = "update MINIO_STORAGE_PATH set DOWNLOAD_NUM='" + DownloadNum + "' where pk='" + item.Pk + "'";
db.Execute(sql);//更新下载次数
}
request.code = "0";
}
else //如果不存在
{
request.msg = "请联系管理员检查!";
request.code = "1";
}
}
if (data != null)
{
request.data = data;//返回数据
}
}
else
{
request.msg = "请联系管理员检查!";
request.code = "1";
}
}
catch (Exception ex)
{
request.msg = "请联系管理员检查!";
request.code = "1";
logger.InfoFormat(ex.ToString());
}
}
return request;
}
#endregion
#region 操作日志接口
///
/// 查询minio文件操作日志
///
///
[HttpPost, Route("GetMinioBusinessLogic")]
public RequestEntity GetMinioBusinessLogic()
{
RequestEntity request = new RequestEntity();
try
{
using (IDbContext db = ObjectContainer.GetObject("db"))
{
request.data = db.Query("select * from MINIO_OPERATIONLOG where 1=1 order by OPERATE_TIME desc");
request.code = "0";
}
}
catch (Exception ex)
{
request.code = "1";
logger.ErrorFormat("主表:MINIO_OPERATIONLOG,查询minio文件操作日志表失败,错误原因:{0}", ex);
}
return request;
}
#endregion
#region 标准清单
///
/// 标准清单导出接口
///
[HttpPost, Route("StandardListGeneration")]
public HttpResponseMessage StandardListGeneration(StandardListGenerationModel master)
{
RequestEntity request = new RequestEntity();//定义返回集合
IList date = new List();
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
using (IDbContext db = ObjectContainer.GetObject("db"))
{
try
{
if (master.InventoryName != null && master.InventoryType != null)
{
string ExcelName = "";//定义导出Excel文件名称
FileStream file = new FileStream(Application.StartupPath + "\\Files\\" + "清单模板.xlsx", FileMode.Open, FileAccess.Read);
XSSFWorkbook hssfworkbook = new XSSFWorkbook(file);
string sql = "select * from MINIO_STORAGE_PATH where 1=1 and FILE_TYPE='1' ";
if (master.InventoryType == "0")//标准类型
{
if (!string.IsNullOrEmpty(master.BeginTime))
{
sql += string.Format(" and CREATE_TIME >= to_date('{0}', 'yyyy/mm/dd hh24:mi:ss')", master.BeginTime);//开始时间
}
if (!string.IsNullOrEmpty(master.EndTime))
{
sql += string.Format(" and CREATE_TIME <= to_date('{0}', 'yyyy/mm/dd hh24:mi:ss')", master.EndTime);//结束时间
}
sql += string.Format(" and FILE_PATH like'%{0}%'", master.InventoryName);//直接添加文件加名称
ExcelName = "标准清单";
}
else
{
#region 清单判断
if (master.InventoryName == "HY企业标准清单")
{
sql += "and upk='103'";
ExcelName = "HY企业标准清单";
}
if (master.InventoryName == "Q-HY企业标准清单")
{
sql += "and upk='106'";
ExcelName = "Q-HY企业标准清单";
}
if (master.InventoryName == "新产品标准清单")
{
//sql += "and (FILE_PATH like'%XYGN%' or FILE_PATH like'%XYGW%')";
sql += "and (upk='91' or upk='93')";
ExcelName = "新产品标准清单";
}
if (master.InventoryName == "标准目录有效清单")
{
sql += "and (FILE_PATH like'企业标准'";
ExcelName = "标准目录有效清单";
}
if (master.InventoryName == "企业标准明细表")
{
sql += "and (FILE_PATH like'企业标准'";
ExcelName = "企业标准明细表";
}
if (master.InventoryName == "过期标准清单")
{
sql += "and (FILE_PATH like'%过期标准%'";
ExcelName = "过期标准清单";
}
if (master.InventoryName == "参考标准清单")
{
sql += "and (FILE_PATH like'%参考标准%'";
ExcelName = "参考标准清单";
}
if (master.InventoryName == "工艺卡清单")
{
sql += "and (FILE_PATH like'%170钢管厂工艺卡%' or FILE_PATH like'%219钢管厂工艺卡%' or FILE_PATH like'%460钢管厂工艺卡%' or FILE_PATH like'%170热处理工艺卡%' or FILE_PATH like'%连模铸系列工艺卡/转炉厂%' or FILE_PATH like'%连模铸系列工艺卡/特冶厂%' or FILE_PATH like'%连模铸系列工艺卡/电炉厂%' or FILE_PATH like'%电炉厂工艺卡%' or FILE_PATH like'%转炉厂工艺卡%' or FILE_PATH like'%特冶厂工艺卡%' or FILE_PATH like'%大棒厂工艺卡%' or FILE_PATH like'%小棒厂工艺卡%' or FILE_PATH like'%中棒厂工艺卡%'or FILE_PATH like'%锻造厂工艺卡%' or FILE_PATH like'%扁棒厂工艺卡%')";
ExcelName = "工艺卡清单";
}
if (master.InventoryName == "工艺规程清单")
{
sql += "and FILE_PATH like'%工艺规程%'";
ExcelName = "工艺规程清单";
}
if (master.InventoryName == "技术制度清单")
{
sql += "and FILE_PATH like'%技术制度%'";
ExcelName = "技术制度清单";
}
if (master.InventoryName == "艺字工艺清单")
{
sql += "and FILE_PATH like'%艺字工艺%'";
ExcelName = "艺字工艺清单";
}
if (master.InventoryName == "技字通知清单")
{
sql += "and FILE_PATH like'%技字通知%'";
ExcelName = "技字通知清单";
}
#endregion
}
date = db.Query(string.Format("{0} order by UPK ASC, CREATOR_TIME ASC ", sql));
if (date.Count > 0)
{
request.data = date;//测试查看数据用后去去掉
var sheet = hssfworkbook.GetSheetAt(0);
for (int i = 0; i < date.Count; i++)
{
if (sheet != null)
{
sheet.CopyRow(2, 3 + i);
}
}
var rowIndex = 3;
for (int i = 0; i < date.Count; i++)
{
var curRow = sheet.GetRow(rowIndex);
var data = date[i];
if (data.FilePath.IndexOf("企业标准/XYGN") >= 0 || data.FilePath.IndexOf("企业标准/XYGW") >= 0)//对国内国外标准进行特殊处理
{
int Num = data.FileName.IndexOf("-") + 8;//取文件名称技术协议编号位数
int name = data.FileName.Length - Num;//取文件名称技术协议名称位数
curRow.GetCell(0).SetCellValue(data.FilePath.Replace(data.FileName, ""));//路径
curRow.GetCell(1).SetCellValue(data.FileName.Substring(0, Num).Trim());//协议编号
curRow.GetCell(2).SetCellValue(data.FileName.Substring(Num, name).Trim());//名称
}
if (data.FilePath.IndexOf("标准通知") >= 0)
{
int Num = data.FileName.IndexOf("号");//取文件名称编号位数
int name = data.FileName.Length - Num;//取文件名称名称位数
curRow.GetCell(0).SetCellValue(data.FilePath.Replace(data.FileName, ""));//路
curRow.GetCell(1).SetCellValue(data.FileName.Substring(0, Num).Trim());//编号
curRow.GetCell(2).SetCellValue(data.FileName.Substring(Num, name).Replace(":", "").Trim());//名称
}
if (data.FilePath.IndexOf("标准通知") == -1 && data.FilePath.IndexOf("企业标准/XYGN") == -1 || data.FilePath.IndexOf("企业标准/XYGW") == -1)
{
var filecontent = data.FileName.Split(' ');//以空格进行区分
curRow.GetCell(0).SetCellValue(data.FilePath.Replace(data.FileName, ""));//路径
curRow.GetCell(1).SetCellValue(filecontent[0].ToString().Trim());//编号
curRow.GetCell(2).SetCellValue(data.FileName.Replace(filecontent[0].ToString(), ""));//名称
}
rowIndex = rowIndex + 1;
}
MemoryStream output = new MemoryStream();
hssfworkbook.Write(output);
output.Seek(0, SeekOrigin.Begin);
result.Content = new StreamContent(output);
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
if (ExcelName != null)
{
result.Content.Headers.ContentDisposition.FileName = ExcelName + ".xlsx";
}
else
{
result.Content.Headers.ContentDisposition.FileName = "标准清单.xlsx";
}
result.Content.Headers.ContentLength = output.Length;
}
}
}
catch (Exception ex)
{
logger.InfoFormat(ex.ToString());
}
}
return result;
}
#endregion
#region 切片上传
///
/// 接收切片
///
[HttpPost, Route("SliceUpload")]
public async Task SliceUploadAsync()
{
Dictionary dic = new Dictionary();
SliceUploadModel master = new SliceUploadModel();
RequestEntity request = new RequestEntity();//定义返回集合
try
{
var multipartMemoryStreamProvider = await Request.Content.ReadAsMultipartAsync();
foreach (var content in multipartMemoryStreamProvider.Contents)
{
if (!string.IsNullOrEmpty(content.Headers.ContentDisposition.FileName))//判断是否是文件
{
using (Stream stream = await content.ReadAsStreamAsync())
{
if (stream.CanSeek) // 确保流支持查询长度和定位
{
long length = stream.Length;
byte[] bytes = new byte[length];
int offset = 0;
int bytesRead;
while ((bytesRead = stream.Read(bytes, offset, bytes.Length - offset)) > 0)
{
offset += bytesRead;
}
// bytes 数组现在包含了整个流的内容
master.FileBase64 = bytes;
}
}
}
else//否则就为其他字段
{
string val = await content.ReadAsStringAsync();
dic.Add(content.Headers.ContentDisposition.Name.Replace("\"", ""), System.Web.HttpUtility.UrlDecode(val));//写入缓存变量中
}
}
#region 字段赋值
master.FileName = dic["FileName"].ToString();//文件名称
master.SliceQuantity = dic["SliceQuantity"].ToString();//切片总数量
master.FileNum = dic["FileNum"].ToString();//文件序号
master.FileId = dic["FileId"].ToString();//文件唯一标识
master.Upk = Convert.ToInt32( dic["Upk"].ToString());//子文件夹的父ID
master.FileType = Convert.ToInt32(dic["FileType"].ToString());//文件类型
master.FilePath = dic["FilePath"].ToString();//文件路径
master.CreatorUser = dic["CreatorUser"].ToString();//创建者
master.DownloadNum = Convert.ToInt32(dic["DownloadNum"].ToString());//下载次数
master.FileLevel = Convert.ToInt32(dic["FileLevel"].ToString());//层级
master.FileCategory = dic["FileCategory"].ToString();//文件格式
#endregion
SliceUploadData.Add(master);//写入数据到全局变量缓存中
request.code = "0";
request.msg = "已接收切片";
}
catch (Exception ex)
{
request.code = "1";
logger.InfoFormat(ex.ToString());
}
return request;
}
///
/// 拼接上传切片文件
///
[HttpPost, Route("SliceUploadOrver")]
public RequestEntity SliceUploadOrver(SliceUploadModel master)
{
RequestEntity request = new RequestEntity();//定义返回集合
MinioBusinessLogic bb = new MinioBusinessLogic();//定义业务逻辑方法
using (IDbContext db = ObjectContainer.GetObject("db"))
{
try
{
#region 定义minio
MinioClient minio = bb.ConnectMinIO();//调用连接minio数据库方法
string BuckName = bb.GetBuckName();//调用取桶名称方法
var bucket = minio.BucketExistsAsync(BuckName);//判断桶是否存在
if (bucket.Result == false) //不存在就创建
{
minio.MakeBucketAsync(BuckName);//创建桶
}
#endregion
string Operate = "";//操作
MinioStoragePath date = new MinioStoragePath();
var file = SliceUploadData.Where(a => a.FileId == master.FileId).OrderBy(a => a.FileNum).ToList();//查询已经传完的唯一标识数据并以切片顺序排序
if (file.Count != 0)//如果查询数据不为0
{
if (file.Count.ToString() == master.SliceQuantity)//如果已经缓存的文件条数与总条数相等
{
List combinedList = new List();
foreach (var item in file)
{
combinedList.AddRange(item.FileBase64);
}
var contentType = file[0].FileCategory;//获取文件类型
byte[] bt = combinedList.ToArray();
var uploadTask = minio.PutObjectAsync(BuckName, "/" + file[0].FileName, new MemoryStream(bt), bt.Length, contentType);//上传文件到minio数据库
uploadTask.Wait();
if (uploadTask.Status == TaskStatus.RanToCompletion && !uploadTask.IsFaulted)//判断是否上传成功
{
#region 文件文件夹路径表字段赋值
date.Pk = Convert.ToInt32(db.QueryScalar("select S_MINIO_STORAGE_PATH.nextval from dual"));///拿到表自增长的值赋值给自增长主键pk
date.Upk = file[0].Upk;//子文件夹的父级id(如果是根目录默认父级id为0)
date.FileName = file[0].FileName;//文件名称
date.FileType = file[0].FileType;//文件类型
date.FilePath = file[0].FilePath;//文件路径
date.CreatorUser = file[0].CreatorUser;//创建者
date.CreatorTime = DateTime.Now;//创建时间
date.ClearFlag = "0";//删除状态为未删除
date.FileLevel = file[0].FileLevel + 1;//层级
date.DocumentType = file[0].DocumentType;//文件种类
#endregion
db.Insert(date);
request.msg = "文件上传成功!";
request.code = "0";
#region 记录日志
Operate = " 上传了一个新的文档,";
bb.MinioOperationLog(file[0].CreatorUser, file[0].FileName, Operate, date.Pk.ToString(), date.Upk.ToString(), db);
#endregion
SliceUploadData.RemoveAll(a => a.FileId == master.FileId);//清除已经上传文件的缓存
}
else
{
request.msg = "文件上传失败,请检查文件是否正确!";
request.code = "1";
}
}
else
{
SliceUploadData.RemoveAll(a => a.FileId == master.FileId);//清除已经上传文件的缓存
request.msg = "文件数量上传失败,请重试!";
request.code = "1";
}
}
}
catch (Exception ex)
{
request.code = "1";
logger.InfoFormat(ex.ToString());
}
}
return request;
}
#endregion
}
}