Example #1
0
        private void btnAddCoupons_Click(object sender, System.EventArgs e)
        {
            string  text     = string.Empty;
            string  arg_0B_0 = string.Empty;
            decimal?amount;
            decimal discountValue;
            int     needPoint;
            int     totalq;

            if (!this.ValidateValues(out amount, out discountValue, out needPoint, out totalq))
            {
                return;
            }
            CouponInfo couponInfo = new CouponInfo();

            #region 使用方式
            int UseType = 0;
            if (this.rdoAll.Checked)
            {
                UseType = int.Parse(this.rdoAll.Value);
            }

            else if (this.rdoProduct.Checked)
            {
                UseType = int.Parse(this.rdoProduct.Value);
            }

            else if (this.rdoProductType.Checked)
            {
                UseType = int.Parse(this.rdoProductType.Value);
            }

            else if (this.rdoBrand.Checked)
            {
                UseType = int.Parse(this.rdoBrand.Value);
            }

            else if (this.rdoSupplier.Checked)
            {
                UseType = int.Parse(this.rdoSupplier.Value);
            }
            #endregion


            #region 发送方式
            int    SendType     = 0;
            string stroverMoney = string.Empty;
            if (this.rdoManually.Checked)
            {
                SendType = int.Parse(this.rdoManually.Value);
            }

            else if (this.rdoOverMoney.Checked)
            {
                SendType     = int.Parse(this.rdoOverMoney.Value);
                stroverMoney = txtOverMoney.Text.ToString();
            }

            else if (this.rdoRegist.Checked)
            {
                SendType = int.Parse(this.rdoRegist.Value);
            }

            else if (this.rdoLq.Checked)
            {
                SendType = int.Parse(this.rdoLq.Value);
            }
            else if (this.rdorefund.Checked)
            {
                SendType = int.Parse(this.rdorefund.Value);
            }
            else if (this.rdZC.Checked)
            {
                SendType = int.Parse(this.rdZC.Value);
            }

            #endregion
            if (SendType != 4)
            {
                if (!this.calendarStartDate.SelectedDate.HasValue)
                {
                    this.ShowMsg("请选择开始日期!", false);
                    return;
                }
                if (!this.calendarEndDate.SelectedDate.HasValue)
                {
                    this.ShowMsg("请选择结束日期!", false);
                    return;
                }
                if (this.calendarStartDate.SelectedDate.Value.Date.CompareTo(this.calendarEndDate.SelectedDate.Value.Date) > 0)
                {
                    this.ShowMsg("开始日期不能晚于结束日期!", false);
                    return;
                }
            }
            string strOverMoney = txtOverMoney.Text;
            if (this.rdoOverMoney.Checked && !Regex.IsMatch(strOverMoney, @"^(?!0+(?:\.0+)?$)(?:[1-9]\d*|0)(?:\.\d{1,2})?$"))
            {
                this.ShowMsg(" 满足金额必须为正数,且最多只能有两位小数", false);
                return;
            }
            if (string.IsNullOrEmpty(base.Request.Form["selectProductsinfo"]) && this.rdoProduct.Checked)
            {
                this.ShowMsg("请选择商品", false);
                return;
            }
            if (!this.outBeginDate.SelectedDate.HasValue)
            {
                this.ShowMsg("请选择发劵开始日期!", false);
                return;
            }
            if (!this.outEndDate.SelectedDate.HasValue)
            {
                this.ShowMsg("请选择发劵结束日期!", false);
                return;
            }
            if (this.outBeginDate.SelectedDate.Value.Date.CompareTo(this.outEndDate.SelectedDate.Value.Date) > 0)
            {
                this.ShowMsg("发劵开始日期不能晚于发劵结束日期!", false);
                return;
            }
            if (SendType != 4)
            {
                if (this.outEndDate.SelectedDate.Value.Date.CompareTo(this.calendarEndDate.SelectedDate.Value.Date) > 0)
                {
                    this.ShowMsg("发劵结束日期不能晚于使用结束日期!", false);
                    return;
                }
            }
            string strValidity = this.txtValidity.Text;
            if (!string.IsNullOrWhiteSpace(strValidity))
            {
                if (!Regex.IsMatch(strValidity, @"^[1-9][0-9]*$"))
                {
                    this.ShowMsg(" 有效期只能是数字,必须大于O!", false);
                    return;
                }
            }
            couponInfo.Name          = this.txtCouponName.Text;
            couponInfo.ClosingTime   = (SendType == 4 ? Convert.ToDateTime("1/1/1753 12:00:00") : this.calendarEndDate.SelectedDate.Value.AddDays(1).AddSeconds(-1));
            couponInfo.StartTime     = (SendType == 4 ? Convert.ToDateTime("1/1/1753 12:00:00") : this.calendarStartDate.SelectedDate.Value);
            couponInfo.Amount        = amount;
            couponInfo.DiscountValue = discountValue;
            couponInfo.NeedPoint     = needPoint;
            couponInfo.OutBeginDate  = this.outBeginDate.SelectedDate.Value;
            couponInfo.OutEndDate    = this.outEndDate.SelectedDate.Value;
            couponInfo.TotalQ        = totalq;
            couponInfo.Remark        = this.txtRemark.Text;
            if (!string.IsNullOrEmpty(txtValidity.Text.Trim()))
            {
                couponInfo.Validity = int.Parse(this.txtValidity.Text);
            }
            if (this.rdoSDisEnable.Checked)
            {
                couponInfo.Status = int.Parse(this.rdoSDisEnable.Value);
            }
            else
            {
                couponInfo.Status = 1;
            }
            ValidationResults validationResults = Validation.Validate <CouponInfo>(couponInfo, new string[]
            {
                "ValCoupon"
            });
            if (!validationResults.IsValid)
            {
                using (System.Collections.Generic.IEnumerator <ValidationResult> enumerator = ((System.Collections.Generic.IEnumerable <ValidationResult>)validationResults).GetEnumerator())
                {
                    if (enumerator.MoveNext())
                    {
                        ValidationResult current = enumerator.Current;
                        text += Formatter.FormatErrorMessage(current.Message);
                        this.ShowMsg(text, false);
                        return;
                    }
                }
            }
            string empty = string.Empty;

            int newcouponId = 0;


            couponInfo.UseType      = UseType;
            couponInfo.SendType     = SendType;
            couponInfo.SendTypeItem = stroverMoney;

            #region 添加优惠券
            if (this.couponId == 0)
            {
                CouponActionStatus couponActionStatus = CouponHelper.CreateCoupon(couponInfo, 0, out empty, out newcouponId);


                if (couponActionStatus == CouponActionStatus.UnknowError)
                {
                    this.ShowMsg("未知错误", false);
                }
                else
                {
                    //if (couponActionStatus == CouponActionStatus.DuplicateName)
                    //{
                    //    this.ShowMsg("已经存在相同的优惠券名称", false);
                    //    return;
                    //}
                    if (couponActionStatus == CouponActionStatus.CreateClaimCodeError)
                    {
                        this.ShowMsg("生成优惠券号码错误", false);
                        return;
                    }

                    #region 插入优惠券子表
                    switch (UseType)
                    {
                    case 0: break;

                    case 1:
                        string   productselectList = base.Request.Form["selectProductsinfo"];
                        string[] array             = productselectList.Split(new char[] { ',' });
                        for (int i = 0; i < array.Length; i++)
                        {
                            //string text2 = array[i];
                            //string[] array2 = text2.Split(new char[] { '|' });
                            CouponsSendTypeItem senditem = new CouponsSendTypeItem();
                            senditem.CouponId = newcouponId;
                            senditem.BindId   = System.Convert.ToInt32(array[i]);
                            senditem.UserId   = HiContext.Current.User.UserId;
                            CouponHelper.CreateCouponsSendTypeItem(senditem);
                        }
                        break;

                    case 2:
                        string   producttypeList  = base.Request.Form["ProductTypelist"];
                        string[] arrayproducttype = producttypeList.Split(new char[] { ',' });
                        for (int i = 0; i < arrayproducttype.Length; i++)
                        {
                            CouponsSendTypeItem senditem = new CouponsSendTypeItem();
                            senditem.CouponId = newcouponId;
                            senditem.BindId   = System.Convert.ToInt32(arrayproducttype[i]);
                            senditem.UserId   = HiContext.Current.User.UserId;
                            CouponHelper.CreateCouponsSendTypeItem(senditem);
                        }
                        break;

                    case 3:
                        foreach (var item in dcBrand)
                        {
                            CouponsSendTypeItem senditem = new CouponsSendTypeItem();
                            senditem.CouponId = newcouponId;
                            senditem.BindId   = item.Key;
                            senditem.UserId   = HiContext.Current.User.UserId;
                            CouponHelper.CreateCouponsSendTypeItem(senditem);
                        }
                        break;

                    case 4:
                        foreach (var item in dcSupplier)
                        {
                            CouponsSendTypeItem senditem = new CouponsSendTypeItem();
                            senditem.CouponId = newcouponId;
                            senditem.BindId   = item.Key;
                            senditem.UserId   = HiContext.Current.User.UserId;
                            CouponHelper.CreateCouponsSendTypeItem(senditem);
                        }
                        break;

                    default: break;
                    }
                    #endregion

                    this.ShowMsg("添加优惠券成功", true);
                    this.RestCoupon();
                    return;
                }
            }

            #endregion

            #region 编辑优惠券

            else
            {
                couponInfo.CouponId = this.couponId;
                CouponActionStatus couponActionStatus = CouponHelper.NewUpdateCoupon(couponInfo);


                if (couponActionStatus == CouponActionStatus.UnknowError)
                {
                    this.ShowMsg("未知错误", false);
                }
                else
                {
                    if (couponActionStatus == CouponActionStatus.DuplicateName)
                    {
                        this.ShowMsg("已经存在相同的优惠券名称", false);
                        return;
                    }
                    if (couponActionStatus == CouponActionStatus.CreateClaimCodeError)
                    {
                        this.ShowMsg("生成优惠券号码错误", false);
                        return;
                    }

                    //删除原来的子表信息
                    CouponHelper.DeleteCouponSendTypeItem(this.couponId);
                    #region 插入优惠券子表
                    switch (UseType)
                    {
                    case 0: break;

                    case 1:
                        string   productselectList = base.Request.Form["selectProductsinfo"];
                        string[] array             = productselectList.Split(new char[] { ',' });
                        for (int i = 0; i < array.Length; i++)
                        {
                            //string text2 = array[i];
                            //string[] array2 = text2.Split(new char[] { '|' });
                            CouponsSendTypeItem senditem = new CouponsSendTypeItem();
                            senditem.CouponId = this.couponId;
                            senditem.BindId   = System.Convert.ToInt32(array[i]);
                            senditem.UserId   = HiContext.Current.User.UserId;
                            CouponHelper.CreateCouponsSendTypeItem(senditem);
                        }
                        break;

                    case 2:
                        string   producttypeList  = base.Request.Form["ProductTypelist"];
                        string[] arrayproducttype = producttypeList.Split(new char[] { ',' });
                        for (int i = 0; i < arrayproducttype.Length; i++)
                        {
                            CouponsSendTypeItem senditem = new CouponsSendTypeItem();
                            senditem.CouponId = this.couponId;
                            senditem.BindId   = System.Convert.ToInt32(arrayproducttype[i]);
                            senditem.UserId   = HiContext.Current.User.UserId;
                            CouponHelper.CreateCouponsSendTypeItem(senditem);
                        }
                        break;

                    case 3:
                        foreach (var item in dcBrand)
                        {
                            CouponsSendTypeItem senditem = new CouponsSendTypeItem();
                            senditem.CouponId = this.couponId;
                            senditem.BindId   = item.Key;
                            senditem.UserId   = HiContext.Current.User.UserId;
                            CouponHelper.CreateCouponsSendTypeItem(senditem);
                        }
                        break;

                    case 4:
                        foreach (var item in dcSupplier)
                        {
                            CouponsSendTypeItem senditem = new CouponsSendTypeItem();
                            senditem.CouponId = this.couponId;
                            senditem.BindId   = item.Key;
                            senditem.UserId   = HiContext.Current.User.UserId;
                            CouponHelper.CreateCouponsSendTypeItem(senditem);
                        }
                        break;

                    default: break;
                    }
                    #endregion

                    this.ShowMsg("编辑优惠券成功", true);
                    this.RestCoupon();
                    return;
                }
            }
            #endregion
        }