1
dengjunjie
2025-10-17 36226b8adcbfb46fe71fc9526134a54886b3312c
н¨Îļþ¼Ð/WIDESEA_WMSServer/WIDESEA_SquareCabinServices/InventoryServices.cs
@@ -84,95 +84,239 @@
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public ApiResponse<Dt_Inventory> OrderFeedback(EdiOrderCallbackRequest request)
        //public ApiResponse<Dt_Inventory> OrderFeedback(EdiOrderCallbackRequest request)
        //{
        //    try
        //    {
        //        if (request == null || request.details == null || !request.details.Any())
        //        {
        //            return new ApiResponse<Dt_Inventory> { code = "500", msg = "请求参数无效" };
        //        }
        //        BaseDal.Db.Ado.BeginTran();//开启事务
        //        foreach (var detail in request.details)
        //        {
        //            // å…ˆæŸ¥åº“存是否存在(按照产品编号和批次来寻找)
        //            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 => decimal.TryParse(x.quantity, out var q) ? q : 0) ?? 0;
        //            //盘点盘亏
        //            decimal diffQty = detail.stocktakingDetails?.Sum(x => Convert.ToDecimal(x.differenceQuantity)) ?? 0;
        //            if (entity == null)
        //            {
        //                // å¦‚果不存在,新建一条
        //                entity = new Dt_Inventory
        //                {
        //                    Goods_no = detail.productCode ?? detail.productName,
        //                    Reservoirarea = "默认库区",
        //                    Batch_num = detail.batchNo,
        //                    //业务库存
        //                    Business_qty = 0,
        //                    //实际库存
        //                    Actual_qty = 0
        //                };
        //            }
        //            switch (request.orderType)
        //            {
        //                case "1": // å…¥åº“
        //                    entity.Business_qty += orderQty;
        //                    entity.Actual_qty += orderQty;
        //                    break;
        //                case "2": // å‡ºåº“
        //                    entity.Business_qty -= orderQty;
        //                    entity.Actual_qty -= orderQty;
        //                    if (entity.Business_qty < 0) entity.Business_qty = 0; // å¯é€‰ï¼šé˜²æ­¢è´Ÿæ•°
        //                    if (entity.Actual_qty < 0) entity.Actual_qty = 0;
        //                    break;
        //                case "3": // ç›˜ç‚¹
        //                    if (detail.stocktakingDetails != null && detail.stocktakingDetails.Any())
        //                    {
        //                        foreach (var stock in detail.stocktakingDetails)
        //                        {
        //                            //每次都只拿一条明细中的数量
        //                            decimal diff = Convert.ToDecimal(stock.differenceQuantity);
        //                            if (stock.IsProfit == "1") // ç›˜ç›ˆ
        //                            {
        //                                entity.Business_qty += diff;//如果盘盈了多了,就将业务数量+,盘亏则相反
        //                                entity.PalletCode = stock.palletCode;
        //                            }
        //                            else // ç›˜äº
        //                            {
        //                                entity.Business_qty -= diff;
        //                                entity.PalletCode = stock.palletCode;
        //                            }
        //                        }
        //                    }
        //                    break;
        //            }
        //            // ä¿å­˜æ•°æ®
        //            if (entity.Id == 0) // æ–°å»º
        //                BaseDal.Db.Insertable(entity).ExecuteCommand();
        //            else // æ›´æ–°
        //                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 };
        //    }
        //}
        public ApiResponse<Dt_InventoryInfo> OrderFeedback(EdiOrderCallbackRequest request)
        {
            try
            {
                if (request == null || request.details == null || !request.details.Any())
                {
                    return new ApiResponse<Dt_Inventory> { code = "500", msg = "请求参数无效" };
                    return new ApiResponse<Dt_InventoryInfo> { code = "500", msg = "请求参数无效" };
                }
                BaseDal.Db.Ado.BeginTran();//开启事务
                BaseDal.Db.Ado.BeginTran(); // å¼€å¯äº‹åŠ¡
                foreach (var detail in request.details)
                {
                    // å…ˆæŸ¥åº“存是否存在(按照产品编号和批次来寻找)
                    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 => decimal.TryParse(x.quantity, out var q) ? Math.Abs(q) : 0)
                        ?? 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;
                    // ç›˜ç‚¹å·®å¼‚æ•°
                    decimal diffQty = detail.stocktakingDetails?
                        .Sum(x => Convert.ToDecimal(x.differenceQuantity))
                        ?? 0;
                    // æŸ¥è¯¢çŽ°æœ‰åº“å­˜æ˜Žç»†
                    var entity = BaseDal.Db.Queryable<Dt_InventoryInfo>()
                        .First(x => x.MaterielCode == detail.productCode && x.BatchNo == detail.batchNo);
                    if (entity == null)
                    {
                        // å¦‚果不存在,新建一条
                        entity = new Dt_Inventory
                        //不存在则新建库存明细记录
                        entity = new Dt_InventoryInfo
                        {
                            Goods_no = detail.productCode ?? detail.productName,
                            Reservoirarea = "默认库区",
                            Batch_num = detail.batchNo,
                            //业务库存
                            Business_qty = 0,
                            //实际库存
                            Actual_qty = 0
                            PalletCode = detail.orderDetails?.FirstOrDefault()?.palletCode ?? "",
                            WarehouseCode = 1.ToString(),  // é»˜è®¤åº“房编号
                            LocationCode = "",
                            StockStatus = 1,   // ç«‹åº“固定为 1
                            MaterielCode = detail.productCode ?? detail.productName,
                            MaterielName = detail.productName ?? "",
                            MaterielSpec = detail.productSpecifications ?? "",
                            BatchNo = detail.batchNo,
                            StockQuantity = 0,
                            OutboundQuantity = 0,
                            SupplyQuantity = 0,
                            InDate = DateTime.Now,
                            ProductionDate = detail.finishDate.ToString("yyyy-MM-dd"),
                            ShelfLife = 0,
                            ValidityPeriod = "",
                            Remark = "WCS回传创建"
                        };
                    }
                    // æ‰¹æ¬¡è¡¨è®°å½•(用于汇总)
                    var batch = BaseDal.Db.Queryable<Dt_Inventory_Batch>()
                        .First(x => x.MaterielCode == detail.productCode && x.BatchNo == detail.batchNo);
                    if (batch == null)
                    {
                        batch = new Dt_Inventory_Batch
                        {
                            MaterielCode = detail.productCode ?? detail.productName,
                            MaterielName = detail.productName ?? "",
                            MaterielSpec = detail.productSpecifications ?? "",
                            BatchNo = detail.batchNo,
                            StockQuantity = 0,
                            OutboundQuantity = 0,
                            SupplyQuantity = 0,
                            ERPStockQuantity = 0,
                            Status = false,
                            ProductionDate = detail.finishDate.ToString("yyyy-MM-dd"),
                            ValidityPeriod = DateTime.Now.AddYears(1).ToString("yyyy-MM-dd"),
                            Remark = "自动创建"
                        };
                    }
                    switch (request.orderType)
                    {
                        case "1": // å…¥åº“
                            entity.Business_qty += orderQty;
                            entity.Actual_qty += orderQty;
                            entity.StockQuantity += orderQty;
                            entity.InDate = DateTime.Now;
                            entity.Remark = "入库单回传";
                            batch.StockQuantity += orderQty;
                            batch.Remark = "入库单回传";
                            break;
                        case "2": // å‡ºåº“
                            entity.Business_qty -= orderQty;
                            entity.Actual_qty -= orderQty;
                            if (entity.Business_qty < 0) entity.Business_qty = 0; // å¯é€‰ï¼šé˜²æ­¢è´Ÿæ•°
                            if (entity.Actual_qty < 0) entity.Actual_qty = 0;
                            entity.OutboundQuantity += orderQty;
                            entity.StockQuantity -= orderQty;
                            if (entity.StockQuantity < 0) entity.StockQuantity = 0;
                            entity.Remark = "出库单回传";
                            batch.OutboundQuantity += orderQty;
                            batch.StockQuantity -= orderQty;
                            if (batch.StockQuantity < 0) batch.StockQuantity = 0;
                            batch.Remark = "出库单回传";
                            break;
                        case "3": // ç›˜ç‚¹
                        case "3": //盘点
                            if (detail.stocktakingDetails != null && detail.stocktakingDetails.Any())
                            {
                                foreach (var stock in detail.stocktakingDetails)
                                {
                                    //每次都只拿一条明细中的数量
                                    decimal diff = Convert.ToDecimal(stock.differenceQuantity);
                                    if (stock.IsProfit == "1") // ç›˜ç›ˆ
                                    {
                                        entity.Business_qty += diff;//如果盘盈了多了,就将业务数量+,盘亏则相反
                                        entity.PalletCode = stock.palletCode;
                                        entity.SupplyQuantity += Math.Abs(diff);
                                        batch.SupplyQuantity += Math.Abs(diff);
                                    }
                                    else // ç›˜äº
                                    {
                                        entity.Business_qty -= diff;
                                        entity.PalletCode = stock.palletCode;
                                        entity.SupplyQuantity -= Math.Abs(diff);
                                        batch.SupplyQuantity -= Math.Abs(diff);
                                        if (entity.SupplyQuantity < 0) entity.SupplyQuantity = 0;
                                        if (batch.SupplyQuantity < 0) batch.SupplyQuantity = 0;
                                    }
                                    entity.PalletCode = stock.palletCode;
                                    entity.Remark = "盘点单回传";
                                    batch.Remark = "盘点单回传";
                                }
                            }
                            break;
                    }
                    // ä¿å­˜æ•°æ®
                    if (entity.Id == 0) // æ–°å»º
                    //保存到数据库
                    if (entity.Id == 0)
                        BaseDal.Db.Insertable(entity).ExecuteCommand();
                    else // æ›´æ–°
                    else
                        BaseDal.Db.Updateable(entity).ExecuteCommand();
                    if (batch.Id == 0)
                        BaseDal.Db.Insertable(batch).ExecuteCommand();
                    else
                        BaseDal.Db.Updateable(batch).ExecuteCommand();
                }
                BaseDal.Db.Ado.CommitTran(); // æäº¤äº‹åŠ¡
                return new ApiResponse<Dt_Inventory> { code = "0", msg = "成功" };
                BaseDal.Db.Ado.CommitTran();
                return new ApiResponse<Dt_InventoryInfo> { code = "0", msg = "成功" };
            }
            catch (Exception ex)
            {
                BaseDal.Db.Ado.RollbackTran();//回滚事务
                return new ApiResponse<Dt_Inventory> { code = "500", msg = ex.Message };
                BaseDal.Db.Ado.RollbackTran();
                return new ApiResponse<Dt_InventoryInfo> { code = "500", msg = ex.Message };
            }
        }