示例#1
0
 public void Remove(WebMobLog weblog)
 {
     if (Context.Entry(weblog).State == EntityState.Detached)
     {
         context.WebMobLog.Attach(weblog);
     }
     context.WebMobLog.Remove(weblog);
 }
示例#2
0
 public DbEntityEntry <WebMobLog> Entry(WebMobLog weblog)
 {
     return(Context.Entry(weblog));
 }
示例#3
0
 public void Add(WebMobLog weblog)
 {
     context.WebMobLog.Add(weblog);
 }
示例#4
0
 public void Attach(WebMobLog weblog)
 {
     context.WebMobLog.Attach(weblog);
 }
        public static void ReadNotifications(int CompanyId, int empId, string lang)
        {
            HrUnitOfWork unitofwork   = new HrUnitOfWork(new HrContextFactory(System.Configuration.ConfigurationManager.ConnectionStrings["HrContext"].ConnectionString));
            var          conditions   = unitofwork.NotificationRepository.ReadNotifications(CompanyId);
            EmailAccount emailaccount = null;
            bool         modified     = false;
            int          counter      = 0;

            holidays = null;

            if (emailaccount != null && modified)
            {
                emailaccount.LastSentDate = DateTime.Now;
                emailaccount.TodayCount   = counter;
                unitofwork.NotificationRepository.Attach(emailaccount);
                unitofwork.NotificationRepository.Entry(emailaccount).State = System.Data.Entity.EntityState.Modified;
            }

            List <AppendMsgViewModel> SignalRAppend = new List <AppendMsgViewModel>();

            foreach (var cond in conditions)
            {
                if (string.IsNullOrEmpty(cond.TableName) || string.IsNullOrEmpty(cond.ColumnName))
                {
                    continue; // can't get required data
                }
                if (string.IsNullOrEmpty(cond.Users) && string.IsNullOrEmpty(cond.CustEmail) && !cond.NotifyRef)
                {
                    continue; // no one to sent
                }
                string[] arr = { "Currencies", "SchedualTasks" };
                // prepare sql statement
                var sql = "SELECT " + cond.ColumnName + (Array.Exists(arr, f => f == cond.TableName) ? "" : ",Id") +
                          (string.IsNullOrEmpty(cond.Fields) ? "" : "," + cond.Fields) +
                          " FROM " + cond.TableName +
                          " WHERE " + (cond.filter == null ? "" : (cond.filter.Replace("\"", "'") + " AND ")) +
                          "CAST( " + cond.ColumnName + " as date )= '" + GetNotificationDate(unitofwork, CompanyId, cond.Event, cond.EventValue).ToString("yyyy-MM-dd") + "'";

                // dynamic create datatable
                var table = GetData(sql);
                if (table.Rows.Count == 0)
                {
                    continue;                        // condition doesn't match records
                }
                var employees = new List <int>();
                if (cond.NotifyRef)
                {
                    foreach (DataRow row in table.Rows)
                    {
                        if (row.Table.Columns.Contains("EmpId"))
                        {
                            if (row["EmpId"] != null && row["EmpId"].ToString().Length > 0)
                            {
                                employees.Add(int.Parse(row["EmpId"].ToString()));
                            }
                        }
                    }
                }

                IList <UserView> users = GetUsers(cond.Users, employees.ToArray <int>());
                if (users == null) // no users to send to
                {
                    continue;
                }

                // create notification for each record
                foreach (DataRow row in table.Rows)
                {
                    string m      = MessageMerge(cond.Message, unitofwork, table, row, cond.TableName, lang);
                    var    notify = new Notification
                    {
                        CompanyId    = CompanyId,
                        ConditionId  = cond.Id,
                        CreationTime = DateTime.Now,
                        EmpId        = GetEmpId(row),
                        RefEmpId     = GetEmpId(row),
                        Message      = MessageMerge(cond.Message, unitofwork, table, row, cond.TableName, lang),
                        Subject      = cond.Subject,
                        SourceId     = GetId(row)
                    };

                    foreach (var u in users.Where(a => a.WebNotify))
                    {
                        WebMobLog log = new WebMobLog
                        {
                            MarkAsRead = false,
                            Message    = notify.Message,
                            Notificat  = notify,
                            SentTime   = DateTime.Now,
                            SentToUser = u.Name,
                            CompanyId  = CompanyId,
                            Subject    = notify.Subject
                        };
                        unitofwork.NotificationRepository.Add(log);
                    }
                    SignalRAppend.Add(new AppendMsgViewModel
                    {
                        User   = users.Where(a => a.WebNotify).Select(a => a.Name).ToList(),
                        Notify = notify
                    });
                    var EmailUsers = users.Where(a => a.EmailNotify).ToList();
                    if (EmailUsers.Count > 0)
                    {
                        SendEmails(unitofwork.NotificationRepository, EmailUsers, notify, ref emailaccount, ref counter, ref modified);
                    }
                    if (cond.Sms)
                    {
                        SendSms(unitofwork.NotificationRepository, users.Where(a => a.SmsNotify).ToList(), notify);
                    }
                }
            }

            unitofwork.SaveChanges();
            foreach (var item in SignalRAppend)
            {
                Append(item.User, SignalR(item.Notify, item.Notify.EmpId));
            }
        }