newSendMsgService.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. var ids []string
  105. for _, v := range userIdArr {
  106. ids = append(ids, fmt.Sprintf(`'%s'`, v))
  107. if len(ids) == 1000 {
  108. idStr := strings.Join(ids, ",")
  109. go Update(idStr, in.MsgLogId)
  110. ids = []string{}
  111. }
  112. }
  113. if len(ids) > 0 {
  114. go Update(strings.Join(ids, ","), in.MsgLogId)
  115. }
  116. //p459 特殊处理 传过来的消息内容格式为 消息内容#jy#微信模板项目名称#jy#服务地址
  117. equityName, equityAddr := "", ""
  118. if in.MsgType == config.ConfigJson.EquityInfoMsgType {
  119. equityRs := strings.Split(in.Content, "#jy#")
  120. if len(equityRs) != 3 {
  121. log.Println("消息内容格式有误:", in.Content)
  122. return errors.New("无效的消息内容格式")
  123. }
  124. in.Content = equityRs[0]
  125. equityName = equityRs[1]
  126. equityAddr = equityRs[2]
  127. }
  128. for i := 0; i < len(userIdArr); i++ {
  129. if userIdArr[i] == "" {
  130. continue
  131. }
  132. //查询
  133. wg.Add(1)
  134. entity.SaveConcurrencyChan <- 1
  135. /*var positionId int64
  136. if len(positionIdArr) == len(userIdArr) {
  137. positionId = common.Int64All(positionIdArr[i])
  138. }*/
  139. go func(v string) {
  140. defer func() {
  141. <-entity.SaveConcurrencyChan
  142. wg.Done()
  143. }()
  144. //微信推送模板消息、app push
  145. pushData := WxTmplAndPush{
  146. MsgType: in.MsgType,
  147. Title: in.Title,
  148. Content: in.Content,
  149. WxPushUrl: in.WxPushUrl,
  150. AppPushUrl: in.AppPushUrl,
  151. ProductName: in.ProductName,
  152. OrderId: in.OrderId,
  153. OrderMoney: in.OrderMoney,
  154. Row4: in.Row4,
  155. }
  156. SentWxTmplAndAppPush(pushData, v, group_id, equityName, equityAddr)
  157. key := fmt.Sprintf(MsgCountKey, v, group_id)
  158. redis.Del(redisModule, key)
  159. if in.MsgType == 11 || in.MsgType == 12 {
  160. key1 := fmt.Sprintf(MsgClassCountKey, v, in.MsgType)
  161. redis.Del(redisModule, key1)
  162. }
  163. }(userIdArr[i])
  164. }
  165. wg.Wait()
  166. return nil
  167. }
  168. func Update(str string, msgLogId int64) {
  169. log.Println(fmt.Sprintf(`alter table message_user_summary UPDATE allMsg = bitmapOr(allMsg,bitmapBuild([toUInt64(%d)])) where userId in (%s)`, msgLogId, str))
  170. 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))
  171. if err1 != nil {
  172. log.Printf("批量更新message_user_summary出错:%s", err1)
  173. return
  174. }
  175. }
  176. //附件下载、剑鱼币活动到期提醒存消息发送记录
  177. func InsertMsgSendLog(in *message.MultipleSaveMsgReq) int64 {
  178. groupId := MsgGroupIdMap[int(in.MsgType)]
  179. id := entity.Mysql.Insert("message_send_log", map[string]interface{}{
  180. "send_usergroup_id": "",
  181. "send_usergroup_name": "",
  182. "msg_type": in.MsgType,
  183. "title": in.Title,
  184. "content": in.Content,
  185. "send_mode": 2,
  186. "send_time": time.Now().Format("2006-01-02 15:04:05"),
  187. "send_status": 4,
  188. "update_time": time.Now().Format("2006-01-02 15:04:05"),
  189. "link": in.Link,
  190. "isdel": 1,
  191. "send_userid": "",
  192. "update_user": "",
  193. "Sign": 5,
  194. "menu_name": "message",
  195. "group_id": groupId,
  196. })
  197. if id > 0 {
  198. //更新消息汇总表
  199. err := SetMsgSummary(id, int64(groupId), in.MsgType)
  200. if err != nil {
  201. log.Println("更新消息汇总表出错:", err)
  202. return 0
  203. }
  204. return id
  205. }
  206. return 0
  207. }
  208. func ConvertToBitmap(num int) (res []uint32) {
  209. binary := strconv.FormatInt(int64(num), 2)
  210. total := len(binary)
  211. for i := total - 1; i >= 0; i-- {
  212. if binary[i] == '1' {
  213. res = append(res, uint32(total-i))
  214. }
  215. }
  216. return
  217. }
  218. type WxTmplAndPush struct {
  219. MsgType int64
  220. Title string
  221. Content string
  222. WxPushUrl string
  223. AppPushUrl string
  224. ProductName string
  225. OrderId string
  226. OrderMoney string
  227. Row4 string
  228. SendUserId string
  229. }
  230. func SentWxTmplAndAppPush(this WxTmplAndPush, v string, group_id int, equityName, equityAddr string) {
  231. nTime := time.Now().Format("2006-01-02 15:04:05")
  232. //发送消息成功,推送微信、app
  233. //fmt.Println("this.MsgType", this.MsgType)
  234. pushConfig, err := GetWxTmplConfig(this.MsgType)
  235. if err != nil {
  236. logx.Error(fmt.Sprintf("SendWxTmplMsg uId %s Error %s", v, err.Error()))
  237. }
  238. p := &WxTmplPush{
  239. Config: pushConfig,
  240. }
  241. p.MgoId = v
  242. if this.MsgType == 10 {
  243. this.Title = this.ProductName
  244. this.Content = this.OrderId
  245. nTime = this.OrderMoney
  246. }
  247. // 消息模版 工单类型 {{thing19.DATA}} 工单标题 {{thing6.DATA}} 项目名称 {{thing13.DATA}} 服务时间 {{time25.DATA}} 服务地址 {{thing26.DATA}}
  248. if this.MsgType != 1 && this.MsgType != 10 {
  249. if this.MsgType == config.ConfigJson.EquityInfoMsgType {
  250. // p459 服务地址特殊处理
  251. err = p.SendMsg(this.WxPushUrl, this.Title, equityName, nTime, this.Row4, equityAddr)
  252. } else {
  253. err = p.SendMsg(this.WxPushUrl, this.Title, this.Content, nTime, this.Row4, "")
  254. }
  255. if err != nil {
  256. logx.Error(fmt.Sprintf("SendWxTmplMsg uId %s Error %s", v, err.Error()))
  257. } else {
  258. logx.Infof("SendWxTmplMsg uId success %s ", v)
  259. }
  260. }
  261. if this.MsgType == 1 {
  262. mst := new(WxTmplConfig)
  263. mst.Switch = AppPushMsgType[group_id]
  264. p.Config = mst
  265. }
  266. uData := p.GetUserPushInfo()
  267. //app推送
  268. if this.MsgType != 10 {
  269. category := ""
  270. if this.SendUserId == "cbgl" {
  271. category = "服务通知_工作事项"
  272. }
  273. if err = AppPushMsg(uData, AppPushMsgType[group_id], this.AppPushUrl, this.Title, this.Content, this.MsgType, category); err != nil {
  274. logx.Error(fmt.Sprintf("SendAppMsg uId %s Error %s", v, err.Error()))
  275. }
  276. }
  277. }