123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- package common
- import (
- "app.yhyue.com/moapp/MessageCenter/entity"
- "app.yhyue.com/moapp/MessageCenter/rpc/type/message"
- "app.yhyue.com/moapp/MessageCenter/util"
- qutil "app.yhyue.com/moapp/jybase/common"
- "app.yhyue.com/moapp/jybase/redis"
- "errors"
- "fmt"
- "log"
- "strings"
- "time"
- )
- type MessageService struct{}
- // 修改消息阅读状态
- func (service *MessageService) ChangeReadStatus(data *message.ChangeReadStatusReq) (int64, string) {
- msg := entity.Mysql.FindOne("message", map[string]interface{}{"id": data.Id, "isdel": 1, "appid": data.Appid}, "", "")
- //log.Println("查询到消息:", msg)
- if msg == nil {
- return 0, "该消息不存在"
- }
- b := entity.Mysql.Update("message", map[string]interface{}{"id": data.Id, "isdel": 1}, map[string]interface{}{"isRead": int(data.ReadStatus)})
- if !b {
- return 0, "修改消息阅读状态失败"
- }
- MsgCountMinusOne(qutil.ObjToString((*msg)["receive_userid"]), data.Appid, qutil.Int64All((*msg)["group_id"]))
- return 1, "修改消息阅读状态成功"
- }
- // 删除消息
- func (service *MessageService) DeleteMessage(id []string, appId string) (int64, string) {
- orm := entity.Engine.NewSession()
- defer orm.Close()
- msg := entity.Mysql.FindOne("message", map[string]interface{}{"id": id, "appid": appId}, "", "")
- if msg == nil {
- return 0, "该消息不存在"
- }
- m := entity.Message{}
- m.Isdel = -1
- count, err := orm.Where("appid=?", appId).In("id", id).Cols("isdel").Update(&m)
- if err != nil || count == 0 {
- log.Println(err)
- orm.Rollback()
- return 0, "删除消息失败"
- }
- err2 := orm.Commit()
- if err2 != nil {
- return 0, "删除消息失败"
- }
- FindUserMsg(message.FindUserMsgReq{
- UserId: qutil.ObjToString((*msg)["receive_userid"]),
- Appid: appId,
- OffSet: 1,
- PageSize: 5,
- MsgType: -1,
- Read: 0,
- }, true)
- return 1, "删除消息成功"
- }
- // 未读消息合计 isRedis 是否需要初始化redis
- func (service *MessageService) CountUnread(userId string, isRedis bool) (map[string]int64, int64) {
- var (
- count int64
- msgTypes, groupIds []string
- )
- data := make(map[string]int64)
- for _, v := range entity.MessageColumn {
- if util.IntAll(v["group_id"]) > 0 && util.IntAll(v["group_id"]) < 999 {
- //去除全部与私信
- msgTypes = append(msgTypes, fmt.Sprintf(`"%s"`, qutil.InterfaceToStr(v["group_id"])))
- key := fmt.Sprintf(MsgCountKey, userId, util.IntAll(v["group_id"]))
- groupIds = append(groupIds, qutil.InterfaceToStr(v["group_id"]))
- if exists, _ := redis.Exists(redisModule, key); exists {
- ct := util.Int64All(redis.GetInt(redisModule, key))
- data[qutil.InterfaceToStr(v["group_id"])] = ct
- count += ct
- }
- }
- }
- if len(msgTypes) > 0 && len(msgTypes) != len(data) {
- count = 0
- query := entity.Mysql.SelectBySql(fmt.Sprintf("SELECT group_id,COUNT(CASE WHEN isRead=0 THEN 1 END) as count FROM message where receive_userid=? and isdel=1 and group_id IS NOT NULL GROUP BY group_id ORDER BY FIELD(`group_id`,%s)", strings.Join(msgTypes, ",")), userId)
- if query != nil && len(*query) > 0 {
- for _, v := range *query {
- unread := qutil.Int64All(v["count"])
- data[qutil.InterfaceToStr(v["group_id"])] = unread
- count += unread
- }
- }
- if isRedis { //初始化未读数
- for _, v1 := range groupIds {
- key := fmt.Sprintf(MsgCountKey, userId, qutil.IntAll(v1))
- redis.Put(redisModule, key, data[v1], -1)
- }
- }
- }
- return data, count
- }
- func (service *MessageService) CountClassUnread(userId string, groupId int64) (classCount map[string]int64, total int64) {
- var (
- count int64
- )
- data := make(map[string]int64)
- if _, ok := entity.ClassSearchMap[groupId]; !ok {
- return
- }
- for classMsgType_, _ := range entity.ClassSearchMap[groupId] {
- key := fmt.Sprintf(MsgClassCountKey, userId, classMsgType_)
- if exists, _ := redis.Exists(MsgClassCountKey, key); exists {
- ct := util.Int64All(redis.GetInt(MsgClassCountKey, key))
- data[fmt.Sprintf("%d", classMsgType_)] = ct
- count += ct
- } else {
- // todo
- entity.Mysql.CountBySql("select * ")
- //key := fmt.Sprintf(MsgClassCountKey, userId,classMsgType_)
- //redis.Put(redisModule, key, , -1)
- }
- }
- return data, count
- }
- // 查询消息详情
- func FindMessageDetail(id, msgLogId int64, userId string) (msg *map[string]interface{}, err error) {
- if id > 0 {
- msg = entity.Mysql.FindOne("message", map[string]interface{}{"id": id}, "", "")
- } else {
- msg = entity.Mysql.FindOne("message", map[string]interface{}{"receive_userid": userId, "msg_log_id": msgLogId}, "", "")
- }
- if msg != nil && len(*msg) > 0 {
- return msg, nil
- }
- return nil, errors.New("没有查询到消息")
- }
- // GetMsgType 消息的分类
- func (service *MessageService) GetMsgType() (data []*message.MsgTypes, err error) {
- types := entity.Mysql.SelectBySql("SELECT * FROM `message_group` WHERE group_id > 0 ORDER BY sequence ASC")
- if types != nil && len(*types) > 0 {
- for _, val := range *types {
- data = append(data, &message.MsgTypes{
- MsgType: qutil.Int64All(val["group_id"]),
- Name: qutil.ObjToString(val["name"]),
- Img: qutil.ObjToString(val["img"]),
- Code: qutil.ObjToString(val["switch"]),
- DisplayPlatform: qutil.ObjToString(val["display_platform"]),
- })
- }
- return data, nil
- }
- return nil, nil
- }
- func (service *MessageService) MsgOpenLog(platFrom, msgLogId int64, userId string) int64 {
- //判断用户是否已经在pc端打开过
- count := entity.Mysql.CountBySql("SELECT COUNT(*) FROM message_open_log WHERE msg_log_id = ? and platform = ? and userid = ?", msgLogId, platFrom, userId)
- if count <= 0 {
- tmp := map[string]interface{}{
- "msg_log_id": msgLogId,
- "platform": platFrom,
- "userid": userId,
- "createtime": time.Now().Format("2006-01-02 15:04:05"),
- }
- SaveCache <- tmp
- }
- return 0
- }
|