yanjinhui
2025-10-14 ff4660980ccfe3e123df8d5fa820266784625c74
´úÂë¹ÜÀí/WIDESEA_WMSServer/WIDESEA_SquareCabinServices/InventoryServices.cs
@@ -9,12 +9,13 @@
using WIDESEA_Core;
using WIDESEA_Core.BaseServices;
using WIDESEA_Core.Helper;
using WIDESEA_DTO.SquareCabin;
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;
namespace WIDESEA_SquareCabinServices
{
@@ -75,6 +76,103 @@
            }
        }
        /// <summary>
        /// wcs回传给我
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        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;//如果盘盈了多了,就将业务数量+,盘亏则相反
                                    }
                                    else // ç›˜äº
                                    {
                                        entity.Business_qty -= diff;
                                        //if (entity.Business_qty < 0) entity.Business_qty = 0;
                                    }
                                }
                            }
                            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 };
            }
        }
        /// <summary>
        /// æŽ¨é€å¼‚常信息给上游系统1.入库单接口;2.入库单报完成接口;3.出库单接口;4.出库报完成接口;5.药品基础信息同步接口;6.供应商信息接口;7.客户信息接口;8.库存
        /// </summary>