111
tjx
6 天以前 f34f0751ef0c6305c94ff342ca7fbe24aa09844e
StandardPda/MES.Service/service/Warehouse/MesInvItemStocksManager.cs
@@ -4,6 +4,8 @@
using SqlSugar;
using Newtonsoft.Json;
using System.Text;
using OfficeOpenXml;
using OfficeOpenXml.Style;
namespace MES.Service.service.Warehouse;
@@ -14,27 +16,27 @@
    /// </summary>
    /// <param name="searchDto">搜索请求参数</param>
    /// <returns>分页结果</returns>
    public PagedResult<ReturnableStockDto> GetReturnableStocks(ReturnableStockSearchDto searchDto)
    public PagedResult<ReturnableStockDto> GetReturnableStocks(
        ReturnableStockSearchDto searchDto)
    {
        // 参数校验
        if (searchDto.PageIndex < 1)
        {
            throw new Exception("页码必须大于0");
        }
        if (!new[] { 10, 20, 50 }.Contains(searchDto.PageSize))
        {
            throw new Exception("每页条数必须为10、20或50");
        }
        // 优化点1: 使用原生SQL ROW_NUMBER()在数据库端完成去重和排序
        var rackingTaskSql = @"
            SELECT ITEM_BARCODE AS ItemBarcode, PALLETCODE AS PalletCode, CODE AS Code
            FROM (
                SELECT ITEM_BARCODE, PALLETCODE, CODE,
                       ROW_NUMBER() OVER (PARTITION BY ITEM_BARCODE ORDER BY ID DESC) AS RN
                FROM XB_RACKING_TASK_SYXT_LOG
                WHERE ITEM_BARCODE IS NOT NULL AND (CODE IS NULL OR CODE != '500')
            ) WHERE RN = 1";
        // 1. 查询XB_RACKING_TASK_SYXT_LOG中所有的条码并去重
        var distinctBarcodes = Db.Queryable<XbRackingTaskSyxtLog>()
            .Where(x => !string.IsNullOrEmpty(x.ItemBarcode))
            .Select(x => x.ItemBarcode)
            .Distinct()
            .ToList();
        var rackingTaskData = Db.Ado.SqlQuery<RackingTaskDto>(rackingTaskSql);
        if (distinctBarcodes == null || !distinctBarcodes.Any())
        if (rackingTaskData == null || !rackingTaskData.Any())
        {
            return new PagedResult<ReturnableStockDto>
            {
@@ -49,98 +51,139 @@
            };
        }
        // 2. 构建查询条件
        // 优化点2: 过滤null值并使用字典提高查找效率
        var validRackingData = rackingTaskData.Where(x => !string.IsNullOrEmpty(x.ItemBarcode)).ToList();
        if (!validRackingData.Any())
        {
            return new PagedResult<ReturnableStockDto>
            {
                TbBillList = new List<ReturnableStockDto>(),
                Pagination = new PaginationInfo
                {
                    CurrentPage = searchDto.PageIndex,
                    PageSize = searchDto.PageSize,
                    TotalRecords = 0,
                    TotalPages = 0
                }
            };
        }
        var rackingTaskDict = validRackingData.ToDictionary(x => x.ItemBarcode);
        var distinctBarcodes = validRackingData.Select(x => x.ItemBarcode).ToList();
        // 优化点3: 构建查询条件
        var query = Db.Queryable<MesInvItemStocks>()
            .LeftJoin<MesItems>((stock, item) => stock.ItemId == item.Id)
            .LeftJoin<MesDepots>((stock, item, depot) => stock.DepotsCode == depot.DepotCode)
            .LeftJoin<Organize>((stock, item, depot, org) => item.UseOrg == org.Id.ToString())
            .LeftJoin<MesUnit>((stock, item, depot, org, unit) => item.ItemUnit == unit.Id.ToString())
            .Where((stock, item, depot, org, unit) =>
                distinctBarcodes.Contains(stock.ItemBarcode) &&
            .LeftJoin<MesDepots>((stock, item, depot) =>
                stock.DepotsCode == depot.DepotCode)
            .LeftJoin<Organize>((stock, item, depot, org) =>
                item.UseOrg == org.Id.ToString())
            .LeftJoin<MesUnit>((stock, item, depot, org, unit) =>
                item.ItemUnit == unit.Id.ToString())
            .Where((stock, item, depot, org, unit) =>
                (distinctBarcodes.Contains(stock.ItemBarcode) ||
                 distinctBarcodes.Contains(stock.StackCode)) &&
                stock.Quantity > 0);
        // 3. 应用搜索条件
        var conditions = searchDto.Conditions;
        if (conditions != null)
        {
            // 精确匹配条件
            if (!string.IsNullOrEmpty(conditions.IqcStatus))
            {
                query = query.Where((stock, item, depot, org, unit) => stock.IqcStatus == conditions.IqcStatus);
                if (conditions.IqcStatus == "1")
                {
                    query = query.Where((stock, item, depot, org, unit) =>
                        stock.IqcStatus == "特采直接使用" ||
                        stock.IqcStatus == "已检" ||
                        stock.IqcStatus == "免检");
                }
                else
                {
                    query = query.Where((stock, item, depot, org, unit) =>
                        stock.IqcStatus == conditions.IqcStatus);
                }
            }
            if (conditions.Quantity.HasValue)
            {
                query = query.Where((stock, item, depot, org, unit) => stock.Quantity == conditions.Quantity.Value);
            }
            // 模糊匹配条件
            if (!string.IsNullOrEmpty(conditions.StackCode))
            {
                query = query.Where((stock, item, depot, org, unit) =>
                    stock.StackCode != null && stock.StackCode.Contains(conditions.StackCode));
                query = query.Where((stock, item, depot, org, unit) =>
                    stock.Quantity == conditions.Quantity.Value);
            }
            if (!string.IsNullOrEmpty(conditions.DepotName))
            {
                query = query.Where((stock, item, depot, org, unit) =>
                    depot.DepotName != null && depot.DepotName.Contains(conditions.DepotName));
                query = query.Where((stock, item, depot, org, unit) =>
                    depot.DepotName != null &&
                    depot.DepotName.Contains(conditions.DepotName));
            }
            if (!string.IsNullOrEmpty(conditions.DepotSectionsCode))
            {
                query = query.Where((stock, item, depot, org, unit) =>
                    stock.DepotSectionsCode != null && stock.DepotSectionsCode.Contains(conditions.DepotSectionsCode));
                query = query.Where((stock, item, depot, org, unit) =>
                    stock.DepotSectionsCode != null &&
                    stock.DepotSectionsCode.Contains(conditions
                        .DepotSectionsCode));
            }
            if (!string.IsNullOrEmpty(conditions.ItemNo))
            {
                query = query.Where((stock, item, depot, org, unit) =>
                    item.ItemNo != null && item.ItemNo.Contains(conditions.ItemNo));
                query = query.Where((stock, item, depot, org, unit) =>
                    item.ItemNo != null &&
                    item.ItemNo.Contains(conditions.ItemNo));
            }
            if (!string.IsNullOrEmpty(conditions.ItemName))
            {
                query = query.Where((stock, item, depot, org, unit) =>
                    item.ItemName != null && item.ItemName.Contains(conditions.ItemName));
                query = query.Where((stock, item, depot, org, unit) =>
                    item.ItemName != null &&
                    item.ItemName.Contains(conditions.ItemName));
            }
            if (!string.IsNullOrEmpty(conditions.ItemModel))
            {
                query = query.Where((stock, item, depot, org, unit) =>
                    item.ItemModel != null && item.ItemModel.Contains(conditions.ItemModel));
                query = query.Where((stock, item, depot, org, unit) =>
                    item.ItemModel != null &&
                    item.ItemModel.Contains(conditions.ItemModel));
            }
            if (!string.IsNullOrEmpty(conditions.ItemUnitName))
            {
                query = query.Where((stock, item, depot, org, unit) =>
                    unit.Fname != null && unit.Fname.Contains(conditions.ItemUnitName));
                query = query.Where((stock, item, depot, org, unit) =>
                    unit.Fname != null &&
                    unit.Fname.Contains(conditions.ItemUnitName));
            }
            if (!string.IsNullOrEmpty(conditions.OrgCode))
            {
                query = query.Where((stock, item, depot, org, unit) =>
                    org.Fnumber != null && org.Fnumber.Contains(conditions.OrgCode));
                query = query.Where((stock, item, depot, org, unit) =>
                    org.Fnumber != null &&
                    org.Fnumber.Contains(conditions.OrgCode));
            }
            if (!string.IsNullOrEmpty(conditions.OrgName))
            {
                query = query.Where((stock, item, depot, org, unit) =>
                    org.Fname != null && org.Fname.Contains(conditions.OrgName));
                query = query.Where((stock, item, depot, org, unit) =>
                    org.Fname != null &&
                    org.Fname.Contains(conditions.OrgName));
            }
            if (!string.IsNullOrEmpty(conditions.ItemBarcode))
            {
                query = query.Where((stock, item, depot, org, unit) =>
                    stock.ItemBarcode != null && stock.ItemBarcode.Contains(conditions.ItemBarcode));
                query = query.Where((stock, item, depot, org, unit) =>
                    (stock.ItemBarcode != null &&
                     stock.ItemBarcode.Contains(conditions.ItemBarcode)) ||
                    (stock.StackCode != null &&
                     stock.StackCode.Contains(conditions.ItemBarcode)));
            }
            // 日期范围条件
            if (!string.IsNullOrEmpty(conditions.IndepDateStart))
            {
                if (DateTime.TryParse(conditions.IndepDateStart, out var startDate))
                if (DateTime.TryParse(conditions.IndepDateStart,
                        out var startDate))
                {
                    query = query.Where((stock, item, depot, org, unit) => stock.IndepDate >= startDate);
                    query = query.Where((stock, item, depot, org, unit) =>
                        stock.IndepDate >= startDate);
                }
            }
@@ -148,25 +191,19 @@
            {
                if (DateTime.TryParse(conditions.IndepDateEnd, out var endDate))
                {
                    query = query.Where((stock, item, depot, org, unit) => stock.IndepDate <= endDate);
                    query = query.Where((stock, item, depot, org, unit) =>
                        stock.IndepDate <= endDate);
                }
            }
        }
        // 4. 查询总记录数
        var totalRecords = query.Count();
        // 5. 计算分页参数
        var totalPages = (int)Math.Ceiling((double)totalRecords / searchDto.PageSize);
        var skip = (searchDto.PageIndex - 1) * searchDto.PageSize;
        // 6. 查询当前页数据(先查出中间数据)
        // 优化点4: 查询数据
        var queryResult = query
            .OrderByDescending((stock, item, depot, org, unit) => stock.IndepDate)
            .OrderByDescending((stock, item, depot, org, unit) =>
                stock.IndepDate)
            .Select((stock, item, depot, org, unit) => new
            {
                stock.IqcStatus,
                stock.StackCode,
                DepotCode = stock.DepotsCode,
                depot.DepotName,
                stock.DepotSectionsCode,
@@ -179,47 +216,121 @@
                stock.IndepDate,
                OrgCode = org.Fnumber,
                OrgName = org.Fname,
                stock.ItemBarcode
                stock.ItemBarcode,
                StockStackCode = stock.StackCode
            })
            .Skip(skip)
            .Take(searchDto.PageSize)
            .ToList();
        // 7. 在内存中转换为DTO
        var dataList = queryResult.Select(x => new ReturnableStockDto
        // 优化点5: 使用字典查找替代Where().FirstOrDefault()
        var tempDataList = queryResult.Select(x =>
        {
            IqcStatus = x.IqcStatus == "已检" ? "1" : "0",
            ItemType = x.DepotName == "原材料仓" ? "0" : "1",
            StackCode = x.StackCode,
            DepotCode = x.DepotCode,
            DepotName = x.DepotName,
            DepotSectionsCode = x.DepotSectionsCode,
            ItemNo = x.ItemNo,
            ItemName = x.ItemName,
            ItemModel = x.ItemModel,
            Quantity = x.Quantity,
            ItemUnit = x.ItemUnit,
            ItemUnitName = x.ItemUnitName,
            IndepDate = x.IndepDate,
            OrgCode = x.OrgCode,
            OrgName = x.OrgName,
            ItemBarcode = x.ItemBarcode
            var barcodeToMatch = !string.IsNullOrEmpty(x.StockStackCode)
                ? x.StockStackCode
                : x.ItemBarcode;
            rackingTaskDict.TryGetValue(barcodeToMatch, out var rackingTask);
            string stockStatus = "进入立库的路上";
            if (rackingTask?.Code != null)
            {
                stockStatus = rackingTask.Code == "200" ? "已在立库中" : "进入立库的路上";
            }
            return new
            {
                IqcStatus = x.IqcStatus == "已检" ? "1" : "0",
                ItemType = x.DepotName == "原材料仓" ? "0" : "1",
                StackCode = rackingTask?.PalletCode,
                x.DepotCode,
                x.DepotName,
                x.DepotSectionsCode,
                x.ItemNo,
                x.ItemName,
                x.ItemModel,
                x.Quantity,
                x.ItemUnit,
                x.ItemUnitName,
                x.IndepDate,
                x.OrgCode,
                x.OrgName,
                ItemBarcode = barcodeToMatch,
                StockStatus = stockStatus
            };
        }).ToList();
        // 8. 应用ItemType筛选(在内存中过滤)
        if (conditions?.ItemType != null)
        if (conditions != null && !string.IsNullOrEmpty(conditions.StackCode))
        {
            dataList = dataList.Where(x => x.ItemType == conditions.ItemType).ToList();
            // 重新计算分页信息
            totalRecords = dataList.Count;
            totalPages = (int)Math.Ceiling((double)totalRecords / searchDto.PageSize);
            dataList = dataList.Skip(skip).Take(searchDto.PageSize).ToList();
            tempDataList = tempDataList
                .Where(x =>
                    x.StackCode != null &&
                    x.StackCode.Contains(conditions.StackCode))
                .ToList();
        }
        // 9. 返回分页结果
        if (conditions != null && !string.IsNullOrEmpty(conditions.StockStatus))
        {
            tempDataList = tempDataList
                .Where(x => x.StockStatus == conditions.StockStatus)
                .ToList();
        }
        var dataList = tempDataList
            .GroupBy(x => new
            {
                x.IqcStatus,
                x.ItemType,
                x.StackCode,
                x.DepotCode,
                x.DepotName,
                x.DepotSectionsCode,
                x.ItemNo,
                x.ItemName,
                x.ItemModel,
                x.ItemUnit,
                x.ItemUnitName,
                x.OrgCode,
                x.OrgName,
                x.ItemBarcode,
                x.StockStatus
            })
            .Select(g => new ReturnableStockDto
            {
                IqcStatus = g.Key.IqcStatus,
                ItemType = g.Key.ItemType,
                StackCode = g.Key.StackCode,
                DepotCode = g.Key.DepotCode,
                DepotName = g.Key.DepotName,
                DepotSectionsCode = g.Key.DepotSectionsCode,
                ItemNo = g.Key.ItemNo,
                ItemName = g.Key.ItemName,
                ItemModel = g.Key.ItemModel,
                Quantity = g.Sum(x => x.Quantity),
                ItemUnit = g.Key.ItemUnit,
                ItemUnitName = g.Key.ItemUnitName,
                IndepDate = g.Max(x => x.IndepDate),
                OrgCode = g.Key.OrgCode,
                OrgName = g.Key.OrgName,
                ItemBarcode = g.Key.ItemBarcode,
                StockStatus = g.Key.StockStatus
            }).ToList();
        if (conditions?.ItemType != null)
        {
            dataList = dataList.Where(x => x.ItemType == conditions.ItemType)
                .ToList();
        }
        var totalRecords = dataList.Count;
        var totalPages =
            (int)Math.Ceiling((double)totalRecords / searchDto.PageSize);
        var skip = (searchDto.PageIndex - 1) * searchDto.PageSize;
        var pagedDataList =
            dataList.Skip(skip).Take(searchDto.PageSize).ToList();
        return new PagedResult<ReturnableStockDto>
        {
            TbBillList = dataList,
            TbBillList = pagedDataList,
            Pagination = new PaginationInfo
            {
                CurrentPage = searchDto.PageIndex,
@@ -236,19 +347,30 @@
    /// <returns>可退货物料库存列表</returns>
    public List<ReturnableStockDto> GetReturnableStocks()
    {
        // 1. 查询XB_RACKING_TASK_SYXT_LOG中所有的条码并去重
        var distinctBarcodes = Db.Queryable<XbRackingTaskSyxtLog>()
            .Where(x => !string.IsNullOrEmpty(x.ItemBarcode))
            .Select(x => x.ItemBarcode)
            .Distinct()
            .ToList();
        // 优化点1: 使用原生SQL在数据库端完成去重和排序,避免全表加载到内存
        // Oracle 11g使用ROW_NUMBER()窗口函数获取每个条码的最新记录
        var rackingTaskSql = @"
            SELECT ITEM_BARCODE AS ItemBarcode, PALLETCODE AS PalletCode, CODE AS Code
            FROM (
                SELECT ITEM_BARCODE, PALLETCODE, CODE,
                       ROW_NUMBER() OVER (PARTITION BY ITEM_BARCODE ORDER BY ID DESC) AS RN
                FROM XB_RACKING_TASK_SYXT_LOG
                WHERE ITEM_BARCODE IS NOT NULL AND (CODE IS NULL OR CODE != '500')
            ) WHERE RN = 1";
        if (distinctBarcodes == null || !distinctBarcodes.Any())
        var rackingTaskData = Db.Ado.SqlQuery<RackingTaskDto>(rackingTaskSql);
        if (rackingTaskData == null || !rackingTaskData.Any())
        {
            return new List<ReturnableStockDto>();
        }
        // 2. 使用条码查询MES_INV_ITEM_STOCKS中的数据,关联MES_ITEMS、MES_DEPOTS、ORGANIZE、MES_UNIT表
        // 优化点2: 过滤null值并使用字典提高条码匹配效率,从O(n)降低到O(1)
        var validRackingData = rackingTaskData.Where(x => !string.IsNullOrEmpty(x.ItemBarcode)).ToList();
        var rackingTaskDict = validRackingData.ToDictionary(x => x.ItemBarcode);
        var distinctBarcodes = validRackingData.Select(x => x.ItemBarcode).ToList();
        // 优化点3: 在数据库层面完成关联查询,一次性获取所有需要的数据
        var queryResult = Db.Queryable<MesInvItemStocks>()
            .LeftJoin<MesItems>((stock, item) => stock.ItemId == item.Id)
            .LeftJoin<MesDepots>((stock, item, depot) =>
@@ -280,28 +402,47 @@
            })
            .ToList();
        // 3. 在内存中进行数据转换
        var result = queryResult.Select(x => new ReturnableStockDto
        // 优化点4: 使用字典查找替代Where().FirstOrDefault(),提高匹配性能
        var result = queryResult.Select(x =>
        {
            IqcStatus = x.IqcStatus == "已检" ? "1" : "0",
            ItemType = x.DepotName == "原材料仓" ? "0" : "1",
            StackCode = x.StackCode,
            DepotCode = x.DepotCode,
            DepotName = x.DepotName,
            DepotSectionsCode = x.DepotSectionsCode,
            ItemNo = x.ItemNo,
            ItemName = x.ItemName,
            ItemModel = x.ItemModel,
            Quantity = x.Quantity,
            ItemUnit = x.ItemUnit,
            ItemUnitName = x.ItemUnitName,
            IndepDate = x.IndepDate,
            OrgCode = x.OrgCode,
            OrgName = x.OrgName,
            ItemBarcode = x.ItemBarcode
            rackingTaskDict.TryGetValue(x.ItemBarcode, out var rackingTask);
            string stockStatus = "0";
            if (rackingTask?.Code != null)
            {
                stockStatus = rackingTask.Code == "200" ? "1" : "2";
            }
            return new ReturnableStockDto
            {
                IqcStatus = x.IqcStatus == "已检" ? "1" : "0",
                ItemType = x.DepotName == "原材料仓" ? "0" : "1",
                StackCode = x.StackCode,
                DepotCode = x.DepotCode,
                DepotName = x.DepotName,
                DepotSectionsCode = x.DepotSectionsCode,
                ItemNo = x.ItemNo,
                ItemName = x.ItemName,
                ItemModel = x.ItemModel,
                Quantity = x.Quantity,
                ItemUnit = x.ItemUnit,
                ItemUnitName = x.ItemUnitName,
                IndepDate = x.IndepDate,
                OrgCode = x.OrgCode,
                OrgName = x.OrgName,
                ItemBarcode = x.ItemBarcode,
                StockStatus = stockStatus
            };
        }).ToList();
        return result;
    }
    private class RackingTaskDto
    {
        public string ItemBarcode { get; set; }
        public string PalletCode { get; set; }
        public string Code { get; set; }
    }
    /// <summary>
@@ -324,13 +465,43 @@
            // 对每个条码单独处理
            foreach (var barcode in dto.ItemBarcodes)
            {
                // 检查最近两分钟内是否已经存在相同的 barcode 被处理过
                var twoMinutesAgo = DateTime.Now.AddMinutes(-2);
                var recentTask = Db.Queryable<XbRackingTaskSyxtLog>()
                    .Where(x =>
                        x.ItemBarcode == barcode &&
                        x.CreateDate >= twoMinutesAgo)
                    .First();
                if (recentTask != null)
                {
                    throw new Exception(
                        $"物料条码 {barcode} 在两分钟内已被扫描处理,请勿重复操作。为避免立库系统任务重复下发,系统限制同一物料条码在两分钟内只能处理一次。");
                }
                decimal messageId = 0;
                string taskCode = "";
                // 根据条码查询XB_RACKING_TASK_SYXT_LOG表,查询max(PALLETCODE)
                var maxPalletCode = Db.Queryable<XbRackingTaskSyxtLog>()
                // 根据条码查询XB_RACKING_TASK_SYXT_LOG表,查询max(PALLETCODE)和对应的widthType
                var rackingTaskInfo = Db.Queryable<XbRackingTaskSyxtLog>()
                    .Where(x => x.ItemBarcode == barcode)
                    .Max(x => x.PalletCode);
                    .OrderByDescending(x => x.Id)
                    .Select(x => new { x.PalletCode, x.WidthType })
                    .First();
                // 检查是否存在未完成的任务(基于PalletCode,Code为null)
                if (!string.IsNullOrEmpty(rackingTaskInfo?.PalletCode))
                {
                    var pendingTask = Db.Queryable<XbRackingTaskSyxtLog>()
                        .Where(x => x.PalletCode == rackingTaskInfo.PalletCode && x.Code == null)
                        .OrderByDescending(x => x.Id)
                        .First();
                    if (pendingTask != null)
                    {
                        throw new Exception($"托盘编号 {rackingTaskInfo.PalletCode} 已存在未完成的立库任务,请等待任务完成后再操作。");
                    }
                }
                try
                {
@@ -339,7 +510,8 @@
                        .LeftJoin<MesItems>((stock, item) =>
                            stock.ItemId == item.Id)
                        .Where((stock, item) =>
                            stock.ItemBarcode == barcode && stock.Quantity > 0)
                            (stock.ItemBarcode == barcode ||
                             stock.StackCode == barcode) && stock.Quantity > 0)
                        .GroupBy((stock, item) =>
                            new { stock.ItemId, stock.LotNo })
                        .Select((stock, item) => new
@@ -348,7 +520,8 @@
                            ItemName = SqlFunc.AggregateMax(item.ItemName),
                            LotNo = stock.LotNo ?? "",
                            Quantity = SqlFunc.AggregateSum(stock.Quantity),
                            StackCode = SqlFunc.AggregateMax(stock.DepotSectionsCode)
                            StackCode =
                                SqlFunc.AggregateMax(stock.DepotSectionsCode)
                        })
                        .ToList();
@@ -370,17 +543,18 @@
                    {
                        taskCode = taskCode,
                        taskType = "1",
                        palletCode = maxPalletCode ?? "",
                        widthType = "180",
                        station = "3"
                        palletCode = rackingTaskInfo?.PalletCode ?? "",
                        widthType = rackingTaskInfo?.WidthType?.ToString() ??
                                    "2000",
                        station = dto.Station
                    };
                    var requestList = new[] { requestData };
                    var jsonRequest = JsonConvert.SerializeObject(requestList);
                    // 5. 记录到MessageCenter表(请求前)
                    var messageCenter = new MessageCenter
                    {
                    {
                        TableName = "XB_RACKING_TASK_SYXT_LOG",
                        Url =
                            "http://172.20.5.5:50080/Services/Wmcs/RetrieveTask",
@@ -394,7 +568,8 @@
                        Data = jsonRequest,
                        DataInserted = jsonRequest
                    };
                    messageId = Db.Insertable(messageCenter).ExecuteReturnIdentity();
                    messageId = Db.Insertable(messageCenter)
                        .ExecuteReturnIdentity();
                    // 6. 调用HTTP接口
                    var apiUrl =
@@ -437,17 +612,15 @@
                            CreateDate = DateTime.Now,
                            TaskCode = taskCode,
                            TaskType = "立库出库请求",
                            PalletCode = maxPalletCode,
                            WidthType = 180,
                            PalletCode = rackingTaskInfo?.PalletCode ?? "",
                            WidthType = rackingTaskInfo?.WidthType ?? 2000,
                            MaterialName = firstStock.ItemName,
                            MaterialCode = firstStock.ItemNo,
                            BatchNo = firstStock.LotNo,
                            Quantity = firstStock.Quantity,
                            WarehousingJson = responseStr,
                            QcStatus = 2,
                            ItemBarcode = barcode,
                            Code = "200",
                            JsonMessage = jsonMessage ?? "成功"
                            ItemBarcode = barcode
                        };
                        Db.Insertable(taskLog).ExecuteCommand();
@@ -510,4 +683,97 @@
            throw;
        }
    }
    /// <summary>
    ///     导出可退货物料库存为Excel
    /// </summary>
    /// <param name="searchDto">搜索请求参数</param>
    /// <returns>Excel文件字节数组</returns>
    public byte[] ExportReturnableStocksToExcel(
        ReturnableStockSearchDto searchDto)
    {
        // 设置 EPPlus 许可证上下文(非商业用途)
        ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
        // 获取所有数据(不分页)
        var tempSearchDto = new ReturnableStockSearchDto
        {
            PageIndex = 1,
            PageSize = int.MaxValue, // 获取所有数据
            Conditions = searchDto.Conditions
        };
        // 调用查询方法获取数据
        var result = GetReturnableStocks(tempSearchDto);
        var dataList = result.TbBillList;
        // 创建 Excel 文件
        using var package = new ExcelPackage();
        var worksheet = package.Workbook.Worksheets.Add("可退货物料库存");
        // 设置表头
        var headers = new[]
        {
            "良品状态",
            "是否成品",
            "母托盘编号",
            "仓库编码",
            "仓库名称",
            "虚拟库位",
            "物料编号",
            "物料名称",
            "物料规格",
            "物料数量",
            "物料单位",
            "入库时间",
            "组织编码",
            "组织名称",
            "物料条码",
            "库存状态"
        };
        // 写入表头
        for (int i = 0; i < headers.Length; i++)
        {
            worksheet.Cells[1, i + 1].Value = headers[i];
            worksheet.Cells[1, i + 1].Style.Font.Bold = true;
            worksheet.Cells[1, i + 1].Style.Fill.PatternType =
                ExcelFillStyle.Solid;
            worksheet.Cells[1, i + 1].Style.Fill.BackgroundColor
                .SetColor(System.Drawing.Color.LightGray);
            worksheet.Cells[1, i + 1].Style.HorizontalAlignment =
                ExcelHorizontalAlignment.Center;
        }
        // 写入数据
        int row = 2;
        foreach (var item in dataList)
        {
            worksheet.Cells[row, 1].Value =
                item.IqcStatus == "1" ? "良品" : "不良品";
            worksheet.Cells[row, 2].Value = item.ItemType == "1" ? "成品" : "非成品";
            worksheet.Cells[row, 3].Value = item.StackCode;
            worksheet.Cells[row, 4].Value = item.DepotCode;
            worksheet.Cells[row, 5].Value = item.DepotName;
            worksheet.Cells[row, 6].Value = item.DepotSectionsCode;
            worksheet.Cells[row, 7].Value = item.ItemNo;
            worksheet.Cells[row, 8].Value = item.ItemName;
            worksheet.Cells[row, 9].Value = item.ItemModel;
            worksheet.Cells[row, 10].Value = item.Quantity;
            worksheet.Cells[row, 11].Value = item.ItemUnitName;
            worksheet.Cells[row, 12].Value =
                item.IndepDate?.ToString("yyyy-MM-dd HH:mm:ss");
            worksheet.Cells[row, 13].Value = item.OrgCode;
            worksheet.Cells[row, 14].Value = item.OrgName;
            worksheet.Cells[row, 15].Value = item.ItemBarcode;
            worksheet.Cells[row, 16].Value = item.StockStatus;
            row++;
        }
        // 自动调整列宽
        worksheet.Cells[worksheet.Dimension.Address].AutoFitColumns();
        // 返回 Excel 文件字节数组
        return package.GetAsByteArray();
    }
}