/// <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 <RoutingKeyRegisterResponse> RoutingKeyRegisterAsync([FromBody] RoutingKeyRegisterRequest request) { RoutingKeyRegisterResponse response = new RoutingKeyRegisterResponse(); try { if (string.IsNullOrEmpty(request.RoutingKey) || string.IsNullOrEmpty(request.ApiUrl)) { response.Successful = false; response.Message = "RoutingKey与ApiUrl不能为空!"; } if (string.IsNullOrEmpty(request.QueueName)) { request.QueueName = Configuration["SubscriptionClientName"]; } var isExistsRoutingKey = _context.RabbitMqRegisterInfo.Any(a => a.RoutingKey == request.RoutingKey); if (isExistsRoutingKey) { response.Successful = false; response.Message = "对应的RoutingKey已存在!"; return(response); } var rabbitMqRegister = new RabbitMQRegisterInfo { RoutingKey = request.RoutingKey, QueueName = request.QueueName, ApiUrl = request.ApiUrl, CreateTime = DateTime.Now, UpdateTime = DateTime.Now }; _context.Add(rabbitMqRegister); _context.SaveChanges(); //绑定RoutingKey和队列 _eventBus.Subscribe(request.QueueName, request.RoutingKey); } catch (Exception ex) { response.Successful = false; response.Message = ex.Message; } return(response); }