using Myshipping.Core;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System.Linq;
using System.Threading.Tasks;
using Myshipping.Application.Entity;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Http;
using Furion;
using Myshipping.Application.ConfigOption;
using System.IO;
using Yitter.IdGenerator;
using Furion.FriendlyException;
using Furion.RemoteRequest.Extensions;
using System.Text;
using System.Web;
using Myshipping.Application.Enum;
namespace Myshipping.Application
{
///
/// 订舱打印模板服务
///
[ApiDescriptionSettings("Application", Name = "BookingPrintTemplate", Order = 1)]
public class BookingPrintTemplateService : IBookingPrintTemplateService, IDynamicApiController, ITransient
{
private readonly SqlSugarRepository _rep;
private readonly ILogger _logger;
public BookingPrintTemplateService(SqlSugarRepository rep, ILogger logger)
{
_rep = rep;
_logger = logger;
}
///
/// 分页查询订舱打印模板
///
///
///
[HttpGet("/BookingPrintTemplate/page")]
public async Task Page([FromQuery] QueryBookingPrintTemplateInput input)
{
_rep.Context.QueryFilter.Clear();
var entities = await _rep.AsQueryable()
.WhereIF(!string.IsNullOrWhiteSpace(input.TypeName), u => u.TypeName.Contains(input.TypeName.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.FileName), u => u.FileName.Contains(input.FileName.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.TenantName), u => u.FileName.Contains(input.TenantName.Trim()))
.ToPagedListAsync(input.PageNo, input.PageSize);
return entities.XnPagedResult();
}
///
/// 增加订舱打印模板
///
///
///
[HttpPost("/BookingPrintTemplate/add")]
public async Task Add(IFormFile file, [FromForm] AddBookingPrintTemplateInput input)
{
//未上传打印模板文件
if (file == null || file.Length == 0)
{
throw Oops.Bah(BookingErrorCode.BOOK113);
}
var opt = App.GetOptions();
var originalFilename = file.FileName; // 文件原始名称
var fileSuffix = Path.GetExtension(file.FileName).ToLower(); // 文件后缀
if (!opt.fileType.Contains(fileSuffix))
{
throw Oops.Bah(BookingErrorCode.BOOK114);
}
var dirAbs = string.Empty;
if (string.IsNullOrEmpty(opt.basePath))
{
dirAbs = Path.Combine(App.WebHostEnvironment.WebRootPath, opt.relativePath);
}
else
{
dirAbs = Path.Combine(opt.basePath, opt.relativePath);
}
if (!Directory.Exists(dirAbs))
Directory.CreateDirectory(dirAbs);
// 先存库获取Id
var id = YitIdHelper.NextId();
var fileSaveName = $"{id}{fileSuffix}".ToLower();
var fileRelaPath = Path.Combine(opt.relativePath, fileSaveName).ToLower();
var fileAbsPath = Path.Combine(dirAbs, fileSaveName).ToLower();
var newFile = new BookingPrintTemplate
{
Id = id,
FileName = originalFilename,
FilePath = fileRelaPath,
TypeCode = input.TypeCode,
TypeName = input.TypeName,
TenantId = input.TenantId,
TenantName = input.TenantName,
};
await _rep.InsertAsync(newFile);
using (var stream = File.Create(fileAbsPath))
{
await file.CopyToAsync(stream);
}
}
///
/// 更新订舱打印模板
///
///
///
[HttpPost("/BookingPrintTemplate/edit")]
public async Task Update(IFormFile file, [FromForm] UpdateBookingPrintTemplateInput input)
{
//未上传打印模板文件
if (file == null || file.Length == 0)
{
throw Oops.Bah(BookingErrorCode.BOOK113);
}
var opt = App.GetOptions();
var originalFilename = file.FileName; // 文件原始名称
var fileSuffix = Path.GetExtension(file.FileName).ToLower(); // 文件后缀
if (!opt.fileType.Contains(fileSuffix))
{
throw Oops.Bah(BookingErrorCode.BOOK114);
}
_rep.Context.QueryFilter.Clear();
var entity = _rep.FirstOrDefault(x => x.Id == input.Id);
if (entity == null)
{
throw Oops.Bah(BookingErrorCode.BOOK112);
}
entity = input.Adapt(entity);
var dirAbs = string.Empty;
if (string.IsNullOrEmpty(opt.basePath))
{
dirAbs = Path.Combine(App.WebHostEnvironment.WebRootPath, opt.relativePath);
}
else
{
dirAbs = Path.Combine(opt.basePath, opt.relativePath);
}
if (!Directory.Exists(dirAbs))
Directory.CreateDirectory(dirAbs);
// 先存库获取Id
var fileSaveName = $"{entity.Id}{fileSuffix}".ToLower();
var fileRelaPath = Path.Combine(opt.relativePath, fileSaveName).ToLower();
var fileAbsPath = Path.Combine(dirAbs, fileSaveName).ToLower();
entity.FileName = originalFilename;
entity.FilePath = fileRelaPath;
await _rep.UpdateAsync(entity);
using (var stream = File.Create(fileAbsPath))
{
await file.CopyToAsync(stream);
}
}
///
/// 删除订舱打印模板
///
///
///
[HttpPost("/BookingPrintTemplate/delete")]
public async Task Delete(GetBookingPrintTemplateInput input)
{
_rep.Context.QueryFilter.Clear();
var entity = await _rep.FirstOrDefaultAsync(u => u.Id == input.Id);
await _rep.DeleteAsync(entity);
}
///
/// 获取订舱打印模板
///
///
///
[HttpGet("/BookingPrintTemplate/detail")]
public async Task Get([FromQuery] GetBookingPrintTemplateInput input)
{
return await _rep.FirstOrDefaultAsync(u => u.Id == input.Id);
}
/////
///// 获取订舱打印模板列表
/////
/////
/////
//[HttpGet("/BookingPrintTemplate/list")]
//public async Task List([FromQuery] QueryBookingPrintTemplateInput input)
//{
// return await _rep.ToListAsync();
//}
///
/// 下载模板文件
///
///
///
[HttpGet("/BookingPrintTemplate/download")]
public async Task Download(long id)
{
var printFile = await _rep.FirstOrDefaultAsync(u => u.Id == id);
if (printFile == null)
{
throw Oops.Bah(BookingErrorCode.BOOK115);
}
var opt = App.GetOptions();
var dirAbs = opt.basePath;
if (string.IsNullOrEmpty(dirAbs))
{
dirAbs = App.WebHostEnvironment.WebRootPath;
}
var fileFullPath = Path.Combine(dirAbs, printFile.FilePath);
if (!File.Exists(fileFullPath))
{
throw Oops.Bah(BookingErrorCode.BOOK115);
}
var fileName = HttpUtility.UrlEncode(printFile.FileName, Encoding.GetEncoding("UTF-8"));
var result = new FileStreamResult(new FileStream(fileFullPath, FileMode.Open), "application/octet-stream") { FileDownloadName = fileName };
return result;
}
}
}