提前两天发邮件

提前两天发邮件,

public static void SendMail(object sender, ElapsedEventArgs e)
        {          
            foreach (KeyValuePair<string, System.Timers.Timer> keyValuePair in Globals.TimerLists)
            {
                if (keyValuePair.Value == sender)
                {
                    string machineId = keyValuePair.Key;
                    using (Database accessor = new Database())
                    {
                        CheckOutInfo info = accessor.SelectCheckoutRecord(machineId);
                        if(info == null)
                        {
                            return;
                        }
                        //info.DueDate is expriation day
                        TimeSpan diff = DateTime.Now.Subtract(info.DueDate) + new TimeSpan(2, 0, 0, 0, 0);
                        diff = diff.Add(new TimeSpan(1, 0, 0)); // Add 1 hour to ignore the short time elapsed during processing.

                        // reSet the interval to let timer trigger next day.
                        if (diff.TotalDays < 0)
                        {
                            Debug.WriteLine("diff.Seconds< 0");
                            System.Timers.Timer reminderTimer = (System.Timers.Timer)sender;
                            reminderTimer.Interval = GetNextTriggerInterval(info.DueDate);
                            Debug.WriteLine(reminderTimer.Interval.ToString());
                            reminderTimer.AutoReset = false;
                            reminderTimer.Enabled = true;
                            break;
                        }
                        //Email reminder should be sent from 2 days before expiration;
                        else if (diff.TotalDays <= 2.0 && diff.TotalDays>=0.0)
                        {
                            VM vm = ShareFactory.GetVMByID(new Guid(machineId));
                            if (vm != null)
                            {
                                System.Timers.Timer reminderTimer = (System.Timers.Timer)sender;
                                reminderTimer.Interval = GetNextTriggerInterval(info.DueDate);
                                Debug.WriteLine(reminderTimer.Interval.ToString());
                                reminderTimer.AutoReset = false;
                                reminderTimer.Enabled = true;

                                string  Language = accessor.GetMachineLanguage(machineId);
                                ReservePolicy reservePolicy = ConfigurationFactory.GetReservePolicy(info.ReserveType);
                                if (info.ExtendTimes < reservePolicy.ExtendTimes)
                                {
                                    Debug.WriteLine("SendExpireEmail");                                 
                                }
                                else
                                {
                                    Debug.WriteLine("SendCheckinEmail");
                                 
                                }
                            }
                            else
                            {
                                // Machine does not exist. Just delete the records.
                               Debug.WriteLine("Machine does not exist. Just delete the records.");                             
                                Globals.TimerLists.Remove(machineId);
                            }

                        }
                        else
                        {
                             Debug.WriteLine("CheckIn");                           
                        }
                    }

                    break;
                }
            }
        }

posted on 2009-05-04 14:12  newr2006  阅读(269)  评论(0编辑  收藏  举报

导航