瀏覽代碼

Merge remote-tracking branch 'origin/dev4.5' into dev4.5

wangkaiyue 4 年之前
父節點
當前提交
4af3259e35

+ 8 - 1
src/jfw/front/pchelper.go

@@ -345,7 +345,14 @@ func (l *PcHelper) PushView() error {
 	if userid != "" {
 		user, _ := jy.SubscribePush.UserInfo(public.MQFW, userid)
 		if user != nil {
-			hasNextPage, list := jy.SubscribePush.Datas(public.Mgo_Bidding, public.DbConf.Mongodb.Bidding.Collection, public.DbConf.Mongodb.Bidding.Collection_back, public.PushMysql, userid, pageNum, "", "")
+			hasNextPage, list := jy.SubscribePush.Datas(&jy.SubPushQueryParam{
+				Mgo_bidding:  public.Mgo_Bidding,
+				Bidding:      public.DbConf.Mongodb.Bidding.Collection,
+				Bidding_back: public.DbConf.Mongodb.Bidding.Collection_back,
+				PushMysql:    public.PushMysql,
+				UserId:       userid,
+				PageNum:      pageNum,
+			})
 			//hasNextPage, list, _ := public.HistoryPush.Datas(userid, pageNum, firstPushTime, "")
 			res["hasNextPage"] = hasNextPage
 			res["data"] = &list

+ 32 - 40
src/jfw/front/swordfish.go

@@ -1881,21 +1881,6 @@ func classify(stp, area, industry string) (string, string, string) {
 	return tpadd, areaadd, induadd
 }
 func (f *Front) HasPushHistory() {
-	//获取时间
-	var formatTime string
-	selectTime := f.GetString("selectTime")
-	//	log.Println(selectTime, "---", f.GetString("area"))
-	lenStr := len([]rune(selectTime))
-	if selectTime != "" {
-		if selectTime != "undefined" && lenStr >= 10 {
-			selectTime = selectTime[0:10]
-			select_time, _ := strconv.ParseInt(selectTime, 10, 64)
-			//转化
-			formatTime = time.Unix(select_time, 0).Format("2006-01-02")
-		} else if lenStr < 10 {
-			formatTime = ""
-		}
-	}
 	userId := util.ObjToString(f.GetSession("userId"))
 	var industry_ = []string{}
 	if userId == "" {
@@ -1971,12 +1956,22 @@ func (f *Front) HasPushHistory() {
 		}
 	}
 	pageNum, _ := f.GetInteger("pageNum")
-	if pageNum == 0 {
-		pageNum = 1
-	}
-	area := f.GetString("area")
-	hasNextPage, list := jy.SubscribePush.Datas(public.Mgo_Bidding, public.DbConf.Mongodb.Bidding.Collection, public.DbConf.Mongodb.Bidding.Collection_back, public.PushMysql, userId, pageNum, formatTime, area)
-	if hasKeyFlag && len(list) == 0 && formatTime == "" && area == "" && !isVipFlag {
+	spqp := &jy.SubPushQueryParam{
+		Mgo_bidding:   public.Mgo_Bidding,
+		Bidding:       public.DbConf.Mongodb.Bidding.Collection,
+		Bidding_back:  public.DbConf.Mongodb.Bidding.Collection_back,
+		PushMysql:     public.PushMysql,
+		UserId:        userId,
+		PageNum:       pageNum,
+		SelectTime:    f.GetString("selectTime"),
+		Area:          f.GetString("area"),
+		Buyerclass:    f.GetString("buyerclass"),
+		Subtype:       f.GetString("subtype"),
+		Subscopeclass: f.GetString("subscopeclass"),
+		Key:           f.GetString("key"),
+	}
+	hasNextPage, list := jy.SubscribePush.Datas(spqp)
+	if hasKeyFlag && len(list) == 0 && spqp.IsEmpty() && !isVipFlag {
 		flag, data := jy.SubscribePush.MakeHistoryDatas(public.MQFW, public.PushMysql, userId, public.PushView)
 		if flag && data != nil {
 			jsonBytes, err := json.Marshal(data)
@@ -2083,26 +2078,23 @@ func (f *Front) Historypush() error {
 }
 
 func (f *Front) HistorypushPaging() error {
-	pageNum, _ := f.GetInteger("pageNum")
 	//
-	selectTime := f.GetString("selectTime")
-	var formatTime string
-	if selectTime != "" {
-		selectTime = selectTime[0:10]
-		select_time, _ := strconv.ParseInt(selectTime, 10, 64)
-		//转化
-		formatTime = time.Unix(select_time, 0).Format("2006-01-02")
-	}
-	area := f.GetString("area")
-	log.Println("~~~~~:", formatTime, "~~~~", selectTime, "~~~", pageNum, "~~~", area)
-	//
-	userId := util.ObjToString(f.GetSession("userId"))
-	if userId != "" {
-		//		user, firstPushTime := public.HistoryPush.UserInfo(userId)
-		//		if user == nil {
-		//			return nil
-		//		}
-		hasNextPage, list := jy.SubscribePush.Datas(public.Mgo_Bidding, public.DbConf.Mongodb.Bidding.Collection, public.DbConf.Mongodb.Bidding.Collection, public.PushMysql, userId, pageNum, formatTime, area)
+	if userId, _ := f.GetSession("userId").(string); userId != "" {
+		pageNum, _ := f.GetInteger("pageNum")
+		hasNextPage, list := jy.SubscribePush.Datas(&jy.SubPushQueryParam{
+			Mgo_bidding:   public.Mgo_Bidding,
+			Bidding:       public.DbConf.Mongodb.Bidding.Collection,
+			Bidding_back:  public.DbConf.Mongodb.Bidding.Collection_back,
+			PushMysql:     public.PushMysql,
+			UserId:        userId,
+			PageNum:       pageNum,
+			SelectTime:    f.GetString("selectTime"),
+			Area:          f.GetString("area"),
+			Buyerclass:    f.GetString("buyerclass"),
+			Subtype:       f.GetString("subtype"),
+			Subscopeclass: f.GetString("subscopeclass"),
+			Key:           f.GetString("key"),
+		})
 		//		hasNextPage, list, _ := public.HistoryPush.Datas(userId, pageNum, firstPushTime, formatTime)
 		f.ServeJson(map[string]interface{}{
 			"hasNextPage": hasNextPage,

+ 31 - 32
src/jfw/modules/app/src/app/front/swordfish.go

@@ -14,7 +14,6 @@ import (
 	"qfw/util/elastic"
 	"qfw/util/jy"
 	"qfw/util/redis"
-	"strconv"
 	"strings"
 	"sync"
 	"time"
@@ -395,20 +394,6 @@ func (m *Front) WxpushViewPaging() {
 
 func (f *Front) HasPushHistory() {
 	//获取时间
-	var formatTime string
-	selectTime := f.GetString("selectTime")
-	lenStr := len([]rune(selectTime))
-	//TODO 可以优化
-	if selectTime != "" {
-		if selectTime != "undefined" && lenStr >= 10 {
-			selectTime = selectTime[0:10]
-			select_time, _ := strconv.ParseInt(selectTime, 10, 64)
-			//转化
-			formatTime = time.Unix(select_time, 0).Format("2006-01-02")
-		} else if lenStr < 10 {
-			formatTime = ""
-		}
-	}
 	var industry_ = []string{}
 	userId, _ := f.GetSession("userId").(string)
 	if userId == "" {
@@ -448,21 +433,31 @@ func (f *Front) HasPushHistory() {
 		}
 	}
 	pageNum, _ := f.GetInteger("pageNum")
-	if pageNum == 0 {
-		pageNum = 1
+	spqp := &jy.SubPushQueryParam{
+		Mgo_bidding:   public.Mgo_Bidding,
+		Bidding:       public.DbConf.Mongodb.Bidding.Collection,
+		Bidding_back:  public.DbConf.Mongodb.Bidding.Collection_back,
+		PushMysql:     public.PushMysql,
+		UserId:        userId,
+		PageNum:       pageNum,
+		SelectTime:    f.GetString("selectTime"),
+		Area:          f.GetString("area"),
+		Buyerclass:    f.GetString("buyerclass"),
+		Subtype:       f.GetString("subtype"),
+		Subscopeclass: f.GetString("subscopeclass"),
+		Key:           f.GetString("key"),
 	}
-	area := f.GetString("area")
-	otherFlag := false
-	hasNextPage, list := jy.SubscribePush.Datas(public.Mgo_Bidding, public.DbConf.Mongodb.Bidding.Collection, public.DbConf.Mongodb.Bidding.Collection_back, public.PushMysql, userId, pageNum, formatTime, area)
+	hasNextPage, list := jy.SubscribePush.Datas(spqp)
 	//首次用户推送查询“其他”
 	t, _ := time.ParseInLocation("2006-01-02", time.Now().Format("2006-01-02"), time.Local)
 	today_1 := t.Unix()
 	today_2 := t.AddDate(0, 0, 1).Unix()
+	otherFlag := false
 	if public.PushMysql.CountBySql("select count(1) as count from pushsubscribe where userid=? and (date between ? and ? ) and isvip=1 and buyerclass=93", f.GetSession("userId"), today_1, today_2) > 0 {
 		otherFlag = true
 	}
 	//formatTime
-	if hasKeyFlag && len(list) == 0 && formatTime == "" && area == "" && !isVipFlag {
+	if hasKeyFlag && len(list) == 0 && spqp.IsEmpty() && !isVipFlag {
 		flag, data := jy.SubscribePush.MakeHistoryDatas(public.MQFW, public.PushMysql, userId, public.PushView)
 		if flag && data != nil {
 			jsonBytes, err := json.Marshal(data)
@@ -515,24 +510,28 @@ func (f *Front) Historypush() error {
 }
 
 func (f *Front) HistorypushPaging() error {
-	pageNum, _ := f.GetInteger("pageNum")
-	selectTime := f.GetString("selectTime")
-	var formatTime string
-	if selectTime != "" {
-		selectTime = selectTime[0:10]
-		select_time, _ := strconv.ParseInt(selectTime, 10, 64)
-		//转化
-		formatTime = time.Unix(select_time, 0).Format("2006-01-02")
-	}
-	area := f.GetString("area")
 	userId, _ := f.GetSession("userId").(string)
 	if userId == "" {
 		return nil
 	}
+	pageNum, _ := f.GetInteger("pageNum")
 	if pageNum == 1 {
 		go updateUserApppushunread(userId)
 	}
-	hasNextPage, list := jy.SubscribePush.Datas(public.Mgo_Bidding, public.DbConf.Mongodb.Bidding.Collection, public.DbConf.Mongodb.Bidding.Collection_back, public.PushMysql, userId, pageNum, formatTime, area)
+	hasNextPage, list := jy.SubscribePush.Datas(&jy.SubPushQueryParam{
+		Mgo_bidding:   public.Mgo_Bidding,
+		Bidding:       public.DbConf.Mongodb.Bidding.Collection,
+		Bidding_back:  public.DbConf.Mongodb.Bidding.Collection_back,
+		PushMysql:     public.PushMysql,
+		UserId:        userId,
+		PageNum:       pageNum,
+		SelectTime:    f.GetString("selectTime"),
+		Area:          f.GetString("area"),
+		Buyerclass:    f.GetString("buyerclass"),
+		Subtype:       f.GetString("subtype"),
+		Subscopeclass: f.GetString("subscopeclass"),
+		Key:           f.GetString("key"),
+	})
 	f.ServeJson(map[string]interface{}{
 		"hasNextPage": hasNextPage,
 		"data":        list,

+ 140 - 52
src/jfw/modules/common/src/qfw/util/jy/subscribepush.go

@@ -9,6 +9,7 @@ import (
 	"qfw/util/elastic"
 	"qfw/util/mysql"
 	"qfw/util/redis"
+	"strconv"
 	"strings"
 	"time"
 
@@ -67,6 +68,26 @@ type PushCa struct {
 	Isvip  int
 }
 
+//查询参数
+type SubPushQueryParam struct {
+	Mgo_bidding   mg.MongodbSim //
+	Bidding       string        //
+	Bidding_back  string        //
+	PushMysql     *mysql.Mysql  //
+	UserId        string        //用户id
+	PageNum       int           //页面
+	SelectTime    string        //时间
+	Area          string        //区域
+	Buyerclass    string        //采购单位行业
+	Subtype       string        //信息类型 二级分类
+	Subscopeclass string        //信息行业
+	Key           string        //订阅词
+}
+
+func (spqp *SubPushQueryParam) IsEmpty() bool {
+	return spqp.SelectTime == "" && spqp.Area == "" && spqp.Buyerclass == "" && spqp.Subscopeclass == "" && spqp.Subtype == "" && spqp.Key == ""
+}
+
 var SubscribePush = &subscribePush{}
 
 type subscribePush struct {
@@ -150,28 +171,47 @@ func (s *subscribePush) InfoFormat(p *PushCa, info *map[string]interface{}) *Sub
 	}
 }
 
-func (s *subscribePush) Datas(Mgo_bidding mg.MongodbSim, bidding, bidding_back string, PushMysql *mysql.Mysql, userId string, pageNum int, selectTime, area string) (hasNextPage bool, result []*SubPushList) {
-	if userId == "" {
+func (s *subscribePush) Datas(spqp *SubPushQueryParam) (hasNextPage bool, result []*SubPushList) {
+	log.Println(spqp.UserId, "subscribePush query param:", "SelectTime", spqp.SelectTime, "Area", spqp.Area, "Subtype", spqp.Subtype, "Subscopeclass", spqp.Subscopeclass, "Buyerclass", spqp.Buyerclass, "Key", spqp.Key, "PageNum", spqp.PageNum)
+	if spqp.UserId == "" {
 		return
 	}
-	if pageNum < 1 {
-		pageNum = 1
-	}
-	now := NowFormat(Date_Short_Layout)
-	start := (pageNum - 1) * pageSize
+	if spqp.PageNum < 1 {
+		spqp.PageNum = 1
+	}
+	starttime, endtime := int64(0), int64(0)
+	now := time.Now()
+	if spqp.SelectTime == "lately-7" { //最近7天
+		starttime = time.Date(now.Year(), now.Month(), now.Day()-7, 0, 0, 0, 0, time.Local).Unix()
+	} else if spqp.SelectTime == "lately-30" { //最近30天
+		starttime = time.Date(now.Year(), now.Month(), now.Day()-30, 0, 0, 0, 0, time.Local).Unix()
+	} else if spqp.SelectTime == "lastyear" { //去年
+		starttime = time.Date(now.Year()-1, 1, 1, 0, 0, 0, 0, time.Local).Unix()
+		endtime = time.Date(now.Year()-1, 12, 31, 23, 59, 59, 0, time.Local).Unix()
+	} else if len(strings.Split(spqp.SelectTime, "_")) == 2 {
+		st := strings.Split(spqp.SelectTime, "_")[0]
+		starttime, _ = strconv.ParseInt(st, 0, 64)
+		et := strings.Split(spqp.SelectTime, "_")[1]
+		endtime, _ = strconv.ParseInt(et, 0, 64)
+		etTime := time.Unix(endtime, 0)
+		endtime = time.Date(etTime.Year(), etTime.Month(), etTime.Day(), 23, 59, 59, 0, time.Local).Unix()
+	}
+	nowFormat := NowFormat(Date_Short_Layout)
+	start := (spqp.PageNum - 1) * pageSize
 	end := start + pageSize
-	if now == selectTime && area == "" {
-		subPush, err := s.GetTodayCache(userId)
+	//时间是今天,没有别的过滤条件
+	if starttime > 0 && starttime == endtime && nowFormat == FormatDateByInt64(&starttime, Date_Short_Layout) && spqp.Area == "" && spqp.Buyerclass == "" && spqp.Subscopeclass == "" && spqp.Subtype == "" && spqp.Key == "" {
+		subPush, err := s.GetTodayCache(spqp.UserId)
 		if err != nil {
-			log.Println(userId, "GetTodayCache Error", err)
+			log.Println(spqp.UserId, "GetTodayCache Error", err)
 		}
-		if err != nil || subPush == nil || subPush.Date != now || len(subPush.Datas) == 0 {
-			list := s.getDatasFromMysql(Mgo_bidding, bidding, bidding_back, PushMysql, userId, pageNum, pageSize, selectTime, area, false)
+		if err != nil || subPush == nil || subPush.Date != nowFormat || len(subPush.Datas) == 0 {
+			list := s.getDatasFromMysql(spqp, starttime, endtime, pageSize, false)
 			subPush = &SubPush{
-				Date:  now,
+				Date:  nowFormat,
 				Datas: list,
 			}
-			s.PutTodayCache(userId, subPush)
+			s.PutTodayCache(spqp.UserId, subPush)
 		}
 		length := len(subPush.Datas)
 		if end > length {
@@ -180,14 +220,15 @@ func (s *subscribePush) Datas(Mgo_bidding mg.MongodbSim, bidding, bidding_back s
 		if start < length {
 			result = subPush.Datas[start:end]
 		}
-	} else if selectTime == "" && area == "" && pageNum <= 5 {
-		allCache, err := s.GetAllCache(userId)
+	} else if spqp.IsEmpty() && spqp.PageNum <= 5 { //全部,没有过滤条件
+		allCache, err := s.GetAllCache(spqp.UserId)
 		if err != nil {
-			log.Println(userId, "GetAllCache Error", err)
+			log.Println(spqp.UserId, "GetAllCache Error", err)
 		}
 		if err != nil || allCache == nil || len(allCache) == 0 {
-			allCache = s.getDatasFromMysql(Mgo_bidding, bidding, bidding_back, PushMysql, userId, 1, AllSubPushCacheSize, selectTime, area, true)
-			s.PutAllCache(userId, allCache)
+			spqp.PageNum = 1
+			allCache = s.getDatasFromMysql(spqp, starttime, endtime, AllSubPushCacheSize, true)
+			s.PutAllCache(spqp.UserId, allCache)
 		}
 		length := len(allCache)
 		if end > length {
@@ -197,7 +238,7 @@ func (s *subscribePush) Datas(Mgo_bidding mg.MongodbSim, bidding, bidding_back s
 			result = allCache[start:end]
 		}
 	} else {
-		result = s.getDatasFromMysql(Mgo_bidding, bidding, bidding_back, PushMysql, userId, pageNum, pageSize, selectTime, area, true)
+		result = s.getDatasFromMysql(spqp, starttime, endtime, pageSize, true)
 	}
 	if result == nil {
 		result = []*SubPushList{}
@@ -205,45 +246,92 @@ func (s *subscribePush) Datas(Mgo_bidding mg.MongodbSim, bidding, bidding_back s
 	hasNextPage = len(result) >= pageSize
 	return
 }
-func (s *subscribePush) getDatasFromMysql(Mgo_bidding mg.MongodbSim, bidding, bidding_back string, PushMysql *mysql.Mysql, userId string, pageNum, myPageSize int, selectTime, area string, isLimit bool) (result []*SubPushList) {
-	findSQL := "select id,date,infoid,isvisit,matchkeys,type,isvip from pushsubscribe where userid = '" + userId + "'"
-	findStr := ""
-	if selectTime != "" {
-		startTime := selectTime + " 00:00:00"
-		endTime := selectTime + " 23:59:59"
-		st, _ := time.ParseInLocation("2006-01-02 15:04:05", startTime, time.Local)
-		et, _ := time.ParseInLocation("2006-01-02 15:04:05", endTime, time.Local)
-		findStr += " and date < " + fmt.Sprint(et.Unix()) + " and date >= " + fmt.Sprint(st.Unix())
-	}
-	if area != "" {
-		var _area = ""
-		for _, v := range strings.Split(area, ",") {
-			if v == "全部" {
-				continue
+func (s *subscribePush) getDatasFromMysql(spqp *SubPushQueryParam, starttime, endtime int64, size int, isLimit bool) (result []*SubPushList) {
+	querys := []string{fmt.Sprintf("userid='%s'", spqp.UserId)}
+	//时间
+	if starttime > 0 && endtime > 0 {
+		querys = append(querys, fmt.Sprintf("date<=%d and date>=%d", starttime, endtime))
+	} else if starttime > 0 && endtime == 0 {
+		querys = append(querys, fmt.Sprintf("date>=%d", starttime))
+	} else if starttime == 0 && endtime > 0 {
+		querys = append(querys, fmt.Sprintf("date<=%d", endtime))
+	}
+	//区域
+	if spqp.Area != "" {
+		area := []string{}
+		for _, v := range strings.Split(spqp.Area, ",") {
+			if PushMapping.Area[v] > 0 {
+				area = append(area, fmt.Sprint(PushMapping.Area[v]))
+			} else {
+				area = append(area, "-1")
 			}
-			if _area != "" {
-				_area += ","
-			}
-			_area += fmt.Sprint(PushMapping.City[v])
 		}
-		if strings.Contains(_area, ",") {
-			findStr += " and city in (" + _area + ")"
-		} else if len(strings.Split(_area, ",")) == 1 {
-			_area += ",9999" //多个in操作 比直接=查询更快
-			findStr += " and city in (" + _area + ")"
+		if len(area) == 1 {
+			area = append(area, "9999")
+		}
+		if len(area) > 0 {
+			querys = append(querys, fmt.Sprintf("area in (%s)", strings.Join(area, ",")))
+		}
+	}
+	//采购单位行业
+	if spqp.Buyerclass != "" {
+		buyerclass := []string{}
+		for _, v := range strings.Split(spqp.Buyerclass, ",") {
+			buyerclass = append(buyerclass, fmt.Sprint(PushMapping.Buyerclass[v]))
+		}
+		if len(buyerclass) == 1 {
+			buyerclass = append(buyerclass, "9999")
+		}
+		if len(buyerclass) > 0 {
+			querys = append(querys, fmt.Sprintf("buyerclass in (%s)", strings.Join(buyerclass, ",")))
+		}
+	}
+	//信息类型
+	if spqp.Subtype != "" {
+		subtype := []string{}
+		for _, v := range strings.Split(spqp.Subtype, ",") {
+			subtype = append(subtype, fmt.Sprint(PushMapping.Subtype[v]))
+		}
+		if len(subtype) == 1 {
+			subtype = append(subtype, "9999")
+		}
+		if len(subtype) > 0 {
+			querys = append(querys, fmt.Sprintf("subtype in (%s)", strings.Join(subtype, ",")))
+		}
+	}
+	//信息行业
+	if spqp.Subscopeclass != "" {
+		find_in_set := []string{}
+		for _, v := range strings.Split(spqp.Subscopeclass, ",") {
+			find_in_set = append(find_in_set, fmt.Sprintf("find_in_set('%d',subscopeclass)", PushMapping.Subscopeclass[v]))
+		}
+		if len(find_in_set) == 1 {
+			querys = append(querys, find_in_set[0])
+		} else if len(find_in_set) > 1 {
+			querys = append(querys, fmt.Sprintf("(%s)", strings.Join(find_in_set, " or ")))
+		}
+	}
+	//关键词
+	if spqp.Key != "" {
+		find_in_set := []string{}
+		for _, v := range strings.Split(spqp.Key, ",") {
+			find_in_set = append(find_in_set, fmt.Sprintf("find_in_set('%s',replace(replace(matchkeys,'+',','),' ',','))", v))
+		}
+		if len(find_in_set) == 1 {
+			querys = append(querys, find_in_set[0])
+		} else if len(find_in_set) > 1 {
+			querys = append(querys, fmt.Sprintf("(%s)", strings.Join(find_in_set, " or ")))
 		}
 	}
-	start := (pageNum - 1) * myPageSize
-	findStr += " order by id desc"
+	findSql := fmt.Sprintf("select id,date,infoid,isvisit,matchkeys,type,isvip from pushsubscribe where %s order by id desc", strings.Join(querys, " and "))
 	if isLimit {
-		findStr += " limit " + fmt.Sprint(start) + "," + fmt.Sprint(myPageSize)
+		findSql += fmt.Sprintf(" limit %d,%d", (spqp.PageNum-1)*size, size)
 	}
-	findSQL = findSQL + findStr
-	// log.Println("findsql:", findSQL)
-	list := PushMysql.SelectBySql(findSQL)
-	if len(*list) > 0 {
+	log.Println(spqp.UserId, "subscribePush query sql:", findSql)
+	list := spqp.PushMysql.SelectBySql(findSql)
+	if list != nil && len(*list) > 0 {
 		pushCas := s.GetJyPushs(*list)
-		result = s.GetInfoByIds(Mgo_bidding, bidding, bidding_back, pushCas)
+		result = s.GetInfoByIds(spqp.Mgo_bidding, spqp.Bidding, spqp.Bidding_back, pushCas)
 	} else {
 		result = []*SubPushList{}
 	}