using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Furion.FriendlyException;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Myshipping.Application.Entity;
using Myshipping.Core;
using Myshipping.Core.Service;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Myshipping.Application.Service
{
///
/// 自动订舱模板
///
[ApiDescriptionSettings("Application", Name = "BookingSoTemplate", Order = 1)]
public class BookingSoTemplateService : IDynamicApiController, ITransient
{
private readonly ILogger _logger;
private readonly ISysCacheService _cache;
private readonly SqlSugarRepository _rep;
public BookingSoTemplateService(ILogger logger,
ISysCacheService cache,
SqlSugarRepository rep)
{
_logger = logger;
_cache = cache;
_rep = rep;
}
///
/// 分页查询订舱模板
///
///
///
[HttpGet("/BookingSoTemplate/page")]
public async Task> Page([FromQuery] QueryBookingSoTemplateInput input)
{
var query = _rep.AsQueryable().Filter(null, true)
.Where(x => x.IsDeleted == false)
.WhereIF(!string.IsNullOrEmpty(input.CustName), u => u.CustName.Contains(input.CustName))
.WhereIF(!string.IsNullOrEmpty(input.Carrier), u => u.Carrier.Contains(input.Carrier))
.WhereIF(!string.IsNullOrEmpty(input.TemplateName), u => u.TemplateName.Contains(input.TemplateName))
.WhereIF(!string.IsNullOrEmpty(input.CarrierId), u => u.CarrierId == input.CarrierId);
if (!string.IsNullOrEmpty(input.SortField) || input.MultiSort == null || input.MultiSort.Count == 0)
{
query = query.OrderBy(PageInputOrder.OrderBuilder(input.SortField, input.DescSort));
}
else
{
query = query.OrderBy(PageInputOrder.MultiOrderBuilder(input.MultiSort));
}
var entities = await query.ToPagedListAsync(input.PageNo, input.PageSize);
return entities.Adapt>();
}
///
/// 保存订舱模板
///
///
///
[HttpPost("/BookingSoTemplate/save")]
public async Task Save(BookingSoTemplateSaveInput input)
{
BookingSoTemplate model = null;
//同一客户,同一船公司,只允许生效一个模板。
if (input.IsEnable)
{
var c = _rep.AsQueryable().Filter(null, true).Count(x => x.IsDeleted == false && x.CustName == input.CustName && x.CarrierId == input.CarrierId && x.IsEnable);
if (c > 0)
{
throw Oops.Bah($"客户:{input.CustName},船司:{input.Carrier} 已存在启用的模板");
}
}
if (input.Id > 0)
{
model = _rep.FirstOrDefault(x => x.Id == input.Id);
input.Adapt(model);
await _rep.UpdateAsync(model);
}
else
{
if (_rep.AsQueryable().Filter(null, true).Count(x => x.IsDeleted == false && x.CustName == input.CustName && x.CarrierId == input.CarrierId) > 0)
{
throw Oops.Bah($"客户:{input.CustName},船司:{input.Carrier} 已创建过模板");
}
model = input.Adapt();
await _rep.InsertAsync(model);
}
return model.Adapt();
}
///
/// 删除订舱模板
///
///
///
[HttpPost("/BookingSoTemplate/delete")]
public async Task Delete(long id)
{
var entity = await _rep.AsQueryable().Filter(null, true)
.FirstAsync(u => u.Id == id && u.IsDeleted == false);
if (entity == null)
{
throw Oops.Bah("未找到模板数据");
}
entity.IsDeleted = true;
await _rep.UpdateAsync(entity);
}
///
/// 获取订舱模板
///
///
///
[HttpGet("/BookingSoTemplate/detail")]
public async Task Get(long id)
{
var model = await _rep.AsQueryable().Filter(null, true)
.FirstAsync(u => u.Id == id && u.IsDeleted == false);
if (model == null)
{
throw Oops.Bah("未找到模板数据");
}
return model.Adapt();
}
}
}