private void frmMailReport_Load(object sender, EventArgs e) { this.WindowState = FormWindowState.Maximized; this.cboFeeBatch.Items.Clear(); //构造数据源(或从数据库中查询) DataTable ADt = new DataTable(); DataColumn ADC1 = new DataColumn("ValueMember", typeof(string)); DataColumn ADC2 = new DataColumn("DisplayMember", typeof(string)); ADt.Columns.Add(ADC1); ADt.Columns.Add(ADC2); MailJobDao mailJobDao = new MailJobDao(); IList<MailJob> mailJobList = mailJobDao.GetList(""); foreach (MailJob mailJob in mailJobList) { // this.cboFeeBatch.Items.Add(mailJob.feeMonth + "|" + mailJob.subject + "|" + mailJob.sendTime); DataRow ADR = ADt.NewRow(); ADR[0] = mailJob.mailJobId; ADR[1] = mailJob.feeMonth + "|" + mailJob.subject + "|" + mailJob.sendTime; ADt.Rows.Add(ADR); } //进行绑定 cboFeeBatch.DisplayMember = "DisplayMember";//控件显示的列名 cboFeeBatch.ValueMember = "ValueMember";//控件值的列名 cboFeeBatch.DataSource = ADt; if (String.IsNullOrEmpty(this.cboFeeBatch.Text)) { this.btnQuery.Enabled = false; } }
/// <summary> /// 异步 开始事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void worker_DoWork(object sender, DoWorkEventArgs e) { //需要执行的代码 Cursor.Current = Cursors.WaitCursor; if (dgAgentFee.RowCount <= 0) { return; } try{ MailData mailData = new MailData(); StringBuilder sbAgent = new StringBuilder(); worker.ReportProgress(1, "准备数据...\r\n"); WechatAction wechatAction = new WechatAction(); for (int i = 0; i < dgAgentFee.RowCount; i++) { if (dgAgentFee[4, i].Value == null) { continue; } if (String.IsNullOrEmpty(dgAgentFee[4, i].Value.ToString())) { continue; } StringBuilder sb = new StringBuilder(); String url = String.Format(Settings.Default.Wechat_Message, dgAgentFee[0, i].Value.ToString(), feeMonth); wechatAction.sendTextMessageToWechat(dgAgentFee[0, i].Value.ToString(), feeMonth + url, Settings.Default.Wechat_Secret, Settings.Default.Wechat_Agent_AppId); sb.Append("agent_no#").Append(dgAgentFee[0, i].Value == null ? "" : dgAgentFee[0, i].Value.ToString()).Append(","); sb.Append("agent_name#").Append(dgAgentFee[1, i].Value == null ? "" : dgAgentFee[1, i].Value.ToString().Replace(",", "、").Replace("#", "%")).Append(","); sb.Append("agent_type#").Append(dgAgentFee[2, i].Value == null ? "" : dgAgentFee[2, i].Value.ToString().Replace(",", "、").Replace("#", "%")).Append(","); sb.Append("agent_type_comment#").Append(dgAgentFee[3, i].Value == null ? "" : dgAgentFee[3, i].Value.ToString().Replace(",", "、").Replace("#", "%")).Append(","); sb.Append("email#").Append(dgAgentFee[4, i].Value == null ? "" : dgAgentFee[4, i].Value.ToString()); mailData.ContactJsonList.Add(sb.ToString()); } String client = Settings.Default.TripolisClient; String userName = Settings.Default.TripoisUserName; String password = Settings.Default.TripolisPassword; mailData.fromAddress = Settings.Default.MailFromAddress; mailData.replyAddress = Settings.Default.MailReplyAddress; mailData.sender = Settings.Default.MailSender; mailData.subject = this.subject; ChinaUnionAdapter mailAdapter = new ChinaUnionAdapter(client, userName, password, null); String databaseId = Settings.Default.TripolisDBId; String workspaceId = Settings.Default.TripolisWorkspaceId; String emailId = Settings.Default.TripolisDirectEmailId; worker.ReportProgress(1, "发送邮件...\r\n"); String message = mailAdapter.sendBatchMail(databaseId, workspaceId, emailId, mailData, this.dtFeeMonth.Value); if (message.Contains("OK:")) { String mailJobId = message.Substring(3); MailJob mailJob = new MailJob(); mailJob.feeMonth = feeMonth; mailJob.mailJobId = mailJobId; mailJob.subject = mailData.subject; mailJob.sendTime = this.dtFeeMonth.Value.ToString("yyyy-MM-dd hh:mm:ss"); MailJobDao mailJobDao = new MailJobDao(); mailJobDao.Delete(mailJob); mailJobDao.Add(mailJob); // WechatAction wechatAction = new WechatAction(); // wechatAction.sendMessageToWechat("@all",this.dtFeeMonth.Value.ToString("yyyy-MM") + Settings.Default.Wechat_Message); MessageBox.Show("邮件重新发送成功"); } else { MessageBox.Show(message); } } catch (Exception ex) { MessageBox.Show(ex.Message); } Cursor.Current = Cursors.Default; }
/// <summary> /// 异步 开始事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void worker_DoWork(object sender, DoWorkEventArgs e) { //需要执行的代码 Cursor.Current = Cursors.WaitCursor; try { String client = Settings.Default.TripolisClient; String userName = Settings.Default.TripoisUserName; String password = Settings.Default.TripolisPassword; ChinaUnionAdapter mailAdapter = new ChinaUnionAdapter(client, userName, password, null); MailData mailData = new MailData(); mailData.fromAddress = Settings.Default.MailFromAddress; mailData.replyAddress = Settings.Default.MailReplyAddress; mailData.sender = Settings.Default.MailSender; mailData.subject = this.subject; String databaseId = Settings.Default.TripolisDBId; String workspaceId = Settings.Default.TripolisWorkspaceId; String emailId = Settings.Default.TripolisDirectEmailId; worker.ReportProgress(1, "准备数据...\r\n"); WechatAction wechatAction = new WechatAction(); for (int i = 0; i < dgAgentFee.RowCount; i++) { if (dgAgentFee[4, i].Value == null) { continue; } if (String.IsNullOrEmpty(dgAgentFee[4, i].Value.ToString())) { continue; } StringBuilder sb = new StringBuilder(); String url = String.Format(Settings.Default.Wechat_Message, dgAgentFee[0, i].Value.ToString(), this.dateTimePicker1.Value.ToString("yyyy-MM")); wechatAction.sendTextMessageToWechat(dgAgentFee[0, i].Value.ToString(), this.feeMonth + url, Settings.Default.Wechat_Secret, Settings.Default.Wechat_Agent_AppId); sb.Append("agent_no#").Append(dgAgentFee[0, i].Value == null ? "" : dgAgentFee[0, i].Value.ToString()).Append(","); sb.Append("agent_name#").Append(dgAgentFee[1, i].Value == null ? "" : dgAgentFee[1, i].Value.ToString().Replace(",", "、").Replace("#", "%")).Append(","); sb.Append("agent_type#").Append(dgAgentFee[2, i].Value == null ? "" : dgAgentFee[2, i].Value.ToString().Replace(",", "、").Replace("#", "%")).Append(","); sb.Append("agent_type_comment#").Append(dgAgentFee[3, i].Value == null ? "" : dgAgentFee[3, i].Value.ToString().Replace(",","、").Replace("#","%")).Append(","); sb.Append("email#").Append(dgAgentFee[4, i].Value == null ? "" : dgAgentFee[4, i].Value.ToString()).Append(","); sb.Append("contact_name#").Append(dgAgentFee[5, i].Value == null ? "" : dgAgentFee[5, i].Value.ToString().Replace(",", "、").Replace("#", "%")).Append(","); sb.Append("agent_fee_seq#").Append(dgAgentFee[6, i].Value == null ? "" : dgAgentFee[6, i].Value.ToString()).Append(","); sb.Append("feemonth#").Append(this.feeMonth).Append(","); /// Dictionary<String, Dictionary<String, String>> CategoryMap = new Dictionary<string, Dictionary<String, String>>(); //按结账科目分类 int rowIndex = i; for (int j = 7; j < dgAgentFee.ColumnCount - 3; j++) { String headText = dgAgentFee.Columns[j].HeaderText; int locationIndex = headText.IndexOf("-"); int endIndex = headText.IndexOf("("); if (locationIndex == -1) { locationIndex = headText.IndexOf("-"); } if (endIndex == -1) { endIndex = headText.IndexOf("("); } String key = dgAgentFee.Columns[j].HeaderText.Substring(locationIndex + 1); if (endIndex != -1) { key = dgAgentFee.Columns[j].HeaderText.Substring(locationIndex + 1, endIndex - locationIndex - 1); } String value = dgAgentFee[j, rowIndex].Value == null ? "" : dgAgentFee[j, rowIndex].Value.ToString(); if (!CategoryMap.ContainsKey(key)) { Dictionary<String, String> valueMap = new Dictionary<string, string>(); valueMap.Add(headText, value); CategoryMap.Add(key, valueMap); } else { Dictionary<String, String> valueMap = CategoryMap[key]; valueMap.Add(headText, value); } } //int index = 1; int feeSeq = 1; int seq = 1; foreach (String itemKey in CategoryMap.Keys) { Dictionary<String, String> valueMap = CategoryMap[itemKey]; float subTotal = 0; foreach (String value in valueMap.Values) { if (!String.IsNullOrEmpty(value)) { subTotal = subTotal + float.Parse(value); } } sb.Append("fee_seq").Append(feeSeq.ToString()).Append("#").Append("").Append(","); sb.Append("fee_name").Append(feeSeq.ToString()).Append("#<b>").Append(itemKey.Replace(",", "、").Replace("#", "%")).Append("</b>,"); if (subTotal<=0) { sb.Append("fee").Append(feeSeq.ToString()).Append("#").Append("").Append(","); } else { sb.Append("fee").Append(feeSeq.ToString()).Append("#<b>").Append(subTotal).Append("</b>,"); } feeSeq++; foreach (String subKey in valueMap.Keys) { sb.Append("fee_name").Append(feeSeq.ToString()).Append("#").Append(subKey.Replace(",", "、").Replace("#", "%")).Append(","); if (String.IsNullOrEmpty(valueMap[subKey]) ||(!String.IsNullOrEmpty(valueMap[subKey]) && valueMap[subKey].Equals("0"))) { sb.Append("fee_seq").Append(feeSeq.ToString()).Append("#").Append("").Append(","); sb.Append("fee").Append(feeSeq.ToString()).Append("#").Append("").Append(","); } else { sb.Append("fee_seq").Append(feeSeq.ToString()).Append("#").Append(seq.ToString()).Append(","); sb.Append("fee").Append(feeSeq.ToString()).Append("#").Append(valueMap[subKey]).Append(","); seq++; } feeSeq++; } } for (int j = feeSeq; j <= 101; j++) { sb.Append("fee_seq").Append(feeSeq.ToString()).Append("#").Append("").Append(","); sb.Append("fee_name").Append(feeSeq.ToString()).Append("#").Append("").Append(","); sb.Append("fee").Append(feeSeq.ToString()).Append("#").Append("").Append(","); feeSeq++; } sb.Append("fee_total_seq").Append("#").Append(seq.ToString()).Append(","); sb.Append("fee_total#").Append(dgAgentFee[dgAgentFee.Columns.Count - 3, i].Value.ToString()).Append(","); seq++; sb.Append("invoice_fee_seq").Append("#").Append(seq.ToString()).Append(","); sb.Append("invoice_fee#").Append(dgAgentFee[dgAgentFee.Columns.Count - 2, i].Value.ToString()).Append(","); seq++; sb.Append("pre_invoice_fee_seq").Append("#").Append(seq.ToString()).Append(","); sb.Append("pre_invoice_fee#").Append(dgAgentFee[dgAgentFee.Columns.Count - 1, i].Value.ToString()); mailData.ContactJsonList.Add(sb.ToString()); Cursor.Current = Cursors.Default; } worker.ReportProgress(2, "发送邮件...\r\n"); String message = mailAdapter.sendBatchMail(databaseId, workspaceId, emailId, mailData, dateTimePicker1.Value); if (message.Contains("OK:")) { String mailJobId = message.Substring(3); MailJob mailJob = new MailJob(); mailJob.feeMonth = this.feeMonth; mailJob.mailJobId = mailJobId; mailJob.subject = mailData.subject; if (dateTimePicker1.Value != null) { mailJob.sendTime = dateTimePicker1.Value.ToString("yyyy-MM-dd hh:mm:ss"); } MailJobDao mailJobDao = new MailJobDao(); mailJobDao.Delete(mailJob); mailJobDao.Add(mailJob); MessageBox.Show("邮件发送成功"); } else { MessageBox.Show(message); } } catch (Exception ex) { MessageBox.Show(ex.Message); } //this.Close(); }