public ActionResult CheckEmployeeManage(long id)
        {
            if (Session["CheckFeed"] == null || !(bool) Session["CheckSystemManage"])
            {
                Response.Write("<script>history.go(0);</script>");
                return View("Error");
            }

            var employeeService = new EmployeeService();
            var roleService = new RoleService();
            var listService = new ListService();
            List<role> roles = roleService.FindByEmployeeId(id);
            employee employee = employeeService.Find(id);
            var employeeModel = new EmployeeModel
                                    {
                                        EmployeeId = employee.Id,
                                        EmployeeNumber = employee.Number,
                                        EmployeeName = employee.Name,
                                        Rfid = employee.RFID,
                                        Sex = employee.Sex,
                                        ConfirmPassWd = employee.Password,
                                        PassWd = employee.Password,
                                        Logon = employee.LogOn,
                                        Birthday = Convert.ToDateTime(employee.Birthday),
                                        Memo = employee.Memo
                                    };
            List<long> roleType = roles.Select(role => role.role_type.Id).ToList();
            employeeModel.RoleTypeId = roleType;
            ViewData["Role"] = listService.GetRoleTypeList();
            ViewData["Sex"] = listService.GetSexTypeList();
            ViewBag.Title = "查看人员信息";
            return View("AddNewEmployeeManage", employeeModel);
        }
 public EmployeeModel FindEmployeeById(long id)
 {
     var employeeService = new EmployeeService();
     var roleService = new RoleService();
     try
     {
         List<role> roles = roleService.FindByEmployeeId(id);
         employee employee = employeeService.Find(id);
         var employeeModel = new EmployeeModel
                                 {
                                     EmployeeId = employee.Id,
                                     EmployeeNumber = employee.Number,
                                     EmployeeName = employee.Name,
                                     Rfid = employee.RFID,
                                     Sex = employee.Sex,
                                     ConfirmPassWd = employee.Password,
                                     PassWd = employee.Password,
                                     Logon = employee.LogOn,
                                     state=employee.State,
                                     Birthday = Convert.ToDateTime(employee.Birthday),
                                     Memo = employee.Memo
                                 };
         List<long> roleType = roles.Select(role => role.role_type.Id).ToList();
         employeeModel.RoleTypeId = roleType;
         return employeeModel;
     }
     catch (Exception)
     {
         return null;
     }
 }
        public bool SaveModifyEmployee(EmployeeModel model)
        {
            var employeeService = new EmployeeService();
            var roleService = new RoleService();
            string result = "";
            try
            {
                //SHA512加密
                if (model.ConfirmPassWd != null)
                {
                    SHA512 sha512 = new SHA512Managed();
                    byte[] s = sha512.ComputeHash(Encoding.UTF8.GetBytes(model.ConfirmPassWd));
                    for (int i = 0; i < s.Length; i++)
                    {
                        result += s[i].ToString("X2");
                    }
                    sha512.Clear();
                }

                //修改人员信息
                employee newemployee = employeeService.Find(model.EmployeeId);
                newemployee.Name = model.EmployeeName;
                newemployee.Number = model.EmployeeNumber;
                newemployee.RFID = model.Rfid;
                newemployee.LogOn = model.Logon;
                newemployee.Sex = model.Sex;
                newemployee.State = model.state;
                newemployee.Birthday = Convert.ToDateTime(model.Birthday);
                newemployee.Memo = model.Memo;
                if (model.ConfirmPassWd != null)
                {
                    newemployee.Password = result.ToLower();
                }
                employeeService.Update(newemployee);

                //修改角色信息
                //如果增加的角色类型数据库中没有,则插入该角色
                foreach (long t in model.RoleTypeId)
                {
                    List<role> roles = roleService.FindByEmployeeId(model.EmployeeId);
                    int flag = 1;
                    foreach (role role in roles)
                    {
                        if (t == role.RoleId)
                        {
                            flag = 0;
                        }
                    }
                    if (flag == 1)
                    {
                        var role = new role
                                       {
                                           EmployeeId = model.EmployeeId,
                                           RoleId = t
                                       };
                        roleService.Insert(role);
                    }
                }

                //   如果删除了一个角色类型,则在数据库删除对应的角色类型
                List<role> anotherroles = roleService.FindByEmployeeId(model.EmployeeId);
                foreach (role anotherrole in anotherroles)
                {
                    int flag = 1;
                    long delete = anotherrole.RoleId;
                    for (int j = 0; j < model.RoleTypeId.Count; j++)
                    {
                        if (anotherrole.RoleId == model.RoleTypeId[j])
                        {
                            flag = 0;
                        }
                    }
                    if (flag == 1)
                    {
                        roleService = new RoleService();
                        roleService.Delete(
                            roleService.Find(roleService.FindByEmployeeIdAndRoleId(model.EmployeeId, delete).Id));
                    }
                }
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }
        public ActionResult DelEmployeeManage(long id)
        {
            if (Session["CheckFeed"] == null || !(bool) Session["DelSystemManage"] ||
                !(bool) Session["CheckSystemManage"])
            {
                Response.Write("<script>history.go(0);</script>");
                return View("Error");
            }

            var employeeService = new EmployeeService();
            var roleService = new RoleService();
            employee employee = employeeService.Find(id);
            List<role> roles = roleService.FindByEmployeeId(id);
            //如果该员工有生产信息与之关联,则无法删除
            if (employee.cure_pig.Count != 0 || employee.feed.Count != 0 || employee.fodder_take.Count != 0 ||
                employee.inspection.Count != 0 || employee.prevention.Count != 0 || employee.sale.Count != 0 ||
                employee.task.Count != 0 || employee.task_employee.Count != 0)
            {
                Response.Write("<script language='JavaScript'>alert('该员工有生产信息与之关联,无法删除.');history.go(-1);</script>");
                return Content("nothing");
            }
            foreach (role role in roles)
            {
                roleService.Delete(role);
            }
            employeeService.Delete(employee);

            return RedirectToAction("EmployeeManage");
        }
        public bool DelEmp(long id)
        {
            var employeeService = new EmployeeService();
            var roleService = new RoleService();
            employee employee = employeeService.Find(id);
            List<role> roles = roleService.FindByEmployeeId(id);
            //如果该员工有生产信息与之关联,则无法删除
            if (employee.cure_pig.Count != 0 || employee.feed.Count != 0 || employee.fodder_take.Count != 0 ||
                employee.inspection.Count != 0 || employee.prevention.Count != 0 || employee.sale.Count != 0 ||
                employee.task.Count != 0 || employee.task_employee.Count != 0)
            {
                return false;
            }
            foreach (role role in roles)
            {
                roleService.Delete(role);
            }
            employeeService.Delete(employee);

            return true;
        }