Beispiel #1
0
        public static string GenerateRandomCode(int length)
        {
            Thread.Sleep(10);
            const string template = "234679ACDEFGHJKLMNPQRTUVWXYZ";
            int          success  = 0;
            Random       random   = new Random();

            random.Next();
            string CouponCode = string.Empty;

            while (success == 0)
            {
                StringBuilder builder   = new StringBuilder();
                int           maxRandom = template.Length - 1;
                for (int i = 0; i < length; i++)
                {
                    builder.Append(template[random.Next(maxRandom)]);
                }
                CouponCode = builder.ToString();
                if (!CouponDA.CheckExistCode(CouponCode, null))
                {
                    success = 1;
                }
            }
            return(CouponCode);
        }
Beispiel #2
0
        public static Coupon Create(Coupon info)
        {
            if (info != null)
            {
                SetCoupon(info);
                CheckCoupon(info);
                using (ECommerce.Utility.ITransaction tran = ECommerce.Utility.TransactionManager.Create())
                {
                    //活动基本信息
                    info.SysNo = CouponDA.CreateMaster(info);

                    //折扣方式
                    CouponDA.AddDiscountRule(info);

                    //使用范围
                    CouponDA.SetCouponSaleRule(info);
                    CouponDA.AddSeleRulesProductCondition(info);
                    CouponDA.AddCustomerCondition(info);
                    //获取方式
                    CouponDA.SetCouponBindRules(info);
                    CouponDA.AddBindRulesProductCondition(info);

                    //优惠券
                    if (info.SysNo.HasValue && info.BindRule != null && info.BindRule.BindCondition == CouponsBindConditionType.None)
                    {
                        if (info.GeneralCode != null && (!string.IsNullOrWhiteSpace(info.GeneralCode.Code)))
                        {
                            if (CouponDA.CheckExistCode(info.GeneralCode.Code, info.SysNo))
                            {
                                throw new ECommerce.Utility.BusinessException(LanguageHelper.GetText("优惠券代码已存在"));
                            }
                            info.GeneralCode.CouponSysNo = info.SysNo.Value;
                            CouponDA.CreateCouponCode(info.GeneralCode, info.InUser);
                        }
                        else if (!string.IsNullOrWhiteSpace(info.ThrowInCodes))
                        {
                            string couponCodeXml = GetCouponCodeXml(info.ThrowInCodes);
                            if (couponCodeXml != null)
                            {
                                info.GeneralCode.CouponSysNo = info.SysNo.Value;
                                CouponDA.BatchCreateCouponCode(couponCodeXml, info, info.InUser);
                            }
                        }
                    }
                    tran.Complete();
                }
                if (info.SysNo.HasValue)
                {
                    info = CouponDA.Load(info.SysNo);
                }
            }
            return(info);
        }
Beispiel #3
0
        public static void CheckCoupon(Coupon info)
        {
            if (info != null)
            {
                if (info.SysNo.HasValue && info.MerchantSysNo > 0)
                {
                    if (info.MerchantSysNo != CouponDA.GetCouponMerchantSysNo(info.SysNo.Value))
                    {
                        throw new BusinessException(LanguageHelper.GetText("您没有权限操作该数据"));
                    }
                }
                if (!info.BeginDate.HasValue)
                {
                    throw new BusinessException(LanguageHelper.GetText("开始时间不能为空"));
                }

                if (!info.EndDate.HasValue)
                {
                    throw new BusinessException(LanguageHelper.GetText("结束时间不能为空"));
                }

                if (info.EndDate < DateTime.Now.Date)
                {
                    throw new BusinessException(LanguageHelper.GetText("结束时间必须大于当前时间"));
                }

                if (info.BeginDate >= info.EndDate)
                {
                    throw new BusinessException(LanguageHelper.GetText("开始时间必须小于结束时间"));
                }

                if (info.DiscountRules == null ||
                    info.DiscountRules.Count < 1)
                {
                    throw new BusinessException(LanguageHelper.GetText("折扣方式不能为空"));
                }

                if (info.BindRule != null)
                {
                    if (info.BindRule != null && info.BindRule.BindCondition == CouponsBindConditionType.None)
                    {
                        if ((info.GeneralCode == null || string.IsNullOrEmpty(info.GeneralCode.Code)) && string.IsNullOrWhiteSpace(info.ThrowInCodes))
                        {
                            throw new BusinessException(LanguageHelper.GetText("触发条件不限的优惠券活动必须生成优惠券代码"));
                        }
                        if (!string.IsNullOrWhiteSpace(info.GeneralCode.Code) && CouponDA.CheckExistCode(info.GeneralCode.Code, info.SysNo))
                        {
                            throw new ECommerce.Utility.BusinessException(LanguageHelper.GetText("优惠券代码已存在"));
                        }
                    }

                    //泰隆暂时不提供此活动
                    if (info.BindRule.BindCondition == CouponsBindConditionType.SO &&
                        info.BindRule.ProductRange.ProductRangeType == ProductRangeType.Limit)
                    {
                        if (info.BindRule.ProductRange.Products == null ||
                            info.BindRule.ProductRange.Products.Count < 1)
                        {
                            throw new BusinessException(LanguageHelper.GetText("优惠券获取方式限制商品列表不能为空"));
                        }
                    }
                    if (info.BindRule.BindCondition == CouponsBindConditionType.SO)
                    {
                        if (info.SaleRule.CustomerRange.Customers == null ||
                            info.SaleRule.CustomerRange.Customers.Count < 1)
                        {
                            throw new BusinessException(LanguageHelper.GetText("优惠券使用规则顾客范围列表不能为空"));
                        }
                    }

                    if (info.BindRule.ValidPeriod == CouponValidPeriodType.CustomPeriod)
                    {
                        if ((!info.BindRule.BindBeginDate.HasValue) ||
                            (!info.BindRule.BindEndDate.HasValue))
                        {
                            throw new BusinessException(LanguageHelper.GetText("自定义优惠券有效期起止时间不能为空"));
                        }
                    }
                }

                if (info.SaleRule != null &&
                    info.SaleRule.ProductRange.ProductRangeType == ProductRangeType.Limit)
                {
                    if (info.SaleRule.ProductRange.Products == null ||
                        info.SaleRule.ProductRange.Products.Count < 1)
                    {
                        throw new BusinessException(LanguageHelper.GetText("优惠券使用范围限制商品列表不能为空"));
                    }
                }

                if (info.SaleRule != null &&
                    info.SaleRule.CustomerRange.CustomerRangeType == CouponCustomerRangeType.Limit)
                {
                    if (info.SaleRule.CustomerRange.Customers == null ||
                        info.SaleRule.CustomerRange.Customers.Count < 1)
                    {
                        throw new BusinessException(LanguageHelper.GetText("优惠券使用范围限制顾客列表不能为空"));
                    }
                }
            }
            else
            {
                throw new BusinessException(LanguageHelper.GetText("优惠券活动信息不能为空"));
            }
        }
Beispiel #4
0
        public static Coupon Update(Coupon info)
        {
            if (info != null && info.SysNo.HasValue)
            {
                CouponStatus status = CouponDA.GetCouponStatus(info.SysNo.Value);
                if (status != CouponStatus.Init)
                {
                    throw new ECommerce.Utility.BusinessException(LanguageHelper.GetText("只有初始态的活动才能保存或发布"));
                }
                SetCoupon(info);
                CheckCoupon(info);
                using (ECommerce.Utility.ITransaction tran = ECommerce.Utility.TransactionManager.Create())
                {
                    //活动基本信息
                    CouponDA.UpdateMaster(info);

                    //折扣方式
                    CouponDA.DeleteDiscountRule(info.SysNo);
                    CouponDA.AddDiscountRule(info);

                    //使用范围
                    CouponDA.SetCouponSaleRule(info);
                    CouponDA.DeleteSeleRulesProductCondition(info.SysNo);
                    CouponDA.AddSeleRulesProductCondition(info);
                    CouponDA.DeleteSeleRulesCustomerCondition(info.SysNo);
                    CouponDA.AddCustomerCondition(info);
                    //获取方式
                    CouponDA.SetCouponBindRules(info);
                    CouponDA.DeleteBindRulesProductCondition(info.SysNo);
                    CouponDA.AddBindRulesProductCondition(info);

                    //优惠券
                    CouponDA.DelAllCouponCode(info.SysNo);
                    //优惠券
                    if (info.SysNo.HasValue && info.BindRule != null && info.BindRule.BindCondition == CouponsBindConditionType.None)
                    {
                        if (info.GeneralCode != null && (!string.IsNullOrWhiteSpace(info.GeneralCode.Code)))
                        {
                            if (CouponDA.CheckExistCode(info.GeneralCode.Code, info.SysNo))
                            {
                                throw new ECommerce.Utility.BusinessException(LanguageHelper.GetText("优惠券代码已存在"));
                            }
                            info.GeneralCode.CouponSysNo = info.SysNo.Value;
                            CouponDA.CreateCouponCode(info.GeneralCode, info.EditUser);
                        }
                        else if (!string.IsNullOrWhiteSpace(info.ThrowInCodes))
                        {
                            string couponCodeXml = GetCouponCodeXml(info.ThrowInCodes);
                            if (couponCodeXml != null)
                            {
                                CouponDA.BatchCreateCouponCode(couponCodeXml, info, info.InUser);
                            }
                        }
                    }

                    //if (info.CouponCodes != null && info.CouponCodes.Count > 0)
                    //{
                    //    foreach (CouponCode code in info.CouponCodes)
                    //    {
                    //        if (CouponDA.CheckExistCode(code.Code))
                    //        {
                    //            throw new ECommerce.Utility.BusinessException(LanguageHelper.GetText("优惠券代码已存在"));
                    //        }
                    //        CouponDA.CreateCouponCode(code, info.InUser);
                    //    }
                    //}
                    tran.Complete();
                }
            }
            return(info);
        }