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 IBKLinker_Minio.Common ;
using NPOI.POIFS.Crypt.Dsig ;
namespace IBKLinker_Minio.Controller.MinioController
{
/// <summary>
/// 作者:孙亮
/// 编写时间: 2023-08-15
/// 编写内容: mino上传接口
/// </summary>
[RoutePrefix("minio/minioapi")]
public class MinioApiController : ApiController
{
/// <summary>
/// 调用log日志文件
/// </summary>
private static readonly ILog logger = LogManager . GetLogger ( MethodBase . GetCurrentMethod ( ) . DeclaringType ) ;
#region minio权限表
/// <summary>
/// 添加minio权限表
/// </summary>
/// <param name="minioAuthority">权限表</param>
[HttpPost, Route("AddAuthority")]
public RequestEntity AddAuthority ( MinioAuthority minioAuthority )
{
RequestEntity request = new RequestEntity ( ) ;
try
{
///创建数据库连接上下文对象
using ( IDbContext db = ObjectContainer . GetObject < IDbContext > ( "db" ) )
{
///拿到表自增长的值赋值给自增长主键pk
minioAuthority . Id = getPk ( "S_MINIO_AUTHORITY" , "MINIO_AUTHORITY" ) ;
///给创建时间赋值
minioAuthority . CreatorTime = DateTime . Now ;
///把需要添加的数据同步到数据库
db . Insert < MinioAuthority > ( minioAuthority ) ;
request . code = "0" ;
}
}
catch ( Exception ex )
{
request . code = "1" ;
logger . ErrorFormat ( "主表:MINIO_AUTHORITY, minio权限表添加数据失败, 错误原因:{0}" , ex ) ;
}
return request ;
}
/// <summary>
/// 修改minio权限表
/// </summary>
/// <param name="data">修改数据</param>
[HttpPost, Route("UpdateAuthority")]
public RequestEntity UpdateAuthority ( List < MinioAuthority > data )
{
RequestEntity request = new RequestEntity ( ) ;
try
{
///创建数据库连接上下文对象
using ( IDbContext db = ObjectContainer . GetObject < IDbContext > ( "db" ) )
{
try
{
///启动事务
db . BeginTransaction ( ) ;
///给修改时间赋值
DateTime time = DateTime . Now ;
db . Update < MinioAuthority > ( 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 ;
}
/// <summary>
/// 删除minio权限表
/// </summary>
/// <param name="master">需要删除的数据</param>
[HttpPost, Route("RemoveAuthority")]
public RequestEntity RemoveAuthority ( List < MinioAuthority > master )
{
RequestEntity request = new RequestEntity ( ) ;
try
{
using ( IDbContext db = ObjectContainer . GetObject < IDbContext > ( "db" ) )
{
db . BeginTransaction ( ) ;
db . Delete < MinioAuthority > ( master ) ;
db . Commit ( ) ;
request . code = "0" ;
}
}
catch ( Exception ex )
{
request . code = "1" ;
logger . ErrorFormat ( "主表:MINIO_AUTHORITY, 创建数据库连接对象失败, 错误原因:{0}" , ex ) ;
}
return request ;
}
/// <summary>
/// 查询minio权限表
/// </summary>
/// <returns></returns>
[HttpPost, Route("GetAuthority")]
public RequestEntity GetAuthority ( MinioAuthority minioAuthority )
{
RequestEntity request = new RequestEntity ( ) ;
try
{
using ( IDbContext db = ObjectContainer . GetObject < IDbContext > ( "db" ) )
{
string sql = string . Empty ;
//IList<MinioAuthority> result = new List<MinioAuthority>();
if ( ! string . IsNullOrEmpty ( minioAuthority . LimitsAuthorityId ) )
{
sql + = " and LIMITS_AUTHORITY_ID ='" + minioAuthority . LimitsAuthorityId + "'" ;
}
request . data = db . Query < AuthorityModel > ( "select a.*,.Role_Name as RoleName from MINIO_AUTHORITY a left join MINIO_ROLE_PERMISSION b on a. LimitsAuthorityId=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用户表
/// <summary>
/// 添加minio角色表
/// </summary>
/// <param name="minioUser">角色表</param>
[HttpPost, Route("AddUSER")]
public RequestEntity AddUSER ( MinioUser minioUser )
{
RequestEntity request = new RequestEntity ( ) ;
try
{
///创建数据库连接上下文对象
using ( IDbContext db = ObjectContainer . GetObject < IDbContext > ( "db" ) )
{
///拿到表自增长的值赋值给自增长主键pk
//minioUser.UserId = getPk("S_MINIO_USER", "MINIO_USER");
///给创建时间赋值
minioUser . CreatorTime = DateTime . Now ;
///把需要添加的数据同步到数据库
db . Insert < MinioUser > ( minioUser ) ;
request . code = "0" ;
}
}
catch ( Exception ex )
{
request . code = "1" ;
logger . ErrorFormat ( "主表:MINIO_USER, minio角色表添加数据失败, 错误原因:{0}" , ex ) ;
}
return request ;
}
/// <summary>
/// 修改minio用户表
/// </summary>
/// <param name="data">修改数据</param>
[HttpPost, Route("UpdateUSER")]
public RequestEntity UpdateUSER ( List < MinioUser > data )
{
RequestEntity request = new RequestEntity ( ) ;
try
{
///创建数据库连接上下文对象
using ( IDbContext db = ObjectContainer . GetObject < IDbContext > ( "db" ) )
{
try
{
///启动事务
db . BeginTransaction ( ) ;
///给修改时间赋值
DateTime time = DateTime . Now ;
db . Update < MinioUser > ( 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 ;
}
/// <summary>
/// 删除minio角色表
/// </summary>
/// <param name="master">需要删除的数据</param>
[HttpPost, Route("RemoveUSER")]
public RequestEntity RemoveUSER ( List < MinioUser > master )
{
RequestEntity request = new RequestEntity ( ) ;
try
{
using ( IDbContext db = ObjectContainer . GetObject < IDbContext > ( "db" ) )
{
db . BeginTransaction ( ) ;
db . Delete < MinioUser > ( master ) ;
db . Commit ( ) ;
request . code = "0" ;
}
}
catch ( Exception ex )
{
request . code = "1" ;
logger . ErrorFormat ( "主表:MINIO_USER, 创建数据库连接对象失败, 错误原因:{0}" , ex ) ;
}
return request ;
}
/// <summary>
/// 查询minio用户表
/// </summary>
/// <returns></returns>
[HttpPost, Route("GetUSER")]
public RequestEntity GetUSER ( MinioUser minioUser )
{
RequestEntity request = new RequestEntity ( ) ;
try
{
using ( IDbContext db = ObjectContainer . GetObject < IDbContext > ( "db" ) )
{
string sql = string . Empty ;
//IList<MinioUser> result = new List<MinioUser>();
if ( ! string . IsNullOrEmpty ( minioUser . UserName ) )
{
sql + = " and a.USER_NAME ='" + 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 ( minioUser . RoleId > 0 )
{
sql + = " and a.ROLE_ID =" + minioUser . RoleId + "" ;
}
request . data = db . Query < UserModel > ( "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文件关联表
/// <summary>
/// 添加minio文件关联表
/// </summary>
/// <param name="minioFileassociation">文件关联表</param>
[HttpPost, Route("AddFileassociation")]
public RequestEntity AddFileassociation ( MinioFileassociation minioFileassociation )
{
RequestEntity request = new RequestEntity ( ) ;
using ( IDbContext db = ObjectContainer . GetObject < IDbContext > ( "db" ) )
{
try
{
db . BeginTransaction ( ) ; //启动事务
///创建数据库连接上下文对象
///拿到表自增长的值赋值给自增长主键pk
minioFileassociation . Pk = getPk ( "S_MINIO_FILEASSOCIATION" , "MINIO_FILEASSOCIATION" ) ;
///给创建时间赋值
minioFileassociation . CreatorTime = DateTime . Now ;
///把需要添加的数据同步到数据库
db . Insert < MinioFileassociation > ( minioFileassociation ) ;
db . Commit ( ) ; //提交事务
request . code = "0" ;
request . msg = "关联成功!" ;
}
catch ( Exception ex )
{
db . Rollback ( ) ; //回滚
request . code = "1" ;
logger . ErrorFormat ( "主表:MINIO_FILEASSOCIATION, minio文件关联表表添加数据失败, 错误原因:{0}" , ex ) ;
}
}
return request ;
}
/// <summary>
/// 修改minio文件关联表
/// </summary>
/// <param name="data">修改数据</param>
[HttpPost, Route("UpdateFileassociation")]
public RequestEntity UpdateFileassociation ( List < MinioFileassociation > data )
{
RequestEntity request = new RequestEntity ( ) ;
///创建数据库连接上下文对象
using ( IDbContext db = ObjectContainer . GetObject < IDbContext > ( "db" ) )
{
try
{
try
{
///启动事务
db . BeginTransaction ( ) ;
///给修改时间赋值
DateTime time = DateTime . Now ;
db . Update < MinioFileassociation > ( 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 ;
}
/// <summary>
/// 删除minio文件关联表
/// </summary>
/// <param name="master">需要删除的数据</param>
[HttpPost, Route("RemoveFileassociation")]
public RequestEntity RemoveFileassociation ( MinioFileassociation master )
{
RequestEntity request = new RequestEntity ( ) ;
///创建数据库连接上下文对象
using ( IDbContext db = ObjectContainer . GetObject < IDbContext > ( "db" ) )
{
try
{
db . BeginTransaction ( ) ; //启动事务
db . Delete < MinioFileassociation > ( master ) ;
db . Commit ( ) ; //提交事务
request . code = "0" ;
request . msg = "删除成功!" ;
}
catch ( Exception ex )
{
db . Rollback ( ) ; //回滚
request . code = "1" ;
logger . ErrorFormat ( "主表:MINIO_FILEASSOCIATION, 创建数据库连接对象失败, 错误原因:{0}" , ex ) ;
}
}
return request ;
}
/// <summary>
/// 查询minio文件关联表
/// </summary>
/// <returns></returns>
[HttpPost, Route("GetFileassociation")]
public RequestEntity GetFileassociation ( string FileName )
{
RequestEntity request = new RequestEntity ( ) ;
try
{
using ( IDbContext db = ObjectContainer . GetObject < IDbContext > ( "db" ) )
{
if ( FileName ! = null )
{
string sql = string . Empty ;
IList < MinioFileassociation > result = new List < MinioFileassociation > ( ) ;
if ( ! string . IsNullOrEmpty ( FileName . ToString ( ) ) )
{
sql + = string . Format ( " and FILE_NAME like'%{0}%' or ASSOCIATION_FILE_NAME like'%{0}%'" , FileName ) ;
}
request . data = db . Query < MinioFileassociation > ( "select * from MINIO_FILEASSOCIATION where 1=1 " + sql + " ORDER BY PK" ) ;
request . code = "0" ;
}
}
}
catch ( Exception ex )
{
request . code = "1" ;
logger . ErrorFormat ( "主表:MINIO_FILEASSOCIATION, 查询minio文件关联表表失败, 错误原因:{0}" , ex ) ;
}
return request ;
}
# endregion
#region minio角色权限表
/// <summary>
/// 添加minio角色权限表
/// </summary>
/// <param name="minioRolePermission">minio角色权限表</param>
[HttpPost, Route("AddRolePermission")]
public RequestEntity AddRolePermission ( RolePermissionModel minioRolePermission )
{
RequestEntity request = new RequestEntity ( ) ;
try
{
List < MinioAuthority > AuthorityList = new List < MinioAuthority > ( ) ;
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 . FileId = int . Parse ( item ) ;
AuthorityList . Add ( ds ) ;
}
///创建数据库连接上下文对象
using ( IDbContext db = ObjectContainer . GetObject < IDbContext > ( "db" ) )
{
db . BeginTransaction ( ) ;
///拿到表自增长的值赋值给自增长主键pk
minioRolePermission . minioRolePermission . Pk = getPk ( "S_MINIO_ROLE_PERMISSION" , "MINIO_ROLE_PERMISSION" ) ;
///给创建时间赋值
minioRolePermission . minioRolePermission . CreatorTime = DateTime . Now ;
///把需要添加的数据同步到数据库
db . Insert < MinioRolePermission > ( minioRolePermission . minioRolePermission ) ;
db . Insert < MinioAuthority > ( AuthorityList ) ;
db . Commit ( ) ;
request . code = "0" ;
}
}
catch ( Exception ex )
{
request . code = "1" ;
logger . ErrorFormat ( "主表:MINIO_ROLE_PERMISSION, minio角色权限表添加数据失败, 错误原因:{0}" , ex ) ;
}
return request ;
}
/// <summary>
/// 修改minio角色表
/// </summary>
/// <param name="data">修改数据</param>
[HttpPost, Route("UpdateRolePermission")]
public RequestEntity UpdateRolePermission ( RolePermissionModel minioRolePermission )
{
RequestEntity request = new RequestEntity ( ) ;
try
{
List < MinioAuthority > AuthorityList = new List < MinioAuthority > ( ) ;
//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 ) ;
AuthorityList . Add ( ds ) ;
}
///创建数据库连接上下文对象
using ( IDbContext db = ObjectContainer . GetObject < IDbContext > ( "db" ) )
{
try
{
///启动事务
db . BeginTransaction ( ) ;
///给修改时间赋值
//DateTime time = DateTime.Now;
db . Update < MinioRolePermission > ( minioRolePermission . minioRolePermission ) ;
//删除历史记录加入新的记录
db . Query ( "delete from MINIO_AUTHORITY where LIMITS_AUTHORITY_ID='" + minioRolePermission . minioRolePermission . LimitsAuthorityId + "' " ) ;
db . Insert < MinioAuthority > ( 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 ;
}
/// <summary>
/// 删除minio角色表
/// </summary>
/// <param name="master">需要删除的数据</param>
[HttpPost, Route("RemoveRolePermission")]
public RequestEntity RemoveRolePermission ( MinioRolePermission data )
{
RequestEntity request = new RequestEntity ( ) ;
IList < MinioUser > MUData = new List < MinioUser > ( ) ;
IList < MinioUser > UdpData = new List < MinioUser > ( ) ;
string sql = "" ;
using ( IDbContext db = ObjectContainer . GetObject < IDbContext > ( "db" ) )
{
try
{
if ( data ! = null )
{
MUData = db . Query < MinioUser > ( "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 = 0 ; //角色ID( 将角色id改成0)
date . CreatorUser = item . CreatorUser ; //创建人
date . State = item . State ; //状态
date . CreatorTime = item . CreatorTime ; //创建时间
UdpData . Add ( date ) ; //将数据加入list中
}
db . Update < MinioUser > ( UdpData ) ;
}
db . Query ( "delete from MINIO_AUTHORITY where LIMITS_AUTHORITY_ID='" + data . LimitsAuthorityId + "' " ) ;
sql = string . Format ( "delete from MINIO_ROLE_PERMISSION where 1=1 and PK={0}" , data . Pk ) ;
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 ;
}
/// <summary>
/// 查询minio角色表
/// </summary>
/// <returns></returns>
[HttpPost, Route("GetRolePermission")]
public RequestEntity GetRolePermission ( MinioRolePermission minioRolePermission )
{
RequestEntity request = new RequestEntity ( ) ;
try
{
using ( IDbContext db = ObjectContainer . GetObject < IDbContext > ( "db" ) )
{
string sql = string . Empty ;
IList < MinioRolePermission > result = new List < MinioRolePermission > ( ) ;
if ( ! string . IsNullOrEmpty ( minioRolePermission . Id ) )
{
sql + = " and ID ='" + minioRolePermission . Id + "'" ;
}
request . data = db . Query < MinioRolePermission > ( "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文件文件夹路径表
/// <summary>
/// 添加minio文件文件夹路径表
/// </summary>
/// <param name="minioStoragePath">minio文件文件夹路径表</param>
[HttpPost, Route("AddStoragePath")]
public RequestEntity AddStoragePath ( List < MinioStoragePath > minioStoragePath )
{
RequestEntity request = new RequestEntity ( ) ;
using ( IDbContext db = ObjectContainer . GetObject < IDbContext > ( "db" ) )
{
try
{
MinioClient minio = new MinioClient ( "172.15.88.212:9000" , "minioadmin" , "minioadmin" ) ; //连接所需minio所在地址、登录名、密码
string buckname = "dayetegang" ; //桶名称
List < MinioStoragePath > date = new List < MinioStoragePath > ( ) ;
List < MinioStoragePath > checkdatelist = new List < MinioStoragePath > ( ) ;
string RecordFileName = "" ; //重复文件名称记录
///创建数据库连接上下文对象
db . BeginTransaction ( ) ; //启动事务
if ( minioStoragePath ! = null )
{
foreach ( var items in minioStoragePath ) //先循环接收数据
{
checkdatelist = db . Query < MinioStoragePath > ( "select t.FILE_NAME from MINIO_STORAGE_PATH where 1=1 and FILE_NAME='" + items . FileName + "' and UPK='" + items . Upk + "'" ) . ToList ( ) ; //查询上传的文件或新建的文件夹在当前文件夹下是否有重复存在
if ( checkdatelist . Count > 0 ) //如果有
{
RecordFileName + = items . FileName + "," ; //记录名称
}
}
if ( RecordFileName = = null ) //如果没有正常上传
{
foreach ( var item in minioStoragePath )
{
MinioStoragePath data = new MinioStoragePath ( ) ;
MinioOperationlog MOdata = new MinioOperationlog ( ) ;
date = db . Query < MinioStoragePath > ( "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 )
{
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
db . Insert < MinioStoragePath > ( data ) ;
}
}
else
{
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 ; //层级
db . Insert < MinioStoragePath > ( data ) ;
}
#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 ) //判断是否上传成功
{
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 ; //层级
db . Insert < MinioStoragePath > ( data ) ;
request . msg = "文件上传成功!" ;
request . code = "0" ;
#region 记录日志
MOdata . Pk = Convert . ToInt32 ( db . QueryScalar ( "select S_MINIO_OPERATIONLOG.nextval from dual" ) ) ; ///拿到表自增长的值赋值给自增长主键pk;
MOdata . OperateUser = item . CreatorUser ; //操作者
MOdata . OperateTime = DateTime . Now ; //操作时间
MOdata . Operater = item . CreatorUser + "上传了一个新的文档," + item . FileName + DateTime . Now . ToString ( ) ; //操作
MOdata . CreationTime = DateTime . Now ; //创建时间
db . Insert < MinioOperationlog > ( MOdata ) ; //写入数据库
# 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 ;
}
/// <summary>
/// 修改minio文件文件夹路径
/// </summary>
/// <param name="data">修改数据</param>
[HttpPost, Route("UpdateStoragePath")]
public RequestEntity UpdateStoragePath ( List < MinioStoragePath > data )
{
RequestEntity request = new RequestEntity ( ) ;
try
{
///创建数据库连接上下文对象
using ( IDbContext db = ObjectContainer . GetObject < IDbContext > ( "db" ) )
{
try
{
///启动事务
db . BeginTransaction ( ) ;
///给修改时间赋值
DateTime time = DateTime . Now ; //取当前时间
MinioStoragePath date = new MinioStoragePath ( ) ;
List < MinioStoragePath > datebase = new List < MinioStoragePath > ( ) ;
foreach ( var item in data )
{
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删除
datebase . Add ( date ) ;
}
db . Update < MinioStoragePath > ( datebase ) ;
db . Commit ( ) ; //提交事务
request . code = "0" ;
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 ;
}
/// <summary>
/// 删除minio文件文件夹路径表
/// </summary>
/// <param name="master">需要删除的数据</param>
[HttpPost, Route("RemoveStoragePath")]
public RequestEntity RemoveStoragePath ( List < MinioStoragePath > master )
{
RequestEntity request = new RequestEntity ( ) ;
using ( IDbContext db = ObjectContainer . GetObject < IDbContext > ( "db" ) )
{
try
{
DateTime time = DateTime . Now ; //取当前时间
MinioClient minio = new MinioClient ( "172.15.88.212:9000" , "minioadmin" , "minioadmin" ) ;
string buckname = "dayetegang" ; //桶名称
db . BeginTransaction ( ) ;
string sql = "" ;
//db.Delete<MinioStoragePath>(master);
foreach ( var item in master )
{
if ( item . FileType = = 1 ) //如果类型是文件
{
sql = "begin " ;
sql + = string . Format ( "update MINIO_STORAGE_PATH set CLEAR_FLAG='1',CLEAR_USERS='{0}',CLEAR_TIME='{1}' where 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 ) ;
//minio.RemoveObjectAsync(buckname, item.FileName);//minio操作删除文件
}
else
{
string sel = string . Format ( "select * from MINIO_STORAGE_PATH where UPK='{0}'" , item . Pk ) ;
IList < MinioStoragePath > date = db . Query < MinioStoragePath > ( sel ) ; //查询删除文件夹时文件夹是否存在文件
if ( date . Count > 0 )
{
sql = "begin " ;
foreach ( var data in date )
{
sql + = string . Format ( "update MINIO_STORAGE_PATH set CLEAR_FLAG='1',CLEAR_USERS='{0}',CLEAR_TIME='{1}' where PK='{2}' " , item . ClearUsers , item . ClearTime , data . Pk ) ;
sql + = string . Format ( " delete from MINIO_FILEASSOCIATION where FILE_PK like'%{0}%' or ASSOCIATION_FILE_PK like'%{0}%';" , item . Pk ) ; //删除关联表中的关联关系
}
sql + = string . Format ( "update MINIO_STORAGE_PATH set CLEAR_FLAG='1',CLEAR_USERS='{0}',CLEAR_TIME='{1}' where PK='{2}' " , item . ClearUsers , time , item . Pk ) ; //更新文件路径表中的删除状态
}
else
{
sql = string . Format ( "update MINIO_STORAGE_PATH set CLEAR_FLAG='1',CLEAR_USERS='{0}',CLEAR_TIME='{1}' where PK='{2}' " , item . ClearUsers , time , item . Pk ) ; //更新文件路径表中的删除状态
}
db . Execute ( sql ) ;
}
}
request . code = "0" ;
request . msg = "删除成功" ;
db . Commit ( ) ;
}
catch ( Exception ex )
{
db . Rollback ( ) ;
request . code = "1" ;
logger . ErrorFormat ( "主表:MINIO_ROLE_PERMISSION, 创建数据库连接对象失败, 错误原因:{0}" , ex ) ;
}
}
return request ;
}
/// <summary>
/// 查询minio文件文件夹路径表
/// </summary>
/// <returns></returns>
[HttpPost, Route("GetStoragePath")]
public RequestEntity GetStoragePath ( string FileName )
{
RequestEntity request = new RequestEntity ( ) ;
try
{
using ( IDbContext db = ObjectContainer . GetObject < IDbContext > ( "db" ) )
{
string sql = string . Empty ;
IList < MinioStoragePath > result = new List < MinioStoragePath > ( ) ;
if ( ! string . IsNullOrEmpty ( FileName . ToString ( ) ) )
{
sql + = " and FILE_NAME like'%" + FileName + "%'" ;
}
request . data = db . Query < MinioStoragePath > ( "select * from MINIO_STORAGE_PATH where 1=1 " + sql + " and CLEAR_FLAG='0' ORDER BY PK" ) ;
request . code = "0" ;
}
}
catch ( Exception ex )
{
request . code = "1" ;
logger . ErrorFormat ( "主表:MINIO_STORAGE_PATH, 查询minio文件文件夹路径表失败, 错误原因:{0}" , ex ) ;
}
return request ;
}
# endregion
#region minio文件菜单递归和权限判断
/// <summary>
/// 根据ID查询子菜单
/// </summary>
/// <param name="PK"></param>
/// <returns></returns>
[HttpPost, Route("GetMenuTree")]
public List < MinioStoragePath > GetMenuTree ( string PK = "" )
{
List < MinioStoragePath > date = new List < MinioStoragePath > ( ) ;
using ( IDbContext db = ObjectContainer . GetObject < IDbContext > ( "db" ) )
{
string sql = string . Empty ;
if ( ! string . IsNullOrEmpty ( PK ) )
{
sql + = " and UPK =" + int . Parse ( PK ) ;
}
date = db . Query < MinioStoragePath > ( "select * from MINIO_STORAGE_PATH where 1=1 " + sql + "and CLEAR_FLAG='0' ORDER BY PK" ) . ToList ( ) ;
}
return date ;
}
/// <summary>
/// 获取根目录和根目录下一级得菜单信息
/// </summary>
/// <returns></returns>
[HttpPost, Route("GetRecursion")]
public RequestEntity GetRecursion ( )
{
RequestEntity request = new RequestEntity ( ) ;
try
{
//拿到根目录
List < RequstStoragePath > requstStoragePaths = new List < RequstStoragePath > ( ) ;
List < MinioStoragePath > date = new List < MinioStoragePath > ( ) ;
using ( IDbContext db = ObjectContainer . GetObject < IDbContext > ( "db" ) )
{
string sql = string . Empty ;
date = db . Query < MinioStoragePath > ( "select * from MINIO_STORAGE_PATH where UPK=0 AND FILE_LEVEL=1 and CLEAR_FLAG='0' ORDER BY PK" ) . ToList ( ) ;
}
if ( date . Count > 0 )
{
foreach ( var item in date )
{
RequstStoragePath model = new RequstStoragePath ( ) ;
model . Pk = item . Pk ;
model . FileName = item . FileName ;
model . FilePath = item . FilePath ;
model . FileType = item . FileType ;
model . FileLever = item . FileLevel ;
model . requstStoragePaths = new List < StoragePath > ( ) ;
List < MinioStoragePath > ds = GetMenuTree ( item . Pk . ToString ( ) ) ;
foreach ( var items in ds )
{
StoragePath models = new StoragePath ( ) ;
models . Pk = items . Pk ;
models . FileName = items . FileName ;
models . FilePath = items . FilePath ;
models . FileType = items . FileType ;
models . FileLever = items . FileLevel ;
model . requstStoragePaths . Add ( models ) ;
}
requstStoragePaths . Add ( model ) ;
}
}
request . data = requstStoragePaths ;
request . code = "0" ;
}
catch ( Exception )
{
request . code = "1" ;
throw ;
}
return request ;
}
/// <summary>
/// 权限查询
/// </summary>
/// <param name="UserID"></param>
/// <param name="FileName"></param>
/// <param name="EmpowerType"></param>
/// <returns></returns>
[HttpPost, Route("GetEmpower")]
public RequestEntity GetEmpower ( string UserID , string FileName , string EmpowerType )
{
RequestEntity request = new RequestEntity ( ) ;
try
{
//用户ID, 文件ID, 文件操作类型( 查看/移动)
using ( IDbContext db = ObjectContainer . GetObject < IDbContext > ( "db" ) )
{
string sql = string . Empty ;
if ( ! string . IsNullOrEmpty ( UserID ) )
{
sql + = "a.USER_ID=" + UserID ;
}
if ( ! string . IsNullOrEmpty ( FileName ) )
{
sql + = "d.File_Name='" + FileName + "'" ;
}
if ( ! string . IsNullOrEmpty ( EmpowerType ) )
{
if ( EmpowerType = = "打印" )
{
sql + = "b.PRINT_FLAG=1" ;
}
if ( EmpowerType = = "查看" )
{
sql + = "b.VIEW_FLAG=1" ;
}
if ( EmpowerType = = "上传" )
{
sql + = "b.UPLOAD_FLAG=1" ;
}
if ( EmpowerType = = "编辑" )
{
sql + = "b.EDIT_FLAG=1" ;
}
if ( EmpowerType = = "移动" )
{
sql + = "b.MOVE_FLAG=1" ;
}
if ( EmpowerType = = "删除" )
{
sql + = "b.DELETE_FLAG=1" ;
}
if ( EmpowerType = = "文件夹查看" )
{
sql + = "b.FILE_VIEW_PERMISSIONS=1" ;
}
if ( EmpowerType = = "文件夹控制" )
{
sql + = "b.FILE_CONTROL_PERMISSIONS=1" ;
}
}
request . data = db . Query < retqustUser > ( "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 a.creator_time" ) . 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 > minioStoragePath )
{
MinioBusinessLogic bb = new MinioBusinessLogic ( ) ; //定义业务逻辑方法
RequestEntity request = new RequestEntity ( ) ; //定义返回集合
List < MinioStoragePath > data = new List < MinioStoragePath > ( ) ; //定义输出下载的list
using ( IDbContext db = ObjectContainer . GetObject < IDbContext > ( "db" ) )
{
try
{
if ( minioStoragePath ! = null )
{
foreach ( var item in minioStoragePath )
{
MinioStoragePath date = new MinioStoragePath ( ) ; //定义单条返回数据
var minio = new MinioClient ( "172.15.88.212:9000" , "minioadmin" , "minioadmin" ) ; //链接minio数据库
string bucketName = "dayetegang" ;
var uploadTask = bb . Download ( bucketName , item . FileName ) ; //异步取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 标准清单
/// <summary>
/// 标准清单导出接口
/// </summary>
[HttpPost, Route("StandardListGeneration")]
public HttpResponseMessage StandardListGeneration ( StandardListGenerationModel master )
{
RequestEntity request = new RequestEntity ( ) ; //定义返回集合
IList < MinioStoragePath > date = new List < MinioStoragePath > ( ) ;
HttpResponseMessage result = new HttpResponseMessage ( HttpStatusCode . OK ) ;
using ( IDbContext db = ObjectContainer . GetObject < IDbContext > ( "db" ) )
{
try
{
if ( master . InventoryName ! = null & & master . InventoryType ! = null )
{
string ExcelName = "" ; //定义导出Excel文件名称
FileStream file = new FileStream ( "D:/linshi/" + "清单模板.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 FILE_PATH like'%HY%'" ;
ExcelName = "HY企业标准清单" ;
}
if ( master . InventoryName = = "Q-HY企业标准清单" )
{
sql + = "and (FILE_PATH like'%Q/HY%'" ;
ExcelName = "Q-HY企业标准清单" ;
}
if ( master . InventoryName = = "新产品标准清单" )
{
sql + = "and (FILE_PATH like'%XYGN%' or FILE_PATH like'%XYGW%')" ;
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 < MinioStoragePath > ( 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 公用方法
/// <summary>
/// 矫正pk值
/// </summary>
/// <param name="sequenceName"></param>
/// <param name="tableName"></param>
/// <returns></returns>
public Int32 getPk ( String sequenceName , String tableName )
{
///创建数据库连接上下文对象
using ( IDbContext db = ObjectContainer . GetObject < IDbContext > ( "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 ;
}
}
/// <summary>
/// 编写内容:下载文件转换流数据方法
/// </summary>
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
}
}