فهرست منبع

wip:查询sql修改

wangkaiyue 2 سال پیش
والد
کامیت
d44605a803
2فایلهای تغییر یافته به همراه70 افزوده شده و 47 حذف شده
  1. 18 13
      jyBXSubscribe/rpc/internal/logic/msgdistributorlogic.go
  2. 52 34
      jyBXSubscribe/rpc/model/push.go

+ 18 - 13
jyBXSubscribe/rpc/internal/logic/msgdistributorlogic.go

@@ -2,7 +2,6 @@ package logic
 
 import (
 	"app.yhyue.com/moapp/jybase/common"
-	"app.yhyue.com/moapp/jybase/encrypt"
 	"app.yhyue.com/moapp/jybase/redis"
 	"context"
 	"database/sql"
@@ -39,21 +38,27 @@ func (l *MsgDistributorLogic) MsgDistributor(in *bxsubscribe.MsgDistributorReq)
 		return &bxsubscribe.StatusResp{ErrorCode: -1, ErrorMsg: "无权限"}, nil
 	}
 	//根据信息查询区域列表
-	var regin, infoIds, staffs []string
+	var regin, pushIds, staffs []string
 	staffs = strings.Split(in.Staffs, ",")
 	var pushCas []*model.PushCa
-	for _, eid := range strings.Split(in.MessageId, ",") {
-		if eid != "" {
-			array := encrypt.DecodeArticleId2ByCheck(eid)
-			if len(array) == 1 && array[0] != "" {
-				infoIds = append(infoIds, array[0])
-				pushCas = append(pushCas, &model.PushCa{InfoId: array[0]})
-			}
+	for _, id := range strings.Split(in.MessageId, ",") {
+		if id != "" {
+			pushIds = append(pushIds, id)
 		}
 	}
-	if len(infoIds) == 0 || in.Staffs == "" {
+	if len(pushIds) == 0 || in.Staffs == "" {
 		return &bxsubscribe.StatusResp{ErrorCode: -1, ErrorMsg: "缺少参数"}, nil
 	}
+	//获取分发信息的地区
+	infoRes := IC.BaseServiceMysql.SelectBySql(fmt.Sprintf("SELECT infoid FROM pushentniche WHERE id in('%s')", strings.Join(pushIds, "','")))
+	if infoRes == nil || len(*infoRes) == 0 {
+		return &bxsubscribe.StatusResp{ErrorCode: -1, ErrorMsg: "获取分发地区异常"}, nil
+	}
+	for _, m := range *infoRes {
+		if infoId, _ := m["infoid"].(string); infoId != "" {
+			pushCas = append(pushCas, &model.PushCa{InfoId: infoId})
+		}
+	}
 	infoList := model.NewSubscribePush().GetInfoByIds(IC.MgoBidding, IC.DB.Mongo.Bidding.Collection, IC.DB.Mongo.Bidding.CollectionBack, pushCas)
 	for _, info := range infoList {
 		if info.Area != "" && info.Area != "全国" {
@@ -75,13 +80,13 @@ func (l *MsgDistributorLogic) MsgDistributor(in *bxsubscribe.MsgDistributorReq)
 		}
 	}
 	//查询分发信息内容
-	msgRes := IC.BaseServiceMysql.SelectBySql(fmt.Sprintf("SELECT p.* FROM pushentniche p,(SELECT infoid,MIN(id) as id FROM pushentniche WHERE infoid in ('%s') and entid =? and source = 2 GROUP BY infoid) b WHERE p.id=b.id", strings.Join(infoIds, "','")), in.EntId)
+	msgRes := IC.BaseServiceMysql.SelectBySql(fmt.Sprintf("SELECT * FROM pushentniche WHERE id in ('%s') and entid =? and source = 2 ", strings.Join(pushIds, "','")), in.EntId)
 	if msgRes == nil || len(*msgRes) == 0 {
 		return &bxsubscribe.StatusResp{ErrorCode: -1, ErrorMsg: "获取分发信息内容异常"}, nil
 	}
 	//分发数据库修改
 	ok := IC.BaseServiceMysql.ExecTx("分发数据库修改", func(tx *sql.Tx) bool {
-		insertRow := []string{"entid", "deptid", "infoid", "matchkeys", "type", "product", "matchways", "matchitems", "disid", "source", "date", "userid", "isvisit", "visittime"}
+		insertRow := []string{"entid", "deptid", "infoid", "matchkeys", "type", "product", "matchways", "matchitems", "disid", "source", "date", "userid", "isvisit", "visittime", "pid"}
 		msgItems := []string{"entid", "deptid", "infoid", "matchkeys", "type", "product", "matchways", "matchitems", "disid"}
 		dateNew := time.Now().Unix()
 		for _, m := range *msgRes {
@@ -91,7 +96,7 @@ func (l *MsgDistributorLogic) MsgDistributor(in *bxsubscribe.MsgDistributorReq)
 			}
 			for _, uid := range staffs {
 				insertValue = append(insertValue, msgValues...)
-				insertValue = append(insertValue, []interface{}{3, dateNew, uid, nil, nil}...)
+				insertValue = append(insertValue, []interface{}{3, dateNew, uid, nil, nil, m["id"]}...)
 			}
 			affected, _ := IC.BaseServiceMysql.InsertBatchByTx(tx, "pushentniche", insertRow, insertValue)
 			if affected == 0 {

+ 52 - 34
jyBXSubscribe/rpc/model/push.go

@@ -373,18 +373,6 @@ func (s *subscribePush) getDatasFromMysql(spqp *SubPushQueryParam, starttime, en
 		} else if starttime == 0 && endtime > 0 {
 			userStr += fmt.Sprintf(" and a.date<=%d", endtime)
 		}
-		// 是否已读
-		if spqp.IsRead != "" {
-			if spqp.IsRead == "0" {
-				userStr += " and a.isvisit IS NULL"
-			} else if spqp.IsRead == "1" {
-				userStr += " and a.isvisit=1"
-			}
-		}
-		// 信息来源
-		if spqp.Source == "1" || spqp.Source == "2" || spqp.Source == "3" {
-			userStr += " and a.source=" + spqp.Source
-		}
 
 		if spqp.Area != "" || spqp.City != "" {
 			var sqlAreaCity = ""
@@ -510,8 +498,8 @@ func (s *subscribePush) getDatasFromMysql(spqp *SubPushQueryParam, starttime, en
 		}
 
 		if spqp.IsEnt {
-
 			isNew := 1
+			var staffs []string
 			//商机管理判断
 			newCount := IC.MainMysql.CountBySql("select  count(id) from  entniche_info where  id =? and  isNew != 1 and  status=1", spqp.EntId)
 			if newCount > 0 {
@@ -522,7 +510,7 @@ func (s *subscribePush) getDatasFromMysql(spqp *SubPushQueryParam, starttime, en
 				//企业主体是企业的企业查询
 				userStr += fmt.Sprintf(" and  a.entid='%s' and (a.source > 1) ", spqp.EntId)
 			} else {
-				userStr += fmt.Sprintf(" and  a.entid='%s' ", spqp.EntId)
+				userStr += fmt.Sprintf(" and  a.entid='%s' and a.source = 2 ", spqp.EntId)
 			}
 			//判断是企业管理员还是部门管理员 部门管理员获取所有子部门
 			userEnt := EntInfo(common.IntAll(spqp.EntId), common.IntAll(spqp.EntUserId))
@@ -530,35 +518,65 @@ func (s *subscribePush) getDatasFromMysql(spqp *SubPushQueryParam, starttime, en
 				result = []*bxsubscribe.SubscribeInfo{}
 				return
 			}
-			if userEnt.Role_admin_department && len(spqp.Staffs) == 0 { //部门管理员 获取所有部门和子部门员工
+			if len(spqp.Staffs) == 0 && 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))
+						staffs = append(staffs, common.InterfaceToStr(v.Id))
 					}
-					userStr += fmt.Sprintf(" and  a.userid in (%s)  ", strings.Join(userIds, ","))
 				} else {
 					log.Printf("部门管理员为获取到部门员工:%s", spqp.EntUserId)
 					result = []*bxsubscribe.SubscribeInfo{}
 					return
 				}
-			} else if len(spqp.Staffs) != 0 {
-				userStr += fmt.Sprintf(" and  a.userid in (%s)  ", strings.Join(spqp.Staffs, ","))
 			}
 
-			//查询数量(需要去重)
-			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 a WHERE source=2 %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 "))
+			// 无查询分配人员、是否已读
+			if (spqp.IsRead == "" && len(spqp.Staffs) == 0) || isNew == 0 {
+				//查询数量(需要去重)
+				//if len(querys) > 1 {
+				countSql = fmt.Sprintf("select count(1) from %s a STRAIGHT_JOIN %s b ON  a.infoid = b.infoid  %s where  %s %s", aboutDbMsg[s.ModuleFlag].MysqlTable, Baseinfo, leftJoinStr, strings.Join(querys, " and "), userStr)
+				//} else {
+				//	countSql = fmt.Sprintf("select count(1) from %s a WHERE 1=1 %s ", aboutDbMsg[s.ModuleFlag].MysqlTable, userStr)
+				//}
+				//列表查询
+				findSql = fmt.Sprintf("select a.id,a.infoid,a.matchkeys,b.isvalidfile as attachment_count from %s a STRAIGHT_JOIN %s b ON a.infoid = b.infoid  %s where  %s %s order by a.date desc,a.id desc",
+					aboutDbMsg[s.ModuleFlag].MysqlTable, Baseinfo, leftJoinStr, strings.Join(querys, " and "), userStr)
 			} 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 source=2 %s ) AS data", aboutDbMsg[s.ModuleFlag].MysqlTable, userStr)
+				//查询分配人员或是否已读
+				staffQuery := " 1=1 "
+				if len(staffs) > 0 {
+					staffQuery += fmt.Sprintf(" and  a.userid in ('%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"
+					}
+				}
+				//if len(querys) > 1 {
+				countSql = fmt.Sprintf("select count(1) from %s a STRAIGHT_JOIN %s b ON  a.infoid = b.infoid  %s where source=2 %s and %s and ((%s) or EXISTS (SELECT 1 from %s c STRAIGHT_JOIN %s b ON c.infoid = b.infoid  %s WHERE c.source=3 %s and %s and a.id=c.pid and (%s))) ", aboutDbMsg[s.ModuleFlag].MysqlTable, Baseinfo, leftJoinStr, userStr, strings.Join(querys, " and "), staffQuery, aboutDbMsg[s.ModuleFlag].MysqlTable, Baseinfo, leftJoinStr, strings.ReplaceAll(userStr, "a.", "c."), strings.Join(querys, " and "), strings.ReplaceAll(staffQuery, "a.", "c."))
+				//} else {
+				//	countSql = fmt.Sprintf("select count(1) from %s a WHERE a.source=2 %s and ((%s) or EXISTS (SELECT 1 from %s c WHERE c.source=3 %s and a.id=pid and (%s))) ", aboutDbMsg[s.ModuleFlag].MysqlTable, userStr, staffQuery, aboutDbMsg[s.ModuleFlag].MysqlTable, strings.ReplaceAll(userStr, "a.", "c."), strings.ReplaceAll(staffQuery, "a.", "c."))
+				//}
+				//列表查询
+				findSql = fmt.Sprintf("select a.id,a.infoid,a.matchkeys,b.isvalidfile as attachment_count from %s a STRAIGHT_JOIN %s b ON  a.infoid = b.infoid  %s where a.source=2 %s and %s and ((%s) or EXISTS (SELECT 1 from %s c STRAIGHT_JOIN %s b ON c.infoid = b.infoid  %s 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, Baseinfo, leftJoinStr, userStr, strings.Join(querys, " and "), staffQuery, aboutDbMsg[s.ModuleFlag].MysqlTable, Baseinfo, leftJoinStr, strings.ReplaceAll(userStr, "a.", "c."), strings.Join(querys, " and "), strings.ReplaceAll(staffQuery, "a.", "c."))
 			}
-			//列表查询
-			findSql = fmt.Sprintf("select a.infoid,a.matchkeys,b.isvalidfile as attachment_count from (SELECT DISTINCT matchkeys,infoid FROM %s a WHERE source=2 %s ) a STRAIGHT_JOIN %s b ON a.infoid = b.infoid  %s where %s"+
-				" order by b.publishtime desc", aboutDbMsg[s.ModuleFlag].MysqlTable, userStr, Baseinfo, leftJoinStr, strings.Join(querys, " and "))
 		} else {
+			// 是否已读
+			if spqp.IsRead != "" {
+				if spqp.IsRead == "0" {
+					userStr += " and a.isvisit IS NULL"
+				} else if spqp.IsRead == "1" {
+					userStr += " and a.isvisit=1"
+				}
+			}
+			// 信息来源
+			if spqp.Source == "1" || spqp.Source == "2" || spqp.Source == "3" {
+				userStr += " and a.source=" + spqp.Source
+			}
+
 			if spqp.BuySubject == 1 {
 				//企业主体是企业的个人查询
 				if spqp.UserType == SubVipFlag {
@@ -579,11 +597,11 @@ func (s *subscribePush) getDatasFromMysql(spqp *SubPushQueryParam, starttime, en
 			}
 
 			//查询数量
-			if len(querys) > 1 {
-				countSql = fmt.Sprintf("select count(1) as count from %s  a STRAIGHT_JOIN %s b ON a.infoid = b.infoid  %s where 1=1 %s and %s", aboutDbMsg[s.ModuleFlag].MysqlTable, Baseinfo, leftJoinStr, userStr, strings.Join(querys, " and "))
-			} else {
-				countSql = fmt.Sprintf("select count(1) as count from %s  a where 1=1 %s", aboutDbMsg[s.ModuleFlag].MysqlTable, userStr)
-			}
+			//if len(querys) > 1 {
+			countSql = fmt.Sprintf("select count(1) as count from %s  a STRAIGHT_JOIN %s b ON a.infoid = b.infoid  %s where 1=1 %s and %s", aboutDbMsg[s.ModuleFlag].MysqlTable, Baseinfo, leftJoinStr, userStr, strings.Join(querys, " and "))
+			//} else {
+			//	countSql = fmt.Sprintf("select count(1) as count from %s  a where 1=1 %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 {