|
@@ -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{}
|
|
|
}
|