task.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. fmt.Println(len(StatisticalUser(false)))
  18. // 每隔10分钟执行一次
  19. LoadMsgOnTime()
  20. c := cron.New(cron.WithSeconds())
  21. c.AddFunc(config.ConfigJson.GlobMsgLoadTime, LoadMsgOnTime)
  22. c.AddFunc(config.ConfigJson.FreeIntelTime, FreeIntelUserPush) //免费用户推送
  23. c.AddFunc(config.ConfigJson.PayIntelTime, PayIntelUserPush) //付费用户推送
  24. go c.Start()
  25. defer c.Stop()
  26. }
  27. func LoadMsgOnTime() {
  28. fmt.Println("开始执行")
  29. msgMap := make(map[int]map[string]interface{})
  30. 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")
  31. if m != nil && len(*m) > 0 {
  32. for _, val := range *m {
  33. msgMap[common.IntAll(val["id"])] = val
  34. }
  35. GlobMsgMap = msgMap
  36. }
  37. fmt.Println("GlobMsgMap len", len(GlobMsgMap))
  38. }
  39. func FreeIntelUserPush() {
  40. data := messageData(config.ConfigJson.FreePushNumber)
  41. if data == nil || len(*data) == 0 {
  42. return
  43. }
  44. users := StatisticalUser(true)
  45. PushData(users, data)
  46. }
  47. func PayIntelUserPush() {
  48. data := messageData(config.ConfigJson.PayPushNumber)
  49. if data == nil || len(*data) == 0 {
  50. return
  51. }
  52. users := StatisticalUser(false)
  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 StatisticalUser(isFree bool) []string {
  108. var ids []string
  109. mUser := make(map[string]bool) //商机管理用户
  110. 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 != ''`)
  111. if data != nil && len(*data) > 0 { //统计商机管理用户
  112. for _, m := range *data {
  113. if common.InterfaceToStr(m["phone"]) != "" {
  114. mUser[common.InterfaceToStr(m["phone"])] = true
  115. }
  116. }
  117. }
  118. log.Printf("获取商机管理用户:%d\n", len(mUser))
  119. //企业大会员或超级订阅
  120. entPayUser, ok := entity.MQFW.Find("ent_user", map[string]interface{}{
  121. "$or": []map[string]interface{}{
  122. { //个人订阅
  123. "i_vip_status": map[string]interface{}{
  124. "$gt": 0,
  125. },
  126. },
  127. { //个人大会员
  128. "i_member_status": map[string]interface{}{
  129. "$gt": 0,
  130. },
  131. },
  132. },
  133. }, nil, "", false, -1, -1)
  134. if ok && entPayUser != nil && len(*entPayUser) > 0 {
  135. var userIds []string
  136. for k, m := range *entPayUser {
  137. userIds = append(userIds, common.InterfaceToStr(m["i_userid"]))
  138. if len(userIds) == 100 || k == len(*entPayUser)-1 {
  139. idSql := fmt.Sprintf(`SELECT phone FROM entniche_user WHERE id in (%s)`, strings.Join(userIds, `,`))
  140. phoneArr := entity.Mysql.SelectBySql(idSql)
  141. if phoneArr != nil && len(*phoneArr) > 0 {
  142. for _, m2 := range *phoneArr {
  143. if common.InterfaceToStr(m2["phone"]) != "" {
  144. mUser[common.InterfaceToStr(m2["phone"])] = true
  145. }
  146. }
  147. }
  148. userIds = []string{}
  149. }
  150. }
  151. }
  152. log.Printf("获取商机管理+企业付费用户:%d\n", len(mUser))
  153. switch isFree {
  154. case false: //付费用户
  155. payMap := make(map[string]bool)
  156. payUsers, _ := entity.MQFW.Find("user", map[string]interface{}{
  157. "$or": []map[string]interface{}{
  158. { //个人订阅
  159. "i_vip_status": map[string]interface{}{
  160. "$gt": 0,
  161. },
  162. },
  163. { //个人大会员
  164. "i_member_status": map[string]interface{}{
  165. "$gt": 0,
  166. },
  167. },
  168. },
  169. }, nil, `{"_id":1,"s_phone":1,"s_m_phone":1}`, false, -1, -1)
  170. if payUsers != nil && len(*payUsers) > 0 {
  171. for _, m := range *payUsers {
  172. phone := common.If(common.InterfaceToStr(m["s_phone"]) == "", common.InterfaceToStr(m["s_m_phone"]), common.InterfaceToStr(m["s_phone"])).(string)
  173. payMap[common.InterfaceToStr(m["_id"])] = true
  174. if mUser[phone] { //避免重复获取
  175. delete(mUser, phone)
  176. }
  177. }
  178. }
  179. var (
  180. phones []string
  181. count int
  182. )
  183. maxCount := len(mUser)
  184. for phone := range mUser { //获取剩余商机管理与企业付费用户
  185. count++
  186. phones = append(phones, phone)
  187. if len(phones) == 100 || count == maxCount {
  188. entPayUsers, _ := entity.MQFW.Find("user", map[string]interface{}{
  189. "$or": []map[string]interface{}{
  190. {"s_phone": map[string]interface{}{ //企业订阅||大会员||商机管理
  191. "$in": phones,
  192. }},
  193. {"s_m_phone": map[string]interface{}{ //企业订阅||大会员||商机管理
  194. "$in": phones,
  195. }},
  196. },
  197. }, nil, `{"_id":1}`, false, -1, -1)
  198. if entPayUsers != nil && len(*entPayUsers) > 0 {
  199. for _, m := range *entPayUsers {
  200. payMap[common.InterfaceToStr(m["_id"])] = true
  201. }
  202. }
  203. phones = []string{}
  204. }
  205. }
  206. for id := range payMap {
  207. ids = append(ids, id)
  208. }
  209. case true:
  210. sess := entity.MQFW.GetMgoConn()
  211. defer entity.MQFW.DestoryMongoConn(sess)
  212. iter := sess.DB("qfw").C("user").Find(map[string]interface{}{
  213. "i_appid": 2,
  214. }).Select(map[string]interface{}{"_id": 1, "i_vip_status": 1, "i_member_status": 1, "s_phone": 1, "s_m_phone": 1}).Iter()
  215. for m := make(map[string]interface{}); iter.Next(&m); {
  216. if common.IntAll(m["i_vip_status"]) <= 0 && common.IntAll(m["i_member_status"]) <= 0 {
  217. phone := common.If(common.InterfaceToStr(m["s_phone"]) == "", common.InterfaceToStr(m["s_m_phone"]), common.InterfaceToStr(m["s_phone"])).(string)
  218. if !mUser[phone] {
  219. ids = append(ids, common.InterfaceToStr(m["_id"]))
  220. }
  221. }
  222. m = map[string]interface{}{}
  223. }
  224. }
  225. log.Printf("用户类型:%v,用户数:%d", isFree, len(ids))
  226. return ids
  227. }
  228. // 获取推送消息
  229. func messageData(number int) *[]map[string]interface{} {
  230. now := time.Now().AddDate(0, 0, -1)
  231. starttime := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).Unix()
  232. enttime := time.Date(now.Year(), now.Month(), now.Day(), 24, 0, 0, 0, time.Local).Unix()
  233. query := map[string]interface{}{
  234. "yucetime": map[string]interface{}{"$gt": starttime, "$lt": enttime},
  235. }
  236. data, _ := entity.Bidding.Find("project_forecast", query, `{"yucetime": -1}`, `{"title":1,"_id":1}`, false, 0, number)
  237. return data
  238. }
  239. func UpdateBatch(ids []string, msgLogId []int64) {
  240. str := fmt.Sprintf(`'%s'`, strings.Join(ids, `','`))
  241. var bits []string
  242. for _, i2 := range msgLogId {
  243. bits = append(bits, fmt.Sprintf("toUInt64(%d)", i2))
  244. }
  245. log.Println(fmt.Sprintf(`alter table message_user_summary UPDATE allMsg = bitmapOr(allMsg,bitmapBuild([%s])) where userId in (%s)`, strings.Join(bits, ","), str))
  246. 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))
  247. if err1 != nil {
  248. log.Printf("批量更新message_user_summary出错:%s", err1)
  249. return
  250. }
  251. for _, id := range ids {
  252. keyString := fmt.Sprintf(MsgCountKey, id, 5)
  253. redis.Del(redisModule, keyString)
  254. }
  255. }