task.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. c, info := elastic.GetWithCount("bidding", "", countQ, query)
  33. if c > 0 {
  34. Count = int(c)
  35. Info = (*info)[0]
  36. }
  37. }
  38. func taskInfo() {
  39. ch := make(chan bool, 3)
  40. wg := &sync.WaitGroup{}
  41. q := map[string]interface{}{
  42. "i_appid": 2,
  43. "s_m_openid": map[string]int{"$exists": 1},
  44. "i_ispush": map[string]int{"$ne": 0},
  45. "s_phone": map[string]int{"$exists": 0},
  46. "s_m_phone": map[string]int{"$exists": 0}}
  47. f := map[string]interface{}{"s_m_openid": 1}
  48. it := config.MgoJy.GetMgoConn().DB(config.Config.MongoDb.JianYu.DbName).C("user").Find(q).Select(f).Iter()
  49. logger.Info("查询数据量:", config.MgoJy.Count("user", q))
  50. count := 0
  51. for tmp := make(map[string]interface{}); it.Next(tmp); count++ {
  52. if count%2000 == 0 {
  53. logger.Info("current ---", count)
  54. }
  55. ch <- true
  56. wg.Add(1)
  57. go func(tmp map[string]interface{}) {
  58. defer func() {
  59. <-ch
  60. wg.Done()
  61. }()
  62. sendWeixin(tmp)
  63. }(tmp)
  64. tmp = make(map[string]interface{})
  65. }
  66. wg.Wait()
  67. Count = 0
  68. Info = make(map[string]interface{})
  69. logger.Info("轮次结束 ---", count)
  70. }
  71. // 微信远程调用,实现模板发送消息
  72. func sendWeixin(user map[string]interface{}) {
  73. config.TaskConfig.Pici = util.Int64All(Info["pici"])
  74. util.WriteSysConfig("./task.json", &config.TaskConfig)
  75. pb := util.Int64All(Info["publishtime"])
  76. pname := util.ObjToString(Info["projectname"])
  77. buyer := util.ObjToString(Info["buyer"])
  78. tmplData := map[string]*qrpc.TmplItem{
  79. "thing1": { //
  80. Value: strings.ReplaceAll(pname, buyer, ""),
  81. Color: config.Config.WxTplMsg.KeyWord1.Color,
  82. },
  83. "thing6": {
  84. Value: buyer,
  85. Color: config.Config.WxTplMsg.KeyWord2.Color,
  86. },
  87. "character_string4": {
  88. Value: fmt.Sprint(Count),
  89. Color: config.Config.WxTplMsg.KeyWord3.Color,
  90. },
  91. "time3": {
  92. Value: date.FormatDateByInt64(&pb, date.Date_Full_Layout),
  93. Color: config.Config.WxTplMsg.KeyWord4.Color,
  94. },
  95. }
  96. wxTmplMsg := &qrpc.WxTmplMsg{
  97. OpenId: util.ObjToString(user["s_m_openid"]),
  98. TplId: config.Config.WxTplMsg.Id,
  99. TmplData: tmplData,
  100. Url: fmt.Sprintf(config.Config.WxDomain+config.Config.WxTplMsg.Url, encrypt.CommonEncodeArticle("content", util.ObjToString(Info["id"]))),
  101. }
  102. _, err := qrpc.WxSendTmplMsg(config.Config.WxRpcServer, wxTmplMsg)
  103. if err != nil {
  104. logger.Info("wx模版消息发送失败", err)
  105. }
  106. }