Ver Fonte

查询优化

WH01243 há 2 anos atrás
pai
commit
f85052d791
1 ficheiros alterados com 19 adições e 31 exclusões
  1. 19 31
      jyBXSubscribe/rpc/model/push.go

+ 19 - 31
jyBXSubscribe/rpc/model/push.go

@@ -325,7 +325,7 @@ func (s *subscribePush) Datas(spqp *SubPushQueryParam) (hasNextPage bool, total
 				Datas: list,
 				Count: countSearch,
 			}
-			if spqp.IsEnt == false {
+			if spqp.IsEnt == false && spqp.NotReturnCount == 0 {
 				s.PutTodayCache(spqp.UserId, spqp.UserType, subPush)
 			}
 		}
@@ -389,11 +389,11 @@ func (s *subscribePush) Datas(spqp *SubPushQueryParam) (hasNextPage bool, total
 
 func (s *subscribePush) getDatasFromMysql(spqp *SubPushQueryParam, starttime, endtime int64, size int, isLimit bool) (result []*bxsubscribe.SubscribeInfo, count int64) {
 	querys := []string{}
-	userStr := " "
+	userStr := " 1=1  "
 	var (
 		countSql, findSql string
 	)
-	querys = append(querys, " 1=1 ")
+	querys = append(querys, " and   1=1 ")
 	if spqp.SelectInfoIds != nil && len(spqp.SelectInfoIds) > 0 {
 		countSql = fmt.Sprintf("select count(1) as count from %s a  where %s", aboutDbMsg[s.ModuleFlag].MysqlTable, fmt.Sprintf("a.id in ('%s')", strings.Join(spqp.SelectInfoIds, "','")))
 		logx.Info("countSql", countSql)
@@ -404,11 +404,11 @@ func (s *subscribePush) getDatasFromMysql(spqp *SubPushQueryParam, starttime, en
 		//公共查询条件
 		//时间
 		if starttime > 0 && endtime > 0 {
-			userStr += fmt.Sprintf(" and a.date>=%d and a.date<=%d", starttime, endtime)
+			querys = append(querys, fmt.Sprintf("  a.date>=%d and a.date<=%d", starttime, endtime))
 		} else if starttime > 0 && endtime == 0 {
-			userStr += fmt.Sprintf(" and a.date>=%d", starttime)
+			querys = append(querys, fmt.Sprintf(" a.date>=%d", starttime))
 		} else if starttime == 0 && endtime > 0 {
-			userStr += fmt.Sprintf(" and a.date<=%d", endtime)
+			querys = append(querys, fmt.Sprintf(" a.date<=%d", endtime))
 		}
 		if spqp.Area != "" || spqp.City != "" {
 			var sqlAreaCity = ""
@@ -542,18 +542,18 @@ func (s *subscribePush) getDatasFromMysql(spqp *SubPushQueryParam, starttime, en
 		// 是否已读
 		if spqp.IsRead != "" {
 			if spqp.IsRead == "0" {
-				userStr += " and a.isvisit IS NULL"
+				querys = append(querys, " a.isvisit IS NULL")
 			} else if spqp.IsRead == "1" {
-				userStr += " and a.isvisit=1"
+				querys = append(querys, " and a.isvisit=1")
 			}
 		}
 		// 信息来源
 		if spqp.Source == "1" || spqp.Source == "2" || spqp.Source == "3" {
-			userStr += " and a.source=" + spqp.Source
+			querys = append(querys, "  a.source="+spqp.Source)
 		}
 		if spqp.IsEnt {
 			var staffs []string
-			userStr += fmt.Sprintf(" and  a.entid='%s' ", spqp.EntId)
+			querys = append(querys, fmt.Sprintf("   a.entid='%s' ", spqp.EntId))
 			//判断是企业管理员还是部门管理员 部门管理员获取所有子部门
 			userEnt := EntInfo(common.IntAll(spqp.EntId), common.IntAll(spqp.EntUserId))
 			if !(userEnt.Role_admin_system || userEnt.Role_admin_department) {
@@ -586,11 +586,10 @@ func (s *subscribePush) getDatasFromMysql(spqp *SubPushQueryParam, starttime, en
 			// 无查询分配人员、是否已读
 			if spqp.IsRead == "" && len(staffs) == 0 {
 				//查询数量(需要去重)
-				//countSql = fmt.Sprintf("select count(1) from %s a    where %s %s %s", aboutDbMsg[s.ModuleFlag].MysqlTable, strings.Join(querys, " and "), userStr, common.If(isNew, " and a.source = 2 ", ""))
-				countSql = fmt.Sprintf("select count(1) from %s a  where %s %s ", aboutDbMsg[s.ModuleFlag].MysqlTable, strings.Join(querys, " and "), userStr)
+				countSql = fmt.Sprintf("select count(1) from %s a  where %s  %s ", aboutDbMsg[s.ModuleFlag].MysqlTable, userStr, strings.Join(querys, " and "))
 				//列表查询
 				findSql = fmt.Sprintf("select a.id,a.infoid,a.matchkeys,a.attachment_count, IF(a.source is NULL,1,a.source) as  source  from %s a  where  %s %s order by a.date desc,a.id desc",
-					aboutDbMsg[s.ModuleFlag].MysqlTable, strings.Join(querys, " and "), userStr)
+					aboutDbMsg[s.ModuleFlag].MysqlTable, userStr, strings.Join(querys, " and "))
 			} else {
 				//查询分配人员或是否已读
 				staffQuery := " 1=1 "
@@ -599,33 +598,22 @@ func (s *subscribePush) getDatasFromMysql(spqp *SubPushQueryParam, starttime, en
 				} else if len(staffs) > 0 {
 					staffQuery += fmt.Sprintf(" and  a.userid in (0,'%s')  ", strings.Join(staffs, "','"))
 				}
-				/*if spqp.IsRead != "" {
-					if spqp.IsRead == "0" {
-						staffQuery += " and a.isvisit IS NULL"
-					} else if spqp.IsRead == "1" {
-						staffQuery += " and a.isvisit=1"
-					}
-				}*/
-				//countSql = fmt.Sprintf("select count(1) from %s a  where %s %s and ((%s) or EXISTS (SELECT 1 from %s c  WHERE c.source=3 %s and %s and a.id=c.pid and (%s))) ", aboutDbMsg[s.ModuleFlag].MysqlTable, strings.Join(querys, " and "), userStr, staffQuery, aboutDbMsg[s.ModuleFlag].MysqlTable, strings.ReplaceAll(userStr, "a.", "c."), strings.Join(querys, " and "), strings.ReplaceAll(staffQuery, "a.", "c."))
-				countSql = fmt.Sprintf("select count(1) from %s a  where %s %s and %s ", aboutDbMsg[s.ModuleFlag].MysqlTable, strings.Join(querys, " and "), userStr, staffQuery)
-
+				countSql = fmt.Sprintf("select count(1) from %s a  where %s %s %s ", aboutDbMsg[s.ModuleFlag].MysqlTable, userStr, strings.Join(querys, " and "), staffQuery)
 				//列表查询
-				//findSql = fmt.Sprintf("select a.id,a.infoid,a.matchkeys,a.attachment_count from %s a where %s %s %s and ((%s) or EXISTS (SELECT 1 from %s c  WHERE c.source=3 %s and %s and a.id=c.pid and (%s))) order by a.date desc,a.id desc ", aboutDbMsg[s.ModuleFlag].MysqlTable, strings.Join(querys, " and "), userStr, common.If(isNew, " and a.source = 2 ", ""), staffQuery, aboutDbMsg[s.ModuleFlag].MysqlTable, strings.ReplaceAll(userStr, "a.", "c."), strings.Join(querys, " and "), strings.ReplaceAll(staffQuery, "a.", "c."))
-				findSql = fmt.Sprintf("select a.id,a.infoid,a.matchkeys,a.attachment_count,IF(a.source is NULL,1,a.source) as  source from %s a  where %s %s and %s order by a.date desc,a.id desc ", aboutDbMsg[s.ModuleFlag].MysqlTable, strings.Join(querys, " and "), userStr, staffQuery)
-
+				findSql = fmt.Sprintf("select a.id,a.infoid,a.matchkeys,a.attachment_count,IF(a.source is NULL,1,a.source) as  source from %s a  where %s %s  %s order by a.date desc,a.id desc ", aboutDbMsg[s.ModuleFlag].MysqlTable, userStr, strings.Join(querys, " and "), staffQuery)
 			}
 		} else {
 			// 是否已读
 			if spqp.IsRead != "" {
 				if spqp.IsRead == "0" {
-					userStr += " and a.isvisit IS NULL"
+					querys = append(querys, " a.isvisit IS NULL")
 				} else if spqp.IsRead == "1" {
-					userStr += " and a.isvisit=1"
+					querys = append(querys, " a.isvisit=1")
 				}
 			}
 			// 信息来源
 			if spqp.Source == "1" || spqp.Source == "2" || spqp.Source == "3" {
-				userStr += " and a.source=" + spqp.Source
+				querys = append(querys, " a.source="+spqp.Source)
 			}
 			if spqp.PositionType == 1 {
 				//企业主体是企业的个人查询
@@ -657,8 +645,8 @@ func (s *subscribePush) getDatasFromMysql(spqp *SubPushQueryParam, starttime, en
 			if s.ModuleFlag != MemberFlag && s.ModuleFlag != EntnicheFlag {
 				findSql += ",a.isvip"
 			}
-			findSql = fmt.Sprintf("%s from %s  a  where  %s %s "+
-				" order by a.date desc,a.id desc", findSql, aboutDbMsg[s.ModuleFlag].MysqlTable, strings.Join(querys, " and "), userStr)
+			findSql = fmt.Sprintf("%s from %s  a  where  %s and  %s "+
+				" order by a.date desc,a.id desc", findSql, aboutDbMsg[s.ModuleFlag].MysqlTable, userStr, strings.Join(querys, " and "))
 		}
 		//查询总数
 		logx.Info("countSql", countSql)