newSendMsgService.go 7.7 KB

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