task.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package task
  2. import (
  3. util "app.yhyue.com/moapp/jybase/common"
  4. "app.yhyue.com/moapp/jybase/date"
  5. "app.yhyue.com/moapp/jybase/encrypt"
  6. elastic "app.yhyue.com/moapp/jybase/es"
  7. "app.yhyue.com/moapp/jybase/logger"
  8. qrpc "app.yhyue.com/moapp/jybase/rpc"
  9. "fmt"
  10. "pushUnbinding/config"
  11. "strings"
  12. "sync"
  13. "time"
  14. )
  15. type TaskInfo struct{}
  16. var (
  17. Count = 0
  18. Info = make(map[string]interface{})
  19. )
  20. func (t *TaskInfo) Run() {
  21. logger.Info("开始TaskInfo")
  22. getBidInfo() //获取最新标讯信息
  23. if Count == 0 {
  24. logger.Info("未获取到最新标讯信息")
  25. return
  26. }
  27. taskInfo()
  28. }
  29. func getBidInfo() {
  30. query := `{"query": {"bool": {"must": [{"range": {"pici": {"gte": ` + fmt.Sprint(config.TaskConfig.Pici) + `,"lt": ` + fmt.Sprint(time.Now().Unix()) + `}}},{"bool": {"should": [{"terms": {"toptype": ["预告","招标","结果","其它"]}}],"minimum_should_match": 1}}]}},"_source": ["id","title","projectname","publishtime","buyer","area","pici"],"sort": [{"publishtime": "desc"}],"size": 1}`
  31. countQ := `{"query": {"bool": {"must": [{"range": {"pici": {"gte": ` + util.ObjToString(config.TaskConfig.Pici) + `,"lt": ` + util.ObjToString(time.Now().Unix()) + `}}},{"bool": {"should": [{"terms": {"toptype": ["预告","招标","结果","其它"]}}],"minimum_should_match": 1}}]}}}`
  32. logger.Info("countQ: ", countQ)
  33. c, info := elastic.GetWithCount("bidding", "", countQ, query)
  34. if c > 0 {
  35. Count = int(c)
  36. Info = (*info)[0]
  37. }
  38. }
  39. func taskInfo() {
  40. ch := make(chan bool, 3)
  41. wg := &sync.WaitGroup{}
  42. q := map[string]interface{}{
  43. "i_appid": 2,
  44. "s_m_openid": map[string]int{"$exists": 1},
  45. "i_ispush": map[string]int{"$ne": 0},
  46. "s_phone": map[string]int{"$exists": 0},
  47. "s_m_phone": map[string]int{"$exists": 0}}
  48. f := map[string]interface{}{"s_m_openid": 1}
  49. it := config.MgoJy.GetMgoConn().DB(config.Config.MongoDb.JianYu.DbName).C("user").Find(q).Select(f).Iter()
  50. logger.Info("查询数据量:", config.MgoJy.Count("user", q))
  51. count := 0
  52. for tmp := make(map[string]interface{}); it.Next(tmp); count++ {
  53. if count%2000 == 0 {
  54. logger.Info("current ---", count)
  55. }
  56. ch <- true
  57. wg.Add(1)
  58. go func(tmp map[string]interface{}) {
  59. defer func() {
  60. <-ch
  61. wg.Done()
  62. }()
  63. sendWeixin(tmp)
  64. }(tmp)
  65. tmp = make(map[string]interface{})
  66. }
  67. wg.Wait()
  68. Count = 0
  69. Info = make(map[string]interface{})
  70. logger.Info("轮次结束 ---", count)
  71. }
  72. // 微信远程调用,实现模板发送消息
  73. func sendWeixin(user map[string]interface{}) {
  74. config.TaskConfig.Pici = util.Int64All(Info["pici"])
  75. util.WriteSysConfig("./task.json", &config.TaskConfig)
  76. pb := util.Int64All(Info["publishtime"])
  77. pname := util.ObjToString(Info["projectname"])
  78. buyer := util.ObjToString(Info["buyer"])
  79. tmplData := map[string]*qrpc.TmplItem{
  80. "thing1": { //
  81. Value: strings.ReplaceAll(pname, buyer, ""),
  82. Color: config.Config.WxTplMsg.KeyWord1.Color,
  83. },
  84. "thing6": {
  85. Value: buyer,
  86. Color: config.Config.WxTplMsg.KeyWord2.Color,
  87. },
  88. "character_string4": {
  89. Value: fmt.Sprint(Count),
  90. Color: config.Config.WxTplMsg.KeyWord3.Color,
  91. },
  92. "time3": {
  93. Value: date.FormatDateByInt64(&pb, date.Date_Full_Layout),
  94. Color: config.Config.WxTplMsg.KeyWord4.Color,
  95. },
  96. }
  97. wxTmplMsg := &qrpc.WxTmplMsg{
  98. OpenId: util.ObjToString(user["s_m_openid"]),
  99. TplId: config.Config.WxTplMsg.Id,
  100. TmplData: tmplData,
  101. Url: fmt.Sprintf(config.Config.WxDomain+config.Config.WxTplMsg.Url, encrypt.CommonEncodeArticle("content", util.ObjToString(Info["id"]))),
  102. }
  103. _, err := qrpc.WxSendTmplMsg(config.Config.WxRpcServer, wxTmplMsg)
  104. if err != nil {
  105. logger.Info("wx模版消息发送失败", err)
  106. }
  107. }