Jelajahi Sumber

fix:添加用户全局检索

zqbao 8 bulan lalu
induk
melakukan
928902e38b
3 mengubah file dengan 115 tambahan dan 0 penghapusan
  1. 91 0
      controllers/user.go
  2. 15 0
      models/user.go
  3. 9 0
      routers/commentsRouter.go

+ 91 - 0
controllers/user.go

@@ -1168,3 +1168,94 @@ func (this *UserController) ChangeList() {
 	br.Success = true
 	br.Ret = 200
 }
+
+// GlobalSearch
+// @Title 全局用户列表
+// @Description 全局用户列表
+// @Param   PageSize   query   int  true       "每页数据条数"
+// @Param   CurrentIndex   query   int  true       "当前页页码,从1开始"
+// @Param   KeyWord   query   string  true       "手机号/邮箱/姓名"
+// @Param   SortParam   query   string  true       "排序字段"
+// @Param   SortType   query   string  true       "排序方式"
+// @Success 200 {object} response.UserListResp
+// @router /global/list [get]
+func (this *UserController) GlobalSearch() {
+	br := new(models.BaseResponse).Init()
+	defer func() {
+		this.Data["json"] = br
+		this.ServeJSON()
+	}()
+
+	pageSize, _ := this.GetInt("PageSize")
+	currentIndex, _ := this.GetInt("CurrentIndex")
+	keyWord := this.GetString("KeyWord")
+	sortParam := this.GetString("SortParam")
+	sortType := this.GetString("SortType")
+
+	if pageSize <= 0 {
+		pageSize = utils.PageSize20
+	}
+	if currentIndex <= 0 {
+		currentIndex = 1
+	}
+
+	var condition string
+	var pars []interface{}
+	var sortCondition string
+
+	if keyWord == "" {
+		br.Ret = 200
+		br.Msg = "查询成功"
+		br.Success = true
+		return
+	} else {
+		condition = ` AND (real_name like ? OR phone like ? OR email like ?)`
+		pars = utils.GetLikeKeywordPars(pars, keyWord, 3)
+	}
+
+	if sortParam != "" && sortType != "" {
+		var param, sort string
+		switch sortParam {
+		case "read_cnt":
+			param = "read_cnt"
+		case "last_update_time":
+			param = "last_update_time"
+		}
+		switch sortType {
+		case "asc":
+			sort = "ASC"
+		case "desc":
+			sort = "DESC"
+		}
+		if param != "" && sort != "" {
+			sortCondition = ` ORDER BY ` + param + ` ` + sort
+		}
+	}
+	if sortCondition == "" {
+		sortCondition = ` ORDER BY u.user_Id DESC`
+	}
+
+	startSize := utils.StartIndex(currentIndex, pageSize)
+	userIds, err := models.GetUserIdListByCondition(condition, pars)
+	if err != nil {
+		br.Msg = "查询失败"
+		br.ErrMsg = "查询失败,系统错误,Err:" + err.Error()
+		return
+	}
+
+	userList, err := models.GetGlobalUserByCondition(userIds, sortCondition, startSize, pageSize)
+	if err != nil {
+		br.Msg = "查询失败"
+		br.ErrMsg = "查询失败,系统错误,Err:" + err.Error()
+		return
+	}
+	page := paging.GetPaging(currentIndex, pageSize, len(userIds))
+	resp := new(response.UserListResp)
+	resp.List = userList
+	resp.Paging = page
+
+	br.Data = resp
+	br.Msg = "查询成功"
+	br.Success = true
+	br.Ret = 200
+}

+ 15 - 0
models/user.go

@@ -2,6 +2,7 @@ package models
 
 import (
 	"context"
+	"eta/eta_mini_crm/utils"
 	"fmt"
 	"strings"
 	"time"
@@ -323,3 +324,17 @@ func DeleteUserById(userId int) (err error) {
 	})
 	return
 }
+
+func GetGlobalUserByCondition(userIds []int, sortCondition string, startSize, pageSize int) (items []*UserView, err error) {
+	if len(userIds) == 0 {
+		return
+	}
+	o := orm.NewOrm()
+	sql := `SELECT u.*, COUNT(ur.user_id) AS read_cnt, MAX(ur.create_time) AS last_update_time
+	FROM user AS u
+	LEFT JOIN user_read_record AS ur
+	ON u.user_id = ur.user_id AND ur.user_id IN (` + utils.GetOrmReplaceHolder(len(userIds)) + `)
+	GROUP BY u.user_id ` + sortCondition + ` LIMIT ?,? `
+	_, err = o.Raw(sql, userIds, startSize, pageSize).QueryRows(&items)
+	return
+}

+ 9 - 0
routers/commentsRouter.go

@@ -394,6 +394,15 @@ func init() {
             Filters: nil,
             Params: nil})
 
+    beego.GlobalControllerRouter["eta/eta_mini_crm/controllers:UserController"] = append(beego.GlobalControllerRouter["eta/eta_mini_crm/controllers:UserController"],
+        beego.ControllerComments{
+            Method: "GlobalSearch",
+            Router: `/global/list`,
+            AllowHTTPMethods: []string{"get"},
+            MethodParams: param.Make(),
+            Filters: nil,
+            Params: nil})
+
     beego.GlobalControllerRouter["eta/eta_mini_crm/controllers:UserController"] = append(beego.GlobalControllerRouter["eta/eta_mini_crm/controllers:UserController"],
         beego.ControllerComments{
             Method: "List",