public ActionResult Delete(int id)
 {
     User user;
     using (IUnitOfWork unitOfWork = new TskDataDataContext(DbUtil.ConnectionString))
     {
         IUserRep userRep = new UserRep(unitOfWork);
         user = userRep.FindById(id);
         if (user != null)
         {
             if (Brilliantech.Tsk.Manage.WebApp.Util.CustomMembershipProvider.CanEdit(user.Name))
             {
                 return View(user);
             }
             else
             {
                 TempData["Message"] = "初始管理员,不可以删除";
                 return RedirectToAction("Index");
             }
         }
         else
         {
             return RedirectToAction("Index");
         }
     }
 }
        public ActionResult Delete(int id, FormCollection collection)
        {
            User user = null;
            try
            {
                using (IUnitOfWork unitOfWork = new TskDataDataContext(DbUtil.ConnectionString))
                {
                    IUserRep userRep = new UserRep(unitOfWork);
                    user = userRep.FindById(id);
                    userRep.Delete(user);
                    unitOfWork.Submit();
                }

                return RedirectToAction("Index");
            }
            catch
            {
                return View(user);
            }
        }
        /// <summary>
        /// Called by the <see cref="IScheduler" /> when a
        /// <see cref="ITrigger" /> fires that is associated with the <see cref="IJob" />.
        /// </summary>
        public virtual void Execute(IJobExecutionContext context)
        {
            //TestFile(context);
            //return;
            try
            {
                JobDataMap job = context.JobDetail.JobDataMap;
                DateTime startQueryDate = (DateTime)job.Get("StartDate");
                DateTime endQueryDate = (DateTime)job.Get("EndDate");

                List<User> users = null;
                using (IUnitOfWork unitOfWork = new TskDataDataContext(DbUtil.ConnectionString))
                {
                    IUserRep userRep = new UserRep(unitOfWork);
                    users = userRep.ListWithEmail();
                }

                SmtpClient smtpServer = new SmtpClient(EmailServerSetting.SmtpHost);
                //smtpServer.Port = EmailServerSetting.SmtpPort;
                smtpServer.Credentials = new System.Net.NetworkCredential(EmailServerSetting.EmailUser, EmailServerSetting.EmailPwd);

                foreach (User user in users)
                {
                    try
                    {
                        List<UserInspect> inspects = new List<UserInspect>();
                        using (IUnitOfWork unitOfWork = new TskDataDataContext(DbUtil.ConnectionString))
                        {
                            IUserInspectRep userInspectRep = new UserInspectRep(unitOfWork);
                            inspects = userInspectRep.ListByUserId(user.Id, startQueryDate, endQueryDate);
                        }
                        if (inspects.Count > 0)
                        {
                            using (MailMessage mail = new MailMessage())
                            {
                                mail.From = new MailAddress(EmailServerSetting.EmailAddress);
                                mail.To.Add(user.Email);
                                mail.Subject = "电测台数据文件";

                                var filename = "Inspect" + DateTime.Now.ToString("yyyyMMddHHmm") + "_" + Guid.NewGuid().ToString("N");
                                var csvfilename = filename + ".csv";
                                //var zipfilename = filename + ".zip";

                                var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "InspectFileTmp");
                                if (!Directory.Exists(filePath))
                                {
                                    Directory.CreateDirectory(filePath);
                                }
                                var csvFilePath = Path.Combine(filePath, csvfilename);
                                //var zipFilePath = Path.Combine(filePath, zipfilename);

                                using (FileStream csv = new FileStream(csvFilePath, FileMode.Create, FileAccess.ReadWrite))
                                {
                                    using (MemoryStream ms = new MemoryStream())
                                    {
                                        using (StreamWriter sw = new StreamWriter(ms, Encoding.UTF8))
                                        {
                                            sw.WriteLine(string.Join(",", InspectQueryModel.CsvHead.ToArray()));
                                            foreach (UserInspect i in inspects)
                                            {
                                                List<string> ii = new List<string>();
                                                foreach (string field in InspectQueryModel.Fileds)
                                                {
                                                    var value = i.GetType().GetProperty(field).GetValue(i, null);
                                                    ii.Add(value == null ? "" : value.ToString());
                                                }
                                                sw.WriteLine(string.Join(",", ii.ToArray()));
                                            }

                                            csv.Write(ms.ToArray(), 0, (int)ms.Length);
                                        }
                                    }
                                }
                                //using (ZipFile zip = new ZipFile())
                                //{
                                //    zip.AddFile(csvFilePath);
                                //    zip.Save(zipfilename);
                                //}
                                Attachment att = new Attachment(csvFilePath);
                                mail.Attachments.Add(att);

                                smtpServer.Send(mail);
                                LogUtil.Logger.Info("Send Inspect Email to:" + user.Email + ", File: " + filename);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        LogUtil.Logger.Error(ex.Message);
                    }
                }
            }
            catch (Exception e)
            {
                LogUtil.Logger.Error(e.Message);
            }
        }
 public ActionResult Index(int? page)
 {
     IPagedList<User> users = null;
     using (IUnitOfWork unitOfWork = new TskDataDataContext(DbUtil.ConnectionString))
     {
         int currentPageIndex = page.HasValue ? (page.Value <= 0 ? 0 : page.Value - 1) : 0;
         IUserRep userRep = new UserRep(unitOfWork);
         users = userRep.Queryable().ToPagedList(currentPageIndex, int.Parse(Resources.PageSize));
     }
     return View(users);
 }
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                User user;

                using (IUnitOfWork unitOfWork = new TskDataDataContext(DbUtil.ConnectionString))
                {
                    IUserRep userRep = new UserRep(unitOfWork);
                    user = userRep.FindById(id);

                    if (Brilliantech.Tsk.Manage.WebApp.Util.CustomMembershipProvider.CanEdit(user.Name))
                    {
                        ViewData["Role"] = new SelectList(UserRoleModel.UserRoleList(), "Key", "Name", user.Role);
                        if (collection.Get("Password").Trim().Length < CustomMembershipProvider.MinRequiredPasswordLength)
                        {
                            ViewBag.Message = "密码长度小于" + CustomMembershipProvider.MinRequiredPasswordLength;
                            return View(user);
                        }
                        else
                        {
                            user.Password = collection.Get("Password").Trim();
                            user.Role = collection.Get("Role");
                            user.Email = collection.Get("Email");
                            unitOfWork.Submit();
                            return RedirectToAction("Index");
                        }
                    }
                    else
                    {
                        TempData["Message"] = "初始管理员,不可以编辑";
                        return RedirectToAction("Index");
                    }
                }
            }
            catch
            {
                return View();
            }
        }