在项目中,聪明的Jenny童鞋提了一个suggestion,即将同一个店同一人提交的请求,经过上级批准之后,邮件内容需要合并。
非常滴合理有木有~~
提交十个申请,将收到十封邮件,的确不友好哦。可是由于中间任何一级supervisor都可以为子店提交request,高level的都可以批准。)
于是,烧脑开始了。。。
(话说别人都不用烧,可怜我二八六的大脑啊。。。)
审批者提交的List如下:
即:
A 提了2个request 关于store 1 --合并为一封邮件
1个request 关于store2 -- 单独一封邮件
B 提了1个request 关于store1 -- 单独一封邮件
啊啊啊,Cynthia给我讲完,我还思考了辣么久,%>_<%,勇敢接受自己吧~
1)定义一个简单的Class.
1 public class Mix 2 { 3 public int StoreId { get; set; } 4 public string Requeseter { get; set; } 5 public int EmployeeId { set; get; } 6 }
2) 赋值
1 List<Mix> list = new List<Mix>(); 2 int i = 1000; 3 list.AddRange(new List<Mix> { new Mix { StoreId = 1, Requeseter = "A", EmployeeId = i + 1 }, new Mix { StoreId = 1, Requeseter = "A", EmployeeId = i + 2 }, new Mix { StoreId = 2, Requeseter = "A", EmployeeId = i + 4 }, new Mix { StoreId = 2, Requeseter = "B", EmployeeId = i + 3 } });
3)取出所有无重复的store 和requester
1 List<int> store = new List<int>(); 2 List<string> requester = new List<string>(); 3 foreach (Mix m in list) 4 { 5 if (!store.Contains(m.StoreId)) 6 { 7 store.Add(m.StoreId); 8 } 9 if (!requester.Contains(m.Requeseter)) 10 { 11 requester.Add(m.Requeseter); 12 } 13 }
4) 将相同store的request归入同一List.
1 foreach (int st in store) 2 { 3 listbystore = list.FindAll(z => z.StoreId == st); 4 ... 5 }
5)将分好的list再次按不同requester进行循环,取得store和外循环相等,requester和内循环相等的最终finallist.
1 foreach (string re in requester) 2 { 3 listbyrequester= listbystore.FindAll(p => p.Requeseter == re); 4 5 listfinal= listbyrequester.FindAll(p => p.StoreId.CompareTo(st) == 0); 6 }
6)根据finallist的元素个数进行判断是否需要合并。
最初,这个地方未做判断,发现输出和输入list竟然一!个!样!
这个地方代码有点怪哈,O(∩_∩)O哈哈~
1 if (listfinal.Count ==1) 2 { 3 listfinal.ForEach(z => { Console.Write(st); Console.Write(":"); Console.Write(z.Requeseter); }); 4 } 5 else if(listfinal.Count>1) 6 { 7 Console.Write( listfinal.Find(z => z.StoreId==st).StoreId); 8 Console.Write(":"); 9 Console.Write(listfinal.Find(z => z.StoreId == st).Requeseter); 10 SentEmail.sendGroupEmail(listfinal); 11 12 }
7)发邮件 大功告成
略草,嘿嘿。
1 public static void sendGroupEmail(List<Mix> list) 2 { 3 StringBuilder sb = new StringBuilder(); 4 sb.Append("<table border='1'><tr><td>Store</td><td>Employee</td><td>Requester</td></tr>"); 5 list.ForEach(p => { sb.Append("<tr>").Append("<td>").Append(p.StoreId.ToString()).Append("</td>").Append("<td>").Append(p.EmployeeId).Append("</td>").Append("<td>").Append(p.Requeseter.ToString()).Append("</td>").Append("</tr>"); }); 6 sb.Append("</table>"); 7 sent(string.Empty,sb.ToString(),string.Empty,string.Empty); 8 } 9 10 11 static void sent(string subject,string content,string mailTo,string mailcc) 12 { 13 MailMessage mailObj = new MailMessage(); 14 mailObj.From = new MailAddress("xxx@163.com"); //发送人邮箱地址 15 mailObj.To.Add("1031275749@qq.com"); //收件人邮箱地址 16 mailObj.Subject = "subject"; //主题 17 //actionrequiredurl = "<a href='" + actionrequiredurl + "'>here</a>"; 18 mailObj.IsBodyHtml = true; 19 mailObj.Body = content; 20 ////附件 21 //foreach (Attachment a in getAttachmentList()) 22 //{ 23 // mailObj.Attachments.Add(a); 24 //} 25 SmtpClient smtp = new SmtpClient(); 26 smtp.Host = "smtp.163.com"; //smtp服务器名称 27 smtp.UseDefaultCredentials = true; 28 smtp.Credentials = new NetworkCredential("***", "****"); //发送人的登录名和密码 29 smtp.Send(mailObj); 30 }
8)结果
虽然很草而且想了很久,还是比较有成就感滴。。。
感谢Cynthia,O(∩_∩)O谢谢~
记录下完整代码以备查阅:
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 List<Mix> list = new List<Mix>(); 6 List<Mix> listbystore = new List<Mix>(); 7 List<Mix> listbyrequester = new List<Mix>(); 8 List<Mix> listfinal = new List<Mix>(); 9 10 List<int> store = new List<int>(); 11 List<string> requester = new List<string>(); 12 int i = 1000; 13 list.AddRange(new List<Mix> { new Mix { StoreId = 1, Requeseter = "A", EmployeeId = i + 1 }, new Mix { StoreId = 1, Requeseter = "A", EmployeeId = i + 2 }, new Mix { StoreId = 2, Requeseter = "A", EmployeeId = i + 4 }, new Mix { StoreId = 2, Requeseter = "B", EmployeeId = i + 3 } }); 14 //list.ForEach(p => Console.WriteLine(p.StoreId)); 15 foreach (Mix m in list) 16 { 17 if (!store.Contains(m.StoreId)) 18 { 19 store.Add(m.StoreId); 20 } 21 if (!requester.Contains(m.Requeseter)) 22 { 23 requester.Add(m.Requeseter); 24 } 25 } 26 27 foreach (int st in store) 28 { 29 listbystore = list.FindAll(z => z.StoreId == st); 30 //listbystore.ForEach(p => { Console.Write(p.StoreId); Console.Write(":"); Console.Write(p.Requeseter); Console.WriteLine(); }); 31 //Console.WriteLine(); 32 //1:A 33 //1:A 34 35 //29:A 36 //29:B 37 foreach (string re in requester) 38 { 39 listbyrequester= listbystore.FindAll(p => p.Requeseter == re); 40 41 listfinal= listbyrequester.FindAll(p => p.StoreId.CompareTo(st) == 0); 42 if (listfinal.Count ==1) 43 { 44 listfinal.ForEach(z => { Console.Write(st); Console.Write(":"); Console.Write(z.Requeseter); }); 45 } 46 else if(listfinal.Count>1) 47 { 48 Console.Write( listfinal.Find(z => z.StoreId==st).StoreId); 49 Console.Write(":"); 50 Console.Write(listfinal.Find(z => z.StoreId == st).Requeseter); 51 SentEmail.sendGroupEmail(listfinal); 52 53 } 54 Console.WriteLine("----------------"); 55 } 56 57 58 59 } 60 Console.ReadKey(); 61 62 } 63 }