11
tjx
2025-12-05 c789e51bbf0dc8eea2689918c7c8cdb4309d3c6c
StandardPda/MES.Service/service/Warehouse/MesInvItemStocksManager.cs
@@ -14,7 +14,8 @@
    /// </summary>
    /// <param name="searchDto">搜索请求参数</param>
    /// <returns>分页结果</returns>
    public PagedResult<ReturnableStockDto> GetReturnableStocks(ReturnableStockSearchDto searchDto)
    public PagedResult<ReturnableStockDto> GetReturnableStocks(
        ReturnableStockSearchDto searchDto)
    {
        // 参数校验
        if (searchDto.PageIndex < 1)
@@ -59,16 +60,21 @@
        }
        // 1.1 提取去重后的条码列表用于查询
        var distinctBarcodes = rackingTaskData.Select(x => x.ItemBarcode).Distinct().ToList();
        var distinctBarcodes = rackingTaskData.Select(x => x.ItemBarcode)
            .Distinct().ToList();
        // 2. 构建查询条件
        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) || distinctBarcodes.Contains(stock.StackCode) ) &&
            .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. 应用搜索条件
@@ -82,85 +88,99 @@
                if (conditions.IqcStatus == "1")
                {
                    // 当IqcStatus为"1"时,查询特采直接使用、已检、免检状态
                    query = query.Where((stock, item, depot, org, unit) =>
                        stock.IqcStatus == "特采直接使用" ||
                        stock.IqcStatus == "已检" ||
                    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);
                    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);
                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));
            }
            // 日期范围条件
            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);
                }
            }
@@ -168,14 +188,16 @@
            {
                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 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,
@@ -197,14 +219,16 @@
            .ToList();
        // 5. 在内存中转换为DTO,关联PalletCode并赋值ItemBarcode(优先使用箱条码StockStackCode)
        var tempDataList = queryResult.Select(x =>
        var tempDataList = queryResult.Select(x =>
        {
            // 优先使用StockStackCode,否则使用ItemBarcode去查找PalletCode
            var barcodeToMatch = !string.IsNullOrEmpty(x.StockStackCode) ? x.StockStackCode : x.ItemBarcode;
            var barcodeToMatch = !string.IsNullOrEmpty(x.StockStackCode)
                ? x.StockStackCode
                : x.ItemBarcode;
            var rackingTask = rackingTaskData
                .Where(r => r.ItemBarcode == barcodeToMatch)
                .FirstOrDefault();
            // 根据Code值确定库存状态: null为立库入库中(0), 200为已在立库内(1)
            string stockStatus = "进入立库的路上"; // 默认为立库入库中
            if (rackingTask?.Code != null)
@@ -238,7 +262,9 @@
        if (conditions != null && !string.IsNullOrEmpty(conditions.StackCode))
        {
            tempDataList = tempDataList
                .Where(x => x.StackCode != null && x.StackCode.Contains(conditions.StackCode))
                .Where(x =>
                    x.StackCode != null &&
                    x.StackCode.Contains(conditions.StackCode))
                .ToList();
        }
@@ -294,16 +320,19 @@
        // 6. 应用ItemType筛选(在内存中过滤)
        if (conditions?.ItemType != null)
        {
            dataList = dataList.Where(x => x.ItemType == conditions.ItemType).ToList();
            dataList = dataList.Where(x => x.ItemType == conditions.ItemType)
                .ToList();
        }
        // 7. 计算总记录数和分页参数(基于最终结果)
        var totalRecords = dataList.Count;
        var totalPages = (int)Math.Ceiling((double)totalRecords / searchDto.PageSize);
        var totalPages =
            (int)Math.Ceiling((double)totalRecords / searchDto.PageSize);
        var skip = (searchDto.PageIndex - 1) * searchDto.PageSize;
        // 8. 对最终结果进行分页
        var pagedDataList = dataList.Skip(skip).Take(searchDto.PageSize).ToList();
        var pagedDataList =
            dataList.Skip(skip).Take(searchDto.PageSize).ToList();
        // 9. 返回分页结果
        return new PagedResult<ReturnableStockDto>
@@ -381,17 +410,21 @@
            .ToList();
        // 3. 在内存中进行数据转换
        var result = queryResult.Select(x => {
        var result = queryResult.Select(x =>
        {
            // 根据条码查找对应的立库任务信息
            var rackingTask = rackingTaskData
                .Where(r => r.ItemBarcode == x.ItemBarcode)
                .FirstOrDefault();
                        // 根据Code值确定库存状态: null为立库入库中(0), 200为已在立库内(1)
            // 根据Code值确定库存状态: null为立库入库中(0), 200为已在立库内(1)
            string stockStatus = "0"; // 默认为立库入库中
            if (rackingTask?.Code != null)
            {
                stockStatus = rackingTask.Code == "200" ? "1" : "2"; // 200对应已在立库中(1),其他非500值为进入立库的路上(2)
                stockStatus =
                    rackingTask.Code == "200"
                        ? "1"
                        : "2"; // 200对应已在立库中(1),其他非500值为进入立库的路上(2)
            }
            return new ReturnableStockDto
@@ -439,15 +472,18 @@
            // 对每个条码单独处理
            foreach (var barcode in dto.ItemBarcodes)
            {
                                // 检查最近两分钟内是否已经存在相同的 barcode 被处理过
                // 检查最近两分钟内是否已经存在相同的 barcode 被处理过
                var twoMinutesAgo = DateTime.Now.AddMinutes(-2);
                var recentTask = Db.Queryable<XbRackingTaskSyxtLog>()
                    .Where(x => x.ItemBarcode == barcode && x.CreateDate >= twoMinutesAgo)
                    .Where(x =>
                        x.ItemBarcode == barcode &&
                        x.CreateDate >= twoMinutesAgo)
                    .First();
                if (recentTask != null)
                {
                    throw new Exception($"物料条码 {barcode} 在两分钟内已被扫描处理,请勿重复操作。为避免立库系统任务重复下发,系统限制同一物料条码在两分钟内只能处理一次。");
                    throw new Exception(
                        $"物料条码 {barcode} 在两分钟内已被扫描处理,请勿重复操作。为避免立库系统任务重复下发,系统限制同一物料条码在两分钟内只能处理一次。");
                }
                decimal messageId = 0;
@@ -467,7 +503,8 @@
                        .LeftJoin<MesItems>((stock, item) =>
                            stock.ItemId == item.Id)
                        .Where((stock, item) =>
                            (stock.ItemBarcode == barcode || stock.StackCode == 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
@@ -476,7 +513,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();
@@ -499,16 +537,17 @@
                        taskCode = taskCode,
                        taskType = "1",
                        palletCode = rackingTaskInfo?.PalletCode ?? "",
                        widthType = rackingTaskInfo?.WidthType?.ToString() ?? "2000",
                        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",
@@ -522,7 +561,8 @@
                        Data = jsonRequest,
                        DataInserted = jsonRequest
                    };
                    messageId = Db.Insertable(messageCenter).ExecuteReturnIdentity();
                    messageId = Db.Insertable(messageCenter)
                        .ExecuteReturnIdentity();
                    // 6. 调用HTTP接口
                    var apiUrl =