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/BookingTemplate/BookingTemplateService.cs

220 lines
8.4 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 Furion.FriendlyException;
2 years ago
using Myshipping.Application.Enum;
2 years ago
using System.ComponentModel;
using System.Collections.Generic;
using Myshipping.Application.Service.BookingTemplate.Dto;
2 years ago
using System.IO;
using MiniExcelLibs;
2 years ago
using NPOI.HSSF.UserModel;
using Myshipping.Core.Helper;
using NPOI.SS.UserModel;
using Furion;
using System;
using System.Web;
using System.Text;
2 years ago
using Myshipping.Application.ConfigOption;
namespace Myshipping.Application
{
/// <summary>
/// 订舱模板服务
/// </summary>
[ApiDescriptionSettings("Application", Name = "BookingTemplate", Order = 1)]
public class BookingTemplateService : IBookingTemplateService, IDynamicApiController, ITransient
{
private readonly SqlSugarRepository<BookingTemplate> _rep;
2 years ago
private readonly SqlSugarRepository<BookingOrder> _repOrder;
2 years ago
2 years ago
private readonly SqlSugarRepository<BookingPrintTemplate> _repPrintTemplate;
private readonly ILogger<BookingTemplate> _logger;
2 years ago
public BookingTemplateService(SqlSugarRepository<BookingTemplate> rep, SqlSugarRepository<BookingOrder> repOrder, SqlSugarRepository<BookingPrintTemplate> repPrintTemplate, ILogger<BookingTemplate> logger)
{
2 years ago
_excelrep = excelrep;
2 years ago
2 years ago
_repOrder = repOrder;
_rep = rep;
_logger = logger;
2 years ago
_repPrintTemplate = repPrintTemplate;
}
/// <summary>
/// 分页查询订舱模板
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpGet("/BookingTemplate/page")]
public async Task<dynamic> Page([FromQuery] QueryBookingTemplateInput input)
{
2 years ago
var entities = await _rep.AsQueryable()
.Where(m => m.CreatedUserId == UserManager.UserId)
.WhereIF(!string.IsNullOrEmpty(input.Title), u => u.Title.Contains(input.Title))
.WhereIF(!string.IsNullOrEmpty(input.Type), u => u.Type.Contains(input.Type))
.ToPagedListAsync(input.PageNo, input.PageSize);
return entities.XnPagedResult();
}
/// <summary>
/// 增加订舱模板
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("/BookingTemplate/add")]
public async Task Add(AddBookingTemplateInput input)
{
2 years ago
//var c = _rep.Count(x => x.Type.Contains(input.Type) && x.Title == input.Title);
//if (c > 0)
//{
// throw Oops.Oh(BookingErrorCode.BOOK101);
//}
var entity = input.Adapt<BookingTemplate>();
await _rep.InsertAsync(entity);
}
/// <summary>
/// 更新订舱模板
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("/BookingTemplate/edit")]
public async Task Update(UpdateBookingTemplateInput input)
{
var bt = _rep.FirstOrDefault(x => x.Id == input.Id);
if (bt == null)
{
2 years ago
throw Oops.Oh(BookingErrorCode.BOOK111);
}
input.Adapt(bt);
await _rep.AsUpdateable(bt).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
}
/// <summary>
/// 删除订舱模板
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("/BookingTemplate/delete")]
public async Task Delete(GetBookingTemplateInput input)
{
var entity = await _rep.FirstOrDefaultAsync(u => u.Id == input.Id);
await _rep.DeleteAsync(entity);
}
/// <summary>
/// 获取订舱模板
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpGet("/BookingTemplate/detail")]
public async Task<BookingTemplate> Get([FromQuery] GetBookingTemplateInput input)
{
return await _rep.FirstOrDefaultAsync(u => u.Id == input.Id);
}
2 years ago
2 years ago
2 years ago
2 years ago
/// <summary>
/// 导出excel
/// </summary>
/// <param name="bookingId"></param>
/// <param name="excelTemplateId"></param>
2 years ago
/// <param name="printTemplateId"></param>
2 years ago
/// <returns></returns>
2 years ago
[HttpGet("/BookingTemplate/ExportExcel")]
2 years ago
public async Task<dynamic> ExportExcel([FromQuery] long bookingId, long excelTemplateId,long printTemplateId)
2 years ago
{
2 years ago
if (bookingId == 0 || excelTemplateId == 0|| printTemplateId==0)
2 years ago
{
throw Oops.Bah("请传入正确参数");
}
2 years ago
var printTemplate = await _repPrintTemplate.AsQueryable().Filter(null, true).FirstAsync(x => x.Id== printTemplateId);
if (printTemplate == null)
{
throw Oops.Bah(BookingErrorCode.BOOK115);
}
var opt = App.GetOptions<PrintTemplateOptions>();
var dirAbs = opt.basePath;
if (string.IsNullOrEmpty(dirAbs))
{
dirAbs = App.WebHostEnvironment.WebRootPath;
}
var fileAbsPath = Path.Combine(dirAbs, printTemplate.FilePath);
_logger.LogInformation($"查找EXCEL模板文件{fileAbsPath}");
if (!File.Exists(fileAbsPath))
{
throw Oops.Bah(BookingErrorCode.BOOK115);
}
_logger.LogInformation($"准备调用EXCEL生成id{bookingId},文件:{printTemplate.FileName}");
var result = new FileStream(fileAbsPath, FileMode.Open) ;
var excelwork = new HSSFWorkbook(result);
var sheet = excelwork.GetSheetAt(0);
2 years ago
var order = await _repOrder.AsQueryable().Where(x => x.Id == bookingId).FirstAsync();
var entity = await _excelsubrep.AsQueryable().Where(x => x.Pid == excelTemplateId).OrderBy(x => x.Row).ToListAsync();
//单元格样式
var cellStyle = NpoiExcelExportHelper._.CreateStyle(excelwork, HorizontalAlignment.Center, VerticalAlignment.Center, 10, true, 0);
for (int _row = 0; _row < entity.Max(x => x.Row); _row++)
{
2 years ago
2 years ago
if (entity.Where(x => x.Row == (_row + 1)).Count() > 0)
{
///创建行
var row = NpoiExcelExportHelper._.CreateRow(sheet, _row, 25);
for (int _cellNum = 0; _cellNum < entity.Max(x => x.Column); _cellNum++)
{
2 years ago
if (entity.Where(x => x.Row == _row + 1 && x.Column == _cellNum + 1).Count() > 0)
{
2 years ago
foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(order))
{
var name = entity.Where(x => x.Row == _row + 1 && x.Column == _cellNum + 1).Select(x => x.Field).FirstOrDefault();
var _name = descriptor.Name.ToLower();
2 years ago
if (name == _name)
{
var value = descriptor.GetValue(order).ToString();
var cell = NpoiExcelExportHelper._.CreateCells(row, cellStyle, _cellNum + 1, value);
2 years ago
}
}
}
}
}
}
2 years ago
var fileFullPath = Path.Combine(App.WebHostEnvironment.WebRootPath, App.GetOptions<TempFileOptions>().Path);//服务器路径
2 years ago
if (!Directory.Exists(fileFullPath))
{
Directory.CreateDirectory(fileFullPath);
}
var fileName = string.Format("订舱{0}.xls", DateTime.Now.Ticks);
2 years ago
_logger.LogInformation("导出excel:" + Path.Combine(fileFullPath, fileName));
2 years ago
var filestream = new FileStream(Path.Combine(fileFullPath, fileName), FileMode.OpenOrCreate, FileAccess.ReadWrite);
excelwork.Write(filestream);
//var result = new FileStreamResult(new FileStream(Path.Combine(fileFullPath, fileName), FileMode.Open), "application/octet-stream") { FileDownloadName = fileName };
return fileName;
2 years ago
}
2 years ago
}
}