Browse Source

fix:修复阅读统计排序

zqbao 10 months ago
parent
commit
accd136389
2 changed files with 57 additions and 11 deletions
  1. 46 6
      controllers/user_read_record.go
  2. 11 5
      models/user.go

+ 46 - 6
controllers/user_read_record.go

@@ -23,7 +23,7 @@ type UserReadRecordController struct {
 // @Param   PageSize   query   int  true       "每页数据条数"
 // @Param   CurrentIndex   query   int  true       "当前页页码,从1开始"
 // @Param   SellerId   query   int  true       "销售id"
-// @Param   Status   query   int  true       "用户状态"
+// @Param   Status   query   string  true       "用户状态"
 // @Param   KeyWord   query   string  true       "手机号/邮箱/姓名"
 // @Param   IsRegistered   query   string  true       "是否注册"
 // @Param   IsSubscribed   query   string  true       "是否关注"
@@ -31,6 +31,8 @@ type UserReadRecordController struct {
 // @Param   RegisterEndDate   query   string  true       "注册结束时间"
 // @Param   CreateStartDate   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 *UserReadRecordController) List() {
@@ -42,7 +44,7 @@ func (this *UserReadRecordController) List() {
 
 	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")
@@ -51,7 +53,11 @@ func (this *UserReadRecordController) List() {
 	registerEndDate := this.GetString("RegisterEndDate")
 	createStartDate := this.GetString("CreateStartDate")
 	createEndDate := this.GetString("CreateEndDate")
+	sortParma := this.GetString("SortParam")
+	sortType := this.GetString("SortType")
+
 	var condition string
+	var sortCondition string
 	var pars []interface{}
 
 	if keyWord != "" {
@@ -68,10 +74,44 @@ func (this *UserReadRecordController) List() {
 		currentIndex = 1
 	}
 
-	if sellerId > 0 {
-		condition += " AND seller_id=? "
-		pars = append(pars, sellerId)
+	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 += `) `
+		}
 	}
+	if sortParma != "" && sortType != "" {
+		sortCondition = " ORDER BY "
+		var param, sort string
+		switch sortParma {
+		case "LastUpdateTime":
+			param = "last_update_time"
+		case "ReadCnt":
+			param = "read_cnt"
+		}
+		switch sortType {
+		case "asc":
+			sort = " ASC "
+		case "desc":
+			sort = " DESC "
+		}
+		if param != "" && sort != "" {
+			sortCondition += param + " " + sort
+		} else {
+			sortCondition = ""
+		}
+	}
+
 	switch status {
 	case "禁用":
 		condition += " AND u.status=? "
@@ -154,7 +194,7 @@ func (this *UserReadRecordController) List() {
 		br.ErrMsg = "获取失败,Err:" + err.Error()
 		return
 	}
-	userList, err := models.GetUserReadList(condition, pars, startSize, pageSize)
+	userList, err := models.GetUserReadList(condition, sortCondition, pars, startSize, pageSize)
 	if err != nil {
 		br.Msg = "查询用户失败"
 		br.Msg = "查询用户失败,系统错误,Err:" + err.Error()

+ 11 - 5
models/user.go

@@ -20,7 +20,7 @@ type User struct {
 	Status         int       `description:"用户类型: 0表示禁用,1表示潜在客户,2表示正式客户"`
 	CreateTime     time.Time `description:"系统中首次新增用户的时间"`
 	ModifyTime     time.Time `description:"系统中用户信息变更的时间"`
-	LastUpdateTime time.Time `description:"最近一次登录时间"`
+	LastUpdateTime time.Time `description:"最近一次阅读时间"`
 	RegisterTime   time.Time `description:"用户首次登录小程序的时间"`
 	IsSubscribed   bool      `description:"是否关注公众号: 0表示没有关注,1表示关注"`
 	IsRegistered   bool      `description:"是否注册: 0表示没有注册,1表示注册"`
@@ -42,7 +42,7 @@ type UserView struct {
 	ReadCnt        int    `description:"用户阅读量"`
 	CreateTime     string `description:"系统中首次新增用户的时间"`
 	ModifyTime     string `description:"系统中用户信息变更的时间"`
-	LastUpdateTime string `description:"最近一次登录时间"`
+	LastUpdateTime string `description:"最近一次阅读时间"`
 	RegisterTime   string `description:"用户首次登录小程序的时间"`
 	IsSubscribed   bool   `description:"是否关注公众号: 0表示没有关注,1表示关注"`
 	IsRegistered   bool   `description:"是否注册: 0表示没有注册,1表示注册"`
@@ -156,8 +156,8 @@ func GetUserListByConditonSort(condition, sortConditon string, pars []interface{
 	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 
+func GetUserReadList(condition, sortCondition 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, Max(ur.create_time) AS last_update_time
 	FROM user AS u
 	LEFT JOIN sys_user AS su
 	ON u.seller_id = su.sys_user_id
@@ -167,7 +167,13 @@ func GetUserReadList(condition string, pars []interface{}, startSize, pageSize i
 	if condition != "" {
 		sql += condition
 	}
-	sql += ` GROUP BY u.user_id ORDER BY read_cnt DESC LIMIT ?,? `
+	sql += ` GROUP BY u.user_id `
+	if sortCondition != "" {
+		sql += sortCondition
+	} else {
+		sql += ` ORDER BY read_cnt DESC `
+	}
+	sql += ` LIMIT ?,? `
 	o := orm.NewOrm()
 	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
 	return