/// <summary>
        /// 发券(批量)
        /// </summary>
        /// <param name="models"></param>
        /// <param name="throwException"></param>
        /// <returns></returns>
        public static async Task <CreatePromotionCodeResult> CreatePromotions(IEnumerable <CreatePromotionModel> models)
        {
            CreatePromotionCodeResult result = null;

            try
            {
                using (var client = new PromotionClient())
                {
                    var serviceResult = await client.CreatePromotionsNewAsync(models);

                    serviceResult.ThrowIfException(true);
                    result = serviceResult.Result;
                }
            }
            catch (Exception ex)
            {
                Logger.Error($"赛券服务报错,Request:{JsonConvert.SerializeObject(models)}", ex);
            }

            return(result);
        }
Example #2
0
        /// <summary>
        ///  批量发送优惠券并更新统计次数  ,记录已发日志
        /// </summary>
        /// <param name="couponRuleList"></param>
        /// <param name="user"></param>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async ValueTask <string> SendCouponsLogicAsync(List <PromotionTaskPromotionListModel> couponRuleList, User user, SendCouponRequest request, CancellationToken cancellationToken)
        {
            var result = new CreatePromotionCodeResult();
            PromotionSingleTaskUsersHistoryEntity entity = new PromotionSingleTaskUsersHistoryEntity()
            {
                PromotionTaskId = request.PromotionTaskId,
                UserCellPhone   = user.MobileNumber,
                SendState       = 0,
                OrderNo         = request.OrderID.ToString(),
                UserID          = user.UserId
            };
            var createPromotionList = new List <CreatePromotionModel>();

            try
            {
                #region 3.创建优惠券 [批量]
                foreach (var couponRule in couponRuleList)
                {
                    for (int i = 0; i < couponRule.Number; i++)
                    {
                        CreatePromotionModel createPromotionModel = new CreatePromotionModel()
                        {
                            Author           = couponRule.Creater,
                            MinMoney         = couponRule.MinMoney,
                            Discount         = couponRule.DiscountMoney,
                            DepartmentName   = couponRule.DepartmentName,
                            IntentionName    = couponRule.IntentionName,
                            BusinessLineName = couponRule.BusinessLineName,
                            Description      = couponRule.PromotionDescription,
                            UserID           = user.UserId,
                            Channel          = "PromotionTask",
                            StartTime        = couponRule.StartTime,
                            EndTime          = couponRule.EndTime,
                            RuleId           = couponRule.CouponRulesId,
                            GetRuleID        = couponRule.GetCouponRuleID,
                            Creater          = couponRule.Creater,
                            IssueChannleId   = request.PromotionTaskId.ToString(),
                            IssueChannle     = "优惠券任务塞券",
                            Issuer           = couponRule.Issuer,
                            //Type = couponRule.,//创建时会自动添加
                            TaskPromotionListId = couponRule.TaskPromotionListId,
                            BathID = request.OrderID,
                        };
                        createPromotionList.Add(createPromotionModel);
                    }
                }
                logger.Info($@"CouponTaskManager CreatePromotionsForYeWuAsync createPromotionList={JsonConvert.SerializeObject(createPromotionList)}");
                var createPromotionResult = await createPromotionService.CreatePromotionsForYeWuAsync(createPromotionList).ConfigureAwait(false);

                result                  = createPromotionResult?.Result;
                entity.IsSuccess        = result.IsSuccess ? 1 : 2;
                entity.Message          = result.ErrorMessage;
                entity.PromotionCodeIDs = result.promotionIds == null ? "" : string.Join(",", result.promotionIds);
                #endregion
            }
            catch (Exception ex)
            {
                logger.LogWarning("CouponTaskManager SendCouponsLogicAsync Exception", ex);
                entity.IsSuccess        = 2;
                entity.Message          = result.ErrorMessage;
                entity.PromotionCodeIDs = result.promotionIds == null ? "" : string.Join(",", result.promotionIds);
                return(entity.PromotionCodeIDs);
            }
            finally
            {
                await promotionSingleTaskUsersHistoryRepository.CreateAsync(entity, cancellationToken).ConfigureAwait(false);

                int successCount = result?.promotionIds?.Count() ?? 0;
                //记录到已发 日志表
                //增加塞券次数
                await promotionTaskRepository.UpdatePromotionTaskCountAsync(request.PromotionTaskId, successCount > 0? 1 : 0, (createPromotionList.Count() - successCount) > 0? 1 : 0, cancellationToken).ConfigureAwait(false);
            }
            return(entity.PromotionCodeIDs);
        }