Пример #1
0
        public ResponseOutput CreateNewOrder(OrderInputDto inputDto)
        {
            try
            {
                _logger.LogInformation("进入CreateNewOrder方法");
                if (inputDto == null)
                {
                    return(new ResponseOutput(null, "-1", "传入参数为空", HttpContext.TraceIdentifier));
                }

                if (inputDto.CouponCode == "0")
                {
                    return(new ResponseOutput(null, "-1", "券号不对", HttpContext.TraceIdentifier));
                }
                if (string.IsNullOrWhiteSpace(inputDto.CouponCode) || string.IsNullOrWhiteSpace(inputDto.Contactor) ||
                    string.IsNullOrWhiteSpace(inputDto.MobilePhone) || string.IsNullOrWhiteSpace(inputDto.Province) ||
                    string.IsNullOrWhiteSpace(inputDto.City) || string.IsNullOrWhiteSpace(inputDto.District) ||
                    string.IsNullOrWhiteSpace(inputDto.Address))
                {
                    return(new ResponseOutput(null, "-1", "必填字段缺失", HttpContext.TraceIdentifier));
                }

                var coupon = _couponService.GetCouponByCode(inputDto.CouponCode);
                if (coupon == null)
                {
                    return(new ResponseOutput(null, "-1", "券不存在", HttpContext.TraceIdentifier));
                }
                //判断券的次数,不够的话就不能下订单
                if (coupon.AvaliableCount <= 0)
                {
                    return(new ResponseOutput(null, "-1", "此券次数已用完,不能下单", HttpContext.TraceIdentifier));
                }


                List <OrderDetail> details = new List <OrderDetail>();

                if (inputDto.DetailDtos == null && inputDto.DetailDtos.Count == 0)
                {
                    return(new ResponseOutput(null, "-1", "商品明细为空", HttpContext.TraceIdentifier));
                }

                foreach (var item in inputDto.DetailDtos)
                {
                    ProductInformation productInfo = _productService.GetProductById(item.ProductId);
                    if (productInfo == null)
                    {
                        return(new ResponseOutput(null, "-1", "商品明细为空", HttpContext.TraceIdentifier));
                    }
                    details.Add(new OrderDetail
                    {
                        ProductId    = item.ProductId,
                        ProductCode  = productInfo.ProductCode,
                        ProductName  = productInfo.ProductName,
                        ProductCount = item.Count,
                    });
                }

                long     currentTicks  = DateTime.Now.Ticks;
                DateTime dtFrom        = new DateTime(1970, 1, 1, 0, 0, 0, 0);
                long     currentMillis = (currentTicks - dtFrom.Ticks) / 10000;
                Order    order         = new Order
                {
                    OrderCode   = currentMillis.ToString(),//订单号用随机数
                    CreateTime  = DateTime.Now,
                    CouponCode  = inputDto.CouponCode,
                    Contactor   = inputDto.Contactor,
                    MobilePhone = inputDto.MobilePhone,
                    Province    = inputDto.Province,
                    City        = inputDto.City,
                    District    = inputDto.District,
                    Address     = inputDto.Address,
                    ZipCode     = int.Parse(inputDto.ZipCode ?? "0"),
                    Telephone   = inputDto.Telephone ?? string.Empty,
                    Details     = details
                };

                #region 事务处理

                var ret = _transactionService.CreateNewOrderTransaction(order, coupon);

                #endregion

                //发送手机短信给用户,用消息队列或者单独开个进程试一下
                _mqProducer.SendMessage(inputDto.MobilePhone);

                if (ret)
                {
                    return(new ResponseOutput(null, "0", "创建订单成功", HttpContext.TraceIdentifier));
                }

                return(new ResponseOutput(null, "-1", "创建订单失败", HttpContext.TraceIdentifier));
            }
            catch (Exception ex)
            {
                _logger.LogError($"异常为{ex.ToString()}");
                return(new ResponseOutput(null, "-1", ex.Message, HttpContext.TraceIdentifier));
            }
        }