From 8b3377273709a6abd89af0e5b2ff9618c167146e Mon Sep 17 00:00:00 2001
From: chenyong <chenyong@hnkhzn.com>
Date: 星期二, 09 十二月 2025 17:20:24 +0800
Subject: [PATCH] 重写库存导出方法

---
 Code Management/WMS/WIDESEA_WMSServer/WIDESEA_StorageBasicServices/Stock/StockInfoService.cs |  113 ++++++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 100 insertions(+), 13 deletions(-)

diff --git a/Code Management/WMS/WIDESEA_WMSServer/WIDESEA_StorageBasicServices/Stock/StockInfoService.cs b/Code Management/WMS/WIDESEA_WMSServer/WIDESEA_StorageBasicServices/Stock/StockInfoService.cs
index 3f05072..5757f64 100644
--- a/Code Management/WMS/WIDESEA_WMSServer/WIDESEA_StorageBasicServices/Stock/StockInfoService.cs
+++ b/Code Management/WMS/WIDESEA_WMSServer/WIDESEA_StorageBasicServices/Stock/StockInfoService.cs
@@ -1,4 +1,6 @@
 锘縰sing AngleSharp.Dom;
+using Magicodes.ExporterAndImporter.Core;
+using Magicodes.ExporterAndImporter.Excel;
 using Mapster;
 using Masuit.Tools;
 using SqlSugar;
@@ -10,6 +12,8 @@
 using WIDESEA_Cache;
 using WIDESEA_Common;
 using WIDESEA_Core;
+using WIDESEA_IStorageBasicService;
+using WIDESEA_Model.Models.BasicModel;
 
 namespace WIDESEA_StorageBasicService;
 
@@ -18,10 +22,12 @@
 
     private readonly ISimpleCacheService _simpleCacheService;
     private readonly ILocationStatusChangeRecordRepository _locationStatusChangeRecordRepository;
-    public StockInfoService(IStockInfoRepository BaseDal, ISimpleCacheService simpleCacheService, ILocationStatusChangeRecordRepository locationStatusChangeRecordRepository) : base(BaseDal)
+    private readonly IStockInfoDetailRepository _IStockInfoDetailRepository;
+    public StockInfoService(IStockInfoRepository BaseDal, ISimpleCacheService simpleCacheService, ILocationStatusChangeRecordRepository locationStatusChangeRecordRepository, IStockInfoDetailRepository stockInfoDetailService) : base(BaseDal)
     {
         _simpleCacheService = simpleCacheService;
         _locationStatusChangeRecordRepository = locationStatusChangeRecordRepository;
+        _IStockInfoDetailRepository = stockInfoDetailService;
     }
 
     /// <summary>
@@ -60,6 +66,7 @@
 
         Expression<Func<DtStockInfo, bool>> locationStatus = null;
         Expression<Func<DtStockInfo, bool>> roadwayNo = null;
+        Expression<Func<DtStockInfo, bool>> materielCode = null;
         foreach (var item in searchParametersList)
         {
             if (item.Name.Contains("locationStatus"))
@@ -70,20 +77,23 @@
             {
                 roadwayNo = x => x.LocationInfo.RoadwayNo.Contains(item.Value);
             }
+            else if (item.Name.Contains("materielCode"))
+            {
+                materielCode = x => x.StockInfoDetails.Any(d => d.MaterielCode.Contains(item.Value));
+            }
         }
-
-        var data = BaseDal.Db.Queryable<DtStockInfo>().IncludesAllFirstLayer().WhereIF(!wheres.IsNullOrEmpty(), wheres).WhereIF(locationStatus != null, locationStatus).WhereIF(roadwayNo != null, roadwayNo).OrderBy(orderByModels).ToPageList(options.Page, options.Rows, ref totalCount);
+        //.IncludesAllFirstLayer()
+        var data = BaseDal.Db.Queryable<DtStockInfo>()
+            .Includes(x => x.StockInfoDetails)
+            .Includes(x => x.LocationInfo)
+            .WhereIF(!wheres.IsNullOrEmpty(), wheres)
+            .WhereIF(locationStatus != null, locationStatus)
+            .WhereIF(roadwayNo != null, roadwayNo)
+            .WhereIF(materielCode != null, materielCode)
+            .OrderBy(orderByModels)
+            .ToPageList(options.Page, options.Rows, ref totalCount);
         new PageGridData<DtStockInfo>(totalCount, data);
         return new PageGridData<DtStockInfo>(totalCount, data);
-        //var data = base.GetPageData(options);
-        //foreach (var item in data.Rows)
-        //{
-        //    if (item.IsFull)
-        //        item.Remark = item.StockInfoDetails.Count().ToString();
-        //    else
-        //        item.Remark = "0";
-        //}
-        //return data;
     }
 
     /// <summary>
@@ -115,7 +125,14 @@
                 _locationStatusChangeRecordRepository.AddLocationStatusChangeRecord(location, lastStatus, (int)StatusChangeTypeEnum.ManualOperation, 0);
             }
 
-            _simpleCacheService.HashDel<DtStockInfo>(CacheConst.Cache_DtStockInfo, stocks.ToArray());
+            //_simpleCacheService.HashDel<DtStockInfo>(CacheConst.Cache_DtStockInfo, stocks.ToArray());
+            //var hty = BaseDal.Db.InsertNav(stockInfos)
+            //    .Include(x => x.StockInfoDetails)
+            //    .ExecuteCommand();
+
+            //var locationd = SqlSugarHelper.DbWMS.Updateable(locationInfos).ExecuteCommandHasChange();
+            //return base.DeleteData(keys);
+
             var hty = BaseDal.Db.InsertNav(stockInfos)
                 .Include(x => x.StockInfoDetails)
                 .ExecuteCommand();
@@ -140,4 +157,74 @@
             .ToDictionary(x => x.Key, x => x.Count());
         return result;
     }
+    public override WebResponseContent Export(PageDataOptions options)
+    {
+        WebResponseContent content = new WebResponseContent();
+        try
+        {
+            // 1. 鏋勫缓涓昏〃鏌ヨ鏉′欢鍜屾帓搴�
+            string wheres = ValidatePageOptions(options);
+            Dictionary<string, OrderByType> orderbyDic = GetPageDataSort(options, TProperties);
+
+            // 2. 鏌ヨ涓昏〃鏁版嵁
+            List<DtStockInfo> mainList = BaseDal.QueryData(wheres, orderbyDic);
+            if (!mainList.Any())
+            {
+                return WebResponseContent.Instance.OK("鏃犳暟鎹彲瀵煎嚭");
+            }
+
+            // 3. 鎵归噺鏌ヨ瀛愯〃鏁版嵁锛堥�氳繃涓昏〃Id鍏宠仈锛�
+            var mainIds = mainList.Select(m => m.Id).Distinct().ToList();
+            List<DtStockInfoDetail> detailList = _IStockInfoDetailRepository.Db
+                .Queryable<DtStockInfoDetail>()
+                .Where(d => mainIds.Contains(d.StockId))
+                .ToList();
+
+            // 4. 鍏宠仈涓昏〃鍜屽瓙琛ㄦ暟鎹紙鍙彇瀛愯〃绗竴鏉★級
+            var exportData = new List<PalletWithDetailExportModel>();
+            foreach (var main in mainList)
+            {
+                // 鍙彇褰撳墠涓昏〃鏁版嵁鍏宠仈鐨勫瓙琛ㄧ涓�鏉★紙鍙�氳繃OrderBy鎸囧畾鎺掑簭锛岀‘淇濆彇鍒版兂瑕佺殑绗竴鏉★級
+                var firstDetail = detailList
+                    .Where(d => d.StockId == main.Id)
+                    .OrderBy(d => d.Id) // 鍙�夛細鎸夊瓙琛↖d鎺掑簭锛岀‘淇濈粨鏋滅ǔ瀹氾紙鍙浛鎹负鍏朵粬瀛楁锛�
+                    .FirstOrDefault();
+
+                var exportModel = new PalletWithDetailExportModel
+                {
+                    Id = main.Id,
+                    PalletCode = main.PalletCode,
+                    LocationCode = main.LocationCode,
+                    IsFull = main.IsFull,
+                    Remark = main.Remark,
+                    Creater = main.Creater,
+                    CreateDate = main.CreateDate,
+                    OutboundTime = (DateTime)main.OutboundTime,
+                    // 鏈夊瓙琛ㄦ暟鎹垯鍙栫涓�鏉$殑鐗╂枡缂栫爜锛屾棤鍒欎负绌�
+                    MaterielCode = firstDetail?.MaterielCode ?? ""
+                };
+                exportData.Add(exportModel);
+            }
+
+            // 5. 瀵煎嚭鏁版嵁锛堥�昏緫涓嶅彉锛�
+            string savePath = AppDomain.CurrentDomain.BaseDirectory + "ExcelExport";
+            IExporter exporter = new ExcelExporter();
+            byte[] data = exporter.ExportAsByteArray(exportData).Result;
+
+            string fileName = "鎵樼洏鍙婄墿鏂欑紪鐮佹暟鎹�.xlsx";
+            FileHelper.WriteFile(savePath, fileName, data);
+
+            content = WebResponseContent.Instance.OK(data: Path.Combine(savePath, fileName));
+        }
+        catch (Exception ex)
+        {
+            content = WebResponseContent.Instance.Error(ex.Message);
+        }
+        return content;
+    }
+
+    //public override WebResponseContent UpdateData(DtStockInfo entity)
+    //{
+    //    return base.UpdateData(entity);
+    //}
 }
\ No newline at end of file

--
Gitblit v1.9.3