修改线下订单退单记录罐码

工作笔记 工作记录
📅 2025-07-31 14:22 👤 admin

private dynamic ReturnScanOrderSubmit(WebParameterContext webContext)
{
    var response = new ServerContext<dynamic>() { ResponseMessage = new ResponseMessage() { Success = true } };
    ActionReturnResponseMessage = () =>
    {
        TransactionResult<dynamic> transactionResult = new TransactionResult<dynamic>();
        response = VerifyReturnScanData(webContext);
        if (response.ResponseMessage.Success == false)
        {
            return response;
        }
        try
        {
            var result = Dalserver.Base.BatchExecuteForTransaction(() =>
            {
                try
                {

                    LogUtil.DoLog("ReturnOrderSubmit-webContext", JSON.ToJSON(webContext));
                    // 获取请求体中的数据
                    string tank_codes = string.Join(",", webContext.RequestBody.tank_codes.ToArray());
                    string StoreID = webContext?.StoreID.ToString();

                    dynamic Datas = DynamicExtension.EmptyDynamic;

                    Datas = Dalserver.DA.Query("CT_GetOffIDData", new { codes = tank_codes, FK_StoreID = StoreID });
                    if (Datas != null && Datas is IEnumerable<dynamic> list && ((IEnumerable<dynamic>)Datas).ToList().Count > 0)
                    {
                        var returnData = new List<dynamic>();
                        returnData = ((IEnumerable<dynamic>)Datas).ToList();

                        var idList = returnData
                            .Select(x => x.ID)
                            .ToList();

                        foreach (var id in idList)
                        {
                            dynamic updatequery = DynamicExtension.EmptyDynamic;
                            updatequery.ID = id;
                            dynamic updateObj = DynamicExtension.EmptyDynamic;
                            updateObj.Status = 2;
                            Dalserver.Base.DataLogicFactory(new SqlDynamicContext("TBProductTankCode", OperationType.EDITE, updatequery, updateObj));
                        }

                        var orderList = returnData
                            .GroupBy(x => x.OrderID)
                            .Select(g => new
                            {
                                OrderID = g.Key,
                            })
                            .ToList();
                        // 为每个OrderID创建一个单独的ProductReturnCreate
                        Dictionary<string, dynamic> orderReturnMap = new Dictionary<string, dynamic>();

                        foreach (var order in orderList)
                        {
                            string orderNumber = GetOrderNumberByID(order.OrderID?.ToString()); // 假设有这个方法获取订单号
                            dynamic returnID = ProductReturnCreate(webContext.Account.Id.ToString(), 0, webContext.StoreID?.ToString(), orderNumber);
                            orderReturnMap.Add(order.OrderID?.ToString(), returnID);
                        }
                        var detailList = returnData
                            .GroupBy(x => new { DetailID = x.DetailID, OrderID = x.OrderID })
                            .Select(g => new
                            {
                                DetailID = g.Key.DetailID,
                                ProductCount = g.Count(),
                                ProductID = g.First().ProductID,
                                OrderID = g.First().OrderID,
                                OnlinePrice = Convert.ToDecimal(g.First().OnlinePrice),
                                TankCodes = g.Select(x => x.TankCode).Where(tc => !string.IsNullOrEmpty(tc)).Distinct().ToList()
                            })
                            .ToList();


                        foreach (var detail in detailList)
                        {
                            UpdateProductOrderDetail(detail.DetailID, detail.ProductCount);
                            dynamic returnID = orderReturnMap[detail.OrderID?.ToString()];
                            CreateReturnDetail(detail, returnID, webContext.Account.Id.ToString(), 1);
                        }

                        foreach (var order in orderList)
                        {
                            UpdateMainProductOrder(order.OrderID);
                        }

                        foreach (var returnID in orderReturnMap.Values)
                        {
                            UpdateMainReturnProductOrder(returnID);
                        }

                        var ProductList = returnData
                            .GroupBy(x => x.ProductID)
                            .Select(g => new
                            {
                                ProductID = g.Key,
                                TankCodes = g.Select(x => x.TankCode).Where(tc => !string.IsNullOrEmpty(tc)).Distinct().ToList(),
                                TankCodeCount = g.Select(x => x.TankCode).Where(tc => !string.IsNullOrEmpty(tc)).Distinct().ToList().Count
                            })
                            .ToList();
                        foreach (var detail in ProductList)
                        {
                            string Message = ReturnStock(detail.ProductID?.ToString(), webContext.StoreID, (int)detail.TankCodeCount, detail.TankCodes);
                            if (!string.IsNullOrEmpty(Message))
                            {
                                response.ResponseMessage.ErrorMessage = Message;
                                response.ResponseMessage.ErrorCode = 500;
                                response.ResponseMessage.Success = false;
                                response.Status = DynamicExtension.EmptyDynamic;
                                return false;
                            }
                        }
                        response.ResponseMessage.ErrorMessage = "退单成功!";
                        response.ResponseMessage.ErrorCode = 200;
                        response.Status = string.Join(",", orderReturnMap.Values);
                    }
                    else
                    {
                        response.ResponseMessage.ErrorMessage = "未查询到订单!";
                        response.ResponseMessage.ErrorCode = 500;
                        response.ResponseMessage.Success = false;
                        response.Status = DynamicExtension.EmptyDynamic;
                        return false;
                    }

                    return true;
                }
                catch (Exception e)
                {
                    LogUtil.LogExceptionInfo("ReturnOrderSubmit-接口异常", e);
                    response.ResponseMessage.Success = false;
                    response.ResponseMessage.ErrorMessage = e.Message;
                    response.ResponseMessage.ErrorCode = 500;
                    return false;
                }
            }, transactionResult);



            return response;
        }
        catch (Exception ex)
        {
            LogUtil.LogExceptionInfo("ReturnOrderSubmit-接口异常", ex);
            response.ResponseMessage.Success = false;
            response.ResponseMessage.ErrorCode = 500;
            response.ResponseMessage.ErrorMessage = "程序异常!" + ex.Message;
            return response;
        }

    };
    return webContext;
}
private dynamic CreateReturnDetail(dynamic detail, dynamic returnID, dynamic AccountID, int ReturnType)
{
    ServerContext<dynamic> data = new ServerContext<dynamic>();
    dynamic factory = DynamicExtension.EmptyDynamic;
    factory.FK_TBProduct_ID = detail.ProductID?.ToString();
    factory.ProductCode = GetProductCodeByID(detail.ProductID?.ToString());
    factory.ProductName = GetProductNameByID(detail.ProductID?.ToString());
    factory.QuantityNum = detail.ProductCount; // 总数量
    factory.QuantityMoney = (decimal)detail.OnlinePrice * (int)detail.ProductCount;
    factory.FK_TBProductReturn_ID = returnID;
    factory.FK_TBProductOrder_ID = detail.OrderID?.ToString(); // 订单来源(固定线下)
    factory.FK_TBProductOrderDetail_ID = detail.DetailID?.ToString(); // 订单来源(固定线下)
    factory.FK_SystemUser_Create_ID = AccountID;    // 创建人ID
    factory.FK_SystemUser_Modify_ID = AccountID;    // 修改人ID
    factory.IsValid = 1;
    factory.CreateTime = DateTime.Now;
    factory.ModifyTime = DateTime.Now;
    SqlDynamicContext sql = new SqlDynamicContext("TBProductReturnDetail", OperationType.ADD, factory: factory);
    data = Dalserver.Base.DataLogicFactory(sql);//新增
    var detailID = data.Status?.ToString();
    if (ReturnType == 1)
    {
        foreach (var tankCode in detail.TankCodes)
        {
            if (!string.IsNullOrEmpty(tankCode?.ToString()))
            {
                CreateReturnTankCode(tankCode, returnID?.ToString(), detail.ProductID?.ToString(), AccountID, detailID);
            }
        }
    }
    return data.Status;


}