|
|
|
|
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;
|
|
|
|
|
using Myshipping.Application.Enum;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using Myshipping.Application.Service.BookingTemplate.Dto;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using MiniExcelLibs;
|
|
|
|
|
using NPOI.HSSF.UserModel;
|
|
|
|
|
using Myshipping.Core.Helper;
|
|
|
|
|
using NPOI.SS.UserModel;
|
|
|
|
|
using Furion;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Web;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
namespace Myshipping.Application
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 订舱模板服务
|
|
|
|
|
/// </summary>
|
|
|
|
|
[ApiDescriptionSettings("Application", Name = "BookingTemplate", Order = 1)]
|
|
|
|
|
public class BookingTemplateService : IBookingTemplateService, IDynamicApiController, ITransient
|
|
|
|
|
{
|
|
|
|
|
private readonly SqlSugarRepository<BookingTemplate> _rep;
|
|
|
|
|
private readonly SqlSugarRepository<BookingOrder> _repOrder;
|
|
|
|
|
private readonly SqlSugarRepository<BookingExcelTemplate> _excelrep;
|
|
|
|
|
private readonly SqlSugarRepository<BookingExcelTemplateSub> _excelsubrep;
|
|
|
|
|
private readonly ILogger<BookingTemplate> _logger;
|
|
|
|
|
|
|
|
|
|
public BookingTemplateService(SqlSugarRepository<BookingTemplate> rep, SqlSugarRepository<BookingOrder> repOrder, SqlSugarRepository<BookingExcelTemplate> excelrep, SqlSugarRepository<BookingExcelTemplateSub> excelsubrep, ILogger<BookingTemplate> logger)
|
|
|
|
|
{
|
|
|
|
|
_excelrep = excelrep;
|
|
|
|
|
_excelsubrep = excelsubrep;
|
|
|
|
|
_repOrder = repOrder;
|
|
|
|
|
_rep = rep;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 分页查询订舱模板
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="input"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpGet("/BookingTemplate/page")]
|
|
|
|
|
public async Task<dynamic> Page([FromQuery] QueryBookingTemplateInput input)
|
|
|
|
|
{
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
//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)
|
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 下拉获取字段名称
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpGet("/BookingTemplate/GetFieldName")]
|
|
|
|
|
public async Task<dynamic> GetFieldName()
|
|
|
|
|
{
|
|
|
|
|
BookingOrder order = new BookingOrder();
|
|
|
|
|
BookingCtn ctn = new BookingCtn();
|
|
|
|
|
BookingEDIExt ext = new BookingEDIExt();
|
|
|
|
|
List<Dictionary<string, string>> list = new List<Dictionary<string, string>>();
|
|
|
|
|
|
|
|
|
|
////bookingorder
|
|
|
|
|
Dictionary<string, string> dic = new Dictionary<string, string>();
|
|
|
|
|
foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(order))
|
|
|
|
|
{
|
|
|
|
|
string name = descriptor.Name;
|
|
|
|
|
if (name == "TenantId" || name == "CreatedTime" || name == "UpdatedTime" || name == "CreatedUserId" || name == "CreatedUserName")
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(descriptor.Description))
|
|
|
|
|
{
|
|
|
|
|
dic.Add(descriptor.Name, descriptor.Description);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
list.Add(dic);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Dictionary<string, string> dic1 = new Dictionary<string, string>();
|
|
|
|
|
foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(ctn))
|
|
|
|
|
{
|
|
|
|
|
string name = descriptor.Name;
|
|
|
|
|
if (name == "TenantId" || name == "CreatedTime" || name == "UpdatedTime" || name == "CreatedUserId" || name == "CreatedUserName")
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(descriptor.Description))
|
|
|
|
|
{
|
|
|
|
|
dic1.Add(descriptor.Name, descriptor.Description);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
list.Add(dic1);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Dictionary<string, string> dic2 = new Dictionary<string, string>();
|
|
|
|
|
foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(ext))
|
|
|
|
|
{
|
|
|
|
|
string name = descriptor.Name;
|
|
|
|
|
if (name == "TenantId" || name == "CreatedTime" || name == "UpdatedTime" || name == "CreatedUserId" || name == "CreatedUserName")
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(descriptor.Description))
|
|
|
|
|
{
|
|
|
|
|
dic2.Add(descriptor.Name, descriptor.Description);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
list.Add(dic2);
|
|
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 新增编辑excel模板
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="dto"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpPost("/BookingTemplate/AddOrUpdateExcelTemplate")]
|
|
|
|
|
public async Task<dynamic> AddOrUpdateExcelTemplate(BookingExcelTemplateDto dto)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if (dto.Id == 0)
|
|
|
|
|
{
|
|
|
|
|
BookingExcelTemplate bookingExcel = new BookingExcelTemplate();
|
|
|
|
|
bookingExcel.Name = dto.Name;
|
|
|
|
|
bookingExcel.Type = dto.Type;
|
|
|
|
|
await _excelrep.InsertAsync(bookingExcel);
|
|
|
|
|
foreach (var item in dto.children)
|
|
|
|
|
{
|
|
|
|
|
var entity = item.Adapt<BookingExcelTemplateSub>();
|
|
|
|
|
entity.Pid = bookingExcel.Id;
|
|
|
|
|
await _excelsubrep.InsertAsync(entity);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var entity = dto.Adapt<BookingExcelTemplate>();
|
|
|
|
|
await _excelrep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
|
|
|
|
|
await _excelsubrep.DeleteAsync(x => x.Pid == dto.Id);
|
|
|
|
|
foreach (var item in dto.children)
|
|
|
|
|
{
|
|
|
|
|
var ent = item.Adapt<BookingExcelTemplateSub>();
|
|
|
|
|
ent.Pid = dto.Id;
|
|
|
|
|
await _excelsubrep.InsertAsync(ent);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取excel模板
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="Name">模板名称</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpGet("/BookingTemplate/BookingExcelTemplateList")]
|
|
|
|
|
public async Task<dynamic> BookingExcelTemplateList(string Name)
|
|
|
|
|
{
|
|
|
|
|
return await _excelrep.AsQueryable().WhereIF(!string.IsNullOrWhiteSpace(Name), x => x.Name.Contains(Name)).ToListAsync();
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取excel模板详情
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="Id">模板名称</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpGet("/BookingTemplate/BookingExcelTemplateDetailList")]
|
|
|
|
|
public async Task<dynamic> BookingExcelTemplateDetailList(long Id)
|
|
|
|
|
{
|
|
|
|
|
return await _excelsubrep.AsQueryable().Where(x => x.Pid == Id).ToListAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 导出excel
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="bookingId"></param>
|
|
|
|
|
/// <param name="excelTemplateId"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpGet("/BookingTemplate/ExportExcel")]
|
|
|
|
|
public async Task<dynamic> ExportExcel([FromQuery] long bookingId, long excelTemplateId)
|
|
|
|
|
{
|
|
|
|
|
if (bookingId == 0 || excelTemplateId == 0)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
throw Oops.Bah("请传入正确参数");
|
|
|
|
|
}
|
|
|
|
|
var excelwork = new HSSFWorkbook();
|
|
|
|
|
var sheet = excelwork.CreateSheet("订舱台账");
|
|
|
|
|
sheet.ForceFormulaRecalculation = true;
|
|
|
|
|
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++)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
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++)
|
|
|
|
|
{
|
|
|
|
|
if (entity.Where(x => x.Row == _row + 1 && x.Column == _cellNum + 1).Count() > 0)
|
|
|
|
|
{
|
|
|
|
|
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();
|
|
|
|
|
if (name == _name)
|
|
|
|
|
{
|
|
|
|
|
var value = descriptor.GetValue(order).ToString();
|
|
|
|
|
var cell = NpoiExcelExportHelper._.CreateCells(row, cellStyle, _cellNum + 1, value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
var opt = App.GetOptions<TempFileOptions>().Path;
|
|
|
|
|
var fileFullPath = Path.Combine(App.WebHostEnvironment.WebRootPath, opt);//服务器路径
|
|
|
|
|
if (!Directory.Exists(fileFullPath))
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(fileFullPath);
|
|
|
|
|
}
|
|
|
|
|
var fileName = string.Format("订舱{0}.xls", DateTime.Now.Ticks);
|
|
|
|
|
_logger.LogInformation("导出excel:" + Path.Combine(fileFullPath, fileName));
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|