using JNPF.Common.Core.Manager; using JNPF.Common.Extension; using JNPF.Common.Filter; using JNPF.Common.Security; using JNPF.Systems.Entitys.System; using JNPF.VisualDev; using JNPF.VisualDev.Entitys.Dto.VisualDevModelData; using Mapster; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json.Linq; using SqlSugar; using Tnb.BasicData.Entities; using Tnb.WarehouseMgr.Entities; using Tnb.WarehouseMgr.Entities.Consts; using Tnb.WarehouseMgr.Entities.Dto; using Tnb.WarehouseMgr.Entities.Dto.ErpInputs; using Tnb.WarehouseMgr.Entities.Dto.Outputs; using Tnb.WarehouseMgr.Entities.Enums; namespace Tnb.WarehouseMgr { /// /// 库存报表服务类 /// [OverideVisualDev(ModuleConsts.MODULE_WMSCARRYSTOCKREPORT_ID)] public class WmsCarryStockReportService : BaseWareHouseService { private readonly ISqlSugarClient _db; private readonly IUserManager _userManager; public WmsCarryStockReportService(ISqlSugarRepository repository, IUserManager userManager) { _db = repository.AsSugarClient(); _userManager = userManager; } [HttpPost, AllowAnonymous] public async Task CarryStock(CarryStockInput input) { var warehouse_id = ""; var carry_code = ""; var material_code = ""; var container_no = ""; var code_batch = ""; var loc_usestatus = ""; if (!input.queryJson.IsNullOrWhiteSpace()) { warehouse_id = JObject.Parse(input.queryJson).Value(nameof(WmsCarryCode.warehouse_id)); carry_code = JObject.Parse(input.queryJson).Value(nameof(WmsCarryCode.carry_code)); material_code = JObject.Parse(input.queryJson).Value(nameof(WmsCarryCode.material_code)); code_batch = JObject.Parse(input.queryJson).Value(nameof(WmsCarryCode.code_batch)); container_no = JObject.Parse(input.queryJson).Value(nameof(WmsCarryCode.container_no)); loc_usestatus = JObject.Parse(input.queryJson).Value("loc_usestatus"); } List items = await _db.Queryable() .LeftJoin((a, b) => b.location_id == a.id) .InnerJoin((a, b, c) => a.wh_id == c.id) .LeftJoin((a, b, c, d) => d.id == b.carrystd_id) .LeftJoin((a, b, c, d, e) => e.carry_id == b.id) .LeftJoin((a, b, c, d, e, f) => f.id == e.material_id) .LeftJoin((a, b, c, d, e, f, g) => g.EnCode == f.unit_id && g.DictionaryTypeId == WmsWareHouseConst.UNITTYPEID) .Where((a, b, c, d, e) => ((!string.IsNullOrEmpty(b.carry_code) && b.carry_status != "6") || string.IsNullOrEmpty(b.carry_code))) .WhereIF(!string.IsNullOrEmpty(warehouse_id), (a, b, c, d) => c.id == warehouse_id) .WhereIF(!string.IsNullOrEmpty(carry_code), (a, b, c, d) => b.carry_code.Contains(carry_code)) .WhereIF(!string.IsNullOrEmpty(material_code), (a, b, c, d, e, f) => f.code.Contains(material_code)) .WhereIF(!string.IsNullOrEmpty(code_batch), (a, b, c, d, e, f) => e.code_batch.Contains(code_batch)) .WhereIF(!string.IsNullOrEmpty(container_no), (a, b, c, d, e, f) => f.material_standard.Contains(container_no)) .WhereIF(!string.IsNullOrEmpty(loc_usestatus), (a, b, c, d, e, f) => a.is_use == loc_usestatus) //.OrderByDescending((a, b, c, d, e, f) => b.carry_code) .Select((a, b, c, d, e, f, g) => new WmsCarryStockReport { warehouse_name = c.whname, carry_id = b.id, carry_code = a.is_use == "1" && string.IsNullOrEmpty(b.carry_code) && c.id == WmsWareHouseConst.WAREHOUSE_YCL_ID ? "空托盘堆垛" : b.carry_code, carry_name = a.is_use == "1" && string.IsNullOrEmpty(b.carry_code) && c.id == WmsWareHouseConst.WAREHOUSE_YCL_ID ? "空托盘堆垛" : b.carry_name, carry_status = b.is_lock == 1 ? "锁定" : "未锁定", carry_type = d.carrystd_name, location_code = a.location_code, location_name = a.location_name, location_status = a.is_lock == 1 ? "锁定" : "未锁定", instock_time = b.instock_time != null ? b.instock_time.Value.ToString("yyyy-MM-dd HH:mm:ss") : "", barcode = e.barcode, code_batch = e.code_batch, qty = e.codeqty, material_id = e.material_id, material_code = e.material_code, material_name = f.name, material_specification = f.material_specification, container_no = f.material_standard, unit = g.FullName, creator = e.create_id, bind_time = e.create_time != null ? e.create_time.Value.ToString("yyyy-MM-dd HH:mm:ss") : "", qc_res = SqlFunc.IF(e.qc_res.Equals("await") || string.IsNullOrEmpty(e.qc_res)).Return("待检").ElseIF(e.qc_res.Equals("vergeOk")).Return("让步接收").ElseIF(e.qc_res.Equals("ok")).Return("合格").ElseIF(e.qc_res.Equals("no")).Return("不合格").End(""), auxprop_gys = e.auxprop_gys, auxprop_xph = e.auxprop_xph, }) .OrderBy((a) => a.location_code) .ToListAsync(); // 料架 List items_LJ = await _db.Queryable() .LeftJoin((a, b) => b.location_id == a.id) .InnerJoin((a, b, c) => a.wh_id == c.id) .InnerJoin((a, b, c, d) => d.id == b.carrystd_id) .InnerJoin((a, b, c, d, e) => e.carry_id == b.id) .InnerJoin((a, b, c, d, e, f) => f.carry_id == e.membercarry_id) .InnerJoin((a, b, c, d, e, f, g) => g.id == f.material_id) .InnerJoin((a, b, c, d, e, f, g, h) => h.id == e.membercarry_id) .LeftJoin((a, b, c, d, e, f, g,h,i) => i.EnCode == f.unit_id && i.DictionaryTypeId == WmsWareHouseConst.UNITTYPEID) .Where((a, b, c, d, e) => b.carry_status != "6") .WhereIF(!string.IsNullOrEmpty(warehouse_id), (a, b, c, d) => c.id == warehouse_id) .WhereIF(!string.IsNullOrEmpty(carry_code), (a, b, c, d, e, f, g, h) => b.carry_code.Contains(carry_code) || h.carry_code.Contains(carry_code)) .WhereIF(!string.IsNullOrEmpty(material_code), (a, b, c, d, e, f, g) => g.code.Contains(material_code)) .WhereIF(!string.IsNullOrEmpty(code_batch), (a, b, c, d, e, f, g) => f.code_batch.Contains(code_batch)) .WhereIF(!string.IsNullOrEmpty(container_no), (a, b, c, d, e, f, g) => g.material_standard.Contains(container_no)) .WhereIF(!string.IsNullOrEmpty(loc_usestatus), (a, b, c, d, e, f) => a.is_use == loc_usestatus) .Select((a, b, c, d, e, f, g, h,i) => new WmsCarryStockReport { warehouse_name = c.whname, carry_id = b.id, carry_code = b.carry_code, carry_name = b.carry_name, membercarry_code = h.carry_code, membercarry_name = h.carry_name, carry_status = b.is_lock == 1 ? "锁定" : "未锁定", carry_type = d.carrystd_name, location_code = a.location_code, location_name = a.location_name, location_status = a.is_lock == 1 ? "锁定" : "未锁定", instock_time = b.instock_time != null ? b.instock_time.Value.ToString("yyyy-MM-dd HH:mm:ss") : "", barcode = f.barcode, code_batch = f.code_batch, qty = f.codeqty, material_id = f.material_id, material_code = f.material_code, material_name = g.name, material_specification = g.material_specification, container_no = g.material_standard, unit = i.FullName, creator = f.create_id, bind_time = f.create_time != null ? f.create_time.Value.ToString("yyyy-MM-dd HH:mm:ss") : "", qc_res = SqlFunc.IF(f.qc_res.Equals("await") || string.IsNullOrEmpty(f.qc_res)).Return("待检").ElseIF(f.qc_res.Equals("vergeOk")).Return("让步接收").ElseIF(f.qc_res.Equals("ok")).Return("合格").ElseIF(f.qc_res.Equals("no")).Return("不合格").End(""), auxprop_gys = f.auxprop_gys, auxprop_xph = f.auxprop_xph, }) .OrderBy((a) => a.location_code) .ToListAsync(); items.AddRange(items_LJ); // d.carrystd_name != "料架" var storeMap = items.DistinctBy(x => new { x.carry_id,x.location_code }).ToDictionary(x => new { x.carry_id, x.location_code }, x => x); IEnumerable result = items.GroupBy(g => new { g.carry_id, g.location_code }).Select(itGroup => { _ = storeMap.TryGetValue(itGroup.Key, out WmsCarryStockReport? report); WmsCarryStockReportH stockReport = report.Adapt(); if (report.carry_code != "空托盘堆垛") { List curCarryCodes = items.FindAll(x => !string.IsNullOrEmpty(x.carry_id) && x.carry_id == itGroup.Key.carry_id); List wmsCarryStockReportCodes = new List(); int index = 0; curCarryCodes.ForEach(x => { index++; WmsCarryStockReportCode wmsCarryStockReportCode = new(); wmsCarryStockReportCode.index = index; //wmsCarryStockReportCode.carry_id = x.carry_id; wmsCarryStockReportCode.carry_code = !string.IsNullOrEmpty(x.membercarry_code) ? x.membercarry_code : x.carry_code; wmsCarryStockReportCode.carry_name = !string.IsNullOrEmpty(x.membercarry_name) ? x.membercarry_name : x.carry_name; //wmsCarryStockReportCode.material_id = x.material_id; wmsCarryStockReportCode.material_code = x.material_code; wmsCarryStockReportCode.material_name = x.material_name; wmsCarryStockReportCode.barcode = x.barcode; wmsCarryStockReportCode.code_batch = x.code_batch; wmsCarryStockReportCode.qty = x.qty; wmsCarryStockReportCode.unit = x.unit; wmsCarryStockReportCode.material_specification = x.material_specification; wmsCarryStockReportCode.container_no = x.container_no; wmsCarryStockReportCode.qc_res = x.qc_res; wmsCarryStockReportCode.auxprop_gys = x.auxprop_gys; wmsCarryStockReportCode.auxprop_xph = x.auxprop_xph; //wmsCarryStockReportCode.创建用户 = x.操作用户; wmsCarryStockReportCode.bind_time = x.bind_time; wmsCarryStockReportCodes.Add(wmsCarryStockReportCode); }); if (input.showDetails == 1) { stockReport.Details = wmsCarryStockReportCodes; } } return stockReport; }); int rowIndex = 0; List pages = result.Skip((input.currentPage - 1) * input.pageSize).Take(input.pageSize).ToList(); pages.ForEach(page => { rowIndex++; page.index = rowIndex; }); SqlSugarPagedList pagedList = new() { list = pages, pagination = new() { CurrentPage = input.currentPage, PageSize = input.pageSize, Total = result.Count() } }; return PageResult.SqlSugarPageResult(pagedList); } [HttpPost, AllowAnonymous] public async Task CarryStockExcel(CarryStockInput input) { var warehouse_id = ""; var carry_code = ""; var material_code = ""; var container_no = ""; var code_batch = ""; if (!input.queryJson.IsNullOrWhiteSpace()) { warehouse_id = JObject.Parse(input.queryJson).Value(nameof(WmsCarryCode.warehouse_id)); carry_code = JObject.Parse(input.queryJson).Value(nameof(WmsCarryCode.carry_code)); material_code = JObject.Parse(input.queryJson).Value(nameof(WmsCarryCode.material_code)); code_batch = JObject.Parse(input.queryJson).Value(nameof(WmsCarryCode.code_batch)); container_no = JObject.Parse(input.queryJson).Value(nameof(WmsCarryCode.container_no)); } List items = await _db.Queryable() .LeftJoin((a, b) => b.location_id == a.id) .InnerJoin((a, b, c) => a.wh_id == c.id) .LeftJoin((a, b, c, d) => d.id == b.carrystd_id) .LeftJoin((a, b, c, d, e) => e.carry_id == b.id) .LeftJoin((a, b, c, d, e, f) => f.id == e.material_id) .Where((a, b, c, d, e) => ((!string.IsNullOrEmpty(b.carry_code) && b.carry_status != "0" && b.carry_status != "6") || string.IsNullOrEmpty(b.carry_code))) .WhereIF(!string.IsNullOrEmpty(warehouse_id), (a, b, c, d) => c.id == warehouse_id) .WhereIF(!string.IsNullOrEmpty(carry_code), (a, b, c, d) => b.carry_code.Contains(carry_code)) .WhereIF(!string.IsNullOrEmpty(material_code), (a, b, c, d, e, f) => e.material_code.Contains(material_code)) .WhereIF(!string.IsNullOrEmpty(code_batch), (a, b, c, d, e, f) => e.code_batch.Contains(code_batch)) .WhereIF(!string.IsNullOrEmpty(container_no), (a, b, c, d, e, f) => f.container_no.Contains(container_no)) .OrderByDescending((a, b, c, d, e, f) => b.carry_code) .Select((a, b, c, d, e, f) => new WmsCarryStockReport { warehouse_name = c.whname, carry_id = b.id, carry_code = a.is_use == "1" && string.IsNullOrEmpty(b.carry_code) && c.id == WmsWareHouseConst.WAREHOUSE_YCL_ID ? "空托盘堆垛" : b.carry_code, carry_name = a.is_use == "1" && string.IsNullOrEmpty(b.carry_code) && c.id == WmsWareHouseConst.WAREHOUSE_YCL_ID ? "空托盘堆垛" : b.carry_name, carry_status = b.is_lock == 1 ? "锁定" : "未锁定", carry_type = d.carrystd_name, location_code = a.location_code, location_name = a.location_name, location_status = a.is_lock == 1 ? "锁定" : "未锁定", instock_time = b.instock_time != null ? b.instock_time.Value.ToString("yyyy-MM-dd HH:mm:ss") : "", barcode = e.barcode, code_batch = e.code_batch, qty = e.codeqty, material_id = e.material_id, material_code = e.material_code, material_name = f.name, unit = e.unit_id, creator = e.create_id, bind_time = e.create_time != null ? e.create_time.Value.ToString("yyyy-MM-dd HH:mm:ss") : "" }) .OrderBy((a) => a.location_code) .ToListAsync(); // 料架 List items_LJ = await _db.Queryable() .LeftJoin((a, b) => b.location_id == a.id) .InnerJoin((a, b, c) => a.wh_id == c.id) .InnerJoin((a, b, c, d) => d.id == b.carrystd_id) .InnerJoin((a, b, c, d, e) => e.carry_id == b.id) .InnerJoin((a, b, c, d, e, f) => f.carry_id == e.membercarry_id) .InnerJoin((a, b, c, d, e, f, g) => g.id == f.material_id) .InnerJoin((a, b, c, d, e, f, g, h) => h.id == e.membercarry_id) .Where((a, b, c, d, e) => a.is_type == ((int)EnumLocationType.存储库位).ToString() && a.is_use == "1" && b.carry_status != "0" && b.carry_status != "6") .WhereIF(!string.IsNullOrEmpty(warehouse_id), (a, b, c, d) => c.id == warehouse_id) .WhereIF(!string.IsNullOrEmpty(carry_code), (a, b, c, d, e, f, g, h) => b.carry_code.Contains(carry_code) || h.carry_code.Contains(carry_code)) .WhereIF(!string.IsNullOrEmpty(material_code), (a, b, c, d, e, f, g) => g.code.Contains(material_code)) .WhereIF(!string.IsNullOrEmpty(code_batch), (a, b, c, d, e, f, g) => f.code_batch.Contains(code_batch)) .WhereIF(!string.IsNullOrEmpty(container_no), (a, b, c, d, e, f, g) => g.container_no.Contains(container_no)) .Select((a, b, c, d, e, f, g, h) => new WmsCarryStockReport { warehouse_name = c.whname, carry_id = b.id, carry_code = b.carry_code, carry_name = b.carry_name, membercarry_code = h.carry_code, membercarry_name = h.carry_name, carry_status = b.is_lock == 1 ? "锁定" : "未锁定", carry_type = d.carrystd_name, location_code = a.location_code, location_name = a.location_name, location_status = a.is_lock == 1 ? "锁定" : "未锁定", instock_time = b.instock_time != null ? b.instock_time.Value.ToString("yyyy-MM-dd HH:mm:ss") : "", barcode = f.barcode, code_batch = f.code_batch, qty = f.codeqty, material_id = f.material_id, material_code = f.material_code, material_name = g.name, unit = f.unit_id, creator = f.create_id, bind_time = f.create_time != null ? f.create_time.Value.ToString("yyyy-MM-dd HH:mm:ss") : "" }) .OrderBy((a) => a.location_code) .ToListAsync(); items.AddRange(items_LJ); // d.carrystd_name != "料架" List wmsCarryStockReportExcels = new List(); int index = 0; items.ForEach(x => { if (x.carry_code == "TP0599") { } index++; WmsCarryStockReportExcel wmsCarryStockReportCode = x.Adapt(); wmsCarryStockReportCode.index = index; //wmsCarryStockReportCode.carry_id = x.carry_id; wmsCarryStockReportCode.carry_code = !string.IsNullOrEmpty(x.membercarry_code) ? x.membercarry_code : x.carry_code; wmsCarryStockReportCode.carry_name = !string.IsNullOrEmpty(x.membercarry_name) ? x.membercarry_name : x.carry_name; //wmsCarryStockReportCode.material_id = x.material_id; wmsCarryStockReportCode.material_code = x.material_code; wmsCarryStockReportCode.material_name = x.material_name; wmsCarryStockReportCode.barcode = x.barcode; wmsCarryStockReportCode.code_batch = x.code_batch; wmsCarryStockReportCode.qty = x.qty; wmsCarryStockReportCode.unit = x.unit; //wmsCarryStockReportCode.创建用户 = x.操作用户; wmsCarryStockReportCode.bind_time = x.bind_time; wmsCarryStockReportExcels.Add(wmsCarryStockReportCode); }); int rowIndex = 0; List pages = wmsCarryStockReportExcels.Skip((input.currentPage - 1) * input.pageSize).Take(input.pageSize).ToList(); pages.ForEach(page => { rowIndex++; page.index = rowIndex; }); SqlSugarPagedList pagedList = new() { list = pages, pagination = new() { CurrentPage = input.currentPage, PageSize = input.pageSize, Total = wmsCarryStockReportExcels.Count() } }; return PageResult.SqlSugarPageResult(pagedList); } } }