You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
117 lines
4.1 KiB
C#
117 lines
4.1 KiB
C#
using DS.Module.Core;
|
|
using DS.Module.SqlSugar;
|
|
using DS.Module.UserModule;
|
|
using DS.WMS.Core.Sys.Dtos;
|
|
using DS.WMS.Core.Sys.Entity;
|
|
using DS.WMS.Core.Sys.Interface;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using SqlSugar;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace DS.WMS.Core.Sys.Method
|
|
{
|
|
public class SysFileService : ISysFileService
|
|
{
|
|
private readonly IServiceProvider _serviceProvider;
|
|
private readonly ISqlSugarClient db;
|
|
private readonly IUser user;
|
|
private readonly IWebHostEnvironment _environment;
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="serviceProvider"></param>
|
|
public SysFileService(IServiceProvider serviceProvider)
|
|
{
|
|
_serviceProvider = serviceProvider;
|
|
db = _serviceProvider.GetRequiredService<ISqlSugarClient>();
|
|
user = _serviceProvider.GetRequiredService<IUser>();
|
|
_environment = _serviceProvider.GetRequiredService<IWebHostEnvironment>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 添加系统附件
|
|
/// </summary>
|
|
/// <param name="file"></param>
|
|
/// <param name="req"></param>
|
|
/// <returns></returns>
|
|
public async Task<DataResult<string>> AddFile(IFormFile file, [FromForm] SysFileReq req)
|
|
{
|
|
//未上传文件
|
|
if (file == null || file.Length == 0)
|
|
{
|
|
return await Task.FromResult(DataResult<string>.Failed("附件不存在!"));
|
|
}
|
|
|
|
var limitFiles = AppSetting.app(new string[] { "FileSettings", "FileType" });
|
|
var originalFilename = file.FileName; // 文件原始名称
|
|
var fileSuffix = Path.GetExtension(file.FileName).ToLower(); // 文件后缀
|
|
if (!limitFiles.Contains(fileSuffix))
|
|
{
|
|
return await Task.FromResult(DataResult<string>.Failed("不允许的文件类型!"));
|
|
}
|
|
var basePath = AppSetting.app(new string[] { "FileSettings", "BasePath" });
|
|
var relativePath = AppSetting.app(new string[] { "FileSettings", "RelativePath" });
|
|
var dirAbs = string.Empty;
|
|
var fileRelaPath = string.Empty;
|
|
var fileAbsPath = string.Empty;
|
|
if (string.IsNullOrEmpty(basePath))
|
|
{
|
|
dirAbs = Path.Combine(_environment.WebRootPath, relativePath);
|
|
}
|
|
else
|
|
{
|
|
dirAbs = Path.Combine(basePath, relativePath);
|
|
}
|
|
|
|
if (!Directory.Exists(dirAbs))
|
|
Directory.CreateDirectory(dirAbs);
|
|
|
|
|
|
// 先存库获取Id
|
|
var id = SnowFlakeSingle.Instance.NextId();
|
|
var fileSaveName = $"{id}{fileSuffix}".ToLower();
|
|
fileRelaPath = Path.Combine(relativePath, fileSaveName).ToLower();
|
|
fileAbsPath = Path.Combine(dirAbs, fileSaveName).ToLower();
|
|
var newFile = new SysFile
|
|
{
|
|
Id = id,
|
|
FileName = originalFilename,
|
|
FilePath = fileRelaPath,
|
|
TypeCode = req.TypeCode,
|
|
TypeName = req.TypeName,
|
|
LinkId = req.LinkId,
|
|
|
|
};
|
|
await db.Insertable(newFile).ExecuteCommandAsync();
|
|
|
|
using (var stream = File.Create(fileAbsPath))
|
|
{
|
|
await file.CopyToAsync(stream);
|
|
}
|
|
|
|
return await Task.FromResult(DataResult<string>.Success(fileRelaPath));
|
|
}
|
|
/// <summary>
|
|
/// 获取系统附件
|
|
/// </summary>
|
|
/// <param name="id">业务id</param>
|
|
/// <returns></returns>
|
|
public DataResult<List<SysFileRes>> GetSysFileList(string id)
|
|
{
|
|
|
|
var data = db.Queryable<SysFile>()
|
|
.Where(a => a.LinkId == long.Parse(id))
|
|
.Select<SysFileRes>()
|
|
.ToList();
|
|
return DataResult<List<SysFileRes>>.Success(data, MultiLanguageConst.DataQuerySuccess);
|
|
}
|
|
}
|
|
}
|