task.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. package common
  2. import (
  3. "app.yhyue.com/moapp/MessageCenter/entity"
  4. "app.yhyue.com/moapp/MessageCenter/rpc/internal/config"
  5. "app.yhyue.com/moapp/jybase/common"
  6. "app.yhyue.com/moapp/jybase/encrypt"
  7. "context"
  8. "fmt"
  9. "github.com/robfig/cron/v3"
  10. "log"
  11. "strings"
  12. "time"
  13. )
  14. var GlobMsgMap map[int]map[string]interface{}
  15. func LoadTask() {
  16. // 每隔10分钟执行一次
  17. LoadMsgOnTime()
  18. c := cron.New(cron.WithSeconds())
  19. c.AddFunc(config.ConfigJson.GlobMsgLoadTime, LoadMsgOnTime)
  20. c.AddFunc(config.ConfigJson.FreeIntelTime, FreeIntelUserPush) //免费用户推送
  21. c.AddFunc(config.ConfigJson.PayIntelTime, PayIntelUserPush) //付费用户推送
  22. go c.Start()
  23. defer c.Stop()
  24. }
  25. func LoadMsgOnTime() {
  26. fmt.Println("开始执行")
  27. msgMap := make(map[int]map[string]interface{})
  28. m := entity.Mysql.SelectBySql("SELECT id,msg_type,title,content,send_time,menu_name,link,group_id,sign FROM message_send_log WHERE send_status = 4 AND isdel = 1 ORDER BY send_time DESC limit 2000")
  29. if m != nil && len(*m) > 0 {
  30. for _, val := range *m {
  31. msgMap[common.IntAll(val["id"])] = val
  32. }
  33. GlobMsgMap = msgMap
  34. }
  35. fmt.Println("GlobMsgMap len", len(GlobMsgMap))
  36. }
  37. func FreeIntelUserPush() {
  38. data := messageData(config.ConfigJson.FreePushNumber)
  39. if data == nil || len(*data) == 0 {
  40. return
  41. }
  42. users := IntelUser(true)
  43. log.Println(len(users), data)
  44. PushData(users, data)
  45. }
  46. func PayIntelUserPush() {
  47. data := messageData(config.ConfigJson.PayPushNumber)
  48. if data == nil || len(*data) == 0 {
  49. return
  50. }
  51. users := IntelUser(false)
  52. log.Println(len(users), data)
  53. PushData(users, data)
  54. }
  55. func PushData(users []string, data *[]map[string]interface{}) {
  56. log.Printf("需推送用户:%d\n", len(users))
  57. var ids []int64
  58. if data != nil {
  59. for _, m := range *data {
  60. _id := common.InterfaceToStr(m["_id"])
  61. var link []string
  62. link = append(link, fmt.Sprintf("/swordfish/page_big_pc/business_detail/%s", encrypt.EncodeArticleId2ByCheck(_id)))
  63. mobLink := fmt.Sprintf("/jy_mobile/business/detail/%s", encrypt.EncodeArticleId2ByCheck(_id))
  64. link = append(link, mobLink)
  65. link = append(link, mobLink)
  66. link = append(link, mobLink)
  67. iData := map[string]interface{}{
  68. "msg_type": 9,
  69. "title": "您有一条专属商机情报",
  70. "content": fmt.Sprintf("【商机情报】%s", common.ObjToString(m["title"])),
  71. "send_mode": 1,
  72. "send_time": time.Now().Format("2006-01-02 15:04:05"),
  73. "send_status": 4,
  74. "update_time": time.Now().Format("2006-01-02 15:04:05"),
  75. "createtime": time.Now().Format("2006-01-02 15:04:05"),
  76. "link": strings.Join(link, ","),
  77. "isdel": 1,
  78. "send_userid": "商机情报定时推送",
  79. "sign": 0,
  80. "group_id": 5,
  81. }
  82. id := entity.Mysql.Insert("message_send_log", iData)
  83. ids = append(ids, id)
  84. }
  85. }
  86. if len(ids) > 0 {
  87. var bits []string
  88. for _, i2 := range ids {
  89. bits = append(bits, fmt.Sprintf("toUInt64(%d)", i2))
  90. }
  91. err := entity.ClickhouseConn.Exec(context.Background(), fmt.Sprintf(`alter table message_summary UPDATE msg_bitmap = bitmapOr(msg_bitmap,bitmapBuild([%s])) where group_id = %d`, strings.Join(bits, ","), 5))
  92. if err != nil {
  93. log.Println("message_summary err=== ", err.Error())
  94. return
  95. }
  96. var userIds []string
  97. for k, user := range users {
  98. userIds = append(userIds, user)
  99. if len(userIds) == 1000 || k == len(users)-1 {
  100. UpdateBatch(userIds, ids)
  101. userIds = []string{}
  102. }
  103. }
  104. }
  105. }
  106. // 用户
  107. func IntelUser(isFree bool) []string {
  108. mUser := make(map[string]bool)
  109. data := entity.Mysql.SelectBySql(`SELECT a.phone as phone FROM entniche_user a INNER JOIN entniche_info b on b.status = 1 and a.power = 1 and a.ent_id = b.id and a.phone != ''`)
  110. if data != nil { //统计商机管理用户
  111. for _, m := range *data {
  112. mUser[common.InterfaceToStr(m["phone"])] = true
  113. }
  114. }
  115. var uData []string
  116. switch isFree {
  117. case false:
  118. user, ok := entity.MQFW.Find("user", map[string]interface{}{
  119. "i_appid": 2,
  120. "$or": []map[string]interface{}{
  121. {
  122. "i_vip_status": map[string]interface{}{"$gt": 0},
  123. },
  124. {
  125. "i_member_status": map[string]interface{}{"$gt": 0},
  126. },
  127. },
  128. }, "", `{"_id":1,"s_phone":1,"s_m_phone":1}`, false, -1, -1)
  129. if ok && user != nil && len(*user) > 0 {
  130. for _, m := range *user {
  131. phone := common.If(common.InterfaceToStr(m["s_phone"]) == "", common.InterfaceToStr(m["s_m_phone"]), common.InterfaceToStr(m["s_phone"])).(string)
  132. uData = append(uData, common.InterfaceToStr(m["_id"]))
  133. if mUser[phone] { //获取剩余商机管理用户
  134. delete(mUser, phone)
  135. }
  136. }
  137. if len(mUser) > 0 { //统计剩余商机管理用户
  138. var (
  139. phones []string
  140. count int
  141. )
  142. for phone := range mUser {
  143. count++
  144. phones = append(phones, phone)
  145. if len(phones) == 100 || count == len(mUser) {
  146. user1, ok1 := entity.MQFW.Find("user", map[string]interface{}{
  147. "i_appid": 2,
  148. "$or": []map[string]interface{}{
  149. {
  150. "s_phone": map[string]interface{}{
  151. "$in": phones,
  152. },
  153. },
  154. {
  155. "s_m_phone": map[string]interface{}{
  156. "$in": phones,
  157. },
  158. },
  159. },
  160. }, "", `{"_id":1}`, false, -1, -1)
  161. if ok1 && user1 != nil && len(*user1) > 0 {
  162. for _, m := range *user1 {
  163. uData = append(uData, common.InterfaceToStr(m["_id"]))
  164. }
  165. }
  166. phones = []string{}
  167. }
  168. }
  169. }
  170. }
  171. case true:
  172. sess := entity.MQFW.GetMgoConn()
  173. defer entity.MQFW.DestoryMongoConn(sess)
  174. iter := sess.DB("qfw").C("user").Find(map[string]interface{}{
  175. "i_appid": 2,
  176. }).Select(map[string]interface{}{"i_vip_status": 1, "i_member_status": 1, "_id": 1, "s_phone": 1, "s_m_phone": 1}).Iter()
  177. for m := make(map[string]interface{}); iter.Next(&m); {
  178. if common.IntAll(m["i_vip_status"]) <= 0 && common.IntAll(m["i_member_status"]) <= 0 {
  179. phone := common.If(common.InterfaceToStr(m["s_phone"]) == "", common.InterfaceToStr(m["s_m_phone"]), common.InterfaceToStr(m["s_phone"])).(string)
  180. if !mUser[phone] {
  181. uData = append(uData, common.InterfaceToStr(m["_id"]))
  182. }
  183. }
  184. m = map[string]interface{}{}
  185. }
  186. }
  187. return uData
  188. }
  189. // 获取推送消息
  190. func messageData(number int) *[]map[string]interface{} {
  191. now := time.Now().AddDate(0, 0, -1)
  192. starttime := fmt.Sprint(time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).Unix())
  193. enttime := fmt.Sprint(time.Date(now.Year(), now.Month(), now.Day(), 24, 0, 0, 0, time.Local).Unix())
  194. query := map[string]interface{}{
  195. "yucetime": map[string]interface{}{"$gt": starttime, "$lt": enttime},
  196. }
  197. data, _ := entity.Bidding.Find("project_forecast", query, map[string]interface{}{"yucetime": -1}, `{"title":1,"_id":1}`, false, 0, number)
  198. return data
  199. }
  200. func UpdateBatch(ids []string, msgLogId []int64) {
  201. str := fmt.Sprintf(`'%s'`, strings.Join(ids, `','`))
  202. var bits []string
  203. for _, i2 := range msgLogId {
  204. bits = append(bits, fmt.Sprintf("toUInt64(%d)", i2))
  205. }
  206. log.Println(fmt.Sprintf(`alter table message_user_summary UPDATE allMsg = bitmapOr(allMsg,bitmapBuild([%s])) where userId in (%s)`, strings.Join(bits, ","), str))
  207. err1 := entity.ClickhouseConn.Exec(context.Background(), fmt.Sprintf(`alter table message_user_summary UPDATE allMsg = bitmapOr(allMsg,bitmapBuild([%s])) where userId in (%s)`, strings.Join(bits, ","), str))
  208. if err1 != nil {
  209. log.Printf("批量更新message_user_summary出错:%s", err1)
  210. return
  211. }
  212. }