Bläddra i källkod

fix:客服列表sql优化

duxin 2 år sedan
förälder
incheckning
2b2278a852
1 ändrade filer med 33 tillägg och 23 borttagningar
  1. 33 23
      service/message_mail_box.go

+ 33 - 23
service/message_mail_box.go

@@ -88,6 +88,7 @@ func (b MessaggeService) Count(newUserId, userType, entUserId int64, isClean boo
 // 用户列表查询
 func (b MessaggeService) UserList(in *messagecenter.UserReq) (data *[]map[string]interface{}, count int64, err error) {
 	sqlStr := ""
+	tm := time.Now()
 	if in.UserType == 2 {
 		//用户最后一次信息查询
 		userSql := fmt.Sprintf("SELECT  MAX( c.id )   as messageId  FROM  socialize_message_mailbox c "+
@@ -164,6 +165,9 @@ func (b MessaggeService) UserList(in *messagecenter.UserReq) (data *[]map[string
 		if in.Page <= 0 {
 			in.Page = 1
 		}
+		if in.Size <= 0 || in.Size > 100 {
+			in.Size = 50
+		}
 		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  "+
@@ -175,35 +179,41 @@ func (b MessaggeService) UserList(in *messagecenter.UserReq) (data *[]map[string
 			util.SOCIALIZE_MESSAGE_MAILBOX, util.SOCIALIZE_CHAT_SESSION, util.BASE_USER,
 			in.EntUserId, startTimeSql, endTimeSql, phoneSql)
 		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)
+		countData := util.Mysql.SelectBySql(fmt.Sprintf("SELECT * FROM (%s) as data  sort by messageId desc ", userSql))
+		if countData != nil && len(*countData) > 0 {
+			log.Printf("查询耗时1:%d;查询数量:%d", time.Since(tm), count)
+			count = quitl.Int64All(len(*countData))
+			var d []map[string]interface{}
+			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]
 			}
+			customerMessageId := util.Inhandle(&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("查询列表sql:", sqlStr)
 		data = util.Mysql.SelectBySql(sqlStr)
+		log.Println("查询耗时2:", time.Since(tm))
 		if in.UserType == 2 && data != nil && len(*data) > 0 {
 			count = quitl.Int64All(len(*data))
 		}