From 18029a5771d12ccd60b128041415ba170731171d Mon Sep 17 00:00:00 2001 From: zhangxiaofeng Date: Thu, 11 Jan 2024 10:24:04 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=95=E5=85=A5=E8=AE=A2=E8=88=B1=EF=BC=9A?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E5=8F=AF=E7=94=A8=E7=9A=84=E8=88=B1=E4=BD=8D?= =?UTF-8?q?=E5=8F=8A=E7=AE=B1=E5=AD=90=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Service/BookingSlot/BookingSlotService.cs | 90 +++++++++++++++++++ .../BookingSlot/Dto/BookingSlotBaseDto.cs | 11 +++ 2 files changed, 101 insertions(+) diff --git a/Myshipping.Application/Service/BookingSlot/BookingSlotService.cs b/Myshipping.Application/Service/BookingSlot/BookingSlotService.cs index 497b8e6a..30d621bf 100644 --- a/Myshipping.Application/Service/BookingSlot/BookingSlotService.cs +++ b/Myshipping.Application/Service/BookingSlot/BookingSlotService.cs @@ -226,6 +226,96 @@ namespace Myshipping.Application //更新库存 await _publisher.PublishAsync(new ChannelEventSource("BookingSlotStock:Update", input)); } + + /// + /// 查询可用的舱位及箱子 + /// + [HttpGet("/BookingSlot/getAvailableSlots")] + public async Task> GetAvailableSlots([FromQuery] BookingSlotBaseDto input) + { + string[] ctnCodeList = null; + if (!string.IsNullOrEmpty(input.CTN_STAT)) + { + ctnCodeList = input.CTN_STAT.Split(','); + } + + // 1. 【舱位基础表】与【箱子表】做关联,并根据【舱位主键】、【箱型】做分组,统计出【总的箱量】,作为queryable1 + var queryable1 = _repBase.Context.Queryable((bas, ctn) => bas.Id == ctn.SLOT_ID) + .WhereIF(!string.IsNullOrEmpty(input.PORTLOAD), bas => bas.PORTLOAD.Contains(input.PORTLOAD)) + .WhereIF(!string.IsNullOrEmpty(input.PORTDISCHARGE), bas => bas.PORTLOAD.Contains(input.PORTLOAD)) + .WhereIF(!string.IsNullOrEmpty(input.VESSEL), bas => bas.VESSEL.Contains(input.VESSEL)) + .WhereIF(!string.IsNullOrEmpty(input.VOYNO), bas => bas.VOYNO.Contains(input.VOYNO)) + .WhereIF(!string.IsNullOrEmpty(input.CARRIAGE_TYPE), bas => bas.CARRIAGE_TYPE == input.CARRIAGE_TYPE) + .WhereIF(!string.IsNullOrEmpty(input.BOOKING_SLOT_TYPE), bas => bas.BOOKING_SLOT_TYPE == input.BOOKING_SLOT_TYPE) + .WhereIF(ctnCodeList != null, (bas, ctn) => ctnCodeList.Contains(ctn.CTNCODE)) + .GroupBy((bas, ctn) => new + { + bas.Id, + ctn.CTNCODE + }) + .Select((bas, ctn) => new + { + id = bas.Id, + ctnCode = ctn.CTNCODE, + numAll = SqlFunc.AggregateSum(ctn.CTNNUM) + }) + .MergeTable(); + + // 2. 【已引入舱位表】与【已使用的箱子表】做关联,并根据【舱位主键】、【箱型】做分组,统计出【已使用的箱量】,作为queryable2 + var queryable2 = _repBase.Context.Queryable((alc, ctn) => alc.Id == ctn.SLOT_ALLOC_ID) + .GroupBy((alc, ctn) => new + { + alc.BOOKING_SLOT_ID, + ctn.CTNCODE + }) + .Select((alc, ctn) => new + { + id = alc.BOOKING_SLOT_ID, + ctnCode = ctn.CTNCODE, + numUse = SqlFunc.AggregateSum(ctn.CTNNUM) + }) + .MergeTable(); + + // 3. 将queryable1 左连接 queryable2,使用【总的箱量】减去【已使用的箱量】,得到【剩余的箱量】,添加【剩余的箱量】> 0 的条件,作为queryable3 + var queryable3 = queryable1.LeftJoin(queryable2, (q1, q2) => q1.id == q2.id && q1.ctnCode == q2.ctnCode) + .Select((q1, q2) => new + { + q1.id, + q1.ctnCode, + numResidue = SqlFunc.IsNull(q1.numAll - q2.numUse, q1.numAll) + }) + .MergeTable() + .Where(r => r.numResidue > 0); + + // 4. 执行ToList(),得到可用的【舱位主键】、【箱型】、【箱量】列表 + var canUselist = await queryable3.ToListAsync(); + + // 查询舱位列表 + var baseIdList = canUselist.Select(c => c.id); + List baseList = await _repBase.AsQueryable() + .Where(u => baseIdList.Contains(u.Id)) + .ToListAsync(); + + List ctnCodeCache = await _cache.GetAllCodeCtn(); + + // 构建结果 + List result = baseList.Adapt>(); + foreach (var item in result) + { + var ctnList = canUselist.Where(c => c.id == item.Id).ToList(); + if (ctnList?.Any() == true) + { + item.CtnList = ctnList.Select(c => new BookingSlotCtnDto() + { + CTNCODE = c.ctnCode, + CTNNUM = c.numResidue, + CTNALL = ctnCodeCache.FirstOrDefault(e => e.Code == c.ctnCode)?.Name ?? throw new Exception($"舱位信息中存在未收录的箱型:{c.ctnCode},需要在箱型字典中补充"), + }).ToList(); + } + } + + return result; + } #endregion } } diff --git a/Myshipping.Application/Service/BookingSlot/Dto/BookingSlotBaseDto.cs b/Myshipping.Application/Service/BookingSlot/Dto/BookingSlotBaseDto.cs index 96117f33..fc87d325 100644 --- a/Myshipping.Application/Service/BookingSlot/Dto/BookingSlotBaseDto.cs +++ b/Myshipping.Application/Service/BookingSlot/Dto/BookingSlotBaseDto.cs @@ -298,4 +298,15 @@ namespace Myshipping.Application.Service.BookingSlot.Dto /// public string UpdatedUserName { get; set; } } + + /// + /// 查询可用舱位信息Dto + /// + public class BookingSlotAvailableDto: BookingSlotBaseDto + { + /// + /// 舱位箱信息 + /// + public List CtnList { get; set; } + } }