task.go 3.8 KB

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