newSendMsgService.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. package common
  2. import (
  3. "app.yhyue.com/moapp/MessageCenter/entity"
  4. "app.yhyue.com/moapp/MessageCenter/rpc/type/message"
  5. "app.yhyue.com/moapp/MessageCenter/util"
  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 MultSave11(this *message.MultipleSaveMsgReq) (int64, string) {
  36. userIdArr := strings.Split(this.UserIds, ",")
  37. positionIdArr := strings.Split(this.PositionIds, ",")
  38. if len(userIdArr) == 0 {
  39. return 0, "无效的用户id"
  40. }
  41. wg := &sync.WaitGroup{}
  42. group_id := MsgGroupIdMap[int(this.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. //消息数组
  60. nTime := time.Now().Format("2006-01-02 15:04:05")
  61. sql3 := `INSERT INTO message(appid,receive_userid,receive_name,send_userid,send_name,title,content,msg_type,link,cite_id,createtime,isRead,isdel,msg_log_id,show_buoy,show_content,group_id,position_id) values ("%s",'%s','%s','%s','%s','%s','%s',%d,'%s',0,'%s',0,1,%d,%d,'%s',%d,?);`
  62. sql3 = fmt.Sprintf(sql3, this.Appid, v, "", this.SendUserId, this.SendName, this.Title, this.Content, this.MsgType, this.Link, nTime, this.MsgLogId, this.ShowBuoy, this.ShowContent, group_id)
  63. in := entity.Mysql.InsertBySql(sql3, common.If(positionId != 0, positionId, nil))
  64. logx.Info("插入消息返回 in1 id:", in, "消息类型:", this.MsgType, "用户id:", v)
  65. if in > -1 {
  66. ok := MsgCountAdd(v, this.Appid, util.Int64All(group_id), this.MsgType)
  67. if !ok {
  68. log.Println("存redis:", ok, v)
  69. }
  70. }
  71. if in > -1 {
  72. //微信推送模板消息、app push
  73. pushData := WxTmplAndPush{
  74. MsgType: this.MsgType,
  75. Title: this.Title,
  76. Content: this.Content,
  77. WxPushUrl: this.WxPushUrl,
  78. AppPushUrl: this.AppPushUrl,
  79. ProductName: this.ProductName,
  80. OrderId: this.OrderId,
  81. OrderMoney: this.OrderMoney,
  82. Row4: this.Row4,
  83. SendUserId: this.SendUserId,
  84. }
  85. SentWxTmplAndAppPush(pushData, v, group_id)
  86. } else {
  87. logx.Error(fmt.Sprintf("SendAppMsg uId %s 发送消息失败", v))
  88. }
  89. }(userIdArr[i], positionId)
  90. }
  91. wg.Wait()
  92. return 0, ""
  93. }
  94. func NewUserSendMsg(in *message.NewUserInsertMsgReq) string {
  95. userIdArr := strings.Split(in.UserIds, ",")
  96. positionIdArr := strings.Split(in.PositionIds, ",")
  97. if len(userIdArr) == 0 {
  98. return "无效的用户id"
  99. }
  100. wg := &sync.WaitGroup{}
  101. group_id := MsgGroupIdMap[int(in.MsgType)]
  102. for i := 0; i < len(userIdArr); i++ {
  103. if userIdArr[i] == "" {
  104. continue
  105. }
  106. //查询
  107. wg.Add(1)
  108. entity.SaveConcurrencyChan <- 1
  109. var positionId int64
  110. if len(positionIdArr) == len(userIdArr) {
  111. positionId = common.Int64All(positionIdArr[i])
  112. }
  113. go func(v string, positionId int64) {
  114. defer func() {
  115. <-entity.SaveConcurrencyChan
  116. wg.Done()
  117. }()
  118. row := entity.ClickhouseConn.QueryRow(context.Background(), fmt.Sprintf("SELECT COUNT(*) from message_user_summary where userId = '%s'", v))
  119. var count uint64
  120. row.Scan(&count)
  121. if count > 0 { //存在则更新
  122. 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))
  123. if err1 != nil {
  124. log.Println("新用户update message_user_summary表出错,error", err1)
  125. return
  126. }
  127. } else {
  128. //新用户需要insert
  129. sql := "INSERT INTO message_user_summary values "
  130. sql += fmt.Sprintf(" ('%s',bitmapBuild([toUInt64(%d)]),bitmapBuild([toUInt64(0)])) ", v, int(in.MsgLogId))
  131. fmt.Println("sql", sql)
  132. err1 := entity.ClickhouseConn.Exec(context.Background(), sql)
  133. if err1 != nil {
  134. //插入失败
  135. log.Println("新用户insert message_user_summary表出错,error", err1)
  136. return
  137. }
  138. }
  139. //微信推送模板消息、app push
  140. pushData := WxTmplAndPush{
  141. MsgType: in.MsgType,
  142. Title: in.Title,
  143. Content: in.Content,
  144. WxPushUrl: in.WxPushUrl,
  145. AppPushUrl: in.AppPushUrl,
  146. ProductName: in.ProductName,
  147. OrderId: in.OrderId,
  148. OrderMoney: in.OrderMoney,
  149. Row4: in.Row4,
  150. }
  151. SentWxTmplAndAppPush(pushData, v, group_id)
  152. }(userIdArr[i], positionId)
  153. }
  154. wg.Wait()
  155. return ""
  156. }
  157. func UpdateUserMsgSummary(in *message.BitmapSaveMsgReq) error {
  158. userIdArr := strings.Split(in.UserIds, ",")
  159. //positionIdArr := strings.Split(in.PositionIds, ",")
  160. if len(userIdArr) == 0 {
  161. return errors.New("无效的用户id")
  162. }
  163. wg := &sync.WaitGroup{}
  164. group_id := MsgGroupIdMap[int(in.MsgType)]
  165. str := ""
  166. for k, v := range userIdArr {
  167. if k != 0 {
  168. str += ","
  169. }
  170. str += "'" + v + "'"
  171. }
  172. fmt.Println(fmt.Sprintf(`alter table message_user_summary UPDATE allMsg = bitmapOr(allMsg,bitmapBuild([toUInt64(%d)])) where userId in (%s)`, in.MsgLogId, str))
  173. 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))
  174. if err1 != nil {
  175. return err1
  176. }
  177. for i := 0; i < len(userIdArr); i++ {
  178. if userIdArr[i] == "" {
  179. continue
  180. }
  181. //查询
  182. wg.Add(1)
  183. entity.SaveConcurrencyChan <- 1
  184. /*var positionId int64
  185. if len(positionIdArr) == len(userIdArr) {
  186. positionId = common.Int64All(positionIdArr[i])
  187. }*/
  188. go func(v string) {
  189. defer func() {
  190. <-entity.SaveConcurrencyChan
  191. wg.Done()
  192. }()
  193. //微信推送模板消息、app push
  194. pushData := WxTmplAndPush{
  195. MsgType: in.MsgType,
  196. Title: in.Title,
  197. Content: in.Content,
  198. WxPushUrl: in.WxPushUrl,
  199. AppPushUrl: in.AppPushUrl,
  200. ProductName: in.ProductName,
  201. OrderId: in.OrderId,
  202. OrderMoney: in.OrderMoney,
  203. Row4: in.Row4,
  204. }
  205. SentWxTmplAndAppPush(pushData, v, group_id)
  206. }(userIdArr[i])
  207. }
  208. wg.Wait()
  209. return nil
  210. }
  211. func ConvertToBitmap(num int) (res []uint32) {
  212. binary := strconv.FormatInt(int64(num), 2)
  213. total := len(binary)
  214. for i := total - 1; i >= 0; i-- {
  215. if binary[i] == '1' {
  216. res = append(res, uint32(total-i))
  217. }
  218. }
  219. return
  220. }
  221. type WxTmplAndPush struct {
  222. MsgType int64
  223. Title string
  224. Content string
  225. WxPushUrl string
  226. AppPushUrl string
  227. ProductName string
  228. OrderId string
  229. OrderMoney string
  230. Row4 string
  231. SendUserId string
  232. }
  233. func SentWxTmplAndAppPush(this WxTmplAndPush, v string, group_id int) {
  234. nTime := time.Now().Format("2006-01-02 15:04:05")
  235. //发送消息成功,推送微信、app
  236. //fmt.Println("this.MsgType", this.MsgType)
  237. pushConfig, err := GetWxTmplConfig(this.MsgType)
  238. if err != nil {
  239. logx.Error(fmt.Sprintf("SendWxTmplMsg uId %s Error %s", v, err.Error()))
  240. }
  241. p := &WxTmplPush{
  242. Config: pushConfig,
  243. }
  244. p.MgoId = v
  245. if this.MsgType == 10 {
  246. this.Title = this.ProductName
  247. this.Content = this.OrderId
  248. nTime = this.OrderMoney
  249. }
  250. // 消息模版 工单类型 {{thing19.DATA}} 工单标题 {{thing6.DATA}} 项目名称 {{thing13.DATA}} 服务时间 {{time25.DATA}} 服务地址 {{thing26.DATA}}
  251. if this.MsgType != 1 && this.MsgType != 10 {
  252. err = p.SendMsg(this.WxPushUrl, this.Title, this.Content, nTime, this.Row4)
  253. if err != nil {
  254. logx.Error(fmt.Sprintf("SendWxTmplMsg uId %s Error %s", v, err.Error()))
  255. } else {
  256. logx.Infof("SendWxTmplMsg uId success %s ", v)
  257. }
  258. }
  259. if this.MsgType == 1 {
  260. mst := new(WxTmplConfig)
  261. mst.Switch = AppPushMsgType[group_id]
  262. p.Config = mst
  263. }
  264. uData := p.GetUserPushInfo()
  265. //app推送
  266. if this.MsgType != 10 {
  267. category := ""
  268. if this.SendUserId == "cbgl" {
  269. category = "服务通知_工作事项"
  270. }
  271. if err = AppPushMsg(uData, AppPushMsgType[group_id], this.AppPushUrl, this.Title, this.Content, this.MsgType, category); err != nil {
  272. logx.Error(fmt.Sprintf("SendAppMsg uId %s Error %s", v, err.Error()))
  273. }
  274. }
  275. }