using JNPF.Common.Core.Manager; using JNPF.Common.Extension; using JNPF.Common.Filter; using JNPF.Common.Security; using JNPF.VisualDev; using JNPF.VisualDev.Entitys.Dto.VisualDevModelData; using Mapster; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json.Linq; using SqlSugar; using Tnb.BasicData.Entities; using Tnb.WarehouseMgr.Entities; using Tnb.WarehouseMgr.Entities.Dto; using Tnb.WarehouseMgr.Entities.Enums; namespace Tnb.WarehouseMgr { /// /// 库存报表服务类 /// [OverideVisualDev(ModuleConsts.MODULE_WMSSTOCKREPORT_ID)] public class WmsStockReportService : BaseWareHouseService { private readonly ISqlSugarClient _db; private readonly IUserManager _userManager; public WmsStockReportService(ISqlSugarRepository repository, IUserManager userManager) { _db = repository.AsSugarClient(); _userManager = userManager; } [HttpPost] public async Task MaterialStock(VisualDevModelListQueryInput input) { var supplier_code = ""; var code_batch = ""; var material_specification = ""; if (!input.queryJson.IsNullOrWhiteSpace()) { supplier_code = JObject.Parse(input.queryJson).Value("supplier_code"); code_batch = JObject.Parse(input.queryJson).Value(nameof(WmsCarryCode.code_batch)); material_specification = JObject.Parse(input.queryJson).Value(nameof(WmsCarryCode.material_specification)); } IEnumerable result; if (string.IsNullOrEmpty(supplier_code)) { List items = await _db.Queryable().InnerJoin((a, b) => a.carry_id == b.id) .InnerJoin((a, b, c) => b.location_id == c.id).InnerJoin((a, b, c, d) => c.wh_id == d.id) .InnerJoin((a, b, c, d, e) => e.barcode == a.barcode) .InnerJoin((a, b, c, d, e, f) => f.id == e.material_id) .LeftJoin((a, b, c, d, e, f, g) => e.require_id == g.id) .LeftJoin((a, b, c, d, e, f, g, h) => h.id == g.bill_id) .WhereIF(!string.IsNullOrEmpty(material_specification), (a, b, c, d, e, f) => f.material_specification.Contains(material_specification)) .WhereIF(!string.IsNullOrEmpty(code_batch), (a, b, c, d, e, f) => a.code_batch.Contains(code_batch)) .Select((a, b, c, d, e, f, g, h) => new WmsStockReportH { org_id = e.org_id, warehouse_id = a.warehouse_id, warehouse_code = d.whcode, warehouse_name = d.whname, material_name = f.name, material_specification = f.material_specification, container_no = f.container_no, create_time = a.create_time, create_id = a.create_id, code_batch = a.code_batch, supplier_code = "", supplier_name = "", }, true).ToListAsync(); List carryCodes = await _db.Queryable().ToListAsync(); var storeMap = items.DistinctBy(x => new { x.warehouse_id, x.material_id, x.code_batch }).ToDictionary(x => new { x.warehouse_id, x.material_id, x.code_batch }, x => x); result = items.GroupBy(g => new { g.warehouse_id, g.material_id, g.code_batch }).Select(itGroup => { _ = storeMap.TryGetValue(itGroup.Key, out WmsStockReportH? stockReport); List curCarryCodes = carryCodes.FindAll(x => x.warehouse_id == itGroup.Key.warehouse_id && x.material_id == itGroup.Key.material_id && x.code_batch == itGroup.Key.code_batch); curCarryCodes.ForEach(x => { stockReport.codeqty += x.codeqty; }); //stockReport.Details = curCarryCodes.Adapt>(); //stockReport.codeqty = 0; //stockReport.Details.ForEach(x => //{ // x.id = SnowflakeIdHelper.NextId(); // x.report_id = stockReport.id; // x.warehouse_id = stockReport.warehouse_id; // x.warehouse_code = stockReport.warehouse_code; // x.warehouse_name = stockReport.warehouse_name; // x.material_specification = stockReport.material_specification; // x.container_no = stockReport.container_no; // stockReport.codeqty += x.codeqty; //}); return stockReport; }); } else { List items = await _db.Queryable().InnerJoin((a, b) => a.carry_id == b.id) .InnerJoin((a, b, c) => b.location_id == c.id).InnerJoin((a, b, c, d) => c.wh_id == d.id) .InnerJoin((a, b, c, d, e) => e.barcode == a.barcode) .InnerJoin((a, b, c, d, e, f) => f.id == e.material_id) .LeftJoin((a, b, c, d, e, f, g) => e.require_id == g.id) .LeftJoin((a, b, c, d, e, f, g, h) => h.id == g.bill_id) .WhereIF(!string.IsNullOrEmpty(material_specification), (a, b, c, d, e, f) => f.material_specification.Contains(material_specification)) .WhereIF(!string.IsNullOrEmpty(code_batch), (a, b, c, d, e, f) => a.code_batch.Contains(code_batch)) .WhereIF(!string.IsNullOrEmpty(supplier_code), (a, b, c, d, e, f, g, h) => h.supplier_code.Contains(supplier_code)) .Select((a, b, c, d, e, f, g, h) => new WmsStockReportH { org_id = e.org_id, warehouse_id = a.warehouse_id, warehouse_code = d.whcode, warehouse_name = d.whname, material_name = f.name, material_specification = f.material_specification, container_no = f.container_no, create_time = a.create_time, create_id = a.create_id, code_batch = a.code_batch, supplier_code = h.supplier_code, supplier_name = h.supplier_name, }, true).ToListAsync(); List carryCodes = await _db.Queryable().ToListAsync(); var storeMap = items.DistinctBy(x => new { x.warehouse_id, x.material_id, x.code_batch, x.supplier_code }).ToDictionary(x => new { x.warehouse_id, x.material_id, x.code_batch, x.supplier_code }, x => x); result = items.GroupBy(g => new { g.warehouse_id, g.material_id, g.code_batch, g.supplier_code }).Select(itGroup => { _ = storeMap.TryGetValue(itGroup.Key, out WmsStockReportH? stockReport); List curCarryCodes = carryCodes.Adapt>().FindAll(x => x.warehouse_id == itGroup.Key.warehouse_id && x.material_id == itGroup.Key.material_id && x.code_batch == itGroup.Key.code_batch && x.supplier_code == itGroup.Key.supplier_code); curCarryCodes.ForEach(x => { stockReport.codeqty += x.codeqty; }); return stockReport; }); } List pages = result.Skip((input.currentPage - 1) * input.pageSize).Take(input.pageSize).ToList(); SqlSugarPagedList pagedList = new() { list = pages, pagination = new() { CurrentPage = input.currentPage, PageSize = input.pageSize, Total = result.Count() } }; return PageResult.SqlSugarPageResult(pagedList); } } }