newSendMsgService.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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/MessageCenter/rpc/type/message"
  6. "app.yhyue.com/moapp/jybase/redis"
  7. "context"
  8. "errors"
  9. "fmt"
  10. "github.com/zeromicro/go-zero/core/logx"
  11. "log"
  12. "strconv"
  13. "strings"
  14. "sync"
  15. "time"
  16. )
  17. var MsgGroupIdMap map[int]int
  18. func SetMsgSummary(newMsg, groupId, msgType int64) error {
  19. //更新所有消息
  20. if groupId == 11 {
  21. groupId = msgType
  22. //根据msgType更新待办二级分类汇总
  23. /*err1 := entity.ClickhouseConn.Exec(context.Background(), fmt.Sprintf(`alter table message_summary UPDATE msg_bitmap = bitmapOr(msg_bitmap,bitmapBuild([toUInt64(%d)])) where group_id = %d`, newMsg, msgType))
  24. if err1 != nil {
  25. //插入失败
  26. return err1
  27. }*/
  28. }
  29. err := entity.ClickhouseConn.Exec(context.Background(), fmt.Sprintf(`alter table message_summary UPDATE msg_bitmap = bitmapOr(msg_bitmap,bitmapBuild([toUInt64(%d)])) where group_id = %d`, newMsg, groupId))
  30. if err != nil {
  31. return err
  32. }
  33. return nil
  34. }
  35. func NewUserSendMsg(in *message.NewUserInsertMsgReq) error {
  36. userIdArr := strings.Split(in.UserIds, ",")
  37. //positionIdArr := strings.Split(in.PositionIds, ",")
  38. if len(userIdArr) == 0 {
  39. return errors.New("无效的用户id")
  40. }
  41. //wg := &sync.WaitGroup{}
  42. group_id := MsgGroupIdMap[int(in.MsgType)]
  43. for i := 0; i < len(userIdArr); i++ {
  44. if userIdArr[i] == "" {
  45. continue
  46. }
  47. //查询
  48. /*var positionId int64
  49. if len(positionIdArr) == len(userIdArr) {
  50. positionId = common.Int64All(positionIdArr[i])
  51. }*/
  52. row := entity.ClickhouseConn.QueryRow(context.Background(), fmt.Sprintf("SELECT COUNT(*) from message_user_summary where userId = '%s'", userIdArr[i]))
  53. var count uint64
  54. row.Scan(&count)
  55. if count > 0 { //存在则更新
  56. err1 := entity.ClickhouseConn.Exec(context.Background(), fmt.Sprintf(`alter table message_user_summary UPDATE allMsg = bitmapOr(allMsg,bitmapBuild([toUInt64(%d)])) where userId = '%s'`, in.MsgLogId, userIdArr[i]))
  57. if err1 != nil {
  58. log.Println("新用户update message_user_summary表出错,error", err1)
  59. return err1
  60. }
  61. } else {
  62. //新用户需要insert
  63. sql := "INSERT INTO message_user_summary values "
  64. sql += fmt.Sprintf(" ('%s',bitmapBuild([toUInt64(%d)]),bitmapBuild([toUInt64(0)])) ", userIdArr[i], int(in.MsgLogId))
  65. log.Println("sql", sql)
  66. err1 := entity.ClickhouseConn.Exec(context.Background(), sql)
  67. if err1 != nil {
  68. //插入失败
  69. log.Println("新用户insert message_user_summary表出错,error", err1)
  70. return err1
  71. }
  72. }
  73. //微信推送模板消息、app push
  74. pushData := WxTmplAndPush{
  75. MsgType: in.MsgType,
  76. Title: in.Title,
  77. Content: in.Content,
  78. WxPushUrl: in.WxPushUrl,
  79. AppPushUrl: in.AppPushUrl,
  80. ProductName: in.ProductName,
  81. OrderId: in.OrderId,
  82. OrderMoney: in.OrderMoney,
  83. Row4: in.Row4,
  84. }
  85. SentWxTmplAndAppPush(pushData, userIdArr[i], group_id, "", "")
  86. key := fmt.Sprintf(MsgCountKey, userIdArr[i], group_id)
  87. redis.Del(redisModule, key)
  88. if in.MsgType == 11 || in.MsgType == 12 {
  89. key1 := fmt.Sprintf(MsgClassCountKey, userIdArr[i], in.MsgType)
  90. redis.Del(redisModule, key1)
  91. }
  92. }
  93. //wg.Wait()
  94. return nil
  95. }
  96. func UpdateUserMsgSummary(in *message.MultipleSaveMsgReq) error {
  97. userIdArr := strings.Split(in.UserIds, ",")
  98. //positionIdArr := strings.Split(in.PositionIds, ",")
  99. if len(userIdArr) == 0 {
  100. return errors.New("无效的用户id")
  101. }
  102. wg := &sync.WaitGroup{}
  103. group_id := MsgGroupIdMap[int(in.MsgType)]
  104. str := ""
  105. i := 0
  106. for _, v := range userIdArr {
  107. i++
  108. if i != 1 {
  109. str += ","
  110. }
  111. str += "'" + v + "'"
  112. if i == 1000 {
  113. go Update(str, in.MsgLogId)
  114. str = ""
  115. i = 0
  116. }
  117. }
  118. if i > 0 {
  119. go Update(str, in.MsgLogId)
  120. }
  121. //p459 特殊处理 传过来的消息内容格式为 消息内容#jy#微信模板项目名称#jy#服务地址
  122. equityName, equityAddr := "", ""
  123. if in.MsgType == config.ConfigJson.EquityInfoMsgType {
  124. equityRs := strings.Split(in.Content, "#jy#")
  125. if len(equityRs) != 3 {
  126. log.Println("消息内容格式有误:", in.Content)
  127. return errors.New("无效的消息内容格式")
  128. }
  129. in.Content = equityRs[0]
  130. equityName = equityRs[1]
  131. equityAddr = equityRs[2]
  132. }
  133. for i := 0; i < len(userIdArr); i++ {
  134. if userIdArr[i] == "" {
  135. continue
  136. }
  137. //查询
  138. wg.Add(1)
  139. entity.SaveConcurrencyChan <- 1
  140. /*var positionId int64
  141. if len(positionIdArr) == len(userIdArr) {
  142. positionId = common.Int64All(positionIdArr[i])
  143. }*/
  144. go func(v string) {
  145. defer func() {
  146. <-entity.SaveConcurrencyChan
  147. wg.Done()
  148. }()
  149. //微信推送模板消息、app push
  150. pushData := WxTmplAndPush{
  151. MsgType: in.MsgType,
  152. Title: in.Title,
  153. Content: in.Content,
  154. WxPushUrl: in.WxPushUrl,
  155. AppPushUrl: in.AppPushUrl,
  156. ProductName: in.ProductName,
  157. OrderId: in.OrderId,
  158. OrderMoney: in.OrderMoney,
  159. Row4: in.Row4,
  160. }
  161. SentWxTmplAndAppPush(pushData, v, group_id, equityName, equityAddr)
  162. key := fmt.Sprintf(MsgCountKey, v, group_id)
  163. redis.Del(redisModule, key)
  164. if in.MsgType == 11 || in.MsgType == 12 {
  165. key1 := fmt.Sprintf(MsgClassCountKey, v, in.MsgType)
  166. redis.Del(redisModule, key1)
  167. }
  168. }(userIdArr[i])
  169. }
  170. wg.Wait()
  171. return nil
  172. }
  173. func Update(str string, msgLogId int64) {
  174. log.Println(fmt.Sprintf(`alter table message_user_summary UPDATE allMsg = bitmapOr(allMsg,bitmapBuild([toUInt64(%d)])) where userId in (%s)`, msgLogId, str))
  175. err1 := entity.ClickhouseConn.Exec(context.Background(), fmt.Sprintf(`alter table message_user_summary UPDATE allMsg = bitmapOr(allMsg,bitmapBuild([toUInt64(%d)])) where userId in (%s)`, msgLogId, str))
  176. if err1 != nil {
  177. log.Printf("批量更新message_user_summary出错:%s", err1)
  178. return
  179. }
  180. }
  181. //附件下载、剑鱼币活动到期提醒存消息发送记录
  182. func InsertMsgSendLog(in *message.MultipleSaveMsgReq) int64 {
  183. groupId := MsgGroupIdMap[int(in.MsgType)]
  184. id := entity.Mysql.Insert("message_send_log", map[string]interface{}{
  185. "send_usergroup_id": "",
  186. "send_usergroup_name": "",
  187. "msg_type": in.MsgType,
  188. "title": in.Title,
  189. "content": in.Content,
  190. "send_mode": 2,
  191. "send_time": time.Now().Format("2006-01-02 15:04:05"),
  192. "send_status": 4,
  193. "update_time": time.Now().Format("2006-01-02 15:04:05"),
  194. "link": in.Link,
  195. "isdel": 1,
  196. "send_userid": "",
  197. "update_user": "",
  198. "Sign": 5,
  199. "menu_name": "message",
  200. "group_id": groupId,
  201. })
  202. if id > 0 {
  203. //更新消息汇总表
  204. err := SetMsgSummary(id, int64(groupId), in.MsgType)
  205. if err != nil {
  206. log.Println("更新消息汇总表出错:", err)
  207. return 0
  208. }
  209. return id
  210. }
  211. return 0
  212. }
  213. func ConvertToBitmap(num int) (res []uint32) {
  214. binary := strconv.FormatInt(int64(num), 2)
  215. total := len(binary)
  216. for i := total - 1; i >= 0; i-- {
  217. if binary[i] == '1' {
  218. res = append(res, uint32(total-i))
  219. }
  220. }
  221. return
  222. }
  223. type WxTmplAndPush struct {
  224. MsgType int64
  225. Title string
  226. Content string
  227. WxPushUrl string
  228. AppPushUrl string
  229. ProductName string
  230. OrderId string
  231. OrderMoney string
  232. Row4 string
  233. SendUserId string
  234. }
  235. func SentWxTmplAndAppPush(this WxTmplAndPush, v string, group_id int, equityName, equityAddr string) {
  236. nTime := time.Now().Format("2006-01-02 15:04:05")
  237. //发送消息成功,推送微信、app
  238. //fmt.Println("this.MsgType", this.MsgType)
  239. pushConfig, err := GetWxTmplConfig(this.MsgType)
  240. if err != nil {
  241. logx.Error(fmt.Sprintf("SendWxTmplMsg uId %s Error %s", v, err.Error()))
  242. }
  243. p := &WxTmplPush{
  244. Config: pushConfig,
  245. }
  246. p.MgoId = v
  247. if this.MsgType == 10 {
  248. this.Title = this.ProductName
  249. this.Content = this.OrderId
  250. nTime = this.OrderMoney
  251. }
  252. // 消息模版 工单类型 {{thing19.DATA}} 工单标题 {{thing6.DATA}} 项目名称 {{thing13.DATA}} 服务时间 {{time25.DATA}} 服务地址 {{thing26.DATA}}
  253. if this.MsgType != 1 && this.MsgType != 10 {
  254. if this.MsgType == config.ConfigJson.EquityInfoMsgType {
  255. // p459 服务地址特殊处理
  256. err = p.SendMsg(this.WxPushUrl, this.Title, equityName, nTime, this.Row4, equityAddr)
  257. } else {
  258. err = p.SendMsg(this.WxPushUrl, this.Title, this.Content, nTime, this.Row4, "")
  259. }
  260. if err != nil {
  261. logx.Error(fmt.Sprintf("SendWxTmplMsg uId %s Error %s", v, err.Error()))
  262. } else {
  263. logx.Infof("SendWxTmplMsg uId success %s ", v)
  264. }
  265. }
  266. if this.MsgType == 1 {
  267. mst := new(WxTmplConfig)
  268. mst.Switch = AppPushMsgType[group_id]
  269. p.Config = mst
  270. }
  271. uData := p.GetUserPushInfo()
  272. //app推送
  273. if this.MsgType != 10 {
  274. category := ""
  275. if this.SendUserId == "cbgl" {
  276. category = "服务通知_工作事项"
  277. }
  278. if err = AppPushMsg(uData, AppPushMsgType[group_id], this.AppPushUrl, this.Title, this.Content, this.MsgType, category); err != nil {
  279. logx.Error(fmt.Sprintf("SendAppMsg uId %s Error %s", v, err.Error()))
  280. }
  281. }
  282. }