Переглянути джерело

wip:企业查询去重提交

wangkaiyue 2 роки тому
батько
коміт
da38673f5e
1 змінених файлів з 82 додано та 69 видалено
  1. 82 69
      jyBXSubscribe/rpc/model/push.go

+ 82 - 69
jyBXSubscribe/rpc/model/push.go

@@ -346,7 +346,7 @@ func (s *subscribePush) getDatasFromMysql(spqp *SubPushQueryParam, starttime, en
 	userStr := " "
 	userStr := " "
 	leftJoinStr := " "
 	leftJoinStr := " "
 	var (
 	var (
-		countSql, searchSql, findSql string
+		countSql, findSql string
 	)
 	)
 	codeMap, err := IC.CodeLib.CodeTransformation(context.Background(), &codeservice.Request{})
 	codeMap, err := IC.CodeLib.CodeTransformation(context.Background(), &codeservice.Request{})
 	if codeMap.Data == nil || err != nil {
 	if codeMap.Data == nil || err != nil {
@@ -357,63 +357,11 @@ func (s *subscribePush) getDatasFromMysql(spqp *SubPushQueryParam, starttime, en
 	if spqp.SelectInfoIds != nil && len(spqp.SelectInfoIds) > 0 {
 	if spqp.SelectInfoIds != nil && len(spqp.SelectInfoIds) > 0 {
 		countSql = fmt.Sprintf("select count(1) as count from %s a LEFT JOIN %s b ON a.infoid = b.infoid where %s", aboutDbMsg[s.ModuleFlag].MysqlTable, Baseinfo, fmt.Sprintf("a.id in ('%s')", strings.Join(spqp.SelectInfoIds, "','")))
 		countSql = fmt.Sprintf("select count(1) as count from %s a LEFT JOIN %s b ON a.infoid = b.infoid where %s", aboutDbMsg[s.ModuleFlag].MysqlTable, Baseinfo, fmt.Sprintf("a.id in ('%s')", strings.Join(spqp.SelectInfoIds, "','")))
 		logx.Info("countSql", countSql)
 		logx.Info("countSql", countSql)
-		searchSql = fmt.Sprintf(" from %s a LEFT JOIN %s b ON a.infoid = b.infoid where %s order by a.date desc,a.id desc", aboutDbMsg[s.ModuleFlag].MysqlTable, Baseinfo, fmt.Sprintf("a.id in ('%s')", strings.Join(spqp.SelectInfoIds, "','")))
+		findSql = fmt.Sprintf("select a.id,a.date,a.infoid,a.isvisit,a.matchkeys,a.type,b.isvalidfile as attachment_count from %s a LEFT JOIN %s b ON a.infoid = b.infoid where %s order by a.date desc,a.id desc", aboutDbMsg[s.ModuleFlag].MysqlTable, Baseinfo, fmt.Sprintf("a.id in ('%s')", strings.Join(spqp.SelectInfoIds, "','")))
 		//查询总数
 		//查询总数
 		count = spqp.BaseServiceMysql.CountBySql(countSql)
 		count = spqp.BaseServiceMysql.CountBySql(countSql)
-		findSql = "select a.id,a.date,a.infoid,a.isvisit,a.matchkeys,a.type,b.isvalidfile as attachment_count"
 	} else {
 	} else {
-		if spqp.IsEnt {
-			isNew := 1
-			//商机管理判断
-			newCount := IC.MainMysql.CountBySql("select  count(id) from  entniche_info where  id =? and  isNew != 1 and  status=1", spqp.EntId)
-			if newCount > 0 {
-				//新商机管理
-				isNew = 0
-			}
-			if isNew == 1 {
-				//企业主体是企业的企业查询
-				userStr = fmt.Sprintf("   a.entid='%s' and (a.source > 1) ", spqp.EntId)
-			} else {
-				userStr = fmt.Sprintf("   a.entid='%s' ", spqp.EntId)
-			}
-			//判断是企业管理员还是部门管理员 部门管理员获取所有子部门
-			userEnt := EntInfo(common.IntAll(spqp.EntId), common.IntAll(spqp.EntUserId))
-			if userEnt.Role_admin_department { //部门管理员 获取所有部门和子部门员工
-				users := GetDisUsers(common.IntAll(spqp.EntId), userEnt.Dept.Id)
-				var userIds []string
-				if users != nil && len(*users) > 0 {
-					for _, v := range *users {
-						userIds = append(userIds, common.InterfaceToStr(v.Id))
-					}
-					userStr += fmt.Sprintf(" a.userid in (%s)  ", strings.Join(userIds, ","))
-				} else {
-					log.Printf("部门管理员为获取到部门员工:%s", spqp.EntUserId)
-					result = []*bxsubscribe.SubscribeInfo{}
-					return
-				}
-			}
-		} else {
-			if spqp.BuySubject == 1 {
-				//企业主体是企业的个人查询
-				if spqp.UserType == SubVipFlag {
-					userStr = fmt.Sprintf("  a.userid='%s' and (  a.product=1 or a.source>1     ) ", common.If(s.ModuleFlag == EntnicheFlag, spqp.EntUserId, common.InterfaceToStr(spqp.NewUserId)))
-				} else if spqp.UserType == MemberFlag {
-					userStr = fmt.Sprintf("  a.userid='%s' and (  a.product=2  or a.source>1  ) ", common.If(s.ModuleFlag == EntnicheFlag, spqp.EntUserId, common.InterfaceToStr(spqp.NewUserId)))
-				} else {
-					userStr = fmt.Sprintf("  a.userid='%s' and (  a.product is null  or a.source>1  ) ", common.If(s.ModuleFlag == EntnicheFlag, spqp.EntUserId, common.InterfaceToStr(spqp.NewUserId)))
-				}
-			} else {
-				if s.ModuleFlag == EntnicheFlag {
-					//老商机管理个人订阅查询
-					userStr = fmt.Sprintf("  a.userid='%s'  and a.product is null", common.If(s.ModuleFlag == EntnicheFlag, spqp.EntUserId, common.InterfaceToStr(spqp.NewUserId)))
-				} else {
-					//个人订阅查询
-					userStr = fmt.Sprintf("  a.userid='%s'", common.If(s.ModuleFlag == EntnicheFlag, spqp.EntUserId, common.InterfaceToStr(spqp.NewUserId)))
-				}
-
-			}
-		}
-
+		//公共查询条件
 		//时间
 		//时间
 		if starttime > 0 && endtime > 0 {
 		if starttime > 0 && endtime > 0 {
 			userStr += fmt.Sprintf(" and a.date>=%d and a.date<=%d", starttime, endtime)
 			userStr += fmt.Sprintf(" and a.date>=%d and a.date<=%d", starttime, endtime)
@@ -544,25 +492,90 @@ func (s *subscribePush) getDatasFromMysql(spqp *SubPushQueryParam, starttime, en
 				querys = append(querys, fmt.Sprintf("b.isvalidfile =0 "))
 				querys = append(querys, fmt.Sprintf("b.isvalidfile =0 "))
 			}
 			}
 		}
 		}
-		if len(querys) > 1 {
-			countSql = fmt.Sprintf("select count(1) as count from %s  a STRAIGHT_JOIN %s b ON %s and a.infoid = b.infoid  %s where %s", aboutDbMsg[s.ModuleFlag].MysqlTable, Baseinfo, userStr, leftJoinStr, strings.Join(querys, " and "))
+
+		if spqp.IsEnt {
+			isNew := 1
+			//商机管理判断
+			newCount := IC.MainMysql.CountBySql("select  count(id) from  entniche_info where  id =? and  isNew != 1 and  status=1", spqp.EntId)
+			if newCount > 0 {
+				//新商机管理
+				isNew = 0
+			}
+			if isNew == 1 {
+				//企业主体是企业的企业查询
+				userStr = fmt.Sprintf("   a.entid='%s' and (a.source > 1) ", spqp.EntId)
+			} else {
+				userStr = fmt.Sprintf("   a.entid='%s' ", spqp.EntId)
+			}
+			//判断是企业管理员还是部门管理员 部门管理员获取所有子部门
+			userEnt := EntInfo(common.IntAll(spqp.EntId), common.IntAll(spqp.EntUserId))
+			if userEnt.Role_admin_department { //部门管理员 获取所有部门和子部门员工
+				users := GetDisUsers(common.IntAll(spqp.EntId), userEnt.Dept.Id)
+				var userIds []string
+				if users != nil && len(*users) > 0 {
+					for _, v := range *users {
+						userIds = append(userIds, common.InterfaceToStr(v.Id))
+					}
+					userStr += fmt.Sprintf(" a.userid in (%s)  ", strings.Join(userIds, ","))
+				} else {
+					log.Printf("部门管理员为获取到部门员工:%s", spqp.EntUserId)
+					result = []*bxsubscribe.SubscribeInfo{}
+					return
+				}
+			}
+
+			//查询数量(需要去重)
+			if len(querys) > 1 {
+				//countSql = fmt.Sprintf("select count(1) as count from %s  a STRAIGHT_JOIN %s b ON %s and a.infoid = b.infoid  %s where %s", aboutDbMsg[s.ModuleFlag].MysqlTable, Baseinfo, userStr, leftJoinStr, strings.Join(querys, " and "))
+				countSql = fmt.Sprintf("SELECT count(1) AS count FROM (SELECT DISTINCT matchkeys,infoid FROM %s WHERE 1=1 and %s )  a STRAIGHT_JOIN %s b ON  a.infoid = b.infoid  %s where %s", aboutDbMsg[s.ModuleFlag].MysqlTable, userStr, Baseinfo, leftJoinStr, strings.Join(querys, " and "))
+			} else {
+				//countSql = fmt.Sprintf("select count(1) as count from %s  a where %s", aboutDbMsg[s.ModuleFlag].MysqlTable, userStr)
+				countSql = fmt.Sprintf("SELECT count(1) AS count FROM ( SELECT DISTINCT matchkeys,infoid FROM %s a WHERE %s ) AS data", aboutDbMsg[s.ModuleFlag].MysqlTable, userStr)
+			}
+			//列表查询
+			findSql = fmt.Sprintf("select a.infoid,a.matchkeys,b.isvalidfile as attachment_count from (SELECT DISTINCT matchkeys,infoid FROM %s WHERE 1=1 and %s ) a STRAIGHT_JOIN %s b ON a.infoid = b.infoid  %s where %s"+
+				" order by a.date desc,a.id desc", aboutDbMsg[s.ModuleFlag].MysqlTable, userStr, Baseinfo, leftJoinStr, strings.Join(querys, " and "))
 		} else {
 		} else {
-			countSql = fmt.Sprintf("select count(1) as count from %s  a where %s", aboutDbMsg[s.ModuleFlag].MysqlTable, userStr)
+			if spqp.BuySubject == 1 {
+				//企业主体是企业的个人查询
+				if spqp.UserType == SubVipFlag {
+					userStr = fmt.Sprintf("  a.userid='%s' and (  a.product=1 or a.source>1     ) ", common.If(s.ModuleFlag == EntnicheFlag, spqp.EntUserId, common.InterfaceToStr(spqp.NewUserId)))
+				} else if spqp.UserType == MemberFlag {
+					userStr = fmt.Sprintf("  a.userid='%s' and (  a.product=2  or a.source>1  ) ", common.If(s.ModuleFlag == EntnicheFlag, spqp.EntUserId, common.InterfaceToStr(spqp.NewUserId)))
+				} else {
+					userStr = fmt.Sprintf("  a.userid='%s' and (  a.product is null  or a.source>1  ) ", common.If(s.ModuleFlag == EntnicheFlag, spqp.EntUserId, common.InterfaceToStr(spqp.NewUserId)))
+				}
+			} else {
+				if s.ModuleFlag == EntnicheFlag {
+					//老商机管理个人订阅查询
+					userStr = fmt.Sprintf("  a.userid='%s'  and a.product is null", common.If(s.ModuleFlag == EntnicheFlag, spqp.EntUserId, common.InterfaceToStr(spqp.NewUserId)))
+				} else {
+					//个人订阅查询
+					userStr = fmt.Sprintf("  a.userid='%s'", common.If(s.ModuleFlag == EntnicheFlag, spqp.EntUserId, common.InterfaceToStr(spqp.NewUserId)))
+				}
+			}
+
+			//查询数量
+			if len(querys) > 1 {
+				countSql = fmt.Sprintf("select count(1) as count from %s  a STRAIGHT_JOIN %s b ON %s and a.infoid = b.infoid  %s where %s", aboutDbMsg[s.ModuleFlag].MysqlTable, Baseinfo, userStr, leftJoinStr, strings.Join(querys, " and "))
+			} else {
+				countSql = fmt.Sprintf("select count(1) as count from %s  a where %s", aboutDbMsg[s.ModuleFlag].MysqlTable, userStr)
+			}
+			//列表查询语句
+			findSql = "select a.id,a.date,a.infoid,a.isvisit,a.matchkeys,a.type,b.isvalidfile as attachment_count"
+			if s.ModuleFlag == EntnicheFlag {
+				findSql += ", IF(a.source is NULL,1,a.source) as  source "
+			}
+			if s.ModuleFlag != MemberFlag && s.ModuleFlag != EntnicheFlag {
+				findSql += ",a.isvip"
+			}
+			findSql = fmt.Sprintf("%s from %s  a STRAIGHT_JOIN %s b ON %s and a.infoid = b.infoid  %s where %s"+
+				" order by a.date desc,a.id desc", findSql, aboutDbMsg[s.ModuleFlag].MysqlTable, Baseinfo, userStr, leftJoinStr, strings.Join(querys, " and "))
 		}
 		}
-		logx.Info("countSql", countSql)
-		searchSql = fmt.Sprintf(" from %s  a STRAIGHT_JOIN %s b ON %s and a.infoid = b.infoid  %s where %s"+
-			" order by a.date desc,a.id desc", aboutDbMsg[s.ModuleFlag].MysqlTable, Baseinfo, userStr, leftJoinStr, strings.Join(querys, " and "))
 		//查询总数
 		//查询总数
+		logx.Info("countSql", countSql)
 		count = spqp.BaseServiceMysql.CountBySql(countSql)
 		count = spqp.BaseServiceMysql.CountBySql(countSql)
-		findSql = "select a.id,a.date,a.infoid,a.isvisit,a.matchkeys,a.type,b.isvalidfile as attachment_count"
-		if s.ModuleFlag == EntnicheFlag {
-			findSql += ", IF(a.source is NULL,1,a.source) as  source "
-		}
-	}
-	if s.ModuleFlag != MemberFlag && s.ModuleFlag != EntnicheFlag {
-		findSql += ",a.isvip"
 	}
 	}
-	findSql += searchSql
 	if isLimit {
 	if isLimit {
 		findSql += fmt.Sprintf(" limit %d,%d", (spqp.PageNum-1)*size, size)
 		findSql += fmt.Sprintf(" limit %d,%d", (spqp.PageNum-1)*size, size)
 	}
 	}