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.
BookingHeChuan/Myshipping.Application/Service/BookingPrintTemplate/BookingPrintTemplateService.cs

306 lines
12 KiB
C#

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;
2 years ago
using Furion.RemoteRequest.Extensions;
using System.Text;
using System.Web;
2 years ago
using Myshipping.Application.Enum;
using System.Collections.Generic;
using Myshipping.Application.Service.BookingPrintTemplate.Dto;
using Myshipping.Core.Entity;
namespace Myshipping.Application
{
/// <summary>
/// 订舱打印模板服务
/// </summary>
[ApiDescriptionSettings("Application", Name = "BookingPrintTemplate", Order = 1)]
public class BookingPrintTemplateService : IBookingPrintTemplateService, IDynamicApiController, ITransient
{
private readonly SqlSugarRepository<BookingPrintTemplate> _rep;
private readonly SqlSugarRepository<BookingPrinttemplateRight> _repRight;
private readonly SqlSugarRepository<SysUser> _repUser;
private readonly ILogger<BookingPrintTemplate> _logger;
public BookingPrintTemplateService(SqlSugarRepository<BookingPrintTemplate> rep, SqlSugarRepository<BookingPrinttemplateRight> repRight, SqlSugarRepository<SysUser> repUser, ILogger<BookingPrintTemplate> logger)
{
_rep = rep;
_repRight = repRight;
_repUser = repUser;
_logger = logger;
}
/// <summary>
/// 分页查询订舱打印模板
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpGet("/BookingPrintTemplate/page")]
public async Task<dynamic> Page([FromQuery] QueryBookingPrintTemplateInput input)
{
var entities = await _rep.AsQueryable().Filter(null, true)
.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.TenantName.Contains(input.TenantName.Trim()))
.WhereIF(input.IsMain.HasValue, u => u.IsMain == input.IsMain)
.WhereIF(input.IsSub.HasValue, u => u.IsSub == input.IsSub)
.ToPagedListAsync(input.PageNo, input.PageSize);
return entities.XnPagedResult();
}
/// <summary>
/// 增加订舱打印模板
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("/BookingPrintTemplate/add")]
public async Task Add(IFormFile file, [FromForm] AddBookingPrintTemplateInput input)
{
//未上传打印模板文件
if (file == null || file.Length == 0)
{
2 years ago
throw Oops.Bah(BookingErrorCode.BOOK113);
}
var opt = App.GetOptions<PrintTemplateOptions>();
var originalFilename = file.FileName; // 文件原始名称
var fileSuffix = Path.GetExtension(file.FileName).ToLower(); // 文件后缀
if (!opt.fileType.Contains(fileSuffix))
{
2 years ago
throw Oops.Bah(BookingErrorCode.BOOK114);
}
var cc = _rep.AsQueryable().Filter(null, true).Count(c => c.TenantId == input.TenantId && c.TypeCode == input.TypeCode);
if (cc > 0)
{
throw Oops.Bah($"{input.TenantName} 已存在 {input.TypeName} 类型的打印模板");
}
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,
2 years ago
TypeCode = input.TypeCode,
TypeName = input.TypeName,
TenantId = input.TenantId,
TenantName = input.TenantName,
IsMain = input.IsMain,
IsSub = input.IsSub,
};
await _rep.InsertAsync(newFile);
using (var stream = File.Create(fileAbsPath))
{
await file.CopyToAsync(stream);
}
}
/// <summary>
/// 更新订舱打印模板
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("/BookingPrintTemplate/edit")]
public async Task Update(IFormFile file, [FromForm] UpdateBookingPrintTemplateInput input)
{
var cc = _rep.AsQueryable().Filter(null, true).Count(c => c.TenantId == input.TenantId && c.TypeCode == input.TypeCode && c.Id != input.Id);
if (cc > 0)
{
throw Oops.Bah($"{input.TenantName} 已存在 {input.TypeName} 类型的打印模板");
}
var entity = _rep.AsQueryable().Filter(null, true).First(x => x.Id == input.Id);
if (entity == null)
{
2 years ago
throw Oops.Bah(BookingErrorCode.BOOK112);
}
entity = input.Adapt(entity);
if (file != null)
{
var opt = App.GetOptions<PrintTemplateOptions>();
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 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;
using (var stream = File.Create(fileAbsPath))
{
await file.CopyToAsync(stream);
}
}
await _rep.UpdateAsync(entity);
}
/// <summary>
/// 删除订舱打印模板
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[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);
}
/// <summary>
/// 获取订舱打印模板
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpGet("/BookingPrintTemplate/detail")]
public async Task<BookingPrintTemplate> Get([FromQuery] GetBookingPrintTemplateInput input)
{
return await _rep.FirstOrDefaultAsync(u => u.Id == input.Id);
}
///// <summary>
///// 获取订舱打印模板列表
///// </summary>
///// <param name="input"></param>
///// <returns></returns>
//[HttpGet("/BookingPrintTemplate/list")]
//public async Task<dynamic> List([FromQuery] QueryBookingPrintTemplateInput input)
//{
// return await _rep.ToListAsync();
//}
2 years ago
/// <summary>
/// 下载模板文件
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet("/BookingPrintTemplate/download")]
public async Task<IActionResult> Download(long id)
{
var printFile = await _rep.FirstOrDefaultAsync(u => u.Id == id);
if (printFile == null)
{
2 years ago
throw Oops.Bah(BookingErrorCode.BOOK115);
}
var opt = App.GetOptions<PrintTemplateOptions>();
var dirAbs = opt.basePath;
if (string.IsNullOrEmpty(dirAbs))
{
dirAbs = App.WebHostEnvironment.WebRootPath;
}
var fileFullPath = Path.Combine(dirAbs, printFile.FilePath);
if (!File.Exists(fileFullPath))
{
2 years ago
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;
}
/// <summary>
/// 新增打印模板权限
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[SqlSugarUnitOfWork]
[HttpPost("/BookingPrintTemplate/AddPrinttemplateRight")]
public async Task AddPrinttemplateRight(BookingPrinttemplateRightDto input)
{
if (input == null)
{
throw Oops.Bah("未上传正确数据");
}
await _repRight.DeleteAsync(x => x.SysUserId == input.SysUserId);
foreach (var item in input.PrintTemplateId)
{
BookingPrinttemplateRight right = new BookingPrinttemplateRight();
right.SysUserId = input.SysUserId;
right.PrintTemplateId = item;
await _repRight.InsertAsync(right);
}
}
/// <summary>
/// 获取打印模板权限
/// </summary>
/// <returns></returns>
[HttpGet("/BookingPrintTemplate/GetPrinttemplateRightList")]
public async Task<dynamic> GetPrinttemplateRightList(long userId)
{
var list = await _repRight.AsQueryable().InnerJoin<BookingPrintTemplate>((d, t) => d.PrintTemplateId == t.Id && t.TenantId == UserManager.TENANT_ID).
2 years ago
WhereIF(userId != 0, d => d.SysUserId == userId).
Select((d, t) => new
{
TypeCode = t.TypeCode,
TypeName = t.TypeName,
FilePath = t.FilePath,
FileName = t.FileName,
TenantName = t.TenantName,
UserName = _repUser.Where(x => x.Id == d.SysUserId).Select(x => x.Name).FirstAsync().Result
}).ToListAsync();
return list;
}
}
}