Browse Source

添加邮件发送模块

luocaiyang 4 years ago
parent
commit
da11af0ac6

+ 26 - 0
wispro.sp.api/Job/ImportReportJob.cs

@@ -77,12 +77,14 @@ namespace wispro.sp.api.Job
 
             if (temCalMonth != null)
             {
+                
                 var iCount = spDb.PerformanceItems.Where<PerformanceItem>(p => p.CalMonthId == temCalMonth.Id).Count<PerformanceItem>();
 
                 if (iCount > 0)
                 {
                     return Task.CompletedTask;
                 }
+                calMonth = temCalMonth;
             }
             else
             {
@@ -101,6 +103,30 @@ namespace wispro.sp.api.Job
 
             //每月绩效统计--中国一次OA授权表
             NewMethod("72454834-afdd-4b98-b42a-0bc912d07610", "每月绩效统计--中国一次OA授权表.xlsx", calMonth,true);
+
+
+            var Staffs = spDb.Staffs.Join(spDb.ItemStaffs.Where<ItemStaff>(p=>p.Item.CalMonthId == calMonth.Id), 
+                                        staff => staff.Id, 
+                                        istaff => istaff.DoPersonId, 
+                                        (staff, istaff) => new Staff
+                                        {
+                                            Id = staff.Id,
+                                            Name = staff.Name,
+                                            Mail = staff.Mail
+                                        });
+             var lstStaff =   spDb.Staffs.Where<Staff>(s=>s.ItemStaffs.Where<ItemStaff>(p=>p.Item.CalMonthId == calMonth.Id).Count<ItemStaff>()>0);
+                
+
+            foreach (var staff in lstStaff.ToList<Staff>())
+            {
+                string strSubject = $"{calMonth.Year}年{calMonth.Month}月绩效数据确认通知";
+                string strBody = $"<html><body>{staff.Name},您好!<br/></br/>&nbsp;&nbsp;&nbsp;&nbsp;<div>{calMonth.Year}年{calMonth.Month}月绩效数据已生成,请在{DateTime.Now.AddDays(4).ToString("yyyy年MM月dd日")}之前确认完成!</div></body></html>";
+                string strTo = "luocaiyang@wispro-china.com";
+
+                QuartzUtil.AddMailJob(strSubject,strBody,strTo);
+
+                break;
+            }
            
             return Task.CompletedTask;
         }

+ 20 - 0
wispro.sp.api/Job/MailJob.cs

@@ -0,0 +1,20 @@
+using Quartz;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+
+namespace wispro.sp.api.Job
+{
+    public class MailJob : IJob
+    {
+        public Task Execute(IJobExecutionContext context)
+        {
+            string Subject = context.JobDetail.JobDataMap.Get("Subject").ToString();
+            string Body = context.JobDetail.JobDataMap.Get("Body").ToString();
+            string To = context.JobDetail.JobDataMap.Get("To").ToString();
+            utility.MailUtil.SendEmail(Subject,Body,To);
+            return Task.CompletedTask;
+        }
+    }
+}

+ 30 - 5
wispro.sp.api/Job/QuartzUtil.cs

@@ -21,10 +21,10 @@ namespace wispro.sp.api.Job
         /// <param name="trigger">触发器</param>
         public static async Task Add(Type type, JobKey jobKey, ITrigger trigger = null)
         {
-            Init();
-            _scheduler = await _schedulerFactory.GetScheduler();
+            await Init();
+            //_scheduler = await _schedulerFactory.GetScheduler();
 
-            await _scheduler.Start();
+            //await _scheduler.Start();
 
             if (trigger == null)
             {
@@ -38,6 +38,28 @@ namespace wispro.sp.api.Job
                 .WithIdentity(jobKey)
                 .Build();
 
+
+            await _scheduler.ScheduleJob(job, trigger);
+        }
+
+        public static async Task AddMailJob(string Subject,string Body,string To)
+        {
+            await Init();
+
+
+            var trigger = TriggerBuilder.Create()
+               .WithDescription("发送邮件通知")
+               .WithSimpleSchedule(x => x.WithIntervalInSeconds(5).WithRepeatCount(0))
+               .Build();
+
+            var job = JobBuilder.Create(typeof(MailJob))
+                .WithIdentity("邮件任务")
+                .Build();
+
+            job.JobDataMap.Put("Subject",Subject);
+            job.JobDataMap.Put("Body", Body);
+            job.JobDataMap.Put("To", To);
+
             await _scheduler.ScheduleJob(job, trigger);
         }
         /// <summary>
@@ -65,11 +87,14 @@ namespace wispro.sp.api.Job
         /// <summary>
         /// 初始化
         /// </summary>
-        private static void Init()
+        private static async Task Init()
         {
-            if (_schedulerFactory == null)
+            if (_scheduler == null)
             {
                 _schedulerFactory = new StdSchedulerFactory();// ServiceLocator.Services.GetService<ISchedulerFactory>();
+                _scheduler = await _schedulerFactory.GetScheduler();
+
+                await _scheduler.Start();
             }
         }
     }

+ 8 - 1
wispro.sp.api/appsettings.json

@@ -26,7 +26,14 @@
     "Account": "caiyangl",
     "Password": "j)wx*lier*@3",
     "ChormeDriverPath": "D:\\source\\repos\\ConsoleApp2\\ConsoleApp2\\bin\\Debug",
-    "ScheduleSetting": "30 35 17 25 * ? *"
+    "ScheduleSetting": "30 02 13 * * ? *"
+  },
 
+  "MailSetting": {
+    "Server": "smtp.qq.com",
+    "Port": "465",
+    "mail": "luocaiyang@qq.com",
+    "Account": "516337988",
+    "Password": "j)wx*lier*@3"
   }
 }

+ 55 - 0
wispro.sp.utility/MailUtil.cs

@@ -0,0 +1,55 @@
+using System;
+using System.Collections.Generic;
+using System.ComponentModel;
+using System.Linq;
+using System.Net.Mail;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace wispro.sp.utility
+{
+    public class MailUtil
+    {
+        public static void SendEmail(string subject, string body, string toEmail)
+        {
+            
+            // 命令行参数必须是SMTP主机
+            SmtpClient client = new SmtpClient(ConfigHelper.GetSectionValue("MailSetting:Server"), int.Parse(ConfigHelper.GetSectionValue("MailSetting:Port")));
+
+            string User = ConfigHelper.GetSectionValue("MailSetting:Account");
+            string PassWord = ConfigHelper.GetSectionValue("MailSetting:Password"); // 服务平台获取
+            client.UseDefaultCredentials = false;
+            client.EnableSsl = true;
+            client.Credentials = new System.Net.NetworkCredential(User, PassWord);
+            // 发送人
+            MailAddress from = new MailAddress(ConfigHelper.GetSectionValue("MailSetting:mail"), "绩效系统", Encoding.UTF8);
+            // 接收人
+            MailAddress to = new MailAddress(toEmail);
+
+            // 指定邮件内容
+            MailMessage message = new MailMessage(from, to);
+            message.Body = body;
+            message.BodyEncoding = Encoding.UTF8;
+            message.IsBodyHtml = true;
+            // 主题
+            message.Subject = subject;
+            message.SubjectEncoding = Encoding.UTF8;
+            // 设置发送操作结束时回调的方法.
+            client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
+            string userState = subject;
+            client.SendAsync(message,null);
+            //Console.WriteLine("发送消息...");
+           
+        }
+
+        private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
+        {
+            if(e.Error != null){
+                System.Diagnostics.Debug.WriteLine(e.Error.ToString());
+            }
+            
+        }
+    }
+
+    
+}

+ 2 - 2
wispro.sp.winClient/Form1.cs

@@ -231,7 +231,7 @@ namespace wispro.sp.winClient
         {
 
             //await InitRules();
-            await NewMethod();
+            await ImportUsers();
             await InputPerformanceItem("ExcelFiles\\21.01-21.06 工程师绩效报表-总表.xlsx", true, false, 0);
 
             CalMonth cal = new CalMonth()
@@ -587,7 +587,7 @@ namespace wispro.sp.winClient
             return null;
         }
 
-        private async Task NewMethod()
+        private async Task ImportUsers()
         {
             DataTable dt = NPOIExcel.ExcelToDataTable("ExcelFiles\\210730-威世博人员清单v1r01-xm.xlsx", true);
             List<StaffGrade> staffGrades = await GetStaffGrades();