Browse Source

fix: 优化系统用户的连表问题

zqbao 10 months ago
parent
commit
a0a680b45e
4 changed files with 57 additions and 17 deletions
  1. 1 2
      controllers/sys_user.go
  2. 7 0
      models/sys_department.go
  3. 7 15
      models/sys_user.go
  4. 42 0
      services/sys_user.go

+ 1 - 2
controllers/sys_user.go

@@ -369,13 +369,12 @@ func (this *SysUserController) List() {
 		br.ErrMsg = "获取失败,Err:" + err.Error()
 		return
 	}
-	list, err := models.GetSysUserList(condition, pars, startSize, pageSize)
+	list, err := services.GetUserList(condition, pars, startSize, pageSize)
 	if err != nil {
 		br.Msg = "获取失败"
 		br.ErrMsg = "获取失败,Err:" + err.Error()
 		return
 	}
-	fmt.Println(list)
 	sysUserList := make([]models.SysUserView, len(list))
 	for i, user := range list {
 		sysUserList[i].SysUserId = user.SysUserId

+ 7 - 0
models/sys_department.go

@@ -120,6 +120,13 @@ func GetSysDepartmentById(sysDepartmentId int) (item *SysDepartment, err error)
 	return
 }
 
+func GetSysDepartmentList() (items []*SysDepartment, err error) {
+	o := orm.NewOrm()
+	sql := `SELECT * FROM sys_department `
+	_, err = o.Raw(sql).QueryRows(&items)
+	return
+}
+
 func GetSysDepartmentCountById(sysDepartmentId int) (count int, err error) {
 	o := orm.NewOrmUsingDB("master")
 	sql := `SELECT COUNT(1) AS count FROM sys_department WHERE sys_department_id=?`

+ 7 - 15
models/sys_user.go

@@ -57,9 +57,9 @@ type SysUserMapping struct {
 	SysRoleId          int       `description:"角色id"`
 	SysRoleName        string    `description:"角色名称"`
 	SysDepartmentId    int       `description:"所属部门id"`
-	SysDepartmentName1 string    `description:"所属部门一级"`
-	SysDepartmentName2 string    `description:"所属部门二级路径"`
-	SysDepartmentName3 string    `description:"所属部门三级路径"`
+	SysDepartmentName1 string    `description:"所属部门一级名称"`
+	SysDepartmentName2 string    `description:"所属部门二级名称"`
+	SysDepartmentName3 string    `description:"所属部门三级名称"`
 	Province           string    `description:"省"`
 	City               string    `description:"市"`
 	IsEnabled          bool      `description:"是否启用"`
@@ -140,21 +140,13 @@ func GetSysUserCountByRoleId(roleId int) (count int, err error) {
 	return
 }
 
-func GetSysUserList(condition string, pars []interface{}, startSize, pageSize int) (items []*SysUserMapping, err error) {
-	sql := `SELECT u.*,d1.sys_department_name sys_department_name1, d2.sys_department_name sys_department_name2, d3.sys_department_name sys_department_name3
-	FROM sys_user u
-	LEFT JOIN sys_department AS d1
-	ON u.sys_department_id1 = d1.sys_department_id
-	LEFT JOIN sys_department AS d2
-	ON u.sys_department_id2 = d2.sys_department_id
-	LEFT JOIN sys_department AS d3
-	ON u.sys_department_id3 = d3.sys_department_id
-	WHERE 1=1 `
+func GetSysUserList(condition string, pars []interface{}, startSize, pageSize int) (items []*SysUser, err error) {
+	sql := `SELECT * FROM sys_user u WHERE 1=1 `
 	if condition != "" {
 		sql += condition
 	}
-	sql += `ORDER BY u.is_enabled, u.modify_time DESC LIMIT ?,?`
-	o := orm.NewOrmUsingDB("master")
+	sql += ` ORDER BY u.is_enabled, u.modify_time DESC LIMIT ?,?`
+	o := orm.NewOrm()
 	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
 	return
 }

+ 42 - 0
services/sys_user.go

@@ -0,0 +1,42 @@
+package services
+
+import "eta/eta_mini_crm/models"
+
+func GetUserList(condition string, pars []interface{}, startSize, pageSize int) (items []*models.SysUserMapping, err error) {
+	sysUserList, err := models.GetSysUserList(condition, pars, startSize, pageSize)
+	if err != nil {
+		return
+	}
+	departmentList, err := models.GetSysDepartmentList()
+	if err != nil {
+		return
+	}
+	departmentIdToName := make(map[int]string)
+	for _, dep := range departmentList {
+		departmentIdToName[dep.SysDepartmentId] = dep.SysDepartmentName
+	}
+	items = make([]*models.SysUserMapping, 0)
+	for _, user := range sysUserList {
+		items = append(items,
+			&models.SysUserMapping{
+				SysUserId:          user.SysUserId,
+				SysUserName:        user.SysUserName,
+				SysRealName:        user.SysRealName,
+				Email:              user.Email,
+				Phone:              user.Phone,
+				AreaCode:           user.AreaCode,
+				SysRoleId:          user.SysRoleId,
+				SysRoleName:        user.SysRealName,
+				SysDepartmentId:    user.SysDepartmentId,
+				SysDepartmentName1: departmentIdToName[user.SysDepartmentId1],
+				SysDepartmentName2: departmentIdToName[user.SysDepartmentId2],
+				SysDepartmentName3: departmentIdToName[user.SysDepartmentId3],
+				Province:           user.Province,
+				City:               user.City,
+				IsEnabled:          user.IsEnabled,
+				CreateTime:         user.CreateTime,
+				ModifyTime:         user.ModifyTime,
+			})
+	}
+	return
+}