sendMsg.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. package common
  2. import (
  3. "app.yhyue.com/moapp/MessageCenter/rpc/internal/config"
  4. "fmt"
  5. "log"
  6. "strconv"
  7. "strings"
  8. "sync"
  9. "time"
  10. "app.yhyue.com/moapp/MessageCenter/entity"
  11. "app.yhyue.com/moapp/MessageCenter/rpc/type/message"
  12. "app.yhyue.com/moapp/MessageCenter/util"
  13. "app.yhyue.com/moapp/jybase/common"
  14. "app.yhyue.com/moapp/jybase/redis"
  15. "github.com/zeromicro/go-zero/core/logx"
  16. )
  17. // 类型的顺序
  18. const order = "1,4"
  19. const MsgCountKey = "count_%s_%d" //redis 消息未读数量 Count.用户id.消息类型=数量
  20. const MsgClassCountKey = "msg_class_count_%s_%d" //redis 用户消息class分类消息数量
  21. const redisModule = "msgCount"
  22. func FindUserMsg(this message.FindUserMsgReq, isClean bool) message.FindUserMsgRes {
  23. var err error
  24. var count int64
  25. cquery := map[string]interface{}{
  26. "receive_userid": this.UserId,
  27. "isdel": 1,
  28. "appid": this.Appid,
  29. }
  30. if this.MsgType > 0 {
  31. cquery["group_id"] = this.MsgType
  32. }
  33. if this.Read != -1 {
  34. cquery["isRead"] = this.Read
  35. }
  36. count = entity.Mysql.Count("message", cquery)
  37. data := message.FindUserMsgRes{}
  38. if this.PageSize == 5 {
  39. //从缓存里边取数据
  40. pc_a, err := entity.GetData(this.UserId)
  41. if err == nil && pc_a != nil {
  42. // 缓存有值
  43. if !isClean {
  44. data.Code = 1
  45. data.Message = "查询成功"
  46. data.Data = pc_a.Data
  47. data.Count = pc_a.Count
  48. return data
  49. }
  50. }
  51. }
  52. count = entity.Mysql.Count("message", cquery)
  53. if count > 0 {
  54. res := entity.Mysql.Find("message", cquery, "", "createtime desc", (int(this.OffSet)-1)*int(this.PageSize), int(this.PageSize))
  55. //log.Println("数据:", res)
  56. if res != nil && len(*res) > 0 {
  57. for _, v := range *res {
  58. _id := util.Int64All(v["id"])
  59. id := strconv.FormatInt(_id, 10)
  60. data.Data = append(data.Data, &message.Messages{
  61. Id: id,
  62. Appid: util.ObjToString(v["appId"]),
  63. ReceiveUserId: util.ObjToString(v["receive_userid"]),
  64. ReceiveName: util.ObjToString(v["receive_name"]),
  65. SendUserId: util.ObjToString(v["send_userid"]),
  66. SendName: util.ObjToString(v["send_name"]),
  67. Createtime: util.ObjToString(v["createtime"]),
  68. Title: util.ObjToString(v["title"]),
  69. MsgType: int64(util.IntAll(v["group_id"])),
  70. Link: util.ObjToString(v["link"]),
  71. CiteId: util.Int64All(v["cite_id"]),
  72. Content: util.ObjToString(v["content"]),
  73. IsRead: util.Int64All(v["isRead"]),
  74. MsgLogId: util.Int64All(v["msg_log_id"]),
  75. })
  76. }
  77. }
  78. }
  79. data.Count = count
  80. if this.PageSize == 5 {
  81. redisData := map[string]interface{}{
  82. "count": count,
  83. "data": data.Data,
  84. }
  85. entity.SetData(this.UserId, redisData, entity.SurvivalTime)
  86. }
  87. if err != nil {
  88. data.Code = 0
  89. data.Message = "查询失败"
  90. } else {
  91. data.Code = 1
  92. data.Message = "查询成功"
  93. }
  94. return data
  95. }
  96. func UserMsgList(this *message.UserMsgListReq) *message.UserMsgList {
  97. var (
  98. unread, count int64
  99. )
  100. m := &MessageService{}
  101. data := new(message.UserMsgList)
  102. if !this.IsMsgList && !this.IsColumnNewMsg && !this.IsColumn { //消息未读数统计
  103. //获取总未读数 初始化
  104. _, unread = m.CountUnread(this.UserId, true)
  105. if this.IsContainLetter { //私信统计
  106. unread += unreadMsg(this)
  107. }
  108. data.Unread = unread
  109. return data
  110. }
  111. cquery := map[string]interface{}{
  112. "receive_userid": this.UserId,
  113. "isdel": 1,
  114. "appid": this.Appid,
  115. }
  116. // p436 细化分类时需要用msg_type 进行查询
  117. if this.MsgType > 0 && this.IsClassSearch {
  118. cquery["msg_type"] = this.MsgType
  119. } else if this.MsgType > 0 {
  120. cquery["group_id"] = this.MsgType
  121. }
  122. if this.Read != -1 {
  123. cquery["isRead"] = this.Read
  124. }
  125. //获取栏目下的数据
  126. sData := make(map[string][]*message.Messages)
  127. t := time.Now()
  128. if this.IsColumnNewMsg && this.SortSize > 0 {
  129. var sortData *[]map[string]interface{}
  130. if this.IsClassSearch { // p436 增加
  131. sortDataQ := fmt.Sprintf(`SELECT title,createtime,msg_type as group_id ,id FROM (
  132. SELECT title,createtime,msg_type,id, ROW_NUMBER() OVER (PARTITION BY msg_type, receive_userid ORDER BY createtime DESC) AS row_num
  133. FROM message
  134. WHERE receive_userid = '%s' and isdel = 1 and appid = %s and group_id=%d
  135. ) AS message_ranked
  136. WHERE row_num <=%d;`, this.UserId, this.Appid, this.MsgType, this.SortSize)
  137. sortData = entity.Mysql.SelectBySql(sortDataQ)
  138. } else {
  139. sortData = entity.Mysql.SelectBySql(fmt.Sprintf(`SELECT title,createtime,group_id,id FROM (
  140. SELECT title,createtime,group_id,id, ROW_NUMBER() OVER (PARTITION BY group_id, receive_userid ORDER BY createtime DESC) AS row_num
  141. FROM message
  142. WHERE receive_userid = '%s' and isdel = 1 and appid = %s
  143. ) AS message_ranked
  144. WHERE row_num <=%d;`, this.UserId, this.Appid, this.SortSize))
  145. }
  146. log.Println("消息列表耗时1:", time.Since(t))
  147. if sortData != nil {
  148. for _, v := range *sortData {
  149. _id := util.Int64All(v["id"])
  150. id := strconv.FormatInt(_id, 10)
  151. var msg = message.Messages{
  152. Id: id,
  153. Createtime: common.InterfaceToStr(v["createtime"]),
  154. Title: common.InterfaceToStr(v["title"]),
  155. MsgType: int64(util.IntAll(v["group_id"])),
  156. }
  157. if sData[common.InterfaceToStr(v["group_id"])] == nil {
  158. sData[common.InterfaceToStr(v["group_id"])] = []*message.Messages{&msg}
  159. } else {
  160. sData[common.InterfaceToStr(v["group_id"])] = append(sData[common.InterfaceToStr(v["group_id"])], &msg)
  161. }
  162. }
  163. }
  164. }
  165. // 消息栏目下的最新消息
  166. var columnData []*message.AllSortData
  167. if this.IsColumn && this.MsgType > 0 && this.IsClassSearch {
  168. // p436 处理消息细分分类要返回的数据
  169. // 获取小分类下的未读数
  170. sortUnread, _ := m.CountClassUnread(this.UserId, this.MsgType)
  171. columnArr := []entity.MsgClass{}
  172. if !this.IsColumnNewMsg { // 用于区分分类列表页和分类详情页 根据不同情况
  173. columnArr = append(columnArr, entity.ClassMap[this.MsgType])
  174. } else {
  175. columnArr = entity.ClassSearchMap[this.MsgType]
  176. }
  177. for i := 0; i < len(columnArr); i++ {
  178. tmp := columnArr[i]
  179. var column message.AllSortData
  180. column.Name = tmp.Name
  181. column.Img = fmt.Sprintf("/common-module/msgCenter/%s.png", tmp.Img)
  182. column.MsgType = tmp.MsgType
  183. // 消息未读数
  184. msgType := common.InterfaceToStr(tmp.MsgType)
  185. column.UnreadMessages = sortUnread[msgType]
  186. unread += sortUnread[msgType]
  187. column.Data = sData[msgType]
  188. column.IsClassSearch = true
  189. columnData = append(columnData, &column)
  190. }
  191. // 未读数量
  192. } else if this.IsColumn {
  193. //获取所有分类未读数 不初始化
  194. sortUnread, _ := m.CountUnread(this.UserId, false)
  195. for _, v := range entity.MessageColumn {
  196. var column message.AllSortData
  197. column.Name = common.InterfaceToStr(v["name"])
  198. column.Img = fmt.Sprintf("/common-module/msgCenter/%s.png", common.InterfaceToStr(v["img"]))
  199. column.MsgType = common.Int64All(v["group_id"])
  200. if column.Name == "私信" {
  201. column.UnreadMessages = unreadMsg(this)
  202. } else if common.IntAll(v["group_id"]) > 0 {
  203. //消息未读数
  204. msgType := common.InterfaceToStr(v["group_id"])
  205. column.UnreadMessages = sortUnread[msgType]
  206. unread += sortUnread[msgType]
  207. column.Data = sData[msgType]
  208. }
  209. // p436 该groupId属于展示细化分类的 如待办 点击待办进入到待参加会议、待处理任务中间列表页等
  210. // 该返回值用于前端后续传参使用
  211. if _, ok := entity.ClassSearchMap[column.MsgType]; ok {
  212. column.IsClassSearch = true
  213. }
  214. columnData = append(columnData, &column)
  215. }
  216. }
  217. data.SortData = columnData
  218. count = entity.Mysql.Count("message", cquery)
  219. if this.IsMsgList {
  220. if count > 0 {
  221. if this.OffSet <= 0 {
  222. this.OffSet = 1
  223. }
  224. res := entity.Mysql.Find("message", cquery, "", "createtime desc", (int(this.OffSet)-1)*int(this.PageSize), int(this.PageSize))
  225. log.Println("消息列表耗时3:", time.Since(t))
  226. if res != nil && len(*res) > 0 {
  227. for _, v := range *res {
  228. _id := util.Int64All(v["id"])
  229. id := strconv.FormatInt(_id, 10)
  230. data.Data = append(data.Data, &message.Messages{
  231. Id: id,
  232. Appid: common.InterfaceToStr(v["appId"]),
  233. ReceiveUserId: common.InterfaceToStr(v["receive_userid"]),
  234. ReceiveName: common.InterfaceToStr(v["receive_name"]),
  235. SendUserId: common.InterfaceToStr(v["send_userid"]),
  236. SendName: common.InterfaceToStr(v["send_name"]),
  237. Createtime: common.InterfaceToStr(v["createtime"]),
  238. Title: common.InterfaceToStr(v["title"]),
  239. MsgType: int64(util.IntAll(v["group_id"])),
  240. Link: common.InterfaceToStr(v["link"]),
  241. CiteId: util.Int64All(v["cite_id"]),
  242. Content: common.InterfaceToStr(v["content"]),
  243. IsRead: util.Int64All(v["isRead"]),
  244. MsgLogId: util.Int64All(v["msg_log_id"]),
  245. })
  246. }
  247. }
  248. }
  249. }
  250. data.Count = count
  251. if this.Read == 0 {
  252. unread = count
  253. if this.IsContainLetter { //是否需要统计私信未读数
  254. unread += unreadMsg(this)
  255. }
  256. }
  257. data.Unread = unread
  258. return data
  259. }
  260. func unreadMsg(this *message.UserMsgListReq) int64 {
  261. if this.PositionId <= 0 {
  262. return 0
  263. }
  264. querySql := fmt.Sprintf("SELECT b.*,(SELECT SUM( a.unread) FROM %s a "+
  265. "LEFT JOIN %s b ON a.message_id = b.id "+
  266. "WHERE a.unread > 0 "+
  267. "AND ( a.my_position_id = %d OR a.user_id = %d )) AS unread "+
  268. "FROM %s a "+
  269. "LEFT JOIN %s b ON a.message_id = b.id "+
  270. "WHERE a.unread > 0 "+
  271. "AND ( a.my_position_id = %d OR a.user_id = %d ) "+
  272. "ORDER BY a.TIMESTAMP DESC LIMIT 0,1", "socialize_summary", "socialize_message", this.PositionId, this.NewUserId,
  273. "socialize_summary", "socialize_message", this.PositionId, this.NewUserId)
  274. log.Println("查询sql", querySql)
  275. msgUnread := entity.BaseMysql.SelectBySql(querySql)
  276. if msgUnread != nil && len(*msgUnread) > 0 {
  277. return common.Int64All((*msgUnread)[0]["unread"])
  278. }
  279. return 0
  280. }
  281. func UserUnreadMsgList(this *message.UserUnreadMsgListReq) (int64, []*message.Messages) {
  282. count := 0
  283. data := []*message.Messages{}
  284. types := entity.Mysql.Find("message_group", map[string]interface{}{}, `"group_id"`, "", -1, -1)
  285. if types != nil && len(*types) > 0 {
  286. for _, v := range *types {
  287. key := fmt.Sprintf(MsgCountKey, this.UserId, util.IntAll(v["group_id"]))
  288. if exists, _ := redis.Exists(redisModule, key); exists {
  289. count += redis.GetInt(redisModule, key)
  290. }
  291. }
  292. }
  293. if this.IsNeedData == 1 && count > 0 {
  294. query := map[string]interface{}{
  295. "receive_userid": this.UserId,
  296. "isdel": 1,
  297. "appid": this.Appid,
  298. "isRead": 0,
  299. }
  300. res := entity.Mysql.Find("message", query, "", "createtime desc", (int(this.OffSet)-1)*int(this.PageSize), int(this.PageSize))
  301. if res != nil && len(*res) > 0 {
  302. for _, val := range *res {
  303. _id := util.Int64All(val["id"])
  304. id := strconv.FormatInt(_id, 10)
  305. links4 := common.InterfaceToStr(val["link"])
  306. link4, androidUrl4, iosUrl4, weChatUrl4 := util.LinkSplit(links4)
  307. url := map[string]string{
  308. "androidUrl": androidUrl4,
  309. "iosUrl": iosUrl4,
  310. "weChatUrl": weChatUrl4,
  311. }
  312. data = append(data, &message.Messages{
  313. Id: id,
  314. Appid: common.InterfaceToStr(val["appid"]),
  315. ReceiveUserId: common.InterfaceToStr(val["receive_userid"]),
  316. ReceiveName: common.InterfaceToStr(val["receive_name"]),
  317. SendUserId: common.InterfaceToStr(val["send_userid"]),
  318. SendName: common.InterfaceToStr(val["send_name"]),
  319. Createtime: common.InterfaceToStr(val["createtime"]),
  320. Title: common.InterfaceToStr(val["title"]),
  321. MsgType: common.Int64All(val["group_id"]),
  322. Link: link4,
  323. CiteId: common.Int64All(val["cite_id"]),
  324. Content: common.InterfaceToStr(val["content"]),
  325. IsRead: common.Int64All(val["isRead"]),
  326. MsgLogId: common.Int64All(val["msg_log_id"]),
  327. Url: url,
  328. })
  329. }
  330. }
  331. }
  332. return util.Int64All(count), data
  333. }
  334. func MessageGetLast(this *message.UserMsgListReq) *message.Messages {
  335. if !this.IsMsgList && !this.IsColumnNewMsg && !this.IsColumn {
  336. return nil
  337. }
  338. query := map[string]interface{}{
  339. "receive_userid": this.UserId,
  340. "isdel": 1,
  341. "appid": this.Appid,
  342. "isRead": 0,
  343. "group_id": 1,
  344. }
  345. lastMsg := entity.Mysql.FindOne("message", query, "", "createtime desc")
  346. if lastMsg != nil && len(*lastMsg) > 0 {
  347. _id := util.Int64All((*lastMsg)["id"])
  348. id := strconv.FormatInt(_id, 10)
  349. msg := message.Messages{
  350. Id: id,
  351. Appid: common.InterfaceToStr((*lastMsg)["appid"]),
  352. ReceiveUserId: common.InterfaceToStr((*lastMsg)["receive_userid"]),
  353. ReceiveName: common.InterfaceToStr((*lastMsg)["receive_name"]),
  354. SendUserId: common.InterfaceToStr((*lastMsg)["send_userid"]),
  355. SendName: common.InterfaceToStr((*lastMsg)["send_name"]),
  356. Createtime: common.InterfaceToStr((*lastMsg)["createtime"]),
  357. Title: common.InterfaceToStr((*lastMsg)["title"]),
  358. MsgType: common.Int64All((*lastMsg)["group_id"]),
  359. Link: common.InterfaceToStr((*lastMsg)["link"]),
  360. CiteId: common.Int64All((*lastMsg)["cite_id"]),
  361. Content: common.InterfaceToStr((*lastMsg)["content"]),
  362. IsRead: common.Int64All((*lastMsg)["isRead"]),
  363. MsgLogId: common.Int64All((*lastMsg)["msg_log_id"]),
  364. }
  365. return &msg
  366. }
  367. return nil
  368. }
  369. // MsgCountAdd 消息未读数量加1
  370. func MsgCountAdd(userId, appId string, msgType int64, msgClassType int64) bool {
  371. keyString := fmt.Sprintf(MsgCountKey, userId, msgType)
  372. classKeyString := fmt.Sprintf(MsgClassCountKey, userId, msgClassType)
  373. if exist, _ := redis.Exists(redisModule, classKeyString); exist {
  374. redis.Incr(redisModule, classKeyString)
  375. }
  376. exists, _ := redis.Exists(redisModule, keyString)
  377. if exists {
  378. in := redis.Incr(redisModule, keyString)
  379. FindUserMsg(message.FindUserMsgReq{
  380. UserId: userId,
  381. Appid: appId,
  382. OffSet: 1,
  383. PageSize: 5,
  384. MsgType: -1,
  385. Read: 0,
  386. }, true)
  387. return in > 0
  388. }
  389. return true
  390. }
  391. // MsgCountMinusOne 根据消息类型未读消息数量减1
  392. func MsgCountMinusOne(userId, appId string, msgType int64, msgClassType int64) bool {
  393. classKeyString := fmt.Sprintf(MsgClassCountKey, userId, msgClassType)
  394. if exist, _ := redis.Exists(redisModule, classKeyString); exist {
  395. if redis.GetInt(redisModule, classKeyString) > 0 {
  396. redis.Decrby(redisModule, classKeyString, 1)
  397. }
  398. }
  399. keyString := fmt.Sprintf(MsgCountKey, userId, msgType)
  400. exists, _ := redis.Exists(redisModule, keyString)
  401. if exists {
  402. FindUserMsg(message.FindUserMsgReq{
  403. UserId: userId,
  404. Appid: appId,
  405. OffSet: 1,
  406. PageSize: 5,
  407. MsgType: -1,
  408. Read: 0,
  409. }, true)
  410. if redis.GetInt(redisModule, keyString) <= 0 {
  411. return true
  412. }
  413. in := redis.Decrby(redisModule, keyString, 1)
  414. return in > 0
  415. }
  416. return true
  417. }
  418. // MsgCountZero 把该消息类型未读数量置0
  419. func MsgCountZero(userId, appId string, msgType int64) bool {
  420. if msgType > 0 && msgType < 999 { //全部私信不统计
  421. keyString := fmt.Sprintf(MsgCountKey, userId, msgType)
  422. fool := redis.Put(redisModule, keyString, 0, -1)
  423. FindUserMsg(message.FindUserMsgReq{
  424. UserId: userId,
  425. Appid: appId,
  426. OffSet: 1,
  427. PageSize: 5,
  428. MsgType: -1,
  429. Read: 0,
  430. }, true)
  431. return fool
  432. }
  433. return true
  434. }
  435. func MultSave(this message.MultipleSaveMsgReq) (int64, string) {
  436. userIdArr := strings.Split(this.UserIds, ",")
  437. userNameArr := strings.Split(this.UserNames, ",")
  438. positionIdArr := strings.Split(this.PositionIds, ",")
  439. if len(userIdArr) == 0 {
  440. return 0, "无效的用户id"
  441. }
  442. wg := &sync.WaitGroup{}
  443. var group_id int
  444. class := entity.Mysql.FindOne("message_class", map[string]interface{}{"msg_type": this.MsgType}, "group_id", "")
  445. if class != nil && len(*class) > 0 {
  446. group_id = util.IntAll((*class)["group_id"])
  447. }
  448. //p459 特殊处理 传过来的消息内容格式为 消息内容#jy#微信模板项目名称#jy#服务地址
  449. equityName, equityAddr := "", ""
  450. if this.MsgType == config.ConfigJson.EquityInfoMsgType {
  451. equityRs := strings.Split(this.Content, "#jy#")
  452. if len(equityRs) != 3 {
  453. log.Println("消息内容格式有误:", this.Content)
  454. return 0, "无效的消息内容格式"
  455. }
  456. this.Content = equityRs[0]
  457. equityName = equityRs[1]
  458. equityAddr = equityRs[2]
  459. }
  460. for i := 0; i < len(userIdArr); i++ {
  461. if userIdArr[i] == "" {
  462. continue
  463. }
  464. name := userNameArr[i]
  465. wg.Add(1)
  466. entity.SaveConcurrencyChan <- 1
  467. var positionId int64
  468. if len(positionIdArr) == len(userIdArr) {
  469. positionId = common.Int64All(positionIdArr[i])
  470. }
  471. go func(v, userName string, positionId int64) {
  472. defer func() {
  473. <-entity.SaveConcurrencyChan
  474. wg.Done()
  475. }()
  476. //消息数组
  477. nTime := time.Now().Format("2006-01-02 15:04:05")
  478. //c := entity.Mysql.Count("conversation", map[string]interface{}{"receive_id": v, "send_id": this.SendUserId})
  479. 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,?);`
  480. sql3 = fmt.Sprintf(sql3, this.Appid, v, userName, this.SendUserId, this.SendName, this.Title, this.Content, this.MsgType, this.Link, nTime, this.MsgLogId, this.ShowBuoy, this.ShowContent, group_id)
  481. var in int64
  482. /*if c <= 0 {
  483. sql1 := `INSERT INTO conversation(appid,secret_key,user_id,receive_id,receive_name,send_id,send_name,sort,createtime) values ('%s','','%s','%s','%s','%s','%s',0,'%s');`
  484. sql1 = fmt.Sprintf(sql1, this.Appid, this.SendUserId, v, userName, this.SendUserId, this.SendName, nTime)
  485. //插入会话表
  486. in1 := entity.Mysql.InsertBySql(sql1)
  487. sql2 := `INSERT INTO conversation(appid,secret_key,user_id,receive_id,receive_name,send_id,send_name,sort,createtime) values ('%s','','%s','%s','%s','%s','%s',0,'%s');`
  488. sql2 = fmt.Sprintf(sql2, this.Appid, v, this.SendUserId, this.SendName, v, userName, nTime)
  489. in2 := entity.Mysql.InsertBySql(sql2)
  490. //插入消息表
  491. in = entity.Mysql.InsertBySql(sql3, common.If(positionId != 0, positionId, nil))
  492. logx.Info(in1, in2, in)
  493. if in1 > -1 && in2 > -1 && in > -1 {
  494. ok1 := MsgCountAdd(v, this.Appid, this.MsgType)
  495. if !ok1 {
  496. log.Println("存redis:", ok1, v)
  497. }
  498. }
  499. } else {*/
  500. in = entity.Mysql.InsertBySql(sql3, common.If(positionId != 0, positionId, nil))
  501. logx.Info("插入消息返回 in1 id:", in, "消息类型:", this.MsgType, "用户id:", v)
  502. if in > -1 {
  503. ok := MsgCountAdd(v, this.Appid, util.Int64All(group_id), this.MsgType)
  504. if !ok {
  505. log.Println("存redis:", ok, v)
  506. }
  507. }
  508. //}
  509. if in > -1 {
  510. //发送消息成功,推送微信、app
  511. pushConfig, err := GetWxTmplConfig(this.MsgType)
  512. if err != nil {
  513. logx.Error(fmt.Sprintf("SendWxTmplMsg uId %s Error %s", v, err.Error()))
  514. }
  515. p := &WxTmplPush{
  516. Config: pushConfig,
  517. CustomWxTpl: this.CustomWxTpl,
  518. }
  519. p.MgoId = v
  520. if this.MsgType == 10 {
  521. this.Title = this.ProductName
  522. this.Content = this.OrderId
  523. nTime = this.OrderMoney
  524. }
  525. // 消息模版 工单类型 {{thing19.DATA}} 工单标题 {{thing6.DATA}} 项目名称 {{thing13.DATA}} 服务时间 {{time25.DATA}} 服务地址 {{thing26.DATA}}
  526. if this.MsgType != 1 && this.MsgType != 10 {
  527. if this.MsgType == config.ConfigJson.EquityInfoMsgType {
  528. // p459 服务地址特殊处理
  529. err = p.SendMsg(this.WxPushUrl, this.Title, equityName, nTime, this.Row4, equityAddr)
  530. } else {
  531. err = p.SendMsg(this.WxPushUrl, this.Title, this.Content, nTime, this.Row4, "")
  532. }
  533. if err != nil {
  534. logx.Error(fmt.Sprintf("SendWxTmplMsg uId %s Error %s", v, err.Error()))
  535. } else {
  536. logx.Infof("SendWxTmplMsg uId success %s ", v)
  537. }
  538. }
  539. if this.MsgType == 1 {
  540. mst := new(WxTmplConfig)
  541. mst.Switch = AppPushMsgType[group_id]
  542. p.Config = mst
  543. }
  544. //app推送
  545. if this.MsgType != 10 {
  546. uData := p.GetUserPushInfo()
  547. category := ""
  548. if this.SendUserId == "cbgl" {
  549. category = "服务通知_工作事项"
  550. }
  551. if err = AppPushMsg(uData, AppPushMsgType[group_id], this.AppPushUrl, this.Title, this.Content, this.MsgType, category); err != nil {
  552. logx.Error(fmt.Sprintf("SendAppMsg uId %s Error %s", v, err.Error()))
  553. }
  554. }
  555. }
  556. }(userIdArr[i], name, positionId)
  557. }
  558. wg.Wait()
  559. return 0, ""
  560. }