/// <summary> /// 添加商品到购物车 /// </summary> public async Task <AddShopCarResponse> AddCarAsync([FromBody] AddShopCarRequest request) { var response = new AddShopCarResponse(); try { var colorId = _context.ColorVersionEntitys.FirstOrDefault(a => a.VersionID == request.VersionID)?.PKID; if (!colorId.HasValue) { response.Successful = false; response.Message = "该版本不存在,请重新选择!"; } var unitPrice = _context.PriceEntitys.FirstOrDefault(p => p.ProductID == request.ProductID && p.VersionID == request.VersionID)?.Price; unitPrice = unitPrice ?? 0; var userInfo = accountService.GetUserInfoByUserNameAsync(new Account.Model.Request.GetShopCarByUserNameRequest { UserName = "******" }); var shopCar = _context.ShopCarEntitys.SingleOrDefault(a => a.UserId == userInfo.userInfo.PKID); if (shopCar == null) { _context.ShopCarEntitys.Add(new ShopCarEntity { UserId = userInfo.userInfo.PKID }); _context.SaveChanges(); shopCar = _context.ShopCarEntitys.SingleOrDefault(a => a.UserId == userInfo.userInfo.PKID); } var carListItem = _context.CarListEntitys.SingleOrDefault(a => a.CarID == shopCar.PKID && a.ProductID == request.ProductID && a.VersionID == request.VersionID); if (carListItem != null) { carListItem.Count += 1; _context.Update(carListItem); } else { _context.CarListEntitys.Add(new CarListEntity { CarID = shopCar.PKID, ProductID = request.ProductID, VersionID = request.VersionID, Count = 1, UnitPrice = unitPrice.Value, ColorID = colorId.Value, IsCheck = true }); } await _context.SaveChangesAsync(); } catch (Exception ex) { response.Successful = false; response.Message = ex.Message; logger.LogError(ex, $"AddCarAsync方法报错 Message:{JsonConvert.SerializeObject(request)}"); } return(response); }
public async Task <RegisterResponse> Register([FromBody] RegisterRequest request) { RegisterResponse response = new RegisterResponse(); response.Message = "收到请求,用户名:" + request.UserName; try { var user = _context.UserEntities.FirstOrDefault(a => a.CustomerPhone.Equals(request.UserName)); if (user != null) { response.Successful = false; response.Message = "该用户名已存在!"; } UserEntity entity = new UserEntity { CustomerPhone = request.UserName, CustomerPwd = request.Password, LastLoginTime = DateTime.Now, ErrorLogin = 0 }; _context.UserEntities.Add(entity); await _context.SaveChangesAsync(); } catch (Exception ex) { response.Successful = false; response.Message = ex.Message; } return(response); }