private ActionResult DoRMA(RMARequest request, UserModel authUser,bool ifNeedValidate)
        {
            var dbContext = Context;
            var orderEntity = dbContext.Set<OrderEntity>().Where(o => o.OrderNo == request.OrderNo && o.CustomerId == authUser.Id).FirstOrDefault();
            if (orderEntity == null)
            {
                return this.RenderError(m => m.Message = "订单号不存在");
            }
          
            if (ifNeedValidate)
            {
                if (orderEntity.Status != (int)OrderStatus.Shipped)
                {
                    return this.RenderError(m => m.Message = "订单状态现在不能申请退货");
                }
                var rmaEntity = dbContext.Set<RMAEntity>().Where(r => r.OrderNo == request.OrderNo
                                 && r.Status != (int)RMAStatus.Reject2Customer && r.Status != (int)RMAStatus.Void).FirstOrDefault();
                if (rmaEntity != null)
                {
                    return this.RenderError(m => m.Message = "已经申请了退货单,不能再次申请!");
                }
            }
            var erpRma = new
            {
                dealCode = orderEntity.OrderNo,
                CUSTOMER_FRT = "0",
                COMPANY_FRT = "0",
                REAL_NAME = orderEntity.ShippingContactPerson,
                USER_SID = "0",
                Detail = new List<dynamic>()
            };
            request.Reason = string.Format("{0}-{1}",dbContext.Set<RMAReasonEntity>().Find(request.RMAReason).Reason,request.Reason??string.Empty);
            using (var ts = new TransactionScope())
            {
                decimal rmaAmount = 0;
                var newRma = _rmaRepo.Insert(new RMAEntity()
                {
                    RMANo = OrderRule.CreateRMACode(),
                    Reason = request.Reason,
                    RMAReason = request.RMAReason,
                    Status = (int)RMAStatus.Created,
                    OrderNo = request.OrderNo,
                    CreateDate = DateTime.Now,
                    CreateUser = authUser.Id,
                    RMAAmount = rmaAmount,
                    RMAType = (int)RMAType.FromOnline,
                    UpdateDate = DateTime.Now,
                    UpdateUser = authUser.Id,
                    ContactPhone = request.ContactPhone,
                    UserId = orderEntity.CustomerId
                });
                foreach (var rma in request.Products2)
                {
                    var orderItemEntity = dbContext.Set<OrderItemEntity>().Where(o => o.OrderNo == request.OrderNo && o.ProductId == rma.ProductId && o.ColorValueId == rma.Properties.ColorValueId && o.SizeValueId == rma.Properties.SizeValueId).FirstOrDefault();
                    if (orderItemEntity == null)
                        return this.RenderError(r => r.Message = string.Format("{0} not in order", rma.ProductId));
                    if (orderItemEntity.Quantity < rma.Quantity)
                        return this.RenderError(r => r.Message = string.Format("{0} 超出购买数量", rma.ProductId));
                    rmaAmount += orderItemEntity.ItemPrice * rma.Quantity;
                    _rmaitemRepo.Insert(new RMAItemEntity()
                    {
                        CreateDate = DateTime.Now,
                        ItemPrice = orderItemEntity.ItemPrice,
                        ExtendPrice = orderItemEntity.ItemPrice * rma.Quantity,
                        ProductDesc = orderItemEntity.ProductDesc,
                        ProductId = orderItemEntity.ProductId,
                        ColorValueId = orderItemEntity.ColorValueId,
                        SizeValueId = orderItemEntity.SizeValueId,
                        Quantity = rma.Quantity,
                        RMANo = newRma.RMANo,
                        Status = (int)DataStatus.Normal,
                        UnitPrice = orderItemEntity.UnitPrice,
                        SizeValueName = orderItemEntity.SizeValueName,
                        ColorValueName = orderItemEntity.ColorValueName,
                        UpdateDate = DateTime.Now,
                        StoreItem = orderItemEntity.StoreItemNo,
                        StoreDesc = orderItemEntity.StoreItemDesc

                    });
                    var exInventory = Context.Set<InventoryEntity>().Where(i => i.ProductId == rma.ProductId && i.PColorId == rma.Properties.ColorValueId && i.PSizeId == rma.Properties.SizeValueId).FirstOrDefault();
                    if (exInventory == null)
                        return this.RenderError(r => r.Message = string.Format("{0} no channel product", rma.ProductId));
                    erpRma.Detail.Add(new
                    {
                        SelectPro_detail_sid = exInventory.ChannelInventoryId,
                        RefundNum = rma.Quantity
                    });
                }
                newRma.RMAAmount = rmaAmount;
                _rmaRepo.Update(newRma);

                _rmalogRepo.Insert(new RMALogEntity
                {
                    CreateDate = DateTime.Now,
                    CreateUser = authUser.Id,
                    RMANo = newRma.RMANo,
                    Operation = "申请线上退货"
                });
                string exRMANo = string.Empty;
                bool isSuccess = ErpServiceHelper.SendHttpMessage(ConfigManager.ErpBaseUrl, new { func = "Agree_Refund", jsonSales = new { Data = new List<dynamic>() { erpRma } } }, r => exRMANo = r.orders_refund_frt_sid
                  , null);
                if (isSuccess)
                {
                    _rmaexRepo.Insert(new RMA2ExEntity()
                    {
                        ExRMA = exRMANo??string.Empty,
                        RMANo = newRma.RMANo,
                        UpdateDate = DateTime.Now
                    });
                    ts.Complete();
                    return this.RenderSuccess<MyRMAResponse>(r => r.Data = new MyRMAResponse().FromEntity<MyRMAResponse>(newRma));
                }
                else
                {
                    return this.RenderError(r => r.Message = "创建失败");
                }
            }
        }
        public ActionResult RMA(RMARequest request, UserModel authUser)
        {

            return DoRMA(request, authUser, true);
        }