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/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. //附件下载、剑鱼币活动到期提醒存消息发送记录
  178. func InsertMsgSendLog(in *message.MultipleSaveMsgReq) int64 {
  179. groupId := MsgGroupIdMap[int(in.MsgType)]
  180. id := entity.Mysql.Insert("message_send_log", map[string]interface{}{
  181. "send_usergroup_id": "",
  182. "send_usergroup_name": "",
  183. "msg_type": in.MsgType,
  184. "title": in.Title,
  185. "content": in.Content,
  186. "send_mode": 2,
  187. "send_time": time.Now().Format("2006-01-02 15:04:05"),
  188. "send_status": 4,
  189. "update_time": time.Now().Format("2006-01-02 15:04:05"),
  190. "link": in.Link,
  191. "isdel": 1,
  192. "send_userid": "",
  193. "update_user": "",
  194. "Sign": 5,
  195. "menu_name": "message",
  196. "group_id": groupId,
  197. })
  198. if id > 0 {
  199. //更新消息汇总表
  200. err := SetMsgSummary(id, int64(groupId), in.MsgType)
  201. if err != nil {
  202. log.Println("更新消息汇总表出错:", err)
  203. return 0
  204. }
  205. return id
  206. }
  207. return 0
  208. }
  209. func ConvertToBitmap(num int) (res []uint32) {
  210. binary := strconv.FormatInt(int64(num), 2)
  211. total := len(binary)
  212. for i := total - 1; i >= 0; i-- {
  213. if binary[i] == '1' {
  214. res = append(res, uint32(total-i))
  215. }
  216. }
  217. return
  218. }
  219. type WxTmplAndPush struct {
  220. MsgType int64
  221. Title string
  222. Content string
  223. WxPushUrl string
  224. AppPushUrl string
  225. ProductName string
  226. OrderId string
  227. OrderMoney string
  228. Row4 string
  229. SendUserId string
  230. }
  231. func SentWxTmplAndAppPush(this WxTmplAndPush, v string, group_id int, equityName, equityAddr string) {
  232. nTime := time.Now().Format("2006-01-02 15:04:05")
  233. //发送消息成功,推送微信、app
  234. //fmt.Println("this.MsgType", this.MsgType)
  235. pushConfig, err := GetWxTmplConfig(this.MsgType)
  236. if err != nil {
  237. logx.Error(fmt.Sprintf("SendWxTmplMsg uId %s Error %s", v, err.Error()))
  238. }
  239. p := &WxTmplPush{
  240. Config: pushConfig,
  241. }
  242. p.MgoId = v
  243. if this.MsgType == 10 {
  244. this.Title = this.ProductName
  245. this.Content = this.OrderId
  246. nTime = this.OrderMoney
  247. }
  248. // 消息模版 工单类型 {{thing19.DATA}} 工单标题 {{thing6.DATA}} 项目名称 {{thing13.DATA}} 服务时间 {{time25.DATA}} 服务地址 {{thing26.DATA}}
  249. if this.MsgType != 1 && this.MsgType != 10 {
  250. if this.MsgType == config.ConfigJson.EquityInfoMsgType {
  251. // p459 服务地址特殊处理
  252. err = p.SendMsg(this.WxPushUrl, this.Title, equityName, nTime, this.Row4, equityAddr)
  253. } else {
  254. err = p.SendMsg(this.WxPushUrl, this.Title, this.Content, nTime, this.Row4, "")
  255. }
  256. if err != nil {
  257. logx.Error(fmt.Sprintf("SendWxTmplMsg uId %s Error %s", v, err.Error()))
  258. } else {
  259. logx.Infof("SendWxTmplMsg uId success %s ", v)
  260. }
  261. }
  262. if this.MsgType == 1 {
  263. mst := new(WxTmplConfig)
  264. mst.Switch = AppPushMsgType[group_id]
  265. p.Config = mst
  266. }
  267. uData := p.GetUserPushInfo()
  268. //app推送
  269. if this.MsgType != 10 {
  270. category := ""
  271. if this.SendUserId == "cbgl" {
  272. category = "服务通知_工作事项"
  273. }
  274. if err = AppPushMsg(uData, AppPushMsgType[group_id], this.AppPushUrl, this.Title, this.Content, this.MsgType, category); err != nil {
  275. logx.Error(fmt.Sprintf("SendAppMsg uId %s Error %s", v, err.Error()))
  276. }
  277. }
  278. }