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/BookingSlot/BookingSlotDemandService.cs

195 lines
7.7 KiB
C#

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
{
/// <summary>
/// 舱位需求预报服务
/// </summary>
[ApiDescriptionSettings("Application", Name = "BookingSlotDemand", Order = 1)]
public class BookingSlotDemandService : IDynamicApiController, ITransient, IBookingSlotDemandService
{
private readonly ILogger<BookingSlotService> _logger;
private readonly ISysCacheService _cache;
private readonly SqlSugarRepository<BookingSlotDemand> _rep;
private readonly SqlSugarRepository<BookingSlotDemandCtn> _repCtn;
public BookingSlotDemandService(ILogger<BookingSlotService> logger,
ISysCacheService cache,
SqlSugarRepository<BookingSlotDemand> rep,
SqlSugarRepository<BookingSlotDemandCtn> repCtn)
{
_logger = logger;
_cache = cache;
_rep = rep;
_repCtn = repCtn;
}
/// <summary>
/// 保存舱位需求预报
/// </summary>
[HttpPost("/BookingSlotDemand/Save")]
public async Task<BookingSlotDemandOutput> 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<BookingSlotDemandCtnBaseDto, BookingSlotDemandCtn>().Map(dest => dest.DemandId, src => input.Id);
var ctnModels = input.CtnList.Adapt<List<BookingSlotDemandCtn>>(config);
await _repCtn.InsertAsync(ctnModels);
}
_rep.CurrentCommitTran();
}
catch (Exception)
{
_rep.CurrentRollbackTran();
throw;
}
}
else
{
_rep.CurrentBeginTran();
try
{
var model = input.Adapt<BookingSlotDemand>();
await _rep.InsertAsync(model);
id = model.Id;
var config = new TypeAdapterConfig();
config.ForType<BookingSlotDemandCtnBaseDto, BookingSlotDemandCtn>().Map(dest => dest.DemandId, src => id);
var ctnModels = input.CtnList.Adapt<List<BookingSlotDemandCtn>>(config);
await _repCtn.InsertAsync(ctnModels);
_rep.CurrentCommitTran();
}
catch (Exception)
{
_rep.CurrentRollbackTran();
throw;
}
}
return await Detail(id);
}
/// <summary>
/// 获取舱位需求预报详情
/// </summary>
[HttpGet("/BookingSlotDemand/Detail")]
public async Task<BookingSlotDemandOutput> Detail([FromQuery] long id)
{
var model = await _rep.FirstOrDefaultAsync(x => x.Id == id);
var result = model.Adapt<BookingSlotDemandOutput>();
// 箱信息
var ctns = await _repCtn.ToListAsync(x => x.DemandId == id);
result.CtnList = ctns.Adapt<List<BookingSlotDemandCtnBaseDto>>();
return result;
}
/// <summary>
/// 分页查询舱位需求预报列表
/// </summary>
[HttpPost("/BookingSlotDemand/Page")]
public async Task<dynamic> Page(BookingSlotDemandPageInput input)
{
var entities = await _rep.AsQueryable()
.WhereIF(input.CUSTOMERID != null && 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<SqlSugarPagedList<BookingSlotDemandOutput>>();
if (result.Items.Any())
{
var idList = result.Items.Select(x => x.Id);
var ctnList = (await _repCtn.ToListAsync(x => idList.Contains(x.DemandId))).Adapt<List<BookingSlotDemandCtnBaseDto>>();
var group = ctnList.GroupBy(x => x.DemandId);
foreach (var item in group)
{
var key = result.Items.FirstOrDefault(x => x.Id == item.Key);
if (key != null)
{
key.CtnList = item.ToList();
}
}
}
return result.XnPagedResult();
}
/// <summary>
/// 批量删除舱位需求预报列表
/// </summary>
/// <param name="Ids">使用,分隔的主键列表</param>
[HttpPost("/BookingSlotDemand/delete")]
public async Task Delete([FromQuery] string Ids)
{
var idArr = Ids.Split(',');
foreach (var idStr in idArr)
{
var id = Convert.ToInt64(idStr);
var slot = await _rep.FirstOrDefaultAsync(x => x.Id == id);
if (slot == null)
{
throw Oops.Oh("舱位需求记录不存在");
}
await _rep.UpdateAsync(x => x.Id == id, x => new BookingSlotDemand()
{
IsDeleted = true,
UpdatedTime = DateTime.Now,
UpdatedUserId = UserManager.UserId,
UpdatedUserName = UserManager.Name
});
await _repCtn.UpdateAsync(x => x.DemandId == id, x => new BookingSlotDemandCtn()
{
IsDeleted = true,
UpdatedTime = DateTime.Now,
UpdatedUserId = UserManager.UserId,
UpdatedUserName = UserManager.Name
});
}
}
}
}