yanjinhui
2025-10-20 614945e153d38d1dadf5beb1e1d4dbc6db07c226
н¨Îļþ¼Ð/WIDESEA_WMSServer/WIDESEA_SquareCabinServices/InventoryServices.cs
@@ -20,8 +20,10 @@
{
    public class InventoryServices : ServiceBase<Dt_Inventory, IRepository<Dt_Inventory>>, IInventoryServices
    {
        public InventoryServices(IRepository<Dt_Inventory> BaseDal) : base(BaseDal)
        private readonly IDeliveryOrderServices _deliveryOrderServices;
        public InventoryServices(IRepository<Dt_Inventory> BaseDal, IDeliveryOrderServices deliveryOrderServices) : base(BaseDal)
        {
            _deliveryOrderServices = deliveryOrderServices;
        }
@@ -38,34 +40,33 @@
            try
            {
                var url = "http://121.37.118.63/GYZ2/95fck/repositoryInfo";
                // å‘起请求
                var result = HttpHelper.Post(url, new { goods_no,batch_num}.ToJsonString());
                var result = HttpHelper.Post(url, new { goods_no, batch_num }.ToJsonString());
                // ååºåˆ—化
                var response = JsonConvert.DeserializeObject<UpstreamResponse<InventoryInfo>>(result);
                if (response.resultCode!="0")
                if (response.resultCode != "0")
                {
                    // è°ƒç”¨å¼‚常接口
                    SendErrorToUpstream(8, "", response.resultMsg ?? "上游接口返回失败", "");
                    return responseContent.Error(response.resultMsg ?? "上游接口返回失败");
                }
                if (response.data == null || !response.data.Any())
                {
                    return responseContent.OK("无新库存数据");
                }
                Db.Ado.BeginTran();
                foreach (var item in response.data)
                {
                    var Inver = new Dt_Inventory
                    {
                        Goods_no = item.goods_no,
                        Batch_num = item.batch_num,
                        Exp_date=item.exp_date,
                        Business_qty = item.business_qty,
                        Actual_qty = item.actural_qty,
                    };
                    AddData(Inver);
                    // ä½¿ç”¨ FirstOrDefault é¿å…æ‰¾ä¸åˆ°è®°å½•时抛出异常
                    var Inver = Db.Queryable<Dt_Inventory_Batch>()
                        .First(x => x.MaterielCode == item.goods_no && x.BatchNo == item.batch_num);
                    if (Inver != null)
                    {
                        Inver.ERPStockQuantity = item.business_qty;
                        Db.Updateable(Inver).ExecuteCommand();
                    }
                }
                Db.Ado.CommitTran();
                return responseContent.OK("库存信息同步完成");
@@ -96,12 +97,11 @@
                {
                    return new ApiResponse<Dt_InventoryInfo> { code = "500", msg = "请求参数无效" };
                }
                // 2️⃣ å¼€å¯äº‹åŠ¡
                BaseDal.Db.Ado.BeginTran();
                foreach (var detail in request.details)
                {
                     var goods = Db.Queryable<Dt_MaterielInfo>().Where(x => x.MaterielCode == detail.productCode).First();
                    // 3️⃣ è®¡ç®—入库数量(取正)
                    decimal orderQty = detail.orderDetails?
                        .Sum(x => decimal.TryParse(x.quantity, out var q) ? Math.Abs(q) : 0)
@@ -109,7 +109,9 @@
                    // 5️⃣ æŸ¥è¯¢åº“存详情
                    var entity = BaseDal.Db.Queryable<Dt_InventoryInfo>()
                        .First(x => x.MaterielCode == detail.productCode && x.BatchNo == detail.batchNo);
                        .First(x => x.MaterielCode == detail.productCode && x.BatchNo == detail.batchNo&&x.LocationCode=="立库");
                    //查询物料表
                    var Goods = BaseDal.Db.Queryable<Dt_MaterielInfo>().First(x => x.MaterielCode == detail.productCode);
                    if (entity == null)
                    {
@@ -117,7 +119,7 @@
                        {
                            PalletCode = detail.orderDetails?.FirstOrDefault()?.palletCode ?? "",
                            WarehouseCode = "001",
                            LocationCode = "",
                            LocationCode = "立库",
                            StockStatus = 1,
                            MaterielCode = detail.productCode ?? detail.productName,
                            MaterielName = detail.productName ?? "",
@@ -164,21 +166,27 @@
                            entity.StockQuantity += orderQty;
                            entity.InDate = DateTime.Now;
                            entity.Remark = "入库单回传";
                            //Goods.Business_qty=
                            batch.StockQuantity += orderQty;
                            batch.Remark = "入库单回传";
                            break;
                        case "2": // å‡ºåº“
                            entity.OutboundQuantity += orderQty;
                            entity.OutboundQuantity += orderQty; //出库数量
                            entity.StockQuantity -= orderQty;
                            if (entity.StockQuantity < 0) entity.StockQuantity = 0;
                            entity.Remark = "出库单回传";
                            //CreateAllocatInOut()
                            batch.OutboundQuantity += orderQty;
                            batch.StockQuantity -= orderQty;
                            if (batch.StockQuantity < 0) batch.StockQuantity = 0;
                            batch.Remark = "出库单回传";
                            //调拨任务
                            _deliveryOrderServices.CreateAllocatInOut(goods);
                            break;
                        case "3": // ç›˜ç‚¹
@@ -201,7 +209,8 @@
                            }
                            break;
                    }
                    // 2️⃣ å¼€å¯äº‹åŠ¡
                    BaseDal.Db.Ado.BeginTran();
                    // 8️⃣ ä¿å­˜æ•°æ®
                    if (entity.Id == 0)
                        BaseDal.Db.Insertable(entity).ExecuteCommand();