protected void btnExport_Click(object sender, System.EventArgs e) { //默认为复杂密码 int pwdtype = 1; if (!string.IsNullOrEmpty(hiddenPwdtype.Value)) { pwdtype = int.Parse(hiddenPwdtype.Value); } int num; if (!int.TryParse(this.tbvoucherNum.Text, out num)) { this.ShowMsg("导出数量必须为正数", false); return; } if (num <= 0) { this.ShowMsg("导出数量必须为正数", false); return; } int voucherId; if (!int.TryParse(this.txtvoucherid.Value, out voucherId)) { this.ShowMsg("参数错误", false); return; } VoucherInfo voucher = VoucherHelper.GetVoucher(voucherId); string empty = string.Empty; VoucherActionStatus voucherActionStatus = VoucherHelper.CreateVoucher(voucher, num, out empty, pwdtype); if (voucherActionStatus == VoucherActionStatus.UnknowError) { this.ShowMsg("未知错误", false); return; } if (voucherActionStatus == VoucherActionStatus.CreateClaimCodeError) { this.ShowMsg("生成现金券号码错误", false); return; } if (voucherActionStatus == VoucherActionStatus.CreateClaimCodeSuccess && !string.IsNullOrEmpty(empty)) { System.Collections.Generic.IList <VoucherItemInfo> voucherItemInfos = VoucherHelper.GetVoucherItemInfos(empty); System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder(); stringBuilder.AppendLine("<table cellspacing=\"0\" cellpadding=\"5\" rules=\"all\" border=\"1\">"); stringBuilder.AppendLine("<tr style=\"font-weight: bold; white-space: nowrap;\">"); stringBuilder.AppendLine("<td>现金券号码</td>"); stringBuilder.AppendLine("<td>现金券密码</td>"); stringBuilder.AppendLine("<td>现金券金额</td>"); stringBuilder.AppendLine("<td>过期时间</td>"); stringBuilder.AppendLine("</tr>"); foreach (VoucherItemInfo current in voucherItemInfos) { stringBuilder.AppendLine("<tr>"); stringBuilder.AppendLine("<td>" + current.ClaimCode + "</td>"); stringBuilder.AppendLine("<td>" + current.Password + "</td>"); stringBuilder.AppendLine("<td>" + voucher.DiscountValue + "</td>"); stringBuilder.AppendLine("<td>" + current.Deadline + "</td>"); stringBuilder.AppendLine("</tr>"); } stringBuilder.AppendLine("</table>"); this.Page.Response.Clear(); this.Page.Response.Buffer = false; this.Page.Response.Charset = "GB2312"; this.Page.Response.AppendHeader("Content-Disposition", "attachment;filename=VoucherInfo_" + System.DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls"); this.Page.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); this.Page.Response.ContentType = "application/ms-excel"; this.Page.EnableViewState = false; this.Page.Response.Write(stringBuilder.ToString()); hiddenPwdtype.Value = ""; this.Page.Response.End(); } }
/// <summary> /// 添加按钮点击事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnAddVouchers_Click(object sender, System.EventArgs e) { string text = string.Empty; string arg_0B_0 = string.Empty; decimal?amount; decimal discountValue; if (!this.ValidateValues(out amount, out discountValue)) { return; } if (!this.calendarStartDate.SelectedDate.HasValue) { this.ShowMsg("请选择开始日期!", false); return; } if (!this.calendarEndDate.SelectedDate.HasValue) { this.ShowMsg("请选择结束日期!", false); return; } if (this.calendarStartDate.SelectedDate.Value.CompareTo(this.calendarEndDate.SelectedDate.Value) >= 0) { this.ShowMsg("开始日期不能晚于结束日期!", false); return; } string strValidity = this.txtValidity.Text; if (!Regex.IsMatch(strValidity, @"^[1-9][0-9]*$")) { this.ShowMsg(" 有效期只能是数字,必须大于等于O!", 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; } VoucherInfo voucherInfo = new VoucherInfo(); voucherInfo.Name = this.txtVoucherName.Text; voucherInfo.ClosingTime = this.calendarEndDate.SelectedDate.Value.AddDays(1).AddSeconds(-1); voucherInfo.StartTime = this.calendarStartDate.SelectedDate.Value; voucherInfo.Amount = amount; voucherInfo.DiscountValue = discountValue; #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); } #endregion voucherInfo.SendType = SendType; voucherInfo.SendTypeItem = stroverMoney; voucherInfo.Validity = int.Parse(this.txtValidity.Text); #region 字段限制验证,通过数据注解验证的方式 ValidationResults validationResults = Validation.Validate <VoucherInfo>(voucherInfo, new string[] { "Voucher" }); 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; } } } #endregion string empty = string.Empty; if (this.voucherId == 0) //创建现金券 { VoucherActionStatus voucherActionStatus = VoucherHelper.CreateVoucher(voucherInfo, 0, out empty, 1); if (voucherActionStatus == VoucherActionStatus.UnknowError) { this.ShowMsg("未知错误", false); } else { if (voucherActionStatus == VoucherActionStatus.DuplicateName) { this.ShowMsg("已经存在相同的现金券名称", false); return; } if (voucherActionStatus == VoucherActionStatus.CreateClaimCodeError) { this.ShowMsg("生成现金券号码错误", false); return; } this.ShowMsg("添加现金券成功", true); this.RestCoupon(); return; } } else //修改现金券 { voucherInfo.VoucherId = this.voucherId; VoucherActionStatus voucherActionStatus = VoucherHelper.UpdateVoucher(voucherInfo); if (voucherActionStatus == VoucherActionStatus.Success) { this.RestCoupon(); this.ShowMsg("成功修改了现金券信息", true); } else { if (voucherActionStatus == VoucherActionStatus.DuplicateName) { this.ShowMsg("修改现金券信息错误,已经具有此现金券名称", false); return; } this.ShowMsg("未知错误", false); this.RestCoupon(); return; } } }