/// <summary> /// 根据pid和数量返回打折活动命中信息 /// </summary> /// <param name="requestList"></param> /// <param name="userId"></param> /// <returns></returns> public async Task <OperationResult <IEnumerable <ProductHitDiscountResponse> > > GetProductAndUserHitDiscountInfoAsync(List <DiscountActivityRequest> requestList, string userId) { if (requestList != null && requestList.Any()) { var productHitDiscountManager = new ProductHitDiscountManager(requestList, userId); var result = await productHitDiscountManager.GetProductHitDiscountInfo(); return(OperationResult.FromResult(result)); } else { return(OperationResult.FromError <IEnumerable <ProductHitDiscountResponse> >(nameof(Resource.ParameterError), $"requestList 为空")); } }
public static async Task <CreateOrderResult> FlashSaleCreateOrder(Order.Request.CreateOrderRequest request) { var flashrequest = new FlashSaleOrderRequest() { Products = new List <OrderItems>() }; var pintuanFlag = false; var pintuanProductGroupId = ""; var pintuanPid = ""; var pintuanCount = 0; try { if (request.Items != null && request.Items.Any()) { foreach (var item in request.Items) { if (item.ActivityId.HasValue) { var activityTypeModel = new ActivityTypeModel(); // 增加拼团校验逻辑 if (item.ActivityId != null && await GroupBuyingManager.CheckProductGroupId(item.ActivityId.Value)) { activityTypeModel = new ActivityTypeModel { ActivityId = item.ActivityId.Value, Type = 7 }; } else if ((await DalSalePromotion.CheckActivityIsEffictive(item.ActivityId.Value.ToString())))///黎先攀的打折活动 { activityTypeModel = new ActivityTypeModel { ActivityId = item.ActivityId.Value, Type = 11 }; } else { // 不是拼团ActivityId activityTypeModel = ActivityManager.SelectActivityTypeByActivityIds(new List <Guid> { item.ActivityId.Value }).FirstOrDefault(); } if (activityTypeModel != null) { var temp = flashrequest.Products.ToList(); temp.Add( new OrderItems { ActivityId = item.ActivityId, Num = item.Num, PID = item.Pid, Type = activityTypeModel.Type, Price = item.Price, ListPrice = item.ListPrice }); flashrequest.Products = temp; if (activityTypeModel.Type == 1) { //用来控制下单是否走老逻辑 item.IsVerifyActivity = false; } if (activityTypeModel.Type == 5) { item.IsVerifyActivity = false; } //砍价 if (activityTypeModel.Type == 9) { item.IsVerifyActivity = false; } if (activityTypeModel.Type == 11) { item.IsVerifyActivity = false; } //拼团 if (activityTypeModel.Type == 7) { request.Status = "0NewPingTuan"; item.IsVerifyActivity = false; var buyLimitInfo = await GroupBuyingManager.GetBuyLimitInfo(item.ActivityId.Value, item.Pid, request.Customer.UserId); if (string.IsNullOrWhiteSpace(buyLimitInfo?.PID)) { return(new CreateOrderResult { ErrorCode = -1000, ErrorMessage = "未找到该拼团产品!" }); } if (buyLimitInfo.BuyLimitCount > 0 && buyLimitInfo.BuyLimitCount <= buyLimitInfo.CurrentOrderCount) { return(new CreateOrderResult { ErrorCode = -1000, ErrorMessage = "已达到限购单数!" }); } if (item.Num > buyLimitInfo.UpperLimitPerOrder && buyLimitInfo.UpperLimitPerOrder != 0) { return(new CreateOrderResult { ErrorCode = -1000, ErrorMessage = "购买数量不符合要求!" }); } using (var client = new PinTuanClient()) { var result = await client.IncreaseSoldCountAsync(buyLimitInfo.ProductGroupId, buyLimitInfo.PID, item.Num); if (!(result.Success && result.Result.Code == 1)) { return(new CreateOrderResult { ErrorCode = -1000, ErrorMessage = result.Result?.Info ?? "出现异常" }); } pintuanFlag = true; pintuanPid = buyLimitInfo.PID; pintuanProductGroupId = buyLimitInfo.ProductGroupId; pintuanCount = item.Num; } } //锦湖轮胎 if (item.Pid.StartsWith("TR-", StringComparison.OrdinalIgnoreCase) && !await CheckIsPassKumhoTireFlashSale(item.ActivityId?.ToString(), request.Customer.UserId)) { return(new CreateOrderResult() { ErrorCode = -1000, ErrorMessage = "此价格为企业客户专享,无法生成订单" }); } } } } } flashrequest.DeviceId = request.DeviceID; flashrequest.UseTel = request.Customer.UserTel; flashrequest.UserId = request.Customer.UserId; ICounter counter = null; #region 限时抢购校验 var fsRequest = new FlashSaleOrderRequest(); var isCheckFlash = false; var flashSaleResult = new List <CheckFlashSaleResponseModel>(); if (flashrequest.Products != null && flashrequest.Products.Any()) { var fsItems = flashrequest.Products.Where(r => r.Type == 1); var orderItemses = fsItems as OrderItems[] ?? fsItems.ToArray(); if (orderItemses.Any()) { isCheckFlash = true; fsRequest.DeviceId = flashrequest.DeviceId; fsRequest.UseTel = flashrequest.UseTel; fsRequest.UserId = flashrequest.UserId; fsRequest.Products = orderItemses; flashSaleResult = await ActivityValidator.CheckFlashSaleAsync(fsRequest); if (flashSaleResult.Any(r => r.Code != Model.CheckFlashSaleStatus.Succeed)) { return(new CreateOrderResult { ErrorCode = (int)flashSaleResult.Where(r => r.Code != Model.CheckFlashSaleStatus.Succeed).Select(c => c.Code).FirstOrDefault(), ErrorMessage = flashSaleResult.Where(r => r.Code != Model.CheckFlashSaleStatus.Succeed).Select(c => c.Code).FirstOrDefault().GetRemark() }); } flashrequest.Products = orderItemses.Select(r => { r.AllPlaceLimitId = flashSaleResult.Where(p => p.PID == r.PID) .Select(_ => _.AllPlaceLimitId) .FirstOrDefault(); return(r); }); } } #endregion #region 保养校验 if (flashrequest.Products != null && flashrequest.Products.Any()) { var byRequest = flashrequest.Products.Where(r => r.Type == 5); var activityId = byRequest.FirstOrDefault(o => o.ActivityId != Guid.Empty)?.ActivityId; if (activityId != null) { ValidateOrderRequest validateRequest = new ValidateOrderRequest() { ActivityId = activityId.Value, UserId = flashrequest.UserId, Products = request.Items.Where( o => o.ActivityId != null && o.ActivityId.HasValue && !o.Pid.StartsWith("FU-") && !o.Pid.StartsWith("TR-")) .Select(o => new OrderProduct() { ProductId = o.Pid, Count = o.Num, ActivityId = o.ActivityId.Value, Price = o.Price, ProductType = "Product" }).ToList(), ShopId = request.Delivery.InstallShopId ?? 0, Channel = request.OrderChannel, InstallType = request.Delivery.InstallType, Vehicle = Converter.Convert(request.Car), RegionId = 1 }; using (var client = new BaoYangClient()) { var baoyangResult = await client.ValidateFixedPriceActivityOrderAsync(validateRequest); if (!baoyangResult.Success || !baoyangResult.Result) { return(new CreateOrderResult() { ErrorCode = (int)CreateOrderErrorCode.ProductValidateFailed, ErrorMessage = CreateOrderMessageDic.GetMessage(CreateOrderErrorCode.ProductValidateFailed) }); } } // 验证活动状态 var validateResult = await ActivityValidator.ValidateBaoyang(activityId.Value); if (validateResult.Item1 == Model.CreateOrderErrorCode.ActivitySatisfied) { // 验证限购数量 counter = new BaoYangCounter(activityId.Value, validateResult.Item2, validateResult.Item3, validateResult.Item4); var countResult = await counter.CanPurchaseAndIncreaseCount(flashrequest.UserId, flashrequest.DeviceId, flashrequest.UseTel); if (countResult.Code != Model.CreateOrderErrorCode.ActivitySatisfied) { return(new CreateOrderResult() { ErrorCode = (int)countResult.Code, ErrorMessage = CreateOrderMessageDic.GetMessage(countResult.Code) }); } } else { return(new CreateOrderResult() { ErrorCode = (int)validateResult.Item1, ErrorMessage = CreateOrderMessageDic.GetMessage(validateResult.Item1) }); } } } #endregion #region 分享砍价活动 bool bargainflag = false; Guid ownerId = new Guid(); string pid = ""; var parameters = new List <BuyLimitDetailModel>(); if (flashrequest.Products != null && flashrequest.Products.Any()) { var item = flashrequest.Products.FirstOrDefault(g => g.Type == 9); if (item != null) { ownerId = flashrequest.UserId; pid = item.PID; parameters.Add(new BuyLimitDetailModel { ModuleName = "sharebargain", LimitObjectId = ownerId.ToString("D"), ObjectType = LimitObjectTypeEnum.UserId.ToString(), Remark = "砍价实物商品下单" }); if (!string.IsNullOrWhiteSpace(flashrequest.DeviceId)) { parameters.Add(new BuyLimitDetailModel { ModuleName = "sharebargain", LimitObjectId = flashrequest.DeviceId, ObjectType = LimitObjectTypeEnum.DeviceId.ToString(), Remark = "砍价实物商品下单" }); } var val = await DalBargain.CheckBargainProductStatusByPID(ownerId, pid); if (!val) { return(new CreateOrderResult() { ErrorCode = -1000, ErrorMessage = "您当前没有资格享受该优惠!" }); } bargainflag = true; } } #endregion IEnumerable <OrderItems> discountProducts = null; #region 黎先攀的打折活动 if (flashrequest.Products != null && flashrequest.Products.Any()) { discountProducts = flashrequest.Products.Where(r => r.Type == 11);///打折活动商品 if (discountProducts.Any()) { var productHitDiscountManager = new ProductHitDiscountManager(discountProducts.Select(s => new DiscountActivityRequest() { Pid = s.PID, Num = s.Num, Price = s.ListPrice }).ToList(), request.Customer.UserId.ToString(), false, true); var realProductDiscounts = (await productHitDiscountManager.GetProductHitDiscountInfo()).ToList();//实时产品折扣信息 var effictiveDiscountNum = discountProducts.Join(realProductDiscounts, x => x.PID, y => y.Pid, (x, y) => { return(y.IsHit && string.Equals(y.DiscountRule?.ActivityId, x.ActivityId.ToString()) && Convert.ToInt32(y.DiscountPrice) == Convert.ToInt32(x.Price)); }).Where(s => s).Count(); if (effictiveDiscountNum != discountProducts.Count()) { var firtNotHitProduct = realProductDiscounts.FirstOrDefault(s => !s.IsHit); foreach (var item in discountProducts) { if (item.ActivityId != null && item.ActivityId != Guid.Empty) { TuhuNotification.SendNotification("DiscountActivityCreateOrder", new DiscountCreateOrderRequest() { ActivityId = item.ActivityId.ToString(), Num = item.Num, Pid = item.PID, UserId = request.Customer.UserId.ToString() }); } } var errorMsg = firtNotHitProduct != null ? firtNotHitProduct.FailMessage : "打折规则已变更"; Logger.Info($"打折活动下单失败:{errorMsg},discountProducts:{JsonConvert.SerializeObject(discountProducts)}," + $"realProductDiscounts:{JsonConvert.SerializeObject(realProductDiscounts)}"); return(new CreateOrderResult() { ErrorCode = -1000, ErrorMessage = errorMsg }); } } } #endregion try { using (var client = new CreateOrderClient()) { var result = await client.CreateOrderAsync(request); result.ThrowIfException(); if (result.Success) { if (counter != null) { await counter.AddOrderRecord(result.Result.OrderId, flashrequest.UserId, flashrequest.DeviceId, flashrequest.UseTel); } if (bargainflag) { var tal = await DalBargain.BuyBargainProductAsync(ownerId, pid, result.Result.OrderId); if (tal > 0) { parameters.ForEach(g => { g.ModuleProductId = tal.ToString(); g.Reference = result.Result.OrderId.ToString(); }); await LimitBuyManager.AddBuyLimitInfo(parameters); Logger.Info($"UserId为{ownerId}的用户享受砍价优惠,已购买pid为{pid}的商品"); } else { Logger.Error($"UserId为{ownerId}的用户享受砍价优惠,购买pid为{pid}的商品,修改购买状态时出错"); } } flashrequest.OrderId = result.Result.OrderId; if (isCheckFlash) { Logger.Info($"下单成功订单号=>{flashrequest.OrderId}发送消息"); try { TuhuNotification.SendNotification(".FlashSaleCreateOrder.", flashrequest); } catch (Exception ex) { var str = ""; try { str = JsonConvert.SerializeObject(flashrequest); } catch (Exception) { str = result.Result.OrderId.ToString(); } Logger.Error($"request-->{str}-->mq发送失败", ex); } } if (discountProducts != null && discountProducts.Any())//如果是黎先攀的打折活动 { foreach (var item in discountProducts) { if (item.ActivityId != null && item.ActivityId != Guid.Empty) { TuhuNotification.SendNotification("DiscountActivityCreateOrder", new DiscountCreateOrderRequest() { ActivityId = item.ActivityId.ToString(), Num = item.Num, OrderId = result.Result.OrderId, Pid = item.PID, UserId = request.Customer.UserId.ToString() }); } } } return(new CreateOrderResult { OrderId = result.Result.OrderId, OrderNo = result.Result.OrderNo }); } else { // 拼团下单失败,修改虚拟库存 if (pintuanFlag) { using (var client2 = new PinTuanClient()) { var result2 = await client2.DecrementSoldCountAsync(pintuanProductGroupId, pintuanPid, pintuanCount); if (!(result2.Success && result2.Result.Code == 1)) { Logger.Error($"拼团产品下单失败,虚拟库存修改失败-->{pintuanProductGroupId}/{pintuanPid}/{pintuanCount}"); } } } if (counter != null) { await counter.DecreasePurchaseCount(flashrequest.UserId, flashrequest.DeviceId, flashrequest.UseTel); } Logger.Info("调用下单接口失败" + result.ErrorCode + result.ErrorMessage); if (isCheckFlash) { await FlashSaleCounter.DecrementAllFlashCount(fsRequest, flashSaleResult); } if (result.ErrorCode == "Order_FlashSale_Error") { return(new CreateOrderResult() { ErrorCode = -1000, ErrorMessage = CreateOrderMessageDic.GetFlashSaleErrorMessage(result.ErrorMessage) }); } if (result.ErrorCode == "Invalid_PromotionCode") { return new CreateOrderResult() { ErrorCode = -1000, ErrorMessage = "优惠券无效!" } } ; else { return(new CreateOrderResult() { ErrorCode = -1000, ErrorMessage = "下单失败!" }); } } } } catch (Exception ex) { if (counter != null) { await counter.DecreasePurchaseCount(flashrequest.UserId, flashrequest.DeviceId, flashrequest.UseTel); } if (isCheckFlash) { await FlashSaleCounter.DecrementAllFlashCount(fsRequest, flashSaleResult); } Logger.Error("调用下单接口失败" + ex.Message + ex.InnerException); return(new CreateOrderResult() { ErrorCode = -1000, ErrorMessage = "下单失败" }); } } catch (Exception ex) { Logger.Error(ex); return(new CreateOrderResult() { ErrorCode = -1000, ErrorMessage = "下单失败" }); } }