Пример #1
0
        /// <summary>
        /// 下单
        /// </summary>
        /// <param name="IP"></param>
        /// <param name="req"></param>
        /// <returns></returns>
        public OrderRes SubmitOrder(string IP, OrderReq req)
        {
            DateTime startTime = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd 00:00:00"));
            DateTime endTime   = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd 23:59:59"));


            if (req.ShopId <= 0)
            {
                throw new Exception($"参数错误,{nameof(req.ShopId)}");
            }
            if (req.MaterialId <= 0)
            {
                throw new Exception($"参数错误,{nameof(req.ShopId)}");
            }

            if (string.IsNullOrEmpty(req.Name) || string.IsNullOrEmpty(req.IDCardNumber))
            {
                throw new Exception("参数错误,姓名和身份证号码不能为空");
            }

            var canPreNum = CanSubmitOrder(IP
                                           , req.ShopId, req.MaterialId
                                           , req.IDCardNumber
                                           , startTime, endTime, out string err);

            if (canPreNum <= 0)
            {
                throw new Exception($"预定失败,{err}");
            }


            #region order code
            string codeTime = DateTime.Now.ToString("yyyyMMddHHmmssfff");

            int    IDCardNumLen   = req.IDCardNumber.Length;
            int    IDCARD_SUB_LEN = 6;
            string codeIDCardNum  = req.IDCardNumber.Substring(IDCardNumLen - IDCARD_SUB_LEN, IDCARD_SUB_LEN);
            string code           = $"{codeTime}_{codeIDCardNum}_{req.ShopId}_{req.MaterialId}";
            #endregion


            using (var db = DBFactory.nCoVMS())
            {
                var order = new Torder
                {
                    Code         = code,
                    CreateTime   = DateTime.Now,
                    IdcardNumber = req.IDCardNumber ?? "",
                    MaterialId   = req.MaterialId,
                    Name         = req.Name ?? "",
                    Num          = canPreNum,
                    ShopId       = req.ShopId,
                    Status       = (int)EOrderStatus.PRE,
                };
                db.Torder.Add(order);
                db.SaveChanges();
                return(MapperHelper.MapperTo <Torder, OrderRes>(order));
            }
        }
Пример #2
0
    public ActionResult Create(int TgoodsId, int Count, int[] AttributeValueIds, int?AddressId, string longitude, string latitude, string FullAddress)
    {
        Address address = new Address();
        List <TgoodsAttributeValue> values = new List <TgoodsAttributeValue>();
        Tgoods currentGoods = _context.Tgoodses.Include(x => x.TgoodsAttributes).FirstOrDefault(x => x.Id == TgoodsId);

        if (currentGoods == null)
        {
            return(StatusCode(StatusCodes.Status404NotFound));
        }
        var attributes = currentGoods.TgoodsAttributes.ToList();

        if (attributes.Any())
        {
            if ((AttributeValueIds == null) || AttributeValueIds.Length != attributes.Count())
            {
                return(StatusCode(StatusCodes.Status400BadRequest));
            }
            values = attributes.SelectMany(x => x.TgoodsAttributeValues).Where(x => AttributeValueIds.Contains(x.Id)).ToList();
        }
        if (!string.IsNullOrEmpty(latitude) && !string.IsNullOrEmpty(longitude))
        {
            //var user = _userManager.GetCurrentUser();
            string userId = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
            address.Longitude     = longitude;
            address.Latitude      = latitude;
            address.MobileDeliver = userId;
            //address.NameDeliver = user.Name + " " + user.Family;
            address.UserId = 1070;
            // address.CityId = 10;
            address.PhoneDeliver = userId;
            // address.Sector = "1";
            address.FullAddress = (string.IsNullOrEmpty(FullAddress)) ? "" : FullAddress;
            // address.ApplicationUser = user;
            _context.Addresses.Add(address);
            _context.SaveChanges();
        }
        var model = new Torder()
        {
            OrderDate    = DateTime.UtcNow.AddHours(3.5),
            Count        = Count,
            TgoodsId     = TgoodsId,
            UserName     = User.Identity.Name,
            TorderStatus = TorderStatus.Registered,
            AddressId    = (!string.IsNullOrEmpty(latitude) && !string.IsNullOrEmpty(longitude)) ? address.Id : AddressId
                           // TgoodsAttributeValues =
        };

        if (values.Any())
        {
            model.TgoodsAttributeValues = values;
        }
        var result = _context.Torders.Add(model);

        _context.SaveChanges();
        //// string SendTokenToOwnerShopResponse = SendNotificationToShopOwner();
        // string tokenOwnerShop =
        // _userManager.FindByUserName(result.Tgoods.Tstore.UserOwner).PushNotificationToken ?? "";
        //bool SendTokenToOwnerShopResponse = await SendNotificationAsync(tokenOwnerShop, "سفارش جدید", "خرید جدیدی ثبت شده است", result.Id.ToString());
        return(Json(new
        {
            model.Id,
            model.Count,
            model.OrderDate,
            model.TorderStatus,
            model.UserName,
            GoodsTitle = model.Tgoods.Title,
            GoodsId = model.Tgoods.Id,
            StoreTitle = model.Tgoods.Tstore.Title,
            StoreId = model.Tgoods.Tstore.Id,
            Attributes = model.TgoodsAttributeValues.Select(x => x.TgoodsAttribute.Name + " " + x.Value),
            // SendTokenToOwnerShopResponse,
        }));
    }