public ActionResult AddOrEditDepartment(Department dep)//AddContract保存相应
        {
            V_HR_Dep testdep = entities.V_HR_Dep.Find(dep.ID);

            dep.Level = GetLevel(dep.ID1, dep.ID2, dep.ID3, dep.ID4);
            if (dep.Level != 0)
            {
                DirectResult r = new DirectResult();
                if (testdep == null)//为空为添加
                {
                    dep.ID          = Tool.ProduceSed64();;
                    dep.Valid       = true;
                    dep.CreaterName = new LoginUser().EmployeeId;
                    dep.CreateTime  = DateTime.Now;
                    dep.PreID       = GetPreID(dep.Level, dep.ID1, dep.ID2, dep.ID3, dep.ID4);

                    if (dep.Level == 1)
                    {
                        entities.T_HR_Department1.Add(dep.ToDB1(1));
                    }
                    if (dep.Level == 2)
                    {
                        entities.T_HR_Department2.Add(dep.ToDB2(1));
                    }
                    if (dep.Level == 3)
                    {
                        entities.T_HR_Department3.Add(dep.ToDB3(1));
                    }
                    if (dep.Level == 4)
                    {
                        entities.T_HR_Department4.Add(dep.ToDB4(1));
                    }
                    if (dep.Level == 5)
                    {
                        entities.T_HR_Department5.Add(dep.ToDB5(1));
                    }

                    try
                    {
                        entities.SaveChanges();
                        r.Success = true;
                        X.Msg.Alert("提示", "保存成功!", new JFunction {
                            Fn = "closewindow"
                        }).Show();
                    }
                    catch (Exception e)
                    {
                        X.Msg.Alert("警告", "数据保存失败!<br /> note:" + e.Message, new JFunction {
                            Fn = "closewindow"
                        }).Show();
                        r.Success = false;
                    }
                }
                else//否则为修改
                {
                    dep.EditorName = new LoginUser().EmployeeId;
                    dep.EditeTime  = DateTime.Now;
                    dep.PreID      = GetPreID(dep.Level, dep.ID1, dep.ID2, dep.ID3, dep.ID4);

                    #region
                    if (testdep.Level == "1")
                    {
                        T_HR_Department1 dep1 = entities.T_HR_Department1.Find(dep.ID);
                        dep1.Department1No   = dep.No;
                        dep1.Department1Name = dep.Name;
                        dep1.Remark          = dep.Remark;
                        dep1.EditorName      = dep.EditorName;
                        dep1.EditeTime       = dep.EditeTime;
                        dep1.DOrder          = dep.DOrder;
                    }
                    if (testdep.Level == "2")
                    {
                        T_HR_Department2 dep2 = entities.T_HR_Department2.Find(dep.ID);
                        dep2.Department2No   = dep.No;
                        dep2.Department2Name = dep.Name;
                        dep2.Remark          = dep.Remark;
                        dep2.EditorName      = dep.EditorName;
                        dep2.EditeTime       = dep.EditeTime;
                        dep2.DOrder          = dep.DOrder;
                    }
                    if (testdep.Level == "3")
                    {
                        T_HR_Department3 dep3 = entities.T_HR_Department3.Find(dep.ID);
                        dep3.Department3No   = dep.No;
                        dep3.Department3Name = dep.Name;
                        dep3.Remark          = dep.Remark;
                        dep3.EditorName      = dep.EditorName;
                        dep3.EditeTime       = dep.EditeTime;
                        dep3.DOrder          = dep.DOrder;
                    }
                    if (testdep.Level == "4")
                    {
                        T_HR_Department4 dep4 = entities.T_HR_Department4.Find(dep.ID);
                        dep4.Department4No   = dep.No;
                        dep4.Department4Name = dep.Name;
                        dep4.Remark          = dep.Remark;
                        dep4.EditorName      = dep.EditorName;
                        dep4.EditeTime       = dep.EditeTime;
                        dep4.DOrder          = dep.DOrder;
                    }
                    if (testdep.Level == "5")
                    {
                        T_HR_Department5 dep5 = entities.T_HR_Department5.Find(dep.ID);
                        dep5.Department5No   = dep.No;
                        dep5.Department5Name = dep.Name;
                        dep5.Remark          = dep.Remark;
                        dep5.EditorName      = dep.EditorName;
                        dep5.EditeTime       = dep.EditeTime;
                        dep5.DOrder          = dep.DOrder;
                    }
                    #endregion

                    string depname = GetDepartment(dep.Level, dep.ID1, dep.ID2, dep.ID3, dep.ID4, dep.Name);
                    var    staffs  = from o in entities.T_HR_Staff
                                     where o.ID1 == dep.ID || o.ID2 == dep.ID || o.ID3 == dep.ID || o.ID4 == dep.ID || o.ID5 == dep.ID
                                     select o;
                    foreach (var item in staffs)
                    {
                        T_HR_Staff staff = entities.T_HR_Staff.Find(item.StaffID);
                        staff.Department = depname;
                    }

                    try
                    {
                        entities.SaveChanges();
                        r.Success = true;
                        X.Msg.Alert("提示", "修改成功!", new JFunction {
                            Fn = "closewindow"
                        }).Show();
                    }
                    catch (Exception e)
                    {
                        X.Msg.Alert("警告", "数据修改失败!<br /> note:" + e.Message, new JFunction {
                            Fn = "closewindow"
                        }).Show();
                        r.Success = false;
                    }
                }
                return(r);
            }
            else
            {
                X.Msg.Alert("警告", "上级部门不能跳过为空!").Show();
                return(this.Direct());
            }
        }
        public ActionResult AddDepartment(string id) //在修改时传递的为contractid
        {
            if (id == "-1")                          //-1为添加
            {
                return(View(new Department()));
            }
            else//否则为修改
            {
                V_HR_Dep item = (from o in entities.V_HR_Dep
                                 where o.ID == id
                                 select o).First();

                Department de = new Department();

                de.ID          = item.ID;
                de.PreID       = item.PreID;
                de.No          = item.No;
                de.Name        = item.Name;
                de.Level       = Convert.ToInt32(item.Level);
                de.DOrder      = Convert.ToInt32(item.DOrder);
                de.Remark      = item.Remark;
                de.CreaterName = item.CreaterName;
                de.CreateTime  = Convert.ToDateTime(item.CreateTime);
                de.EditorName  = item.EditorName;
                de.EditeTime   = Convert.ToDateTime(item.EditeTime);
                de.Valid       = Convert.ToBoolean(item.Valid);

                if (de.Level == 2)
                {
                    T_HR_Department1 d1 = (from o in entities.T_HR_Department1
                                           where o.ID1 == de.PreID
                                           select o).First();
                    de.ID1 = d1.ID1;
                }

                if (de.Level == 3)
                {
                    T_HR_Department2 d2 = (from o in entities.T_HR_Department2
                                           where o.ID2 == de.PreID
                                           select o).First();
                    de.ID2 = d2.ID2;
                    de.ID1 = d2.ID1;
                }

                if (de.Level == 4)
                {
                    T_HR_Department3 d3 = (from o in entities.T_HR_Department3
                                           where o.ID3 == de.PreID
                                           select o).First();
                    de.ID3 = d3.ID3;
                    de.ID2 = d3.ID2;

                    T_HR_Department2 d2 = (from o in entities.T_HR_Department2
                                           where o.ID2 == de.ID2
                                           select o).First();
                    de.ID1 = d2.ID1;
                }

                if (de.Level == 5)
                {
                    T_HR_Department4 d4 = (from o in entities.T_HR_Department4
                                           where o.ID4 == de.PreID
                                           select o).First();
                    de.ID4 = d4.ID4;
                    de.ID3 = d4.ID3;

                    T_HR_Department3 d3 = (from o in entities.T_HR_Department3
                                           where o.ID3 == de.ID3
                                           select o).First();
                    de.ID2 = d3.ID2;

                    T_HR_Department2 d2 = (from o in entities.T_HR_Department2
                                           where o.ID2 == de.ID2
                                           select o).First();
                    de.ID1 = d2.ID1;
                }

                var x = X.GetCmp <FieldSet>("BaseList");
                x.Hidden = true;

                return(View(de));
            }
        }