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;
using Myshipping.Application.ConfigOption;
namespace Myshipping.Application
{
///
/// 订舱模板服务
///
[ApiDescriptionSettings("Application", Name = "BookingTemplate", Order = 1)]
public class BookingTemplateService : IBookingTemplateService, IDynamicApiController, ITransient
{
private readonly SqlSugarRepository _rep;
private readonly SqlSugarRepository _repOrder;
private readonly SqlSugarRepository _repPrintTemplate;
private readonly SqlSugarRepository _repPrintTemplateShare;
private readonly ILogger _logger;
public BookingTemplateService(SqlSugarRepository rep, SqlSugarRepository repOrder, SqlSugarRepository repPrintTemplate, SqlSugarRepository repPrintTemplateShare,ILogger logger)
{
_repOrder = repOrder;
_rep = rep;
_logger = logger;
_repPrintTemplate = repPrintTemplate;
_repPrintTemplateShare = repPrintTemplateShare;
}
///
/// 分页查询订舱模板
///
///
///
[HttpGet("/BookingTemplate/page")]
public async Task 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();
}
///
/// 增加订舱模板
///
///
///
[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();
await _rep.InsertAsync(entity);
}
///
/// 更新订舱模板
///
///
///
[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();
}
///
/// 删除订舱模板
///
///
///
[HttpPost("/BookingTemplate/delete")]
public async Task Delete(GetBookingTemplateInput input)
{
var entity = await _rep.FirstOrDefaultAsync(u => u.Id == input.Id);
await _rep.DeleteAsync(entity);
}
///
/// 获取订舱模板
///
///
///
[HttpGet("/BookingTemplate/detail")]
public async Task Get([FromQuery] GetBookingTemplateInput input)
{
return await _rep.FirstOrDefaultAsync(u => u.Id == input.Id);
}
///
/// 新增编辑分享
///
///
///
[HttpPost("/BookingTemplate/SaveTemplateShare")]
public async Task SaveTemplateShare(List dto)
{
if (dto==null|| dto.Count==0) {
throw Oops.Oh("请上传正确数据");
}
_repPrintTemplateShare.Delete(x => x.TemplateId == dto[0].TemplateId);
foreach (var item in dto)
{
var entity = item.Adapt();
await _repPrintTemplateShare.InsertAsync(entity);
}
}
///
/// 获取分享列表
///
///
[HttpGet("/BookingTemplate/GetTemplateShareList")]
public async Task GetTemplateShareList(long Id)
{
var list = _repPrintTemplateShare.AsQueryable().Filter(null, true).Where(x => x.IsDeleted == false&&x.TemplateId==Id).ToList();
return list;
}
///
/// 删除分享
///
///
///
[HttpPost("/BookingTemplate/DeleteTemplateShare")]
public async Task DeleteTemplateShare(string Ids)
{
var arr = Ids.Split(",");
if (arr.Count() > 0)
{
foreach (var ar in arr)
{
long Id = Convert.ToInt64(ar);
var entity = await _repPrintTemplateShare.FirstOrDefaultAsync(u => u.Id == Id);
await _repPrintTemplateShare.DeleteAsync(entity);
}
}
}
///
/// 获取分享列表
///
///
[HttpGet("/BookingTemplate/GetTemplateShare")]
public async Task GetTemplateShare(string TemplateName,string Type)
{
var TemplateIds=_repPrintTemplateShare.AsQueryable().Filter(null, true).Where(x => x.IsDeleted == false && x.ShareToId == UserManager.UserId).Select(x => x.TemplateId).ToList();
var list =await _rep.AsQueryable().Filter(null, true).Where(x => x.IsDeleted == false && (TemplateIds.Contains(x.Id) || x.CreatedUserId == UserManager.UserId)).
WhereIF(!string.IsNullOrEmpty(TemplateName),x=>x.Title.Contains(TemplateName)).
WhereIF(!string.IsNullOrEmpty(Type), x => x.Type.Contains(Type)).ToListAsync();
return list;
}
}
}