task.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. "app.yhyue.com/moapp/jybase/redis"
  8. "context"
  9. "fmt"
  10. "github.com/robfig/cron/v3"
  11. "log"
  12. "strings"
  13. "time"
  14. )
  15. var GlobMsgMap map[int]map[string]interface{}
  16. func LoadTask() {
  17. // 每隔10分钟执行一次
  18. LoadMsgOnTime()
  19. c := cron.New(cron.WithSeconds())
  20. c.AddFunc(config.ConfigJson.GlobMsgLoadTime, LoadMsgOnTime)
  21. c.AddFunc(config.ConfigJson.FreeIntelTime, FreeIntelUserPush) //免费用户推送
  22. c.AddFunc(config.ConfigJson.PayIntelTime, PayIntelUserPush) //付费用户推送
  23. go c.Start()
  24. defer c.Stop()
  25. }
  26. func LoadMsgOnTime() {
  27. fmt.Println("开始执行")
  28. msgMap := make(map[int]map[string]interface{})
  29. 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")
  30. if m != nil && len(*m) > 0 {
  31. for _, val := range *m {
  32. msgMap[common.IntAll(val["id"])] = val
  33. }
  34. GlobMsgMap = msgMap
  35. }
  36. fmt.Println("GlobMsgMap len", len(GlobMsgMap))
  37. }
  38. func FreeIntelUserPush() {
  39. data := messageData(config.ConfigJson.FreePushNumber)
  40. if data == nil || len(*data) == 0 {
  41. return
  42. }
  43. users := StatisticalUser(true)
  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 := StatisticalUser(false)
  52. PushData(users, data)
  53. }
  54. func PushData(users []string, data *[]map[string]interface{}) {
  55. log.Printf("需推送用户:%d\n", len(users))
  56. var ids []int64
  57. if data != nil {
  58. for _, m := range *data {
  59. _id := common.InterfaceToStr(m["_id"])
  60. var link []string
  61. link = append(link, fmt.Sprintf("/swordfish/page_big_pc/business_detail/%s", encrypt.EncodeArticleId2ByCheck(_id)))
  62. mobLink := fmt.Sprintf("/jy_mobile/business/detail/%s", encrypt.EncodeArticleId2ByCheck(_id))
  63. link = append(link, mobLink)
  64. link = append(link, mobLink)
  65. link = append(link, mobLink)
  66. iData := map[string]interface{}{
  67. "msg_type": 9,
  68. "title": "您有一条专属商机情报",
  69. "content": fmt.Sprintf("【商机情报】%s", common.ObjToString(m["title"])),
  70. "send_mode": 1,
  71. "send_time": time.Now().Format("2006-01-02 15:04:05"),
  72. "send_status": 4,
  73. "update_time": time.Now().Format("2006-01-02 15:04:05"),
  74. "createtime": time.Now().Format("2006-01-02 15:04:05"),
  75. "link": strings.Join(link, ","),
  76. "isdel": 1,
  77. "send_userid": "商机情报定时推送",
  78. "sign": 0,
  79. "group_id": 5,
  80. }
  81. id := entity.Mysql.Insert("message_send_log", iData)
  82. ids = append(ids, id)
  83. }
  84. }
  85. if len(ids) > 0 {
  86. var bits []string
  87. for _, i2 := range ids {
  88. bits = append(bits, fmt.Sprintf("toUInt64(%d)", i2))
  89. }
  90. 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))
  91. if err != nil {
  92. log.Println("message_summary err=== ", err.Error())
  93. return
  94. }
  95. var userIds []string
  96. for k, user := range users {
  97. userIds = append(userIds, user)
  98. if len(userIds) == 1000 || k == len(users)-1 {
  99. UpdateBatch(userIds, ids)
  100. userIds = []string{}
  101. }
  102. }
  103. }
  104. }
  105. // 用户
  106. func StatisticalUser(isFree bool) []string {
  107. var ids []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 && len(*data) > 0 { //统计商机管理用户
  111. for _, m := range *data {
  112. if common.InterfaceToStr(m["phone"]) != "" {
  113. mUser[common.InterfaceToStr(m["phone"])] = true
  114. }
  115. }
  116. }
  117. log.Printf("获取商机管理用户:%d\n", len(mUser))
  118. //企业大会员或超级订阅
  119. entPayUser, ok := entity.MQFW.Find("ent_user", map[string]interface{}{
  120. "$or": []map[string]interface{}{
  121. { //个人订阅
  122. "i_vip_status": map[string]interface{}{
  123. "$gt": 0,
  124. },
  125. },
  126. { //个人大会员
  127. "i_member_status": map[string]interface{}{
  128. "$gt": 0,
  129. },
  130. },
  131. },
  132. }, nil, `{"i_userid":1}`, false, -1, -1)
  133. if ok && entPayUser != nil && len(*entPayUser) > 0 {
  134. var userIds []string
  135. for k, m := range *entPayUser {
  136. userIds = append(userIds, common.InterfaceToStr(m["i_userid"]))
  137. if len(userIds) == 100 || k == len(*entPayUser)-1 {
  138. idSql := fmt.Sprintf(`SELECT phone FROM entniche_user WHERE id in (%s)`, strings.Join(userIds, `,`))
  139. phoneArr := entity.Mysql.SelectBySql(idSql)
  140. if phoneArr != nil && len(*phoneArr) > 0 {
  141. for _, m2 := range *phoneArr {
  142. if common.InterfaceToStr(m2["phone"]) != "" {
  143. mUser[common.InterfaceToStr(m2["phone"])] = true
  144. }
  145. }
  146. }
  147. userIds = []string{}
  148. }
  149. }
  150. }
  151. log.Printf("获取商机管理+企业付费用户:%d\n", len(mUser))
  152. switch isFree {
  153. case false: //付费用户
  154. payMap := make(map[string]bool)
  155. payUsers, _ := entity.MQFW.Find("user", map[string]interface{}{
  156. "$or": []map[string]interface{}{
  157. { //个人订阅
  158. "i_vip_status": map[string]interface{}{
  159. "$gt": 0,
  160. },
  161. },
  162. { //个人大会员
  163. "i_member_status": map[string]interface{}{
  164. "$gt": 0,
  165. },
  166. },
  167. },
  168. }, nil, `{"_id":1,"s_phone":1,"s_m_phone":1}`, false, -1, -1)
  169. if payUsers != nil && len(*payUsers) > 0 {
  170. for _, m := range *payUsers {
  171. phone := common.If(common.InterfaceToStr(m["s_phone"]) == "", common.InterfaceToStr(m["s_m_phone"]), common.InterfaceToStr(m["s_phone"])).(string)
  172. payMap[common.InterfaceToStr(m["_id"])] = true
  173. if mUser[phone] { //避免重复获取
  174. delete(mUser, phone)
  175. }
  176. }
  177. }
  178. var (
  179. phones []string
  180. count int
  181. )
  182. maxCount := len(mUser)
  183. for phone := range mUser { //获取剩余商机管理与企业付费用户
  184. count++
  185. phones = append(phones, phone)
  186. if len(phones) == 100 || count == maxCount {
  187. entPayUsers, _ := entity.MQFW.Find("user", map[string]interface{}{
  188. "$or": []map[string]interface{}{
  189. {"s_phone": map[string]interface{}{ //企业订阅||大会员||商机管理
  190. "$in": phones,
  191. }},
  192. {"s_m_phone": map[string]interface{}{ //企业订阅||大会员||商机管理
  193. "$in": phones,
  194. }},
  195. },
  196. }, nil, `{"_id":1}`, false, -1, -1)
  197. if entPayUsers != nil && len(*entPayUsers) > 0 {
  198. for _, m := range *entPayUsers {
  199. payMap[common.InterfaceToStr(m["_id"])] = true
  200. }
  201. }
  202. phones = []string{}
  203. }
  204. }
  205. for id := range payMap {
  206. ids = append(ids, id)
  207. }
  208. case true:
  209. sess := entity.MQFW.GetMgoConn()
  210. defer entity.MQFW.DestoryMongoConn(sess)
  211. iter := sess.DB("qfw").C("user").Find(map[string]interface{}{
  212. "i_appid": 2,
  213. }).Select(map[string]interface{}{"_id": 1, "i_vip_status": 1, "i_member_status": 1, "s_phone": 1, "s_m_phone": 1}).Iter()
  214. for m := make(map[string]interface{}); iter.Next(&m); {
  215. if common.IntAll(m["i_vip_status"]) <= 0 && common.IntAll(m["i_member_status"]) <= 0 {
  216. phone := common.If(common.InterfaceToStr(m["s_phone"]) == "", common.InterfaceToStr(m["s_m_phone"]), common.InterfaceToStr(m["s_phone"])).(string)
  217. if !mUser[phone] {
  218. ids = append(ids, common.InterfaceToStr(m["_id"]))
  219. }
  220. }
  221. m = map[string]interface{}{}
  222. }
  223. }
  224. log.Printf("用户类型:%v,用户数:%d", isFree, len(ids))
  225. return ids
  226. }
  227. // 获取推送消息
  228. func messageData(number int) *[]map[string]interface{} {
  229. now := time.Now().AddDate(0, 0, -1)
  230. starttime := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).Unix()
  231. enttime := time.Date(now.Year(), now.Month(), now.Day(), 24, 0, 0, 0, time.Local).Unix()
  232. query := map[string]interface{}{
  233. "yucetime": map[string]interface{}{"$gt": starttime, "$lt": enttime},
  234. }
  235. data, _ := entity.Bidding.Find("project_forecast", query, `{"yucetime": -1}`, `{"title":1,"_id":1}`, false, 0, number)
  236. return data
  237. }
  238. func UpdateBatch(ids []string, msgLogId []int64) {
  239. str := fmt.Sprintf(`'%s'`, strings.Join(ids, `','`))
  240. var bits []string
  241. for _, i2 := range msgLogId {
  242. bits = append(bits, fmt.Sprintf("toUInt64(%d)", i2))
  243. }
  244. log.Println(fmt.Sprintf(`alter table message_user_summary UPDATE allMsg = bitmapOr(allMsg,bitmapBuild([%s])) where userId in (%s)`, strings.Join(bits, ","), str))
  245. 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))
  246. if err1 != nil {
  247. log.Printf("批量更新message_user_summary出错:%s", err1)
  248. return
  249. }
  250. for _, id := range ids {
  251. keyString := fmt.Sprintf(MsgCountKey, id, 5)
  252. redis.Del(redisModule, keyString)
  253. }
  254. }