newSendMsgService.go 7.3 KB

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