wangxinhui
昨天 a8756c3526832332db4ef5685348d9b188c2bf2b
´úÂë¹ÜÀí/WMS/WIDESEA_WMSServer/WIDESEA_StockService/StockViewService.cs
@@ -1,5 +1,7 @@

using HslCommunication.Secs.Types;
using Magicodes.ExporterAndImporter.Core;
using Magicodes.ExporterAndImporter.Excel;
using Microsoft.AspNetCore.Http;
using SqlSugar;
using System;
@@ -44,6 +46,220 @@
        {
            try
            {
                ISugarQueryable<Dt_StockInfo> sugarQueryable1 = _dbBase.Queryable<Dt_StockInfo>().Includes(x => x.Details);
                if (!string.IsNullOrEmpty(options.Wheres))
                {
                    try
                    {
                        List<SearchParameters> searchParametersList = options.Wheres.DeserializeObject<List<SearchParameters>>();
                        if (searchParametersList?.Any() == true)
                        {
                            foreach (var param in searchParametersList)
                            {
                                switch (param.Name)
                                {
                                    case var name when name == nameof(Dt_StockInfoDetail.MaterielCode).FirstLetterToLower():
                                        if (!string.IsNullOrEmpty(param.Value?.ToString()))
                                        {
                                            sugarQueryable1 = sugarQueryable1
                                                .Where(x => x.Details.Any(v => v.MaterielCode.Contains(param.Value.ToString())));
                                        }
                                        break;
                                    case var name when name == nameof(Dt_StockInfoDetail.BatchNo).FirstLetterToLower():
                                        if (!string.IsNullOrEmpty(param.Value?.ToString()))
                                        {
                                            sugarQueryable1 = sugarQueryable1
                                                .Where(x => x.Details.Any(v => v.BatchNo.Contains(param.Value.ToString())));
                                        }
                                        break;
                                    case var name when name == nameof(Dt_StockInfo.LocationCode).FirstLetterToLower():
                                        if (!string.IsNullOrEmpty(param.Value?.ToString()))
                                        {
                                            sugarQueryable1 = sugarQueryable1
                                                .Where(x => x.LocationCode == param.Value.ToString());
                                        }
                                        break;
                                    case var name when name == nameof(Dt_StockInfo.CreateDate).FirstLetterToLower():
                                        if (DateTime.TryParse(param.Value?.ToString(), out DateTime minDate))
                                        {
                                            LinqExpressionType expressionType = param.DisplayType.GetLinqCondition();
                                            if(expressionType == LinqExpressionType.ThanOrEqual)
                                            {
                                                sugarQueryable1 = sugarQueryable1.Where(x => x.CreateDate >= minDate);
                                            }
                                            else if(expressionType == LinqExpressionType.LessThanOrEqual)
                                            {
                                                sugarQueryable1 = sugarQueryable1.Where(x => x.CreateDate <= minDate);
                                            }
                                        }
                                        break;
                                    case var name when name == nameof(Dt_StockInfoDetail.EffectiveDate).FirstLetterToLower():
                                        // å‰ç«¯ä¼ é€’的参数需是 yyyy-MM-dd æ ¼å¼ï¼ˆå¦‚ "2025-05-08"),直接作为字符串处理
                                        string effectiveDateStr = param.Value?.ToString().Trim();
                                        if (!string.IsNullOrEmpty(effectiveDateStr) && effectiveDateStr.Length == 10)
                                        {
                                            LinqExpressionType expressionType = param.DisplayType.GetLinqCondition();
                                            if (expressionType == LinqExpressionType.ThanOrEqual)
                                            {
                                                sugarQueryable1 = sugarQueryable1.Where(x => x.Details
                                                    .Any(d =>!string.IsNullOrEmpty(d.EffectiveDate) && d.EffectiveDate.CompareTo(effectiveDateStr) >= 0));
                                            }
                                            else if (expressionType == LinqExpressionType.LessThanOrEqual)
                                            {
                                                sugarQueryable1 = sugarQueryable1.Where(x => x.Details
                                                    .Any(d =>!string.IsNullOrEmpty(d.EffectiveDate) && d.EffectiveDate.CompareTo(effectiveDateStr) <= 0));
                                            }
                                        }
                                        break;
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                    }
                }
                string whereSto = GetDataRole(typeof(Dt_StockInfo));
                sugarQueryable1 = sugarQueryable1.Where(whereSto);
                EntityProperties.ValidatePageOptions(options, ref sugarQueryable1);
                string whereLo = GetDataRole(typeof(Dt_LocationInfo));
                ISugarQueryable<Dt_LocationInfo> sugarQueryable = _dbBase.Queryable<Dt_LocationInfo>()
                    .Where(whereLo);
                EntityProperties.ValidatePageOptions(options, ref sugarQueryable);
                ISugarQueryable<StockViewDTO> list = sugarQueryable1
                    .InnerJoin(sugarQueryable, (b, a) => a.LocationCode == b.LocationCode)
                    .Select((b, a) => new StockViewDTO
                    {
                        LocationCode = b.LocationCode,
                        Expirationlabel = b.Expirationlabel,
                        Column = a.Column,
                        WarehouseId = b.WarehouseId,
                        CreateDate = b.CreateDate,
                        Creater = b.Creater,
                        Depth = a.Depth,
                        EnalbeStatus = a.EnableStatus,
                        Layer = a.Layer,
                        LocationName = a.LocationName,
                        LocationStatus = a.LocationStatus,
                        LocationType = a.LocationType,
                        Modifier = b.Modifier,
                        ModifyDate = b.ModifyDate,
                        PalletCode = b.PalletCode,
                        StockRemark = b.Remark,
                        RoadwayNo = a.RoadwayNo,
                        Row = a.Row,
                        StockId = b.Id,
                        StockStatus = b.StockStatus,
                        Details = b.Details,
                    });
                // å¹³åº“位数据查询
                var pklist = sugarQueryable1
                    .Where(b => b.LocationCode == "平库位")
                    .Select(b => new StockViewDTO
                    {
                        LocationCode = b.LocationCode,
                        Column = 0,
                        WarehouseId = b.WarehouseId,
                        CreateDate = b.CreateDate,
                        Expirationlabel = b.Expirationlabel,
                        Creater = b.Creater,
                        Depth = 0,
                        EnalbeStatus = EnableStatusEnum.Normal.ObjToInt(),
                        Layer = 0,
                        LocationName = "平库位",
                        LocationStatus = LocationStatusEnum.InStock.ObjToInt(),
                        LocationType = LocationTypeEnum.Undefined.ObjToInt(),
                        Modifier = b.Modifier,
                        ModifyDate = b.ModifyDate,
                        PalletCode = b.PalletCode,
                        StockRemark = b.Remark,
                        RoadwayNo = "平库",
                        Row = 0,
                        StockId = b.Id,
                        StockStatus = b.StockStatus,
                        Details = b.Details,
                    });
                int totalCount = 0;
                int pktotalCount = 0;
                var stockViewDTOs = list.ToPageList(options.Page, options.Rows, ref totalCount);
                stockViewDTOs.AddRange(pklist.ToPageList(options.Page, options.Rows, ref pktotalCount));
                stockViewDTOs.ForEach(x =>
                {
                    x.MaterielCode = string.Join(",", x.Details.Select(d => d.MaterielCode).Distinct());
                    x.BatchNo = string.Join(",", x.Details.Select(d => d.BatchNo).Distinct());
                    x.MaterielSpec = x.Details.FirstOrDefault()?.MaterielSpec ?? "";
                    x.MaterielName = x.Details.FirstOrDefault()?.MaterielName ?? "";
                });
                return new PageGridData<StockViewDTO>(totalCount + pktotalCount, stockViewDTOs);
            }
            catch (Exception ex)
            {
            }
            return new PageGridData<StockViewDTO>();
        }
        public virtual object GetDetailPage(PageDataOptions pageData)
        {
            Type t = typeof(StockViewDTO);
            if (pageData.Value == null) return new PageGridData<object>(total: 0, null);
            string keyName = t.GetKeyName();
            ////生成查询条件
            //Expression<Func<TEntity, bool>> whereExpression = keyName.CreateExpression<TEntity>(pageData.Value, LinqExpressionType.Equal);
            int totalCount = 0;
            PropertyInfo? propertyInfo = t.GetProperties().FirstOrDefault(x => x.GetCustomAttribute<Navigate>() != null);
            if (propertyInfo != null)
            {
                Type detailType = propertyInfo.PropertyType.GetGenericArguments()[0];
                Navigate? navigate = propertyInfo.GetCustomAttribute<Navigate>();
                if (navigate != null)
                {
                    List<Dt_StockInfoDetail> list = _dbBase.Queryable<Dt_StockInfoDetail>().Where(navigate.GetName(), "=", pageData.Value).ToPageList(pageData.Page, pageData.Rows, ref totalCount);
                    decimal sum = 0;
                    list.ForEach(x =>
                    {
                        sum += Convert.ToDecimal(x.StockQuantity);
                    });
                    PageGridData<Dt_StockInfoDetail> pageGridData = new PageGridData<Dt_StockInfoDetail>()
                    {
                        Total = totalCount,
                        Rows = list,
                        Summary = new
                        {
                            StockQuantity = Math.Round(sum, 3),
                        }
                    };
                    return pageGridData;
                }
            }
            return new PageGridData<object>(total: 0, null);
        }
        public virtual WebResponseContent Export(PageDataOptions options)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                Type t = typeof(StockViewDTO);
                string savePath = AppDomain.CurrentDomain.BaseDirectory + $"ExcelExport";
                IExporter exporter = new ExcelExporter();
                options.Page = 1;
                options.Rows = 30;
                options.Order = "desc";
                options.Sort = "stockId";
                List<StockViewDTO> stockViewDTOs = new List<StockViewDTO>();
                string where = string.Empty;
@@ -53,28 +269,76 @@
                    try
                    {
                        List<SearchParameters> searchParametersList = options.Wheres.DeserializeObject<List<SearchParameters>>();
                        if (searchParametersList.Count > 0)
                        if (searchParametersList?.Any() == true)
                        {
                            foreach (var param in searchParametersList)
                            {
                                SearchParameters? searchParameters = searchParametersList.FirstOrDefault(x => x.Name == nameof(Dt_StockInfoDetail.MaterielCode).FirstLetterToLower());
                                if (searchParameters != null)
                                switch (param.Name)
                                {
                                    sugarQueryable1 = sugarQueryable1.Where(x => x.Details.Any(v => v.MaterielCode.Contains(searchParameters.Value)));
                                    case var name when name == nameof(Dt_StockInfoDetail.MaterielCode).FirstLetterToLower():
                                        if (!string.IsNullOrEmpty(param.Value?.ToString()))
                                        {
                                            sugarQueryable1 = sugarQueryable1
                                                .Where(x => x.Details.Any(v => v.MaterielCode.Contains(param.Value.ToString())));
                                        }
                                        break;
                                    case var name when name == nameof(Dt_StockInfoDetail.BatchNo).FirstLetterToLower():
                                        if (!string.IsNullOrEmpty(param.Value?.ToString()))
                                        {
                                            sugarQueryable1 = sugarQueryable1
                                                .Where(x => x.Details.Any(v => v.BatchNo.Contains(param.Value.ToString())));
                                        }
                                        break;
                                    case var name when name == nameof(Dt_StockInfo.LocationCode).FirstLetterToLower():
                                        if (!string.IsNullOrEmpty(param.Value?.ToString()))
                                        {
                                            sugarQueryable1 = sugarQueryable1
                                                .Where(x => x.LocationCode == param.Value.ToString());
                                        }
                                        break;
                                    case var name when name == nameof(Dt_StockInfo.CreateDate).FirstLetterToLower():
                                        if (DateTime.TryParse(param.Value?.ToString(), out DateTime minDate))
                                        {
                                            LinqExpressionType expressionType = param.DisplayType.GetLinqCondition();
                                            if (expressionType == LinqExpressionType.ThanOrEqual)
                                            {
                                                sugarQueryable1 = sugarQueryable1.Where(x => x.CreateDate >= minDate);
                                            }
                                            else if (expressionType == LinqExpressionType.LessThanOrEqual)
                                            {
                                                sugarQueryable1 = sugarQueryable1.Where(x => x.CreateDate <= minDate);
                                            }
                                        }
                                        break;
                                    case var name when name == nameof(Dt_StockInfoDetail.EffectiveDate).FirstLetterToLower():
                                        // å‰ç«¯ä¼ é€’的参数需是 yyyy-MM-dd æ ¼å¼ï¼ˆå¦‚ "2025-05-08"),直接作为字符串处理
                                        string effectiveDateStr = param.Value?.ToString().Trim();
                                        if (!string.IsNullOrEmpty(effectiveDateStr) && effectiveDateStr.Length == 10)
                                        {
                                            LinqExpressionType expressionType = param.DisplayType.GetLinqCondition();
                                            if (expressionType == LinqExpressionType.ThanOrEqual)
                                            {
                                                sugarQueryable1 = sugarQueryable1.Where(x => x.Details
                                                    .Any(d => !string.IsNullOrEmpty(d.EffectiveDate) && d.EffectiveDate.CompareTo(effectiveDateStr) >= 0));
                                            }
                                            else if (expressionType == LinqExpressionType.LessThanOrEqual)
                                            {
                                                sugarQueryable1 = sugarQueryable1.Where(x => x.Details
                                                    .Any(d => !string.IsNullOrEmpty(d.EffectiveDate) && d.EffectiveDate.CompareTo(effectiveDateStr) <= 0));
                                            }
                                        }
                                        break;
                                }
                            }
                            {
                                SearchParameters? searchParameters = searchParametersList.FirstOrDefault(x => x.Name == nameof(Dt_StockInfoDetail.BatchNo).FirstLetterToLower());
                                if (searchParameters != null)
                                {
                                    sugarQueryable1 = sugarQueryable1.Where(x => x.Details.Any(v => v.BatchNo.Contains(searchParameters.Value)));
                                }
                            }
                        }
                    }
                    catch { }
                    catch (Exception ex)
                    {
                    }
                }
                string whereSto = GetDataRole(typeof(Dt_StockInfo));
                sugarQueryable1 = sugarQueryable1.Where(whereSto);
@@ -90,6 +354,7 @@
                    {
                        LocationCode = b.LocationCode,
                        Column = a.Column,
                        Expirationlabel = b.Expirationlabel,
                        WarehouseId = b.WarehouseId,
                        CreateDate = b.CreateDate,
                        Creater = b.Creater,
@@ -115,6 +380,7 @@
                    LocationCode = b.LocationCode,
                    Column = 0,
                    WarehouseId = b.WarehouseId,
                    Expirationlabel = b.Expirationlabel,
                    CreateDate = b.CreateDate,
                    Creater = b.Creater,
                    Depth = 0,
@@ -133,51 +399,32 @@
                    StockStatus = b.StockStatus,
                    Details = b.Details,
                });
                int totalCount = 0;
                int pktotalCount = 0;
                stockViewDTOs = list.ToPageList(options.Page, options.Rows, ref totalCount);
                stockViewDTOs.AddRange(pklist.ToPageList(options.Page, options.Rows, ref pktotalCount));
                stockViewDTOs = list.ToList();
                stockViewDTOs.AddRange(pklist.ToList());
                stockViewDTOs.ForEach(x =>
                {
                    x.MaterielCode = string.Join(",", x.Details.Select(x => x.MaterielCode).Distinct());
                    x.BatchNo = string.Join(",", x.Details.Select(x => x.BatchNo).Distinct());
                    x.MaterielCode = (string.Join(",", x.Details.Select(x => x.MaterielCode).Distinct())) ?? "空箱";
                    x.BatchNo = (string.Join(",", x.Details.Select(x => x.BatchNo).Distinct())) ?? "无";
                    x.StockCounts = Math.Round(x.Details.Sum(x => x.StockQuantity),3).ToString() + x.Details.FirstOrDefault()?.Unit;
                    x.MaterielSpec= x.Details.FirstOrDefault()?.MaterielSpec ?? "";
                    x.MaterielName= x.Details.FirstOrDefault()?.MaterielName ?? "";
                }
                );
                return new PageGridData<StockViewDTO>(totalCount + pktotalCount, stockViewDTOs);
                byte[] data = exporter.ExportAsByteArray(stockViewDTOs).Result;
                string fileName = "库存视图.xlsx";
                FileHelper.WriteFile(savePath, fileName, data);
                content = WebResponseContent.Instance.OK(data: savePath + "\\" + fileName);
            }
            catch (Exception ex)
            {
                content = WebResponseContent.Instance.Error(ex.Message);
            }
            return new PageGridData<StockViewDTO>();
            return content;
        }
        public virtual object GetDetailPage(PageDataOptions pageData)
        {
            Type t = typeof(StockViewDTO);
            if (pageData.Value == null) return new PageGridData<object>(total: 0, null);
            string keyName = t.GetKeyName();
            ////生成查询条件
            //Expression<Func<TEntity, bool>> whereExpression = keyName.CreateExpression<TEntity>(pageData.Value, LinqExpressionType.Equal);
            int totalCount = 0;
            PropertyInfo? propertyInfo = t.GetProperties().FirstOrDefault(x => x.GetCustomAttribute<Navigate>() != null);
            if (propertyInfo != null)
            {
                Type detailType = propertyInfo.PropertyType.GetGenericArguments()[0];
                Navigate? navigate = propertyInfo.GetCustomAttribute<Navigate>();
                if (navigate != null)
                {
                    List<ExpandoObject> list = _dbBase.Queryable(detailType.Name, "detail").Where(navigate.GetName(), "=", pageData.Value).ToPageList(pageData.Page, pageData.Rows, ref totalCount);
                    return new PageGridData<ExpandoObject>(totalCount, list);
                }
            }
            return new PageGridData<object>(total: 0, null);
        }
        private string GetDataRole(Type type)
        {
            try