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 != 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>(); if (result.Items.Any()) { var idList = result.Items.Select(x => x.Id); var ctnList = (await _repCtn.ToListAsync(x => idList.Contains(x.DemandId))).Adapt>(); 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(); } /// /// 批量删除舱位需求预报列表 /// /// 使用,分隔的主键列表 [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 }); } } } }