浏览代码

fix:客服列表sql优化

duxin 2 年之前
父节点
当前提交
90cb47c48b
共有 2 个文件被更改,包括 28 次插入49 次删除
  1. 0 15
      entity/util.go
  2. 28 34
      service/message_mail_box.go

+ 0 - 15
entity/util.go

@@ -45,21 +45,6 @@ func Inhandle(data *[]map[string]interface{}) (messId string) {
 	return
 }
 
-// in数据处理
-func InFmt(data []map[string]interface{}) (messId string) {
-	if len(data) == 0 {
-		messId = "''"
-		return
-	}
-	for k, m := range data {
-		if k > 0 {
-			messId += `,`
-		}
-		messId += "'" + quitl.InterfaceToStr(m["messageId"]) + "'"
-	}
-	return
-}
-
 type SubPush struct {
 	Data  map[string]interface{} `json:"data"`
 	Count int                    `json:"count"`

+ 28 - 34
service/message_mail_box.go

@@ -161,6 +161,9 @@ func (b MessaggeService) UserList(in *messagecenter.UserReq) (data *[]map[string
 		if in.EndTime != "" {
 			endTimeSql = "AND DATE_FORMAT(c.create_time,'%Y-%m-%d') <= '" + in.EndTime + "' "
 		}
+		if in.Page <= 0 {
+			in.Page = 1
+		}
 		userSql := fmt.Sprintf("SELECT   MAX( c.id ) as messageId   FROM  %s c   "+
 			"LEFT JOIN %s d ON   c.own_type=1 AND  c.own_id=d.id   "+
 			"LEFT JOIN %s b ON if (c.send_user_type=1 ,c.receive_user_id,c.send_user_id)=b.id  "+
@@ -171,44 +174,35 @@ func (b MessaggeService) UserList(in *messagecenter.UserReq) (data *[]map[string
 			"GROUP BY   (   CASE  WHEN  c.send_user_type =2 THEN  CONCAT( c.send_user_id )   WHEN c.send_user_type =1 THEN  CONCAT( c.receive_user_id )  END  ) ",
 			util.SOCIALIZE_MESSAGE_MAILBOX, util.SOCIALIZE_CHAT_SESSION, util.BASE_USER,
 			in.EntUserId, startTimeSql, endTimeSql, phoneSql)
-		countData := util.Mysql.SelectBySql(userSql)
-		var d []map[string]interface{}
-		if countData != nil && len(*countData) > 0 {
-			util.SortData(countData, "messageId", true)
-			count = quitl.Int64All(len(*countData))
-			if in.Page <= 0 {
-				in.Page = 1
-			}
-			if in.Page*in.Size >= count {
-				d = (*countData)[(in.Page-1)*in.Size:]
-			} else {
-				d = (*countData)[(in.Page-1)*in.Size : in.Page*in.Size]
+		log.Println("查询最新消息数量sql:", userSql)
+		count = util.Mysql.CountBySql(userSql)
+		if count > 0 {
+			countData := util.Mysql.SelectBySql(fmt.Sprintf("SELECT * FROM (%s) as data  order by messageId desc LIMIT %d,%d", userSql, in.Page-1, in.Size))
+			if countData != nil && len(*countData) > 0 {
+				customerMessageId := util.Inhandle(countData)
+				//客服的用户列表
+				sqlStr = fmt.Sprintf("SELECT  "+
+					"(  CASE        WHEN SUBSTR( b.nickname, 1, 3 ) = 'JY_' THEN    CONCAT( SUBSTR( b.phone, 1, 3 ), '****', SUBSTR( b.phone, 8, 11 ) )     WHEN b.nickname = '' THEN    CONCAT( SUBSTR( b.phone, 1, 3 ), '****', SUBSTR( b.phone, 8, 11 ) ) ELSE b.nickname    END    )"+
+					" AS name,  b.id,  e.title,  b.headimg,  e.type,  e.link,  e.content,  a.create_time,  "+
+					"(  SELECT   count( h.id )   FROM   %s h   "+
+					"LEFT JOIN %s i ON h.own_type = 1 AND h.own_id = i.id     "+
+					"WHERE  h.own_type = 1  "+
+					"AND i.ent_id=f.ent_id "+
+					"AND  i.user_id=f.user_id    "+
+					"AND h.isread = 0    "+
+					"AND  i.customer_service_id= %d) AS number "+
+					"FROM  %s a  "+
+					"LEFT JOIN %s b ON if (a.send_user_type=1 ,a.receive_user_id,a.send_user_id)=b.id  "+
+					"LEFT JOIN %s e ON e.id = a.messag_id  "+
+					"LEFT JOIN %s f ON a.own_type=1 AND  a.own_id=f.id   "+
+					"WHERE  a.id IN  ( %s) %s ORDER BY  a.create_time DESC",
+					util.SOCIALIZE_MESSAGE_MAILBOX, util.SOCIALIZE_CHAT_SESSION,
+					in.EntUserId, util.SOCIALIZE_MESSAGE_MAILBOX, util.BASE_USER, util.SOCIALIZE_MESSAGE, util.SOCIALIZE_CHAT_SESSION, customerMessageId, phoneSql)
 			}
 		}
-		if len(d) > 0 {
-			customerMessageId := util.InFmt(d)
-			//客服的用户列表
-			sqlStr = fmt.Sprintf("SELECT  "+
-				"(  CASE        WHEN SUBSTR( b.nickname, 1, 3 ) = 'JY_' THEN    CONCAT( SUBSTR( b.phone, 1, 3 ), '****', SUBSTR( b.phone, 8, 11 ) )     WHEN b.nickname = '' THEN    CONCAT( SUBSTR( b.phone, 1, 3 ), '****', SUBSTR( b.phone, 8, 11 ) ) ELSE b.nickname    END    )"+
-				" AS name,  b.id,  e.title,  b.headimg,  e.type,  e.link,  e.content,  a.create_time,  "+
-				"(  SELECT   count( h.id )   FROM   %s h   "+
-				"LEFT JOIN %s i ON h.own_type = 1 AND h.own_id = i.id     "+
-				"WHERE  h.own_type = 1  "+
-				"AND i.ent_id=f.ent_id "+
-				"AND  i.user_id=f.user_id    "+
-				"AND h.isread = 0    "+
-				"AND  i.customer_service_id= %d) AS number "+
-				"FROM  %s a  "+
-				"LEFT JOIN %s b ON if (a.send_user_type=1 ,a.receive_user_id,a.send_user_id)=b.id  "+
-				"LEFT JOIN %s e ON e.id = a.messag_id  "+
-				"LEFT JOIN %s f ON a.own_type=1 AND  a.own_id=f.id   "+
-				"WHERE  a.id IN  ( %s) %s ORDER BY  a.create_time DESC",
-				util.SOCIALIZE_MESSAGE_MAILBOX, util.SOCIALIZE_CHAT_SESSION,
-				in.EntUserId, util.SOCIALIZE_MESSAGE_MAILBOX, util.BASE_USER, util.SOCIALIZE_MESSAGE, util.SOCIALIZE_CHAT_SESSION, customerMessageId, phoneSql)
-		}
 	}
 	if sqlStr != "" {
-		log.Println(sqlStr)
+		log.Println("查询列表sql:", sqlStr)
 		data = util.Mysql.SelectBySql(sqlStr)
 		if in.UserType == 2 && data != nil && len(*data) > 0 {
 			count = quitl.Int64All(len(*data))