huangxiaoqiang
5 天以前 2640679b2823cd68d74e255442cce1634aac773c
Code Management/WMS/WIDESEA_WMSServer/WIDESEA_StorageBasicServices/Stock/StockInfoService.cs
@@ -1,4 +1,6 @@
using 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>
@@ -151,6 +157,71 @@
            .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) // 可选:按子表Id排序,确保结果稳定(可替换为其他字段)
                    .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)
    //{