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