Explorar o código

fix:完善用户列表的排序

zqbao hai 10 meses
pai
achega
5325aee829
Modificáronse 2 ficheiros con 65 adicións e 9 borrados
  1. 47 9
      controllers/user.go
  2. 18 0
      models/user.go

+ 47 - 9
controllers/user.go

@@ -434,7 +434,7 @@ func (this *UserController) Check() {
 // @Description 用户列表
 // @Param   PageSize   query   int  true       "每页数据条数"
 // @Param   CurrentIndex   query   int  true       "当前页页码,从1开始"
-// @Param   SellerId   query   int  true       "销售id"
+// @Param   SellerId   query   string  true       "销售id"
 // @Param   Status   query   int  true       "用户状态"
 // @Param   KeyWord   query   string  true       "手机号/邮箱/姓名"
 // @Param   IsRegistered   query   string  true       "是否注册"
@@ -443,8 +443,8 @@ func (this *UserController) Check() {
 // @Param   RegisterEndDate   query   string  true       "注册结束时间"
 // @Param   CreateStartDate   query   string  true       "创建开始时间"
 // @Param   CreateEndDate   query   string  true       "创建结束时间"
-// @Param   Last   query   string  true       "创建结束时间"
-// @Param   CreateEndDate   query   string  true       "创建结束时间"
+// @Param   SortParam   query   string  true       "排序字段"
+// @Param   SortType   query   string  true       "排序方式"
 // @Success 200 {object} response.UserListResp
 // @router /list [get]
 func (this *UserController) List() {
@@ -453,10 +453,9 @@ func (this *UserController) List() {
 		this.Data["json"] = br
 		this.ServeJSON()
 	}()
-
 	pageSize, _ := this.GetInt("PageSize")
 	currentIndex, _ := this.GetInt("CurrentIndex")
-	sellerId, _ := this.GetInt("SellerId")
+	sellerIdStr := this.GetString("SellerId")
 	status := this.GetString("Status")
 	keyWord := this.GetString("KeyWord")
 	IsRegistered := this.GetString("IsRegisterd")
@@ -465,8 +464,11 @@ func (this *UserController) List() {
 	registerEndDate := this.GetString("RegisterEndDate")
 	createStartDate := this.GetString("CreateStartDate")
 	createEndDate := this.GetString("CreateEndDate")
+	sortParma := this.GetString("SortParam")
+	sortType := this.GetString("SortTye")
 
 	var condition string
+	var sortCondition string
 	var pars []interface{}
 
 	if keyWord != "" {
@@ -483,9 +485,45 @@ func (this *UserController) List() {
 		currentIndex = 1
 	}
 
-	if sellerId > 0 {
-		condition += " AND u.seller_id=? "
-		pars = append(pars, sellerId)
+	if sortParma != "" && sortType != "" {
+		sortCondition = " ORDER BY "
+		var param, sort string
+		switch sortParma {
+		case "RegisterTime":
+			param = "u.register_time"
+		case "CreateTime":
+			param = "u.create_time"
+		case "RestDate":
+			param = "u.rest_date"
+		}
+		switch sortType {
+		case "asc":
+			sort = " ASC "
+		case "desc":
+			sort = " DESC "
+		}
+		if param != "" && sort != "" {
+			sortCondition += param + " " + sort
+		} else {
+			sortCondition = ""
+		}
+	}
+
+	if sellerIdStr != "" {
+		sellerIds := strings.Split(sellerIdStr, ",")
+		if len(sellerIds) != 0 {
+			condition += ` AND ( `
+			for i, id := range sellerIds {
+				if i == 0 {
+					condition += ` u.seller_id = ? `
+					pars = append(pars, id)
+				} else {
+					condition += ` OR u.seller_id = ? `
+					pars = append(pars, id)
+				}
+			}
+			condition += `) `
+		}
 	}
 	switch status {
 	case "禁用":
@@ -569,7 +607,7 @@ func (this *UserController) List() {
 		br.ErrMsg = "获取失败,Err:" + err.Error()
 		return
 	}
-	userList, err := models.GetUserList(condition, pars, startSize, pageSize)
+	userList, err := models.GetUserListByConditonSort(condition, sortCondition, pars, startSize, pageSize)
 	if err != nil {
 		br.Msg = "查询用户失败"
 		br.Msg = "查询用户失败,系统错误,Err:" + err.Error()

+ 18 - 0
models/user.go

@@ -37,6 +37,7 @@ type UserView struct {
 	Company        string `description:"所属公司"`
 	ValidStartTime string `description:"有效期开始时间"`
 	ValidEndTime   string `description:"有效期结束时间"`
+	RestDate       int    `description:"剩余天数"`
 	Status         int    `description:"用户类型: 0表示禁用,1表示潜在客户,2表示正式客户"`
 	ReadCnt        int    `description:"用户阅读量"`
 	CreateTime     string `description:"系统中首次新增用户的时间"`
@@ -137,6 +138,23 @@ func GetUserList(condition string, pars []interface{}, startSize, pageSize int)
 	return
 }
 
+func GetUserListByConditonSort(condition, sortConditon string, pars []interface{}, startSize, pageSize int) (items []*UserView, err error) {
+	sql := `SELECT u.*, su.sys_real_name AS seller_name FROM user AS u
+	LEFT JOIN sys_user AS su
+	ON u.seller_id = su.sys_user_id
+	WHERE 1=1 AND (u.phone IS NOT NULL OR u.email IS NOT NULL) `
+	if condition != "" {
+		sql += condition
+	}
+	if sortConditon != "" {
+		sql += sortConditon
+	}
+	sql += ` LIMIT ?,? `
+	o := orm.NewOrm()
+	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
+	return
+}
+
 func GetUserReadList(condition string, pars []interface{}, startSize, pageSize int) (items []*UserView, err error) {
 	sql := `SELECT u.*, su.sys_real_name AS seller_name, COUNT(ur.user_id) AS read_cnt 
 	FROM user AS u