yanjinhui
2025-10-14 ff4660980ccfe3e123df8d5fa820266784625c74
´úÂë¹ÜÀí/WIDESEA_WMSServer/WIDESEA_SquareCabinServices/InventoryServices.cs
@@ -13,7 +13,6 @@
using WIDESEA_ISquareCabinRepository;
using WIDESEA_ISquareCabinServices;
using WIDESEA_Model.Models;
using WIDESEA_Model.Models.SquareCabin;
using WIDESEA_SquareCabinRepository;
using static WIDESEA_DTO.SquareCabin.OrderDto;
using static WIDESEA_DTO.SquareCabin.TowcsDto;
@@ -91,18 +90,19 @@
                {
                    return new ApiResponse<Dt_Inventory> { code ="500",msg = "请求参数无效" };
                }
                BaseDal.Db.Ado.BeginTran();//开启事务
                foreach (var detail in request.details)
                {
                    // å…ˆæŸ¥åº“存是否存在
                    var entity = Db.Queryable<Dt_Inventory>()
                                   .First(x => x.Goods_no == detail.productCode);
                    // å…ˆæŸ¥åº“存是否存在(按照产品编号和批次来寻找)
                    var entity = BaseDal.Db.Queryable<Dt_Inventory>()
                                   .First(x => x.Goods_no == detail.productCode && x.Batch_num == detail.batchNo);
                    //将集合中的数量进行累加
                    decimal orderQty = detail.orderDetails?.Sum(x => Convert.ToDecimal(x.quantity)) ?? 0;
                    decimal diffQty = detail.stocktakingDetails?.Sum(x => Convert.ToDecimal(x.differenceQuantity)) ?? 0;
                    //decimal orderQty = detail.orderDetails?.Sum(x => Convert.ToDecimal(x.quantity)) ?? 0;
                    //decimal diffQty = detail.stocktakingDetails?.Sum(x => Convert.ToDecimal(x.differenceQuantity)) ?? 0;
                    // å®‰å…¨è½¬æ¢
                    //正常出入库
                    decimal orderQty = detail.orderDetails?.Sum(x => decimal.TryParse(x.quantity, out var q) ? q : 0) ?? 0;
                    //盘点盘亏
                    decimal diffQty = detail.stocktakingDetails?.Sum(x => Convert.ToDecimal(x.differenceQuantity)) ?? 0;
                    if (entity == null)
                    {
                        // å¦‚果不存在,新建一条
@@ -111,7 +111,9 @@
                            Goods_no = detail.productCode ?? detail.productName,
                            Reservoirarea = "默认库区",
                            Batch_num = detail.batchNo,
                            //业务库存
                            Business_qty = 0,
                            //实际库存
                            Actual_qty = 0
                        };
                    }
@@ -135,15 +137,16 @@
                            {
                                foreach (var stock in detail.stocktakingDetails)
                                {
                                    //每次都只拿一条明细中的数量
                                    decimal diff = Convert.ToDecimal(stock.differenceQuantity);
                                    if (stock.IsProfit == "1") // ç›˜ç›ˆ
                                    {
                                        entity.Actual_qty += diff;
                                        entity.Business_qty += diff;//如果盘盈了多了,就将业务数量+,盘亏则相反
                                    }
                                    else // ç›˜äº
                                    {
                                        entity.Actual_qty -= diff;
                                        if (entity.Actual_qty < 0) entity.Actual_qty = 0;
                                        entity.Business_qty -= diff;
                                        //if (entity.Business_qty < 0) entity.Business_qty = 0;
                                    }
                                }
                            }
@@ -152,19 +155,21 @@
                    // ä¿å­˜æ•°æ®
                    if (entity.Id == 0) // æ–°å»º
                        Db.Insertable(entity).ExecuteCommand();
                        BaseDal.Db.Insertable(entity).ExecuteCommand();
                    else // æ›´æ–°
                        Db.Updateable(entity).ExecuteCommand();
                        BaseDal.Db.Updateable(entity).ExecuteCommand();
                }
                BaseDal.Db.Ado.CommitTran(); // æäº¤äº‹åŠ¡
                return new ApiResponse<Dt_Inventory> {code = "0", msg = "成功" };
            }
            catch (Exception ex)
            {
                BaseDal.Db.Ado.RollbackTran();//回滚事务
                return new ApiResponse<Dt_Inventory> { code = "500", msg = ex.Message };
            }
        }