using AngleSharp.Dom; using Dm; using Mapster; using Masuit.Tools; using NewLife.Reflection; using OfficeOpenXml.FormulaParsing.Excel.Functions.Math; using OfficeOpenXml.FormulaParsing.Excel.Functions.Text; using SqlSugar; using System.Collections; using System.Collections.Generic; using System.Drawing.Printing; using System.Linq.Expressions; using System.Threading.Tasks; using System.Xml.Linq; using WIDESEA_Cache; using WIDESEA_Common; using WIDESEA_Core; using WIDESEA_Core.Enums; using WIDESEA_Core.Helper; using WIDESEA_DTO; using WIDESEA_DTO.Basic; using WIDESEA_DTO.Stock; using WIDESEA_DTO.WMS; using WIDESEA_Model.Models; using WIDESEA_StorageTaskRepository; using JsonSerializer = System.Text.Json.JsonSerializer; namespace WIDESEA_StorageBasicService; public class StockInfoService : ServiceBase, IStockInfoService { private readonly LogFactory LogFactory = new LogFactory(); private readonly ILocationStatusChangeRecordRepository _locationStatusChangeRecordRepository; private readonly IUnitOfWorkManage _unitOfWorkManage; private readonly IStockInfoDetailRepository _stockInfoDetailRepository; private readonly IDt_TaskService _taskService; private readonly ILocationInfoRepository _locationRepository; private readonly IDt_TaskRepository _taskRepository; private readonly IDt_Task_HtyRepository _task_HtyRepository; public StockInfoService(IStockInfoRepository BaseDal, ILocationStatusChangeRecordRepository locationStatusChangeRecordRepository, IUnitOfWorkManage unitOfWorkManage, IStockInfoDetailRepository stockInfoDetailRepository, IDt_TaskService taskService, ILocationInfoRepository locationRepository, IDt_TaskRepository taskRepository, IDt_Task_HtyRepository task_HtyRepository) : base(BaseDal) { _locationStatusChangeRecordRepository = locationStatusChangeRecordRepository; _unitOfWorkManage = unitOfWorkManage; _stockInfoDetailRepository = stockInfoDetailRepository; _taskService = taskService; _locationRepository = locationRepository; _taskRepository = taskRepository; _task_HtyRepository = task_HtyRepository; } #region 方法重写 /// /// 分页 /// /// /// public override PageGridData GetPageData(PageDataOptions options) { string wheres = ValidatePageOptions(options); //获取排序字段 Dictionary orderbyDic = GetPageDataSort(options, TProperties); List orderByModels = new List(); foreach (var item in orderbyDic) { OrderByModel orderByModel = new() { FieldName = item.Key, OrderByType = item.Value }; orderByModels.Add(orderByModel); } int totalCount = 0; List searchParametersList = new List(); if (!string.IsNullOrEmpty(options.Wheres)) { try { searchParametersList = options.Wheres.DeserializeObject>(); options.Filter = searchParametersList; } catch { } } Expression> locationStatus = null; Expression> floor = null; Expression> areaId = null; Expression> materielCode = null; Expression> materielName = null; Expression> demandClassification = null; foreach (var item in searchParametersList) { if (item.Name.Contains("locationStatus")) { locationStatus = x => x.LocationInfo.LocationStatus == Convert.ToInt32(item.Value); } else if (item.Name.Contains("floor")) { floor = x => x.LocationInfo.Floor.Contains(item.Value); } else if (item.Name.Contains("areaId")) { areaId = x => x.LocationInfo.AreaId == Convert.ToInt32(item.Value); } else if (item.Name.Contains("materielCode")) { materielCode = x => x.StockInfoDetails.Any(d => d.MaterielCode.Contains(item.Value)); } else if (item.Name.Contains("materielName")) { materielName = x => x.StockInfoDetails.Any(d => d.MaterielName.Contains(item.Value)); } else if (item.Name.Contains("demandClassification")) { demandClassification = x => x.StockInfoDetails.Any(d => d.DemandClassification.Contains(item.Value)); } } //.IncludesAllFirstLayer() var data = BaseDal.Db.Queryable() .Includes(x => x.StockInfoDetails) .Includes(x => x.LocationInfo) .WhereIF(!wheres.IsNullOrEmpty(), wheres) .WhereIF(locationStatus != null, locationStatus) .WhereIF(floor != null, floor) .WhereIF(areaId != null, areaId) .WhereIF(materielCode != null, materielCode) .WhereIF(materielName != null, materielName) .WhereIF(demandClassification != null, demandClassification) .OrderBy(orderByModels) .ToPageList(options.Page, options.Rows, ref totalCount); new PageGridData(totalCount, data); return new PageGridData(totalCount, data); } public List DeserializeList(List> rawData) { if (rawData == null) return new List(); var processedData = rawData.Select(dict => { var newDict = new Dictionary(); foreach (var kvp in dict) { var newKey = char.ToUpper(kvp.Key[0]) + kvp.Key.Substring(1); newDict[newKey] = kvp.Value; } return newDict; }).ToList(); var json = JsonSerializer.Serialize(processedData); return JsonSerializer.Deserialize>(json); } #endregion #region 库存视图 public List GetStockSelectViews(GetStockSelectViewDto viewDto) { var stocks = BaseDal.Db.Queryable() .Includes(x => x.StockInfoDetails) .Includes(x => x.LocationInfo) .Where(x => x.LocationInfo.LocationStatus == (int)LocationEnum.InStock && x.LocationInfo.EnalbeStatus == (int)EnableEnum.Enable) .Where(x => x.StockInfoDetails.Any(d => d.MaterielCode == viewDto.materielCode && d.Quantity > 0 && d.Quantity > d.OutboundQuantity)).ToList().OrderBy(x => x.CreateDate); List locationInfos = new List(); List stockNew = new List(); if (stocks != null || stocks.Count() > 0) { var locations = stocks.Select(s => s.LocationInfo).ToList(); stockNew = stocks.Where(s => s.LocationInfo != null && locationInfos.Contains(s.LocationInfo)).ToList(); } var result = stockNew.Select(s => new StockSelectViewDTO { MaterielCode = s.StockInfoDetails .FirstOrDefault(d => d.MaterielCode == viewDto.materielCode)?.MaterielCode ?? string.Empty, MaterielName = s.StockInfoDetails .FirstOrDefault(d => d.MaterielCode == viewDto.materielCode)?.MaterielName ?? string.Empty, UseableQuantity = s.StockInfoDetails .Where(d => d.MaterielCode == viewDto.materielCode && d.Quantity > 0 && d.Quantity > d.OutboundQuantity) .Sum(d => (decimal?)d.Quantity - d.OutboundQuantity) ?? 0, // 处理空值情况 PalletCode = s.PalletCode ?? string.Empty, LocationCode = s.LocationInfo?.LocationCode ?? string.Empty }).ToList(); return result; } #endregion }