123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- package pusher
- import (
- "fmt"
- "strings"
- "time"
- "app.yhyue.com/moapp/jybase/mail"
- util "app.yhyue.com/moapp/jybase/common"
- . "app.yhyue.com/moapp/jybase/mongodb"
- . "bp.jydev.jianyu360.cn/pushpkg/db"
- . "bp.jydev.jianyu360.cn/pushpkg/p"
- "github.com/donnie4w/go-logger/logger"
- )
- var VarPush = &Push{}
- type Push struct {
- Mgo *MongodbSim
- DbName string
- CollName string
- }
- func (p *Push) AfterPush(ids []interface{}) {
- if err := MyMgo.DelBulk(p.Mgo, nil, p.DbName, p.CollName, &ids); err != nil {
- logger.Error("推送任务", ids, "删除出错", err)
- }
- }
- /*获取一批次推送的数据
- *batchIndex < 0 不走分批次加载
- */
- func (p *Push) GetPushDatas(taskType, batchIndex, pushBatch int, startId *string, unique string, query map[string]interface{}) (bool, *map[string]*PushInfo) {
- logger.Info(taskType, p.CollName, "开始加载第", batchIndex, "批用户", query)
- users := map[string]*PushInfo{}
- i := 0
- sess := p.Mgo.GetMgoConn()
- defer p.Mgo.DestoryMongoConn(sess)
- it := sess.DB(p.DbName).C(p.CollName).Find(query).Sort(unique).Iter()
- prevUnique := ""
- for temp := make(map[string]interface{}); it.Next(&temp); {
- thisUnique := fmt.Sprint(temp[unique])
- if batchIndex > 0 && i >= pushBatch && prevUnique != thisUnique {
- break
- }
- i++
- *startId = thisUnique
- pushInfo := users[thisUnique]
- if pushInfo == nil {
- pushInfo = &PushInfo{Info: temp}
- } else {
- oldList, _ := pushInfo.Info["list"].([]interface{})
- newList, _ := temp["list"].([]interface{})
- newList = append(newList, oldList...)
- pushInfo.Info["list"] = newList
- }
- pushInfo.Ids = append(pushInfo.Ids, temp["_id"])
- users[thisUnique] = pushInfo
- prevUnique = thisUnique
- temp = make(map[string]interface{})
- }
- logger.Info(taskType, p.CollName, "第", batchIndex, "批用户加载结束", *startId)
- return batchIndex < 0 || i < pushBatch, &users
- }
- //获取当前有效的推送模式
- func (p *Push) ValieRateModes(pushWeek string, pushDay int) []int {
- now := time.Now()
- rateMode := []int{}
- if strings.ToLower(now.Weekday().String()) == strings.ToLower(pushWeek) {
- rateMode = append(rateMode, 3)
- }
- if now.Day() == pushDay {
- rateMode = append(rateMode, 4)
- }
- return rateMode
- }
- //推送邮件
- func (p *Push) SendMail(isPushMail bool, mailSleep int, gmails []*mail.GmailAuth, email, title, html string) bool {
- defer util.Catch()
- if !isPushMail || len(gmails) == 0 {
- return true
- }
- if mailSleep > 0 {
- time.Sleep(time.Duration(mailSleep) * time.Millisecond)
- }
- return mail.PollingMail(email, gmails, func(gmail *mail.GmailAuth) bool {
- return mail.GSendMail("剑鱼标讯", email, "", "", title, html, "", "", gmail)
- })
- }
|