コード例 #1
0
        /// <summary>
        /// 验证优惠券
        /// </summary>
        /// <param name="ruleGuid"></param>
        /// <param name="models"></param>
        /// <returns></returns>
        public Tuple <bool, string> ValidatedUploadData(Guid ruleGuid, List <VipPaintPromotionTemplateModel> models)
        {
            if (ruleGuid == Guid.Empty || models == null || !models.Any())
            {
                return(Tuple.Create(false, "验证优惠券相关信息不能为空"));
            }
            try
            {
                var baoYangManager  = new VipBaoYangPackageManager();
                var simplePromotion = baoYangManager.GetPromotionSimpleInfoByGetRuleGuid(ruleGuid);
                if (simplePromotion == null)
                {
                    return(Tuple.Create(false, "优惠券有误"));
                }
                var existsData = GetImportedPromotionDetail(ruleGuid, models.Select(x => x.MobileNumber));
                var lsit       = (from x in models
                                  join y in existsData on x.MobileNumber equals y.MobileNumber into temp
                                  from z in temp.DefaultIfEmpty()
                                  select new
                {
                    x.MobileNumber,
                    x.PromotionCount,
                    GetQuantity = z?.PromotionCount ?? 0
                }).ToList();

                //单个用户领取数量限制验证
                var sqValidatedResult = lsit.Where(x => x.PromotionCount + x.GetQuantity > simplePromotion.SingleQuantity);
                if (sqValidatedResult.Any())
                {
                    return(Tuple.Create(false, $"{string.Join("\n", sqValidatedResult.Select(x => x.MobileNumber))}超过单个用户优惠券领取数量{simplePromotion.SingleQuantity}, 请先修改券领取数量再进行塞券"));
                }

                //总的数量限制验证
                if (simplePromotion.Quantity != null)
                {
                    var quantity      = simplePromotion.Quantity.Value;
                    var totalQuantity = models.Sum(x => x.PromotionCount);
                    if (totalQuantity > quantity - simplePromotion.GetQuantity)
                    {
                        return(Tuple.Create(false, $"领取数量{totalQuantity}超过剩余取数量{quantity - simplePromotion.GetQuantity}限制(其中总数量为{quantity},已经领取数量为{simplePromotion.GetQuantity}), 请先修改券发行量再进行塞券"));
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Error("ValidatedUploadData", ex);
                return(Tuple.Create(false, "验证优惠券异常"));
            }
            return(Tuple.Create(true, string.Empty));
        }