【无私分享:从入门到精通ASP.NET MVC】从0开始,一起搭框架、做项目 (10)部门管理、岗位管理和员工管理

声明:本系列为原创,分享本人现用框架,未经本人同意,禁止转载!http://yuangang.cnblogs.com

希望大家好好一步一步做,所有的技术和项目,都毫无保留的提供,希望大家能自己跟着做一套,还有,请大家放心,只要大家喜欢,有人需要,绝对不会烂尾,我会坚持写完~

如果你感觉文章有帮助,点一下推荐,让更多的朋友参与进来,也是对本人劳动成果的鼓励,谢谢大家!由于还要工作,所以基本都是牺牲午休时间来写博客的,写博客呢不是简单的Ctrl+C、Ctrl+V,我是要挨着做一遍的,这也是对大家负责,所以有些时候更新不及时,或者问题没有及时解答,希望大家谅解,再次感谢大家!!

因为我引用了许多以前积累的类库,所以有些东西是重复的(后来更新),有些东西是过时的,包括我写的代码,希望大家不要纯粹的复制,取其精华去其糟粕>_<。

索引

【无私分享:从入门到精通ASP.NET MVC】从0开始,一起搭框架、做项目 目录索引

简述

今天我们来增加部门管理、岗位管理和员工管理

项目准备

我们用的工具是:VS 2013 + SqlServer 2012 + IIS7.5

希望大家对ASP.NET MVC有一个初步的理解,理论性的东西我们不做过多解释,有些地方不理解也没关系,会用就行了,用的多了,用的久了,自然就理解了。

项目开始

我们前面讲过很多增删改查和列表的操作了,这里不再继续罗列这些操作了,直接上代码,员工管理是比较复杂的部门,要为员工分配角色、部门岗位、还有档案信息,我们重点介绍一下这一块

一、部门管理

首先,部门表SYS_DEPARTMENT

表名:SYS_DEPARTMENT
序号 列名 数据类型 长度 小数位 标识 主键 外键 允许空 默认值 说明
1 ID nvarchar 36 0       主键ID
2 CODE nvarchar 100 0         部门编号,横向增长
3 NAME nvarchar 200 0         部门名称
4 BUSINESSLEVEL int 4 0         业务等级
5 SHOWORDER int 4 0         部门排序
6 CREATEPERID nvarchar 36 0         创建者编号
7 CREATEDATE date 3 0         创建时间
8 PARENTID nvarchar 36 0         上级部门ID
9 UPDATEDATE date 3 0         修改时间
10 UPDATEUSER nvarchar 36 0         修改人
11 PARENTCODE nvarchar 100 0          

T-SQL:

 1 USE [wkmvc_db]
 2 GO
 3 /****** Object:  Table [dbo].[SYS_DEPARTMENT]    Script Date: 2016/6/20 15:55:28 ******/
 4 SET ANSI_NULLS ON
 5 GO
 6 SET QUOTED_IDENTIFIER ON
 7 GO
 8 CREATE TABLE [dbo].[SYS_DEPARTMENT](
 9     [ID] [nvarchar](36) NOT NULL,
10     [CODE] [nvarchar](100) NULL,
11     [NAME] [nvarchar](200) NULL,
12     [BUSINESSLEVEL] [int] NULL,
13     [SHOWORDER] [int] NULL,
14     [CREATEPERID] [nvarchar](36) NULL,
15     [CREATEDATE] [date] NULL,
16     [PARENTID] [nvarchar](36) NULL,
17     [UPDATEDATE] [date] NULL,
18     [UPDATEUSER] [nvarchar](36) NULL,
19     [PARENTCODE] [nvarchar](100) NULL,
20  CONSTRAINT [PK_SYS_DEPARTMENT] PRIMARY KEY CLUSTERED 
21 (
22     [ID] ASC
23 )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
24 ) ON [PRIMARY]
25 
26 GO
27 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'主键ID' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_DEPARTMENT', @level2type=N'COLUMN',@level2name=N'ID'
28 GO
29 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'部门编号,横向增长' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_DEPARTMENT', @level2type=N'COLUMN',@level2name=N'CODE'
30 GO
31 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'部门名称' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_DEPARTMENT', @level2type=N'COLUMN',@level2name=N'NAME'
32 GO
33 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'业务等级' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_DEPARTMENT', @level2type=N'COLUMN',@level2name=N'BUSINESSLEVEL'
34 GO
35 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'部门排序' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_DEPARTMENT', @level2type=N'COLUMN',@level2name=N'SHOWORDER'
36 GO
37 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'创建者编号' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_DEPARTMENT', @level2type=N'COLUMN',@level2name=N'CREATEPERID'
38 GO
39 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'创建时间' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_DEPARTMENT', @level2type=N'COLUMN',@level2name=N'CREATEDATE'
40 GO
41 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'上级部门ID' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_DEPARTMENT', @level2type=N'COLUMN',@level2name=N'PARENTID'
42 GO
43 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'修改时间' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_DEPARTMENT', @level2type=N'COLUMN',@level2name=N'UPDATEDATE'
44 GO
45 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'修改人' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_DEPARTMENT', @level2type=N'COLUMN',@level2name=N'UPDATEUSER'
46 GO
View Code

 

1、在SysManage下新建控制器

 

2、还是注意的 别忘了配置xml注入,不然会提示未将对象设置引用到实例的啊

 

3、声明容器

 

4、部门管理就是简单的一个 增删改查,我们前面做过很多很多这样的操作了,这里我就不在单步解释了,我把方法给大家

4.1加载主页

 1 /// <summary>
 2         /// 加载主页
 3         /// </summary>
 4         [UserAuthorizeAttribute(ModuleAlias = "Department", OperaAction = "View")]
 5         public ActionResult Index()
 6         {
 7             try
 8             {
 9                 ViewBag.Search = base.keywords;
10                 return View(BindList());
11             }
12             catch (Exception e)
13             {
14                 WriteLog(Common.Enums.enumOperator.Select, "部门管理加载主页:", e);
15                 throw e.InnerException;
16             }
17         }
View Code
/// <summary>
        /// 分页查询部门列表
        /// </summary>
        private object BindList()
        {
            //基础数据
            var query = this.DepartmentManage.LoadAll(null);

            //递归排序(无分页)
            var result = this.DepartmentManage.RecursiveDepartment(query.ToList())
                .Select(p => new
            {
                p.ID,
                p.NAME,
                DepartName=DepartmentManage.GetDepartmentName(p.NAME,p.BUSINESSLEVEL),
                p.BUSINESSLEVEL,
                p.SHOWORDER,
                p.CREATEDATE
            });

            //查询关键字
            if (!string.IsNullOrEmpty(keywords))
            {
                result = result.Where(p => p.NAME.Contains(keywords));
            }

            return Common.JsonConverter.JsonClass(result);
        }
View Code

4.2加载详情页

 1 /// <summary>
 2         /// 加载详情页
 3         /// </summary>
 4         [UserAuthorizeAttribute(ModuleAlias = "Department", OperaAction = "Detail")]
 5         public ActionResult Detail(string id)
 6         {
 7             try
 8             {
 9                 var entity = new Domain.SYS_DEPARTMENT();
10 
11                 ViewBag.moduleparent = this.DepartmentManage.GetDepartmentByDetail();
12 
13                 //添加子部门
14                 string parentId = Request.QueryString["parentId"];
15 
16                 if (!string.IsNullOrEmpty(parentId))
17                 {
18                     entity.PARENTID = parentId;
19                 }
20                 if (!string.IsNullOrEmpty(id))
21                 {
22                     entity = this.DepartmentManage.Get(p => p.ID == id);
23                 }
24                 return View(entity);
25             }
26             catch (Exception e)
27             {
28                 WriteLog(Common.Enums.enumOperator.Select, "部门管理加载详情页:", e);
29                 throw e.InnerException;
30             }
31         }
View Code

4.3保存部门

  1 /// <summary>
  2         /// 保存部门
  3         /// </summary>
  4         [ValidateInput(false)]
  5         [UserAuthorizeAttribute(ModuleAlias = "Department", OperaAction = "Add,Edit")]
  6         public ActionResult Save(Domain.SYS_DEPARTMENT entity)
  7         {
  8             bool isEdit = false;
  9             var json = new JsonHelper() { Msg = "保存成功", Status = "n" };
 10             try
 11             {
 12                 var _entity = new Domain.SYS_DEPARTMENT();
 13                 if (entity != null)
 14                 {
 15                     if (!string.IsNullOrEmpty(entity.ID))
 16                     {
 17                         #region 修改
 18                         _entity = this.DepartmentManage.Get(p => p.ID == entity.ID);
 19                         entity.CREATEDATE = _entity.CREATEDATE;
 20                         entity.CREATEPERID = _entity.CREATEPERID;
 21                         entity.UPDATEDATE = DateTime.Now;
 22                         entity.UPDATEUSER = this.CurrentUser.Name;
 23                         if (entity.PARENTID != _entity.PARENTID)
 24                         {
 25                             entity.CODE = this.DepartmentManage.CreateCode(entity.PARENTID);
 26                         }
 27                         else
 28                         {
 29                             entity.CODE = _entity.CODE;
 30                         }
 31                         //获取父级记录
 32                         if (string.IsNullOrEmpty(_entity.PARENTID))
 33                         {
 34                             //业务等级
 35                             entity.BUSINESSLEVEL = 1;
 36                             entity.PARENTCODE = null;
 37                         }
 38                         else
 39                         {
 40                             var parententity = this.DepartmentManage.Get(p => p.ID == entity.PARENTID);
 41                             entity.BUSINESSLEVEL = parententity.BUSINESSLEVEL + 1;
 42                             entity.PARENTCODE = parententity.CODE;
 43                         }
 44                         #endregion
 45                         isEdit = true;
 46                         _entity = entity;
 47                     }
 48                     else
 49                     {
 50                         #region 添加
 51                         _entity = entity;
 52                         _entity.ID = Guid.NewGuid().ToString();
 53                         _entity.CREATEDATE = DateTime.Now;
 54                         _entity.CREATEPERID = this.CurrentUser.Name;
 55                         _entity.UPDATEDATE = DateTime.Now;
 56                         _entity.UPDATEUSER = this.CurrentUser.Name;
 57                         //根据上级部门的ID确定当前部门的CODE
 58                         _entity.CODE = this.DepartmentManage.CreateCode(entity.PARENTID);
 59                         //获取父级记录
 60                         if (string.IsNullOrEmpty(entity.PARENTID))
 61                         {
 62                             //业务等级
 63                             entity.BUSINESSLEVEL = 1;
 64                             entity.PARENTCODE = null;
 65                         }
 66                         else
 67                         {
 68                             var parententity = this.DepartmentManage.Get(p => p.ID == entity.PARENTID);
 69                             entity.BUSINESSLEVEL = parententity.BUSINESSLEVEL + 1;
 70                             entity.PARENTCODE = parententity.CODE;
 71                         }
 72                         #endregion
 73                     }
 74                     //判断同一个部门下,是否重名 
 75                     var predicate = PredicateBuilder.True<Domain.SYS_DEPARTMENT>();
 76                     predicate = predicate.And(p => p.PARENTID == _entity.PARENTID);
 77                     predicate = predicate.And(p => p.NAME == _entity.NAME);
 78                     predicate = predicate.And(p => p.ID != _entity.ID);
 79                     if (!this.DepartmentManage.IsExist(predicate))
 80                     {
 81                         if (this.DepartmentManage.SaveOrUpdate(_entity, isEdit))
 82                         {
 83                             json.Status = "y";
 84                         }
 85                         else
 86                         {
 87                             json.Msg = "保存失败";
 88                         }
 89                     }
 90                     else
 91                     {
 92                         json.Msg = "部门" + entity.NAME + "已存在,不能重复添加";
 93                     }
 94                 }
 95                 else
 96                 {
 97                     json.Msg = "未找到需要保存的部门信息";
 98                 }
 99                 if (isEdit)
100                 {
101                     WriteLog(Common.Enums.enumOperator.Edit, "修改部门信息,结果:" + json.Msg, Common.Enums.enumLog4net.INFO);
102                 }
103                 else
104                 {
105                     WriteLog(Common.Enums.enumOperator.Add, "添加部门信息,结果:" + json.Msg, Common.Enums.enumLog4net.INFO);
106                 }
107             }
108             catch (Exception e)
109             {
110                 json.Msg = "保存部门信息发生内部错误!";
111                 WriteLog(Common.Enums.enumOperator.None, "保存部门信息:", e);
112             }
113             return Json(json);
114 
115         }
View Code

4.4删除部门

 1 /// <summary>
 2         /// 删除部门
 3         /// </summary>
 4         [UserAuthorizeAttribute(ModuleAlias = "Department", OperaAction = "Remove")]
 5         public ActionResult Delete(string idList)
 6         {
 7             JsonHelper json = new JsonHelper() { Msg = "删除部门成功", ReUrl = "/Department/Index", Status = "n" };
 8             try
 9             {
10                 if (!string.IsNullOrEmpty(idList))
11                 {
12                     idList = idList.TrimEnd(',');
13                     //判断是否有下属部门
14                     if (!this.DepartmentManage.DepartmentIsExists(idList))
15                     {
16                         //判断该部门是否有岗位
17                         if (!this.PostManage.IsExist(p => idList.Contains(p.FK_DEPARTID)))
18                         {                           
19                                 var idList1 = idList.Split(',').ToList();
20                                 this.DepartmentManage.Delete(p => idList.Contains(p.ID));
21                                 json.Status = "y";                          
22                         }
23                         else
24                         {
25                             json.Msg = "该部门有岗位信息不能删除";
26                         }
27                     }
28                     else
29                     {
30                         json.Msg = "该部门有下属部门不能删除";
31                     }
32                 }
33                 else
34                 {
35                     json.Msg = "未找到要删除的部门记录";
36                 }
37                 WriteLog(Common.Enums.enumOperator.Remove, "删除部门:" + json.Msg, Common.Enums.enumLog4net.WARN);
38             }
39             catch (Exception e)
40             {
41                 json.Msg = "删除部门发生内部错误!";
42                 WriteLog(Common.Enums.enumOperator.Remove, "删除部门:", e);
43             }
44             return Json(json);
45         }
View Code

4.5在岗位管理中 我们需要用到的部门的树形菜单

 1 /// <summary>
 2         /// 获取部门树形菜单
 3         /// </summary>
 4         public ActionResult GetTree()
 5         {
 6             var json = new JsonHelper() { Msg = "Success", Status = "y" };
 7 
 8             try
 9             {
10                 //获取部门列表 按照 SHOWORDER字段 升序排列
11                 var query = this.DepartmentManage.LoadAll(null).OrderBy(p => p.SHOWORDER).ToList();
12                 var result = query.Select(m => new
13                 {
14                     id = m.ID,
15                     parent = !string.IsNullOrEmpty(m.PARENTID) ? m.PARENTID.ToString() : "#",
16                     text = m.NAME,
17                     icon = m.BUSINESSLEVEL == 0 ? "fa fa-circle text-warning" : "fa fa-circle text-navy"
18                 }).ToList();
19 
20                 json.Data = result;
21             }
22             catch (Exception e)
23             {
24                 json.Status = "n";
25                 json.Msg = "服务器忙,请稍后再试!";
26                 WriteLog(Common.Enums.enumOperator.Select, "权限管理,获取模块树:", e);
27             }
28             return Json(json);
29         }
View Code

【无私分享:从入门到精通ASP.NET MVC】从0开始,一起搭框架、做项目 (10)部门管理、岗位管理和员工管理_第1张图片

【无私分享:从入门到精通ASP.NET MVC】从0开始,一起搭框架、做项目 (10)部门管理、岗位管理和员工管理_第2张图片

【无私分享:从入门到精通ASP.NET MVC】从0开始,一起搭框架、做项目 (10)部门管理、岗位管理和员工管理_第3张图片

 

二、岗位管理

岗位管理,我们参照权限管理的展示方式,左右分栏展示,左侧显示部门,右侧显示部门下的岗位

【无私分享:从入门到精通ASP.NET MVC】从0开始,一起搭框架、做项目 (10)部门管理、岗位管理和员工管理_第4张图片

 

1、岗位表SYS_POST

 

表名:SYS_POST
序号 列名 数据类型 长度 小数位 标识 主键 外键 允许空 默认值 说明
1 ID nvarchar 36 0       主键ID
2 FK_DEPARTID nvarchar 36 0        
3 POSTNAME nvarchar 100 0         岗位名称
4 POSTTYPE nvarchar 36 0         岗位类型
5 REMARK nvarchar 500 0         岗位备注
6 SHOWORDER int 4 0         排序
7 CREATEUSER nvarchar 50 0         创建者ID
8 CREATEDATE datetime 8 3         创建时间
9 UPDATEDATE datetime 8 3         修改时间
10 UPDATEUSER nvarchar 36 0         修改者

T-SQL:

 1 USE [wkmvc_db]
 2 GO
 3 /****** Object:  Table [dbo].[SYS_POST]    Script Date: 2016/6/20 16:13:10 ******/
 4 SET ANSI_NULLS ON
 5 GO
 6 SET QUOTED_IDENTIFIER ON
 7 GO
 8 CREATE TABLE [dbo].[SYS_POST](
 9     [ID] [nvarchar](36) NOT NULL,
10     [FK_DEPARTID] [nvarchar](36) NOT NULL,
11     [POSTNAME] [nvarchar](100) NULL,
12     [POSTTYPE] [nvarchar](36) NOT NULL,
13     [REMARK] [nvarchar](500) NULL,
14     [SHOWORDER] [int] NULL,
15     [CREATEUSER] [nvarchar](50) NULL,
16     [CREATEDATE] [datetime] NOT NULL,
17     [UPDATEDATE] [datetime] NULL,
18     [UPDATEUSER] [nvarchar](36) NULL,
19  CONSTRAINT [PK_SYS_POST] PRIMARY KEY CLUSTERED 
20 (
21     [ID] ASC
22 )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
23 ) ON [PRIMARY]
24 
25 GO
26 ALTER TABLE [dbo].[SYS_POST]  WITH CHECK ADD  CONSTRAINT [FK_SYS_POST_SYS_DEPARTMENT] FOREIGN KEY([FK_DEPARTID])
27 REFERENCES [dbo].[SYS_DEPARTMENT] ([ID])
28 GO
29 ALTER TABLE [dbo].[SYS_POST] CHECK CONSTRAINT [FK_SYS_POST_SYS_DEPARTMENT]
30 GO
31 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'主键ID' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_POST', @level2type=N'COLUMN',@level2name=N'ID'
32 GO
33 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'岗位名称' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_POST', @level2type=N'COLUMN',@level2name=N'POSTNAME'
34 GO
35 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'岗位类型' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_POST', @level2type=N'COLUMN',@level2name=N'POSTTYPE'
36 GO
37 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'岗位备注' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_POST', @level2type=N'COLUMN',@level2name=N'REMARK'
38 GO
39 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'排序' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_POST', @level2type=N'COLUMN',@level2name=N'SHOWORDER'
40 GO
41 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'创建者ID' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_POST', @level2type=N'COLUMN',@level2name=N'CREATEUSER'
42 GO
43 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'创建时间' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_POST', @level2type=N'COLUMN',@level2name=N'CREATEDATE'
44 GO
45 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'修改时间' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_POST', @level2type=N'COLUMN',@level2name=N'UPDATEDATE'
46 GO
47 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'修改者' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_POST', @level2type=N'COLUMN',@level2name=N'UPDATEUSER'
48 GO
View Code

 

2、同样在SysManage 下新建岗位控制器 PostController

【无私分享:从入门到精通ASP.NET MVC】从0开始,一起搭框架、做项目 (10)部门管理、岗位管理和员工管理_第5张图片

 

3、声明容器

 1 #region 声明容器
 2         /// <summary>
 3         /// 岗位
 4         /// </summary>
 5         IPostManage PostManage { get; set; }
 6         /// <summary>
 7         /// 部门
 8         /// </summary>
 9         IDepartmentManage DepartmentManage { get; set; }
10         /// <summary>
11         /// 字典编码
12         /// </summary>
13         ICodeManage CodeManage { get; set; }
14         /// <summary>
15         /// 岗位人员
16         /// </summary>
17         IPostUserManage PostUserManage { get; set; }
18         #endregion
View Code

 

4、既然是左右分栏,所以我们需要一个Home视图页,

 

5、当点击左侧的部门的时候 我们就为Iframe 赋值了页面地址 Post/Index 并且传入了部门ID 

5.1岗位管理 岗位列表

 1  /// <summary>
 2         /// 岗位管理 岗位列表
 3         /// </summary>
 4         /// <returns></returns>
 5         [UserAuthorizeAttribute(ModuleAlias = "Post", OperaAction = "View")]
 6         public ActionResult Index()
 7         {
 8             try
 9             {
10                 #region 处理查询参数
11                 //获取部门ID
12                 var departId = Request.QueryString["departId"] ?? (Request.Form["departId"] ?? "");
13                 //岗位类型
14                 string posttype = Request.QueryString["posttype"];
15 
16                 ViewBag.Search = base.keywords;
17                 #endregion
18 
19                 //如果部门ID不为空或NULL
20                 if (!string.IsNullOrEmpty(departId))
21                 {                    
22                     //部门信息
23                     var department = this.DepartmentManage.Get(p => p.ID == departId);
24 
25                     ViewBag.Department = department;
26 
27                     ViewData["post"] = posttype;
28 
29                     ViewData["PostType"] = this.CodeManage.GetCode("POSTTYPE");
30 
31                     return View(BindList(posttype, departId));
32                 }
33 
34                 return View();
35             }
36             catch (Exception e)
37             {
38                 WriteLog(Common.Enums.enumOperator.Select, "对模块权限按钮的管理加载主页:", e);
39                 throw e.InnerException;
40             }
41         }
View Code
 1 /// <summary>
 2         /// 分页查询岗位列表
 3         /// </summary>
 4         private Common.PageInfo BindList(string posttype, string departId)
 5         {
 6             //基础数据
 7             var query = this.PostManage.LoadAll(null);
 8             //岗位类型
 9             if (!string.IsNullOrEmpty(posttype))
10             {
11                 query = query.Where(p => p.POSTTYPE == posttype && p.FK_DEPARTID == departId);
12             }
13             else
14             {
15                 query = query.Where(p =>  p.FK_DEPARTID == departId);
16             }
17             //查询关键字
18             if (!string.IsNullOrEmpty(keywords))
19             {
20                 query = query.Where(p => p.POSTNAME.Contains(keywords));
21             }
22             //排序
23             query = query.OrderBy(p => p.SHOWORDER);
24             //分页
25             var result = this.PostManage.Query(query, page, pagesize);
26 
27             var list = result.List.Select(p => new
28             {
29                 p.ID,
30                 p.POSTNAME,
31                 POSTTYPE = this.CodeManage.GetCode("POSTTYPE", p.POSTTYPE).First().NAMETEXT,
32                 p.CREATEDATE,
33                 USERCOUNT = PostUserManage.LoadAll(m=>m.FK_POSTID==p.ID).Count()
34 
35             }).ToList();
36 
37             return new Common.PageInfo(result.Index, result.PageSize, result.Count, Common.JsonConverter.JsonClass(list));
38         }
View Code

5.2加载详情

 1 /// <summary>
 2         /// 加载详情
 3         /// </summary>
 4         /// <returns></returns>
 5         [HttpGet]
 6         [UserAuthorizeAttribute(ModuleAlias = "Post", OperaAction = "Detail")]
 7         public ActionResult Detail(string id)
 8         {
 9             try
10             {
11                 //岗位类型
12                 ViewData["PostType"] = this.CodeManage.GetCode("POSTTYPE");
13                 //获取部门ID
14                 var departId = Request.QueryString["departId"];
15 
16                 var _entity = this.PostManage.Get(p => p.ID == id) ?? new Domain.SYS_POST() { FK_DEPARTID = departId };
17 
18                 return View(_entity);
19             }
20             catch (Exception e)
21             {
22                 WriteLog(Common.Enums.enumOperator.Select, "岗位管理加载详情:", e);
23                 throw e.InnerException;
24             }
25         }
View Code

5.3保存岗位

 1 /// <summary>
 2         /// 保存岗位
 3         /// </summary>
 4         [UserAuthorizeAttribute(ModuleAlias = "Post", OperaAction = "Add,Edit")]
 5         public ActionResult Save(Domain.SYS_POST entity)
 6         {
 7 
 8             bool isEdit = false;
 9             var json = new JsonHelper() { Msg = "保存岗位成功", Status = "n", ReUrl = "/Post/Index" };
10             try
11             {
12                 if (entity != null)
13                 {
14                     //添加
15                     if (string.IsNullOrEmpty(entity.ID))
16                     {
17                         entity.ID = Guid.NewGuid().ToString();
18                         entity.CREATEDATE = DateTime.Now;
19                         entity.CREATEUSER = CurrentUser.Name;
20                         entity.UPDATEDATE = DateTime.Now;
21                         entity.UPDATEUSER = this.CurrentUser.Name;
22                     }
23                     else //修改
24                     {
25                         entity.UPDATEDATE = DateTime.Now;
26                         entity.UPDATEUSER = this.CurrentUser.Name;
27                         isEdit = true;
28                     }
29                     //判断岗位是否重名 
30                     if (!this.PostManage.IsExist(p => p.POSTNAME == entity.POSTNAME && p.ID != entity.ID))
31                     {
32                         if (PostManage.SaveOrUpdate(entity, isEdit))
33                         {
34                             json.Status = "y";
35                         }
36                         else
37                         {
38                             json.Msg = "保存失败";
39                         }
40                     }
41                     else
42                     {
43                         json.Msg = "岗位" + entity.POSTNAME + "已存在,不能重复添加";
44                     }
45                 }
46                 else
47                 {
48                     json.Msg = "未找到需要保存的岗位";
49                 }
50                 if (isEdit)
51                 {
52                     WriteLog(Common.Enums.enumOperator.Edit, "修改岗位,结果:" + json.Msg, Common.Enums.enumLog4net.INFO);
53                 }
54                 else
55                 {
56                     WriteLog(Common.Enums.enumOperator.Add, "添加岗位,结果:" + json.Msg, Common.Enums.enumLog4net.INFO);
57                 }
58             }
59             catch (Exception e)
60             {
61                 json.Msg = "保存岗位发生内部错误!";
62                 WriteLog(Common.Enums.enumOperator.None, "保存岗位:", e);
63             }
64             return Json(json);
65 
66 
67         }
View Code

5.4删除岗位

 1 /// <summary>
 2         /// 删除岗位
 3         /// </summary>
 4         [UserAuthorizeAttribute(ModuleAlias = "Post", OperaAction = "Remove")]
 5         public ActionResult Delete(string idList)
 6         {
 7             JsonHelper json = new JsonHelper() { Msg = "删除岗位完毕", Status = "n" };
 8             try
 9             {
10                 if (!string.IsNullOrEmpty(idList))
11                 {
12                     idList = idList.Trim(',');
13                         //判断岗位是否分配人员
14                         if (!this.PostUserManage.IsExist(p => idList.Contains(p.FK_POSTID)))
15                         {
16                             this.PostManage.Delete(p => idList.Contains(p.ID));
17                             json.Status = "y";
18                         }
19                         else
20                         {
21                             json.Msg = "该岗位已经分配人员,不能删除";
22                         }                   
23                 }
24                 else
25                 {
26                     json.Msg = "未找到要删除的岗位记录";
27                 }
28                 WriteLog(Common.Enums.enumOperator.Remove, "删除岗位,结果:" + json.Msg, Common.Enums.enumLog4net.WARN);
29             }
30             catch (Exception e)
31             {
32                 json.Msg = "删除岗位发生内部错误!";
33                 WriteLog(Common.Enums.enumOperator.Remove, "删除岗位:", e);
34             }
35             return Json(json);
36         }
View Code

5.5员工管理需要用到的获取岗位列表

 1  /// <summary>
 2         /// 获取岗位列表
 3         /// </summary>
 4         /// <returns></returns>
 5         public ActionResult GetPostByDepart()
 6         {
 7             var departId = Request.Form["departId"];
 8             if (!string.IsNullOrEmpty(departId))
 9             {
10                 return Json(this.PostManage.LoadAll(p=>p.FK_DEPARTID==departId)
11                 .Select(p => new
12                 {
13                     ID = p.ID,
14                     NAME=p.POSTNAME
15                 }).ToList(), JsonRequestBehavior.AllowGet);
16             }
17             return new EmptyResult();
18         }
View Code

【无私分享:从入门到精通ASP.NET MVC】从0开始,一起搭框架、做项目 (10)部门管理、岗位管理和员工管理_第6张图片

 

三、员工管理

1、员工管理相关的一些表

数据库名:wkmvc_db
表名:SYS_POST_USER
 
序号 列名 数据类型 长度 小数位 标识 主键 外键 允许空 默认值 说明
1 ID int 4 0     主键ID
2 FK_USERID int 4 0         用户ID
3 FK_POSTID nvarchar 36 0         岗位部门ID
表名:SYS_USER
 
序号 列名 数据类型 长度 小数位 标识 主键 外键 允许空 默认值 说明
1 ID int 4 0     主键ID
2 NAME nvarchar 50 0         真实姓名
3 ACCOUNT nvarchar 20 0         用户帐号
4 PASSWORD nvarchar 1000 0         用户密码
5 ISCANLOGIN bit 1 0         是否锁定(0否1是)
6 SHOWORDER1 int 4 0         部门内的排序
7 SHOWORDER2 int 4 0         公司内的排序
8 PINYIN1 nvarchar 50 0         姓名全拼
9 PINYIN2 nvarchar 50 0         姓名首字符
10 FACE_IMG nvarchar 200 0         用户头像
11 LEVELS nvarchar 36 0         级别(对接SYS_CODE)
12 DPTID nvarchar 36 0         主部门ID,用户所在的部门
13 CREATEPER nvarchar 36 0         创建者
14 CREATEDATE datetime 8 3         创建时间
15 UPDATEUSER nvarchar 36 0         修改者
16 UPDATEDATE datetime 8 3         修改时间
17 LastLoginIP nvarchar 50 0         最后一次登录IP
表名:SYS_USER_PERMISSION
 
序号 列名 数据类型 长度 小数位 标识 主键 外键 允许空 默认值 说明
1 ID int 4 0     主键ID
2 FK_USERID int 4 0         用户ID
3 FK_PERMISSIONID int 4 0       授权ID
表名:SYS_USER_ROLE
 
序号 列名 数据类型 长度 小数位 标识 主键 外键 允许空 默认值 说明
1 ID int 4 0     主键ID
2 FK_USERID int 4 0       用户ID
3 FK_ROLEID int 4 0         角色ID
表名:SYS_USERINFO
 
序号 列名 数据类型 长度 小数位 标识 主键 外键 允许空 默认值 说明
1 ID int 4 0     主键ID
2 USERID int 4 0       用户ID,外键
3 POSTCODE int 4 0         职务,编码
4 PHONE nvarchar 200 0         手机号码
5 OFFICEPHONE nvarchar 200 0         办公电话
6 EMAILADDRESS nvarchar 200 0         Email
7 SECONDPHONE nvarchar 200 0         第二手机号码
8 WORKCODE int 4 0         在岗状态,编码
9 SEXCODE int 4 0         性别,编码
10 BIRTHDAY datetime 8 3         出生日期
11 NATIONCODE int 4 0         民族,编码
12 IDNUMBER nvarchar 18 0         身份证号码
13 MARRYCODE int 4 0         婚姻状况,编码
14 IDENTITYCODE int 4 0         政治面貌,编码
15 HomeTown nvarchar 200 0         籍贯,编码(关联至TBCode_Area的CodeValue)
16 ACCOUNTLOCATION nvarchar 200 0         户籍所在地
17 XUELI int 4 0         学历,编码
18 ZHICHENG int 4 0         职称,编码
19 GRADUATIONSCHOOL nvarchar 200 0         毕业院校
20 SPECIALTY nvarchar 200 0         专业
21 PHOTOOLDNAME nvarchar 200 0         照片原文件名
22 PHOTONEWNAME nvarchar 200 0         照片新文件名
23 PHOTOTYPE nvarchar 200 0         照片格式
24 RESUMEOLDNAME nvarchar 200 0         简历原文件名
25 RESUMENEWNAME nvarchar 200 0         简历新文件名
26 RESUMETYPE nvarchar 200 0         简历格式
27 HuJiSuoZaiDi nvarchar 200 0         户籍所在地,编码(关联至TBCode_Area的CodeValue)
28 HUJIPAICHUSUO nvarchar 200 0         户籍所在派出所
29 WORKDATE datetime 8 3         工作时间
30 JINRUDATE datetime 8 3         加入公司时间
31 CARNUMBER nvarchar 200 0         车牌号
32 QQ nvarchar 15 0         QQ号
33 WEBCHATOPENID nvarchar 200 0         微信公众账号对应的OpenID
34 CREATEDATE datetime 8 3         创建时间
35 CREATEUSER nvarchar 36 0         创建者
36 UPDATEDATE datetime 8 3         修改时间
37 UPDATEUSER nvarchar 36 0         修改者

 

T-SQL:

  1 USE [wkmvc_db]
  2 GO
  3 /****** Object:  Table [dbo].[SYS_POST_USER]    Script Date: 2016/6/20 16:28:44 ******/
  4 SET ANSI_NULLS ON
  5 GO
  6 SET QUOTED_IDENTIFIER ON
  7 GO
  8 CREATE TABLE [dbo].[SYS_POST_USER](
  9     [ID] [int] IDENTITY(1,1) NOT NULL,
 10     [FK_USERID] [int] NOT NULL,
 11     [FK_POSTID] [nvarchar](36) NOT NULL,
 12  CONSTRAINT [PK_SYS_POST_USER] PRIMARY KEY CLUSTERED 
 13 (
 14     [ID] ASC
 15 )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
 16 ) ON [PRIMARY]
 17 
 18 GO
 19 /****** Object:  Table [dbo].[SYS_USER]    Script Date: 2016/6/20 16:28:44 ******/
 20 SET ANSI_NULLS ON
 21 GO
 22 SET QUOTED_IDENTIFIER ON
 23 GO
 24 CREATE TABLE [dbo].[SYS_USER](
 25     [ID] [int] IDENTITY(1,1) NOT NULL,
 26     [NAME] [nvarchar](50) NULL,
 27     [ACCOUNT] [nvarchar](20) NULL,
 28     [PASSWORD] [nvarchar](1000) NULL,
 29     [ISCANLOGIN] [bit] NOT NULL,
 30     [SHOWORDER1] [int] NULL,
 31     [SHOWORDER2] [int] NULL,
 32     [PINYIN1] [nvarchar](50) NULL,
 33     [PINYIN2] [nvarchar](50) NULL,
 34     [FACE_IMG] [nvarchar](200) NULL,
 35     [LEVELS] [nvarchar](36) NULL,
 36     [DPTID] [nvarchar](36) NULL,
 37     [CREATEPER] [nvarchar](36) NULL,
 38     [CREATEDATE] [datetime] NULL,
 39     [UPDATEUSER] [nvarchar](36) NULL,
 40     [UPDATEDATE] [datetime] NULL,
 41     [LastLoginIP] [nvarchar](50) NULL,
 42  CONSTRAINT [PK_SYS_USER] PRIMARY KEY CLUSTERED 
 43 (
 44     [ID] ASC
 45 )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
 46 ) ON [PRIMARY]
 47 
 48 GO
 49 /****** Object:  Table [dbo].[SYS_USER_PERMISSION]    Script Date: 2016/6/20 16:28:44 ******/
 50 SET ANSI_NULLS ON
 51 GO
 52 SET QUOTED_IDENTIFIER ON
 53 GO
 54 CREATE TABLE [dbo].[SYS_USER_PERMISSION](
 55     [ID] [int] IDENTITY(1,1) NOT NULL,
 56     [FK_USERID] [int] NOT NULL,
 57     [FK_PERMISSIONID] [int] NOT NULL,
 58  CONSTRAINT [PK_SYS_USER_PERMISSION] PRIMARY KEY CLUSTERED 
 59 (
 60     [ID] ASC
 61 )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
 62 ) ON [PRIMARY]
 63 
 64 GO
 65 /****** Object:  Table [dbo].[SYS_USER_ROLE]    Script Date: 2016/6/20 16:28:44 ******/
 66 SET ANSI_NULLS ON
 67 GO
 68 SET QUOTED_IDENTIFIER ON
 69 GO
 70 CREATE TABLE [dbo].[SYS_USER_ROLE](
 71     [ID] [int] IDENTITY(1,1) NOT NULL,
 72     [FK_USERID] [int] NOT NULL,
 73     [FK_ROLEID] [int] NOT NULL,
 74  CONSTRAINT [PK_SYS_USER_ROLE] PRIMARY KEY CLUSTERED 
 75 (
 76     [ID] ASC
 77 )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
 78 ) ON [PRIMARY]
 79 
 80 GO
 81 /****** Object:  Table [dbo].[SYS_USERINFO]    Script Date: 2016/6/20 16:28:44 ******/
 82 SET ANSI_NULLS ON
 83 GO
 84 SET QUOTED_IDENTIFIER ON
 85 GO
 86 CREATE TABLE [dbo].[SYS_USERINFO](
 87     [ID] [int] IDENTITY(1,1) NOT NULL,
 88     [USERID] [int] NOT NULL,
 89     [POSTCODE] [int] NULL,
 90     [PHONE] [nvarchar](200) NULL,
 91     [OFFICEPHONE] [nvarchar](200) NULL,
 92     [EMAILADDRESS] [nvarchar](200) NULL,
 93     [SECONDPHONE] [nvarchar](200) NULL,
 94     [WORKCODE] [int] NULL,
 95     [SEXCODE] [int] NULL,
 96     [BIRTHDAY] [datetime] NULL,
 97     [NATIONCODE] [int] NULL,
 98     [IDNUMBER] [nvarchar](18) NULL,
 99     [MARRYCODE] [int] NULL,
100     [IDENTITYCODE] [int] NULL,
101     [HomeTown] [nvarchar](200) NULL,
102     [ACCOUNTLOCATION] [nvarchar](200) NULL,
103     [XUELI] [int] NULL,
104     [ZHICHENG] [int] NULL,
105     [GRADUATIONSCHOOL] [nvarchar](200) NULL,
106     [SPECIALTY] [nvarchar](200) NULL,
107     [PHOTOOLDNAME] [nvarchar](200) NULL,
108     [PHOTONEWNAME] [nvarchar](200) NULL,
109     [PHOTOTYPE] [nvarchar](200) NULL,
110     [RESUMEOLDNAME] [nvarchar](200) NULL,
111     [RESUMENEWNAME] [nvarchar](200) NULL,
112     [RESUMETYPE] [nvarchar](200) NULL,
113     [HuJiSuoZaiDi] [nvarchar](200) NULL,
114     [HUJIPAICHUSUO] [nvarchar](200) NULL,
115     [WORKDATE] [datetime] NULL,
116     [JINRUDATE] [datetime] NULL,
117     [CARNUMBER] [nvarchar](200) NULL,
118     [QQ] [nvarchar](15) NULL,
119     [WEBCHATOPENID] [nvarchar](200) NULL,
120     [CREATEDATE] [datetime] NULL,
121     [CREATEUSER] [nvarchar](36) NULL,
122     [UPDATEDATE] [datetime] NULL,
123     [UPDATEUSER] [nvarchar](36) NULL,
124  CONSTRAINT [PK_SYS_USERINFO] PRIMARY KEY CLUSTERED 
125 (
126     [ID] ASC
127 )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
128 ) ON [PRIMARY]
129 
130 GO
131 ALTER TABLE [dbo].[SYS_USER_PERMISSION]  WITH CHECK ADD  CONSTRAINT [FK_SYS_USER_PERMISSION_PERID] FOREIGN KEY([FK_PERMISSIONID])
132 REFERENCES [dbo].[SYS_PERMISSION] ([ID])
133 GO
134 ALTER TABLE [dbo].[SYS_USER_PERMISSION] CHECK CONSTRAINT [FK_SYS_USER_PERMISSION_PERID]
135 GO
136 ALTER TABLE [dbo].[SYS_USER_PERMISSION]  WITH CHECK ADD  CONSTRAINT [FK_SYS_USER_PERMISSION_USERID] FOREIGN KEY([FK_USERID])
137 REFERENCES [dbo].[SYS_USER] ([ID])
138 GO
139 ALTER TABLE [dbo].[SYS_USER_PERMISSION] CHECK CONSTRAINT [FK_SYS_USER_PERMISSION_USERID]
140 GO
141 ALTER TABLE [dbo].[SYS_USER_ROLE]  WITH CHECK ADD  CONSTRAINT [FK_SYS_USER_ROLE_ROLEID] FOREIGN KEY([FK_ROLEID])
142 REFERENCES [dbo].[SYS_ROLE] ([ID])
143 GO
144 ALTER TABLE [dbo].[SYS_USER_ROLE] CHECK CONSTRAINT [FK_SYS_USER_ROLE_ROLEID]
145 GO
146 ALTER TABLE [dbo].[SYS_USER_ROLE]  WITH CHECK ADD  CONSTRAINT [FK_SYS_USER_ROLE_USERID] FOREIGN KEY([FK_USERID])
147 REFERENCES [dbo].[SYS_USER] ([ID])
148 GO
149 ALTER TABLE [dbo].[SYS_USER_ROLE] CHECK CONSTRAINT [FK_SYS_USER_ROLE_USERID]
150 GO
151 ALTER TABLE [dbo].[SYS_USERINFO]  WITH CHECK ADD  CONSTRAINT [FK_SYS_USERINFO_SYSUSERID] FOREIGN KEY([USERID])
152 REFERENCES [dbo].[SYS_USER] ([ID])
153 GO
154 ALTER TABLE [dbo].[SYS_USERINFO] CHECK CONSTRAINT [FK_SYS_USERINFO_SYSUSERID]
155 GO
156 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'主键ID' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_POST_USER', @level2type=N'COLUMN',@level2name=N'ID'
157 GO
158 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'用户ID' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_POST_USER', @level2type=N'COLUMN',@level2name=N'FK_USERID'
159 GO
160 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'岗位部门ID' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_POST_USER', @level2type=N'COLUMN',@level2name=N'FK_POSTID'
161 GO
162 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'主键ID' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USER', @level2type=N'COLUMN',@level2name=N'ID'
163 GO
164 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'真实姓名' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USER', @level2type=N'COLUMN',@level2name=N'NAME'
165 GO
166 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'用户帐号' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USER', @level2type=N'COLUMN',@level2name=N'ACCOUNT'
167 GO
168 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'用户密码' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USER', @level2type=N'COLUMN',@level2name=N'PASSWORD'
169 GO
170 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'是否锁定(0否1是)' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USER', @level2type=N'COLUMN',@level2name=N'ISCANLOGIN'
171 GO
172 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'部门内的排序' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USER', @level2type=N'COLUMN',@level2name=N'SHOWORDER1'
173 GO
174 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'公司内的排序' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USER', @level2type=N'COLUMN',@level2name=N'SHOWORDER2'
175 GO
176 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'姓名全拼' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USER', @level2type=N'COLUMN',@level2name=N'PINYIN1'
177 GO
178 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'姓名首字符' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USER', @level2type=N'COLUMN',@level2name=N'PINYIN2'
179 GO
180 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'用户头像' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USER', @level2type=N'COLUMN',@level2name=N'FACE_IMG'
181 GO
182 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'级别(对接SYS_CODE)' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USER', @level2type=N'COLUMN',@level2name=N'LEVELS'
183 GO
184 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'主部门ID,用户所在的部门' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USER', @level2type=N'COLUMN',@level2name=N'DPTID'
185 GO
186 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'创建者' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USER', @level2type=N'COLUMN',@level2name=N'CREATEPER'
187 GO
188 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'创建时间' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USER', @level2type=N'COLUMN',@level2name=N'CREATEDATE'
189 GO
190 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'修改者' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USER', @level2type=N'COLUMN',@level2name=N'UPDATEUSER'
191 GO
192 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'修改时间' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USER', @level2type=N'COLUMN',@level2name=N'UPDATEDATE'
193 GO
194 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'最后一次登录IP' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USER', @level2type=N'COLUMN',@level2name=N'LastLoginIP'
195 GO
196 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'主键ID' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USER_PERMISSION', @level2type=N'COLUMN',@level2name=N'ID'
197 GO
198 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'用户ID' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USER_PERMISSION', @level2type=N'COLUMN',@level2name=N'FK_USERID'
199 GO
200 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'授权ID' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USER_PERMISSION', @level2type=N'COLUMN',@level2name=N'FK_PERMISSIONID'
201 GO
202 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'主键ID' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USER_ROLE', @level2type=N'COLUMN',@level2name=N'ID'
203 GO
204 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'用户ID' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USER_ROLE', @level2type=N'COLUMN',@level2name=N'FK_USERID'
205 GO
206 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'角色ID' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USER_ROLE', @level2type=N'COLUMN',@level2name=N'FK_ROLEID'
207 GO
208 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'主键ID' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USERINFO', @level2type=N'COLUMN',@level2name=N'ID'
209 GO
210 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'用户ID,外键' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USERINFO', @level2type=N'COLUMN',@level2name=N'USERID'
211 GO
212 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'职务,编码' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USERINFO', @level2type=N'COLUMN',@level2name=N'POSTCODE'
213 GO
214 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'手机号码' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USERINFO', @level2type=N'COLUMN',@level2name=N'PHONE'
215 GO
216 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'办公电话' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USERINFO', @level2type=N'COLUMN',@level2name=N'OFFICEPHONE'
217 GO
218 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Email' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USERINFO', @level2type=N'COLUMN',@level2name=N'EMAILADDRESS'
219 GO
220 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'第二手机号码' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USERINFO', @level2type=N'COLUMN',@level2name=N'SECONDPHONE'
221 GO
222 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'在岗状态,编码' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USERINFO', @level2type=N'COLUMN',@level2name=N'WORKCODE'
223 GO
224 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'性别,编码' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USERINFO', @level2type=N'COLUMN',@level2name=N'SEXCODE'
225 GO
226 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'出生日期' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USERINFO', @level2type=N'COLUMN',@level2name=N'BIRTHDAY'
227 GO
228 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'民族,编码' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USERINFO', @level2type=N'COLUMN',@level2name=N'NATIONCODE'
229 GO
230 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'身份证号码' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USERINFO', @level2type=N'COLUMN',@level2name=N'IDNUMBER'
231 GO
232 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'婚姻状况,编码' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USERINFO', @level2type=N'COLUMN',@level2name=N'MARRYCODE'
233 GO
234 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'政治面貌,编码' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USERINFO', @level2type=N'COLUMN',@level2name=N'IDENTITYCODE'
235 GO
236 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'籍贯,编码(关联至TBCode_Area的CodeValue)' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USERINFO', @level2type=N'COLUMN',@level2name=N'HomeTown'
237 GO
238 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'户籍所在地' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USERINFO', @level2type=N'COLUMN',@level2name=N'ACCOUNTLOCATION'
239 GO
240 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'学历,编码' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USERINFO', @level2type=N'COLUMN',@level2name=N'XUELI'
241 GO
242 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'职称,编码' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USERINFO', @level2type=N'COLUMN',@level2name=N'ZHICHENG'
243 GO
244 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'毕业院校' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USERINFO', @level2type=N'COLUMN',@level2name=N'GRADUATIONSCHOOL'
245 GO
246 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'专业' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USERINFO', @level2type=N'COLUMN',@level2name=N'SPECIALTY'
247 GO
248 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'照片原文件名' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USERINFO', @level2type=N'COLUMN',@level2name=N'PHOTOOLDNAME'
249 GO
250 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'照片新文件名' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USERINFO', @level2type=N'COLUMN',@level2name=N'PHOTONEWNAME'
251 GO
252 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'照片格式' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USERINFO', @level2type=N'COLUMN',@level2name=N'PHOTOTYPE'
253 GO
254 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'简历原文件名' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USERINFO', @level2type=N'COLUMN',@level2name=N'RESUMEOLDNAME'
255 GO
256 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'简历新文件名' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USERINFO', @level2type=N'COLUMN',@level2name=N'RESUMENEWNAME'
257 GO
258 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'简历格式' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USERINFO', @level2type=N'COLUMN',@level2name=N'RESUMETYPE'
259 GO
260 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'户籍所在地,编码(关联至TBCode_Area的CodeValue)' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USERINFO', @level2type=N'COLUMN',@level2name=N'HuJiSuoZaiDi'
261 GO
262 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'户籍所在派出所' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USERINFO', @level2type=N'COLUMN',@level2name=N'HUJIPAICHUSUO'
263 GO
264 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'工作时间' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USERINFO', @level2type=N'COLUMN',@level2name=N'WORKDATE'
265 GO
266 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'加入公司时间' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USERINFO', @level2type=N'COLUMN',@level2name=N'JINRUDATE'
267 GO
268 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'车牌号' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USERINFO', @level2type=N'COLUMN',@level2name=N'CARNUMBER'
269 GO
270 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'QQ号' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USERINFO', @level2type=N'COLUMN',@level2name=N'QQ'
271 GO
272 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'微信公众账号对应的OpenID' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USERINFO', @level2type=N'COLUMN',@level2name=N'WEBCHATOPENID'
273 GO
274 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'创建时间' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USERINFO', @level2type=N'COLUMN',@level2name=N'CREATEDATE'
275 GO
276 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'创建者' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USERINFO', @level2type=N'COLUMN',@level2name=N'CREATEUSER'
277 GO
278 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'修改时间' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USERINFO', @level2type=N'COLUMN',@level2name=N'UPDATEDATE'
279 GO
280 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'修改者' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_USERINFO', @level2type=N'COLUMN',@level2name=N'UPDATEUSER'
281 GO
View Code

 

2、新建控制器UserController

【无私分享:从入门到精通ASP.NET MVC】从0开始,一起搭框架、做项目 (10)部门管理、岗位管理和员工管理_第7张图片

3、声明容器

 1 #region 声明容器
 2         /// <summary>
 3         /// 部门
 4         /// </summary>
 5         IDepartmentManage DepartmentManage { get; set; }
 6         /// <summary>
 7         /// 岗位管理
 8         /// </summary>
 9         IPostManage PostManage { get; set; }
10         /// <summary>
11         /// 用户岗位
12         /// </summary>
13         IPostUserManage PostUserManage { get; set; }
14         /// <summary>
15         /// 用户信息
16         /// </summary>
17         IUserInfoManage UserInfoManage { get; set; }
18         /// <summary>
19         /// 字典编码
20         /// </summary>
21         ICodeManage CodeManage { get; set; }
22         /// <summary>
23         /// 角色
24         /// </summary>
25         IRoleManage RoleManage { get; set; }
26         #endregion
View Code

 4、加载首页

 1 /// <summary>
 2         /// 加载首页
 3         /// </summary>
 4         /// <returns></returns>
 5         [UserAuthorizeAttribute(ModuleAlias = "User", OperaAction = "View")]
 6         public ActionResult Index()
 7         {
 8             try
 9             {
10 
11                 #region 处理查询参数
12                 string DepartId = Request.QueryString["DepartId"];
13                 ViewBag.Search = base.keywords;
14                 ViewData["DepartId"] = DepartId;
15                 #endregion
16 
17                 ViewBag.dpt = this.DepartmentManage.GetDepartmentByDetail();
18 
19                 return View(BindList(DepartId));
20             }
21             catch (Exception e)
22             {
23                 WriteLog(Common.Enums.enumOperator.Select, "员工管理加载主页:", e);
24                 throw e.InnerException;
25             }
26 
27         }
View Code
 1 /// <summary>
 2         /// 分页查询用户列表
 3         /// </summary>
 4         private Common.PageInfo BindList(string DepartId)
 5         {
 6             //基础数据
 7             var query = this.UserManage.LoadAll(p => p.ID > 1);
 8 
 9             //部门(本部门用户及所有下级部门用户)
10             if (!string.IsNullOrEmpty(DepartId))
11             {
12                 var childDepart = this.DepartmentManage.LoadAll(p => p.PARENTID == DepartId).Select(p=>p.ID).ToList();
13                 query = query.Where(p => p.DPTID == DepartId || childDepart.Any(e => e == p.DPTID));
14             }
15             
16             //查询关键字
17             if (!string.IsNullOrEmpty(keywords))
18             {
19                 keywords = keywords.ToLower();
20                 query = query.Where(p => p.NAME.Contains(keywords) || p.ACCOUNT.Contains(keywords) || p.PINYIN2.Contains(keywords) || p.PINYIN1.Contains(keywords));
21             }
22             //排序
23             query = query.OrderBy(p=>p.SHOWORDER1).OrderByDescending(p => p.CREATEDATE);
24             //分页
25             var result = this.UserManage.Query(query, page, pagesize);
26 
27             var list = result.List.Select(p => new
28             {
29                 p.ID,
30                 p.NAME,
31                 p.ACCOUNT,
32                 DPTNAME = this.DepartmentManage.GetDepartmentName(p.DPTID),
33                 POSTNAME = GetPostName(p.SYS_POST_USER),
34                 ROLENAME = GetRoleName(p.SYS_USER_ROLE),
35                 p.CREATEDATE,
36                 ZW = this.CodeManage.Get(m => m.CODEVALUE == p.LEVELS && m.CODETYPE == "ZW").NAMETEXT,
37                 ISCANLOGIN = !p.ISCANLOGIN ? "<i class=\"fa fa-circle text-navy\"></i>" : "<i class=\"fa fa-circle text-danger\"></i>"
38 
39             }).ToList();
40 
41             return new Common.PageInfo(result.Index, result.PageSize, result.Count, Common.JsonConverter.JsonClass(list));
42         }
43         /// <summary>
44         /// 根据岗位集合获取岗位名称
45         /// </summary>
46         private string GetPostName(ICollection<Domain.SYS_POST_USER> collection)
47         {
48             string retval = string.Empty;
49             if (collection != null && collection.Count > 0)
50             {
51                 var postlist = String.Join(",", collection.Select(p => p.FK_POSTID).ToList()).Trim(',').Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(p => p).ToList();
52                 retval = String.Join(",", PostManage.LoadAll(p => postlist.Any(e => e == p.ID)).Select(p => p.POSTNAME).ToList());
53             }
54             return retval = retval.TrimEnd(',');
55         }
56         /// <summary>
57         /// 根据角色集合获取角色名称
58         /// </summary>
59         private string GetRoleName(ICollection<Domain.SYS_USER_ROLE> collection)
60         {
61             string retval = string.Empty;
62             if (collection != null && collection.Count > 0)
63             {
64                 var rolelist = String.Join(",", collection.Select(p => p.FK_ROLEID).ToList()).Trim(',').Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(p => int.Parse(p)).ToList();
65                 retval = String.Join(",", RoleManage.LoadAll(p => rolelist.Any(e => e == p.ID)).Select(p => p.ROLENAME).ToList());
66             }
67             return retval = retval.TrimEnd(',');
68         }
View Code

5、加载用户详情信息(基本)

 1 /// <summary>
 2         /// 加载用户详情信息(基本)
 3         /// </summary>
 4         [UserAuthorizeAttribute(ModuleAlias = "User", OperaAction = "Detail")]
 5         public ActionResult Detail(int? id)
 6         {
 7             try
 8             {
 9                 var _entity = new Domain.SYS_USER();
10 
11                 var Postlist="";
12 
13                 if (id != null && id > 0)
14                 {
15                     _entity = UserManage.Get(p => p.ID == id);
16                     Postlist = String.Join(",", _entity.SYS_POST_USER.Select(p => p.FK_POSTID).ToList());
17                 }
18                 ViewBag.dpt = this.DepartmentManage.GetDepartmentByDetail();
19                 ViewBag.zw = this.CodeManage.LoadAll(p => p.CODETYPE == "ZW").ToList();
20                 ViewData["Postlist"] = Postlist;
21                 return View(_entity);
22             }
23             catch (Exception e)
24             {
25                 WriteLog(Common.Enums.enumOperator.Select, "加载用户详情发生错误:", e);
26                 throw e.InnerException;
27             }
28         }
View Code

6、保存人员基本信息

 1  /// <summary>
 2         /// 保存人员基本信息
 3         /// </summary>
 4         [ValidateInput(false)]
 5         [UserAuthorizeAttribute(ModuleAlias = "User", OperaAction = "Add,Edit")]
 6         public ActionResult Save(Domain.SYS_USER entity)
 7         {
 8             bool isEdit = false;
 9             var json = new JsonHelper() { Msg = "保存成功", Status = "n" };
10             try
11             {
12                 if (entity != null)
13                 {
14                     if (entity.ID <= 0) //添加
15                     {
16                         entity.CREATEDATE = DateTime.Now;
17                         entity.CREATEPER = this.CurrentUser.Name;
18                         entity.UPDATEDATE = DateTime.Now;
19                         entity.UPDATEUSER = this.CurrentUser.Name;
20                         entity.PASSWORD = new Common.CryptHelper.AESCrypt().Encrypt("111111");
21                         entity.PINYIN1 = Common.ConvertHzToPz.Convert(entity.NAME).ToLower();
22                         entity.PINYIN2 = Common.ConvertHzToPz.ConvertFirst(entity.NAME).ToLower();
23                     }
24                     else //修改
25                     {
26                         entity.UPDATEUSER = this.CurrentUser.Name;
27                         entity.UPDATEDATE = DateTime.Now;
28                         entity.PINYIN1 = Common.ConvertHzToPz.Convert(entity.NAME).ToLower();
29                         entity.PINYIN2 = Common.ConvertHzToPz.ConvertFirst(entity.NAME).ToLower();
30                         isEdit = true;
31                     }
32                     //检测此用户名是否重复
33                     if (!this.UserManage.IsExist(p => p.ACCOUNT.Equals(entity.ACCOUNT) && p.ID != entity.ID))
34                     {
35                         if (this.UserManage.SaveOrUpdate(entity, isEdit))
36                         {
37                            //员工岗位
38                             var postlist = Request.Form["postlist"];
39                             if(!string.IsNullOrEmpty(postlist))
40                             {
41                                 //删除员工岗位
42                                 if(PostUserManage.IsExist(p=>p.FK_USERID==entity.ID))
43                                 {
44                                     PostUserManage.Delete(p => p.FK_USERID == entity.ID);
45                                 }
46                                 //添加新的员工岗位
47                                 List<Domain.SYS_POST_USER> PostUser = new List<Domain.SYS_POST_USER>();
48                                 foreach(var item in postlist.Trim(',').Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(p => p).ToList())
49                                 {
50                                     PostUser.Add(new Domain.SYS_POST_USER() { FK_POSTID = item, FK_USERID = entity.ID });
51                                 }
52                                 PostUserManage.SaveList(PostUser);
53                             }
54                         }
55                         json.Status = "y";
56                     }
57                     else
58                     {
59                         json.Msg = "登录账号已被使用,请修改后再提交!";
60                     }
61                 }
62                 else
63                 {
64                     json.Msg = "未找到要操作的用户记录";
65                 }
66                 if (isEdit)
67                 {
68                     WriteLog(Common.Enums.enumOperator.Edit, "修改用户,结果:" + json.Msg, Common.Enums.enumLog4net.INFO);
69                 }
70                 else
71                 {
72                     WriteLog(Common.Enums.enumOperator.Add, "添加用户,结果:" + json.Msg, Common.Enums.enumLog4net.INFO);
73                 }
74             }
75             catch (Exception e)
76             {
77                 json.Msg = "保存人员信息发生内部错误!";
78                 WriteLog(Common.Enums.enumOperator.None, "保存用户错误:", e);
79             }
80             return Json(json);
81 
82         }
View Code

7、删除用户

/// 验证规则:1、超级管理员不能删除
///               2、当前登录用户不能删除
///               3、正常状态用户不能删除
///               4、上级部门用户不能删除
/// 删除原则:1、删除用户档案
///        2、删除用户角色关系
///        3、删除用户权限关系
///        4、删除用户岗位关系
///        5、删除用户部门关系
///        6、删除用户

 1 /// <summary>
 2         /// 方法注解:删除用户
 3         /// 验证规则:1、超级管理员不能删除
 4         ///           2、当前登录用户不能删除
 5         ///           3、正常状态用户不能删除
 6         ///           4、上级部门用户不能删除
 7         /// 删除原则:1、删除用户档案
 8         ///           2、删除用户角色关系
 9         ///           3、删除用户权限关系
10         ///           4、删除用户岗位关系
11         ///           5、删除用户部门关系
12         ///           6、删除用户
13         /// </summary>
14         [UserAuthorizeAttribute(ModuleAlias = "User", OperaAction = "Remove")]
15         public ActionResult Delete(string idList)
16         {
17             var json = new JsonHelper() { Status = "n", Msg = "删除用户成功" };
18             try
19             {
20                 //是否为空
21                 if (string.IsNullOrEmpty(idList)) { json.Msg = "未找到要删除的用户"; return Json(json); }
22                 string[] id = idList.Trim(',').Split(',');
23                 for (int i = 0; i < id.Length; i++)
24                 {
25                     int userId = int.Parse(id[i]);                   
26                     if (this.UserManage.IsAdmin(userId))
27                     {
28                         json.Msg = "被删除用户存在超级管理员,不能删除!";
29                         WriteLog(Common.Enums.enumOperator.Remove, "删除用户:" + json.Msg, Common.Enums.enumLog4net.ERROR);
30                         return Json(json);
31                     }
32                     if (this.CurrentUser.Id == userId)
33                     {
34                         json.Msg = "当前登录用户,不能删除!";
35                         WriteLog(Common.Enums.enumOperator.Remove, "删除用户:" + json.Msg, Common.Enums.enumLog4net.ERROR);
36                         return Json(json);
37                     }
38                     if (this.UserManage.Get(p => p.ID == userId).ISCANLOGIN)
39                     {
40                         json.Msg = "用户未锁定,不能删除!";
41                         WriteLog(Common.Enums.enumOperator.Remove, "删除用户:" + json.Msg, Common.Enums.enumLog4net.ERROR);
42                         return Json(json);
43                     }
44                     if (this.CurrentUser.DptInfo!=null)
45                     {
46                         string dptid = this.UserManage.Get(p => p.ID == userId).DPTID;
47                         if (this.DepartmentManage.Get(m => m.ID == dptid).BUSINESSLEVEL < this.CurrentUser.DptInfo.BUSINESSLEVEL)
48                         {
49                             json.Msg = "不能删除上级部门用户!";
50                             WriteLog(Common.Enums.enumOperator.Remove, "删除用户:" + json.Msg, Common.Enums.enumLog4net.ERROR);
51                             return Json(json);
52                         }
53                     }
54                     this.UserManage.Remove(userId);
55                     json.Status = "y";
56                     WriteLog(Common.Enums.enumOperator.Remove, "删除用户:" + json.Msg, Common.Enums.enumLog4net.WARN);
57                 }
58             }
59             catch (Exception e)
60             {
61                 json.Msg = "删除用户发生内部错误!";
62                 WriteLog(Common.Enums.enumOperator.Remove, "删除用户:", e);
63             }
64             return Json(json);
65         }
View Code

这是最基本的用户的操作

7、下面我们先做一下初始化密码:

 1 /// <summary>
 2         /// 方法描述:根据传入的用户编号重置当前用户密码
 3         /// </summary>
 4         /// <param name="Id">用户编号</param>
 5         /// <returns></returns>
 6         [UserAuthorizeAttribute(ModuleAlias = "User", OperaAction = "PwdReset")]
 7         public ActionResult ResetPwd(string idList)
 8         {
 9             var json = new JsonHelper() { Status = "n", Msg = "操作成功" };
10             try
11             {
12                 //校验用户编号是否为空
13                 if (string.IsNullOrEmpty(idList))
14                 {
15                     json.Msg = "校验失败,用户编号不能为空";
16                     WriteLog(Common.Enums.enumOperator.Edit, "重置当前用户密码:" + json.Msg, Common.Enums.enumLog4net.ERROR);
17                     return Json(json);
18                 }
19                 var idlist1 = idList.Trim(',').Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(p => int.Parse(p)).ToList();
20                 if (idlist1 != null && idlist1.Count > 0)
21                 {
22                     foreach (var newid in idlist1)
23                     {
24                         var _user = UserManage.Get(p => p.ID == newid);
25                         _user.PASSWORD = new Common.CryptHelper.AESCrypt().Encrypt("111111");
26                         UserManage.Update(_user);
27                     }
28                 }
29                 json.Status = "y";
30                 WriteLog(Common.Enums.enumOperator.Edit, "重置当前用户密码:" + json.Msg, Common.Enums.enumLog4net.INFO);
31             }
32             catch (Exception e)
33             {
34                 json.Msg = "操作失败";
35                 WriteLog(Common.Enums.enumOperator.Edit, "重置当前用户密码:", e);
36             }
37             return Json(json);
38         }
View Code

8、分配角色(角色控制器 RoleController)

8.1角色分配

 1  /// <summary>
 2         /// 用户角色分配
 3         /// </summary>
 4         /// <param name="id">用户ID</param>
 5         /// <returns></returns>
 6         [UserAuthorizeAttribute(ModuleAlias = "User", OperaAction = "AllocationRole")]
 7         public ActionResult RoleCall(int? id)
 8         {
 9             try
10             {
11                 if(id!=null && id>0)
12                 {
13                     //用户ID
14                     ViewData["userId"] = id;
15                     //获取用户角色信息
16                     var userRoleList = this.UserRoleManage.LoadAll(p => p.FK_USERID == id).Select(p => p.FK_ROLEID).ToList();
17                     return View(Common.JsonConverter.JsonClass(this.RoleManage.LoadAll(p => this.CurrentUser.System_Id.Any(e => e == p.FK_BELONGSYSTEM)).OrderBy(p => p.FK_BELONGSYSTEM).OrderByDescending(p=>p.ID).ToList().Select(p => new { p.ID, p.ROLENAME, ISCUSTOMSTATUS = p.ISCUSTOM ? "<i class=\"fa fa-circle text-navy\"></i>" : "<i class=\"fa fa-circle text-danger\"></i>", SYSNAME = SystemManage.Get(m => m.ID == p.FK_BELONGSYSTEM).NAME,IsChoosed=userRoleList.Contains(p.ID) })));
18                 }
19                 else
20                 {
21                     return View();
22                 }
23                 
24             }
25             catch(Exception e)
26             {
27                 WriteLog(Common.Enums.enumOperator.Select, "获取用户分配的角色:", e);
28                 throw e.InnerException;
29             }
30         }
View Code

8.2设置用户角色

 1 /// <summary>
 2         /// 设置用户角色
 3         /// </summary>
 4         [UserAuthorizeAttribute(ModuleAlias = "Role", OperaAction = "Allocation")]
 5         public ActionResult UserRole()
 6         {
 7             var json = new JsonHelper()
 8             {
 9                 Msg = "设置用户角色成功",
10                 Status = "n"
11             };
12             string userId = Request.Form["UserId"];
13             string roleId = Request.Form["checkbox_name"];
14             if (string.IsNullOrEmpty(userId))
15             {
16                 json.Msg = "未找到要分配角色用户";
17                 return Json(json);
18             }
19             roleId = roleId.TrimEnd(',');
20 
21             try
22             {
23                 //设置用户角色
24                 this.UserRoleManage.SetUserRole(int.Parse(userId), roleId);
25                 json.Status = "y";
26                 WriteLog(Common.Enums.enumOperator.Allocation, "设置用户角色:" + json.Msg, Common.Enums.enumLog4net.INFO);
27             }
28             catch (Exception e)
29             {
30                 json.Msg = "设置失败,错误:" + e.Message;
31                 WriteLog(Common.Enums.enumOperator.Allocation, "设置用户角色:", e);
32             }
33             return Json(json);
34         }
View Code

9、分配权限,当用户分配角色之后就拥有了 相应操作系统的角色的权限,这个分配权限的功能是对于特殊的用户添加角色之外的权限,例如,网站编辑拥有对文章的操作权限,但是有个小编,我想让他自己负责栏目的管理,这时候我们就为这个员工添加单独的栏目的操作权限了。

这个功能我们在做角色管理的时候已经做过了,大家看下权限管理控制器PermissionController  里面的 PerAllocation方法和SaveAllocation ,我们只需要参考角色 分配权限的方法 把tp参数传递为user就OK了

【无私分享:从入门到精通ASP.NET MVC】从0开始,一起搭框架、做项目 (10)部门管理、岗位管理和员工管理_第8张图片

【无私分享:从入门到精通ASP.NET MVC】从0开始,一起搭框架、做项目 (10)部门管理、岗位管理和员工管理_第9张图片

10、人员档案

10.1加载人员档案

 1 /// <summary>
 2         /// 加载人员档案
 3         /// </summary>
 4         /// <param name="id"></param>
 5         /// <returns></returns>
 6         [UserAuthorizeAttribute(ModuleAlias = "User", OperaAction = "UserInfo")]
 7         public ActionResult UserInfo(int? userid)
 8         {
 9             try
10             {
11                 //是否为人事部
12                 var IsMatters = true;
13 
14                 var entity = new Domain.SYS_USERINFO();
15 
16                 var UserName = CurrentUser.Name;
17 
18                 if (userid != null && userid > 0)
19                 {
20                     entity = UserInfoManage.Get(p => p.USERID == userid) ?? new Domain.SYS_USERINFO() { USERID = int.Parse(userid.ToString()) };
21                     UserName = UserManage.Get(p => p.ID == userid).NAME;
22                     if((CurrentUser.DptInfo!=null && CurrentUser.DptInfo.NAME != "人事部") || !CurrentUser.IsAdmin)
23                     {
24                         IsMatters = false;
25                     }
26                 }
27                 else
28                 {
29                     entity = UserInfoManage.Get(p => p.USERID == CurrentUser.Id) ?? new Domain.SYS_USERINFO() { USERID = CurrentUser.Id };
30                 }
31 
32                 ViewData["UserName"] = UserName;
33 
34                 ViewBag.IsMatters = IsMatters;
35 
36                 Dictionary<string, string> dic = Common.Enums.ClsDic.DicCodeType;
37                 var dictype = this.CodeManage.GetDicType();
38                 //在岗状态
39                 string zgzt = dic["在岗状态"];
40                 ViewData["zgzt"] = dictype.Where(p => p.CODETYPE == zgzt).ToList();
41                 //婚姻状况
42                 string hyzk = dic["婚姻状况"];
43                 ViewData["hunyin"] = dictype.Where(p => p.CODETYPE == hyzk).ToList();
44                 //政治面貌
45                 string zzmm = dic["政治面貌"];
46                 ViewData["zzmm"] = dictype.Where(p => p.CODETYPE == zzmm).ToList();
47                 //民族
48                 string mz = dic["民族"];
49                 ViewData["mz"] = dictype.Where(p => p.CODETYPE == mz).ToList();
50                 //职称级别
51                 string zcjb = dic["职称"];
52                 ViewData["zcjb"] = dictype.Where(p => p.CODETYPE == zcjb).ToList();
53                 //学历
54                 string xl = dic["学历"];
55                 ViewData["xl"] = dictype.Where(p => p.CODETYPE == xl).ToList();
56 
57                 return View(entity);
58             }
59             catch (Exception e)
60             {
61                 WriteLog(Common.Enums.enumOperator.Select, "加载人员档案:", e);
62                 throw e.InnerException;
63             }
64 
65         }
View Code

10.2保存人员档案

 1 /// <summary>
 2         /// 保存人员档案
 3         /// </summary>
 4         public ActionResult SetUserInfo(Domain.SYS_USERINFO entity)
 5         {
 6             bool isEdit = false;
 7             var json = new JsonHelper() { Msg = "保存人员档案成功", Status = "n" };
 8             try
 9             {               
10                     if (entity != null)
11                     {
12                         #region 获取html标签值
13 
14                         //籍贯
15                         entity.HomeTown = Request.Form["jgprov"] + "," + Request.Form["jgcity"] + "," +
16                                           Request.Form["jgcountry"];
17                         //户口所在地
18                         entity.HuJiSuoZaiDi = Request.Form["hkprov"] + "," + Request.Form["hkcity"] + "," +
19                                               Request.Form["hkcountry"];
20 
21                         #endregion
22 
23                         //添加
24                         if (entity.ID<=0)
25                         {
26                             entity.CREATEUSER = CurrentUser.Name;
27                             entity.CREATEDATE = DateTime.Now;
28                             entity.UPDATEUSER = CurrentUser.Name;
29                             entity.UPDATEDATE = DateTime.Now;
30                         }
31                         else
32                         {
33                             entity.UPDATEUSER = CurrentUser.Name;
34                             entity.UPDATEDATE = DateTime.Now;
35                             isEdit = true;
36                         }
37 
38 
39 
40                         //修改用户档案
41                         if (this.UserInfoManage.SaveOrUpdate(entity, isEdit))
42                         {
43                             json.Status = "y";
44 
45                         }
46                         else
47                         {
48                             json.Msg = "保存用户档案失败";
49 
50                         }
51 
52                     }
53                     else
54                     {
55                         json.Msg = "未找到要编辑的用户记录";
56                     }
57                     if (isEdit)
58                     {
59                         WriteLog(Common.Enums.enumOperator.Edit, "保存人员档案:" + json.Msg, Common.Enums.enumLog4net.INFO);
60                     }
61                     else
62                     {
63                         WriteLog(Common.Enums.enumOperator.Add, "保存人员档案:" + json.Msg, Common.Enums.enumLog4net.INFO);
64                     }                
65             }
66             catch (Exception e)
67             {                
68                 json.Msg = e.InnerException.Message;
69                 WriteLog(Common.Enums.enumOperator.None, "保存人员档案:", e);
70             }
71             return Json(json);
72         }
View Code

【无私分享:从入门到精通ASP.NET MVC】从0开始,一起搭框架、做项目 (10)部门管理、岗位管理和员工管理_第10张图片

【无私分享:从入门到精通ASP.NET MVC】从0开始,一起搭框架、做项目 (10)部门管理、岗位管理和员工管理_第11张图片

 

 

 

原创文章 转载请尊重劳动成果 http://yuangang.cnblogs.com

你可能感兴趣的:(【无私分享:从入门到精通ASP.NET MVC】从0开始,一起搭框架、做项目 (10)部门管理、岗位管理和员工管理)