コード例 #1
0
ファイル: ProjectTask.cs プロジェクト: diliger/AdCrm
        public void SendNotification(string TemplateName)
        {
            User          responsible = this.UserResponsible;
            Employee      employee    = this.Employee;
            List <string> emails      = new List <string>();
            string        empemail    = employee != null?employee.GetEmails() : string.Empty;

            if (responsible != null && responsible.Email.IsNotNullOrEmpty())
            {
                emails.Add(responsible.Email);
            }

            if (empemail.IsNotNullOrEmpty())
            {
                emails.Add(empemail);
            }

            EmailHelper   emailHelper = new EmailHelper();
            EmailTemplate template    = Settings.GetEmailTemplate(TemplateName);

            if (template.Body.IsNullOrEmpty() || !emails.Any())
            {
                return;
            }

            DynamicDocuments.DataProvider dp = new Models.DynamicDocuments.DataProvider()
            {
                Project = this.Project,
                Number  = this.ID.ToString(),
                User    = this.UserResponsible,
                Data    = new
                {
                    User     = this.UserResponsible,
                    Task     = this,
                    Employee = this.Employee,
                    Status   = this.Status
                }
            };

            try
            {
                string mailSubject = DynamicDocumentGenerator.Generator.GenerateText(template.Subject, dp);
                string mailBody    = DynamicDocumentGenerator.Generator.GenerateText(template.Body, dp);

                emailHelper.SendEmail(mailSubject, mailBody, string.Join(";", emails.Distinct()));
            }
            catch (Exception ex)
            {
                new Log().Error(ex);
            }
        }
コード例 #2
0
        public void SendNotification(BuildingEntities db)
        {
            string tcreator     = ProjectTask != null ? ProjectTask.Creator : string.Empty;
            string TemplateName = "ProjectTaskMessage_Inserted";

            string[] notifies = this.Notify.StringAndTrim().Split(';');

            List <string> emails = new List <string>();

            this.Notify = "";
            foreach (string n in notifies)
            {
                this.Notify += n + ":";
                switch (n)
                {
                case "contractor":
                    if (Contractor != null)
                    {
                        string email = Contractor.Email.StringAndTrim();
                        if (email.IsNotNullOrEmpty())
                        {
                            emails.Add(email);
                            this.Notify += email + ";";
                        }
                        email = Contractor.GetEmail(db).StringAndTrim();
                        emails.AddRange(email.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries));
                        this.Notify += email;
                        this.Notify  = this.Notify.Trim(';');
                    }
                    break;

                case "employee":
                    if (Employee != null)
                    {
                        string email = Employee.GetEmails();
                        emails.Add(email);
                        this.Notify += email;
                    }
                    break;

                case "creator":
                    if (tcreator.IsNotNullOrEmpty())
                    {
                        User user = db.Users.FirstOrDefault(val => !val.Deleted && val.Login == tcreator);
                        if (user != null && user.Email.IsNotNullOrEmpty())
                        {
                            emails.Add(user.Email);
                            this.Notify += user.Email;
                        }
                    }
                    break;

                case "responsible":
                    if (Responsible != null)
                    {
                        emails.Add(Responsible.Email);
                        this.Notify += Responsible.Email;
                    }
                    break;

                case "last":
                    if (ProjectTask != null)
                    {
                        ProjectTaskMessage m = ProjectTask.Messages.OrderBy(val => val.ID).LastOrDefault();
                        if (m != null && m.ID > 0)
                        {
                            User user = db.Users.FirstOrDefault(val => !val.Deleted && val.Login == m.Creator);
                            if (user != null && user.Email.IsNotNullOrEmpty())
                            {
                                emails.Add(user.Email);
                                this.Notify += user.Email;
                            }
                        }
                    }
                    break;

                default:
                    if (n.StartsWith("another:"))
                    {
                        int  userID = n.Split(':').Last().ToIntOrDefault() ?? 0;
                        User user   = db.Users.FirstOrDefault(val => !val.Deleted && val.ID == userID);
                        if (user != null && user.Email.IsNotNullOrEmpty())
                        {
                            emails.Add(user.Email);
                            this.Notify += user.Email;
                        }
                    }
                    break;
                }
                this.Notify += ";";
            }
            this.Notify = this.Notify.Trim(';');

            emails = emails.Where(val => val.IsNotNullOrEmpty()).ToList();

            EmailTemplate template = Settings.GetEmailTemplate(TemplateName);

            if (template.Body.IsNullOrEmpty() || !emails.Any())
            {
                return;
            }

            DynamicDocuments.DataProvider dp = new Models.DynamicDocuments.DataProvider()
            {
                Project = this.ProjectTask.Project,
                Number  = this.ID.ToString(),
                //User = this.UserResponsible,
                Data = new
                {
                    //User = this.UserResponsible,
                    Task    = this.ProjectTask,
                    Message = this
                              //Employee = this.Employee,
                              //Status = this.Status
                }
            };

            try
            {
                string mailSubject = DynamicDocumentGenerator.Generator.GenerateText(template.Subject, dp);
                string mailBody    = DynamicDocumentGenerator.Generator.GenerateText(template.Body, dp);

                EmailHelper emailHelper = new EmailHelper();
                emailHelper.SendEmail(mailSubject, mailBody, string.Join(";", emails.Distinct()));
            }
            catch (Exception ex)
            {
                new Log().Error(ex);
            }
        }