using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Furion.FriendlyException;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Myshipping.Application.Service.BookingSlot.Dto;
using Myshipping.Core;
using Myshipping.Core.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Myshipping.Application
{
///
/// 舱位需求预报服务
///
[ApiDescriptionSettings("Application", Name = "BookingSlotDemand", Order = 1)]
public class BookingSlotDemandService : IDynamicApiController, ITransient, IBookingSlotDemandService
{
private readonly ILogger _logger;
private readonly ISysCacheService _cache;
private readonly SqlSugarRepository _rep;
private readonly SqlSugarRepository _repCtn;
public BookingSlotDemandService(ILogger logger,
ISysCacheService cache,
SqlSugarRepository rep,
SqlSugarRepository repCtn)
{
_logger = logger;
_cache = cache;
_rep = rep;
_repCtn = repCtn;
}
///
/// 保存舱位需求预报
///
[HttpPost("/BookingSlotDemand/Save")]
public async Task Save(BookingSlotDemandInput input)
{
long id = 0;
if (input.Id > 0)
{
id = input.Id;
_rep.CurrentBeginTran();
try
{
var old = await _rep.FirstOrDefaultAsync(x => x.Id == input.Id);
if (old == null)
{
throw Oops.Bah("舱位需求预报不存在");
}
input.Adapt(old);
await _rep.UpdateAsync(old);
await _repCtn.DeleteAsync(x => x.DemandId == input.Id);
if (input.CtnList.Any() == true)
{
var config = new TypeAdapterConfig();
config.ForType().Map(dest => dest.DemandId, src => input.Id);
var ctnModels = input.CtnList.Adapt>(config);
await _repCtn.InsertAsync(ctnModels);
}
_rep.CurrentCommitTran();
}
catch (Exception)
{
_rep.CurrentRollbackTran();
throw;
}
}
else
{
_rep.CurrentBeginTran();
try
{
var model = input.Adapt();
await _rep.InsertAsync(model);
id = model.Id;
var config = new TypeAdapterConfig();
config.ForType().Map(dest => dest.DemandId, src => id);
var ctnModels = input.CtnList.Adapt>(config);
await _repCtn.InsertAsync(ctnModels);
_rep.CurrentCommitTran();
}
catch (Exception)
{
_rep.CurrentRollbackTran();
throw;
}
}
return await Detail(id);
}
///
/// 获取舱位需求预报详情
///
[HttpGet("/BookingSlotDemand/Detail")]
public async Task Detail([FromQuery] long id)
{
var model = await _rep.FirstOrDefaultAsync(x => x.Id == id);
var result = model.Adapt();
// 箱信息
var ctns = await _repCtn.ToListAsync(x => x.DemandId == id);
result.CtnList = ctns.Adapt>();
return result;
}
///
/// 分页查询舱位需求预报列表
///
[HttpPost("/BookingSlotDemand/Page")]
public async Task Page(BookingSlotDemandPageInput input)
{
var entities = await _rep.AsQueryable()
.WhereIF(input.CUSTOMERID != 0, x => x.CUSTOMERID == input.CUSTOMERID)
.WhereIF(!string.IsNullOrEmpty(input.REMARK), x => x.REMARK.Contains(input.REMARK))
.WhereIF(!string.IsNullOrEmpty(input.PORTLOADID), x => x.PORTLOADID == input.PORTLOADID)
.WhereIF(!string.IsNullOrEmpty(input.PORTDISCHARGEID), x => x.PORTDISCHARGEID == input.PORTDISCHARGEID)
.WhereIF(!string.IsNullOrEmpty(input.CARRIERID), x => x.CARRIERID == input.CARRIERID)
.WhereIF(input.ETD_START.HasValue, x => x.ETD >= input.ETD_START.Value)
.WhereIF(input.ETD_END.HasValue, x => x.ETD < input.ETD_END.Value.AddDays(1))
.WhereIF(input.ETA_START.HasValue, x => x.ETA >= input.ETA_START.Value)
.WhereIF(input.ETA_END.HasValue, x => x.ETA < input.ETA_END.Value.AddDays(1))
.ToPagedListAsync(input.PageNo, input.PageSize);
var result = entities.Adapt>();
return result.XnPagedResult();
}
}
}