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" "strconv" ) 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)["msg_type"])) 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, "删除消息成功" } // 未读消息合计 func (service *MessageService) CountUnread(userId string, appId string) (int64, string, int) { count := 0 types := entity.Mysql.Find("message_column", map[string]interface{}{}, `"msg_type"`, "", -1, -1) if types != nil && len(*types) > 0 { for _, v := range *types { key := fmt.Sprintf(MsgCountKey, userId, qutil.IntAll(v["msg_type"])) if exists, _ := redis.Exists(redisModule, key); exists { count += redis.GetInt(redisModule, key) } } } return 1, "查询未读消息成功", count } // 获取指定用户指定分类最新一条消息 func (service *MessageService) LastMessage(userId string, appId string, msgType int64, isRead int64) (*message.Messages, error) { query := map[string]interface{}{ "receive_userid": userId, "isdel": 1, "appid": appId, } if isRead != -1 { query["isRead"] = isRead } if msgType != -1 { query["msg_type"] = msgType } lastMsg := entity.Mysql.FindOne("message", query, "", "createtime desc") if lastMsg != nil && len(*lastMsg) > 0 { _id := util.Int64All((*lastMsg)["id"]) id := strconv.FormatInt(_id, 10) msg := message.Messages{ Id: id, Appid: qutil.ObjToString((*lastMsg)["appid"]), ReceiveUserId: qutil.ObjToString((*lastMsg)["receive_userid"]), ReceiveName: qutil.ObjToString((*lastMsg)["receive_name"]), SendUserId: qutil.ObjToString((*lastMsg)["send_userid"]), SendName: qutil.ObjToString((*lastMsg)["send_name"]), Createtime: qutil.ObjToString((*lastMsg)["createtime"]), Title: qutil.ObjToString((*lastMsg)["title"]), MsgType: qutil.Int64All((*lastMsg)["msg_type"]), Link: qutil.ObjToString((*lastMsg)["link"]), CiteId: qutil.Int64All((*lastMsg)["cite_id"]), Content: qutil.ObjToString((*lastMsg)["content"]), IsRead: qutil.Int64All((*lastMsg)["isRead"]), MsgLogId: qutil.Int64All((*lastMsg)["msg_log_id"]), } return &msg, nil } else { return nil, nil } } // 查询消息详情 func FindMessageDetail(id string) (entity.Message, error) { orm := entity.Engine.NewSession() defer orm.Close() mess := entity.Message{} err := orm.Table("message").Select("*").Where("id = ?", id).Find(&mess) if err != nil { return mess, err } return mess, nil } // 获取用户未读消息分类及数量 及分类下的最新一条消息 func (service *MessageService) ClassCountAndMessage(userId string, appId string) ([]*message.ResCount, error) { query := entity.Mysql.SelectBySql("SELECT msg_type,COUNT(CASE WHEN isRead=0 THEN 1 END) as count FROM message where receive_userid=? and isdel=1 and appid=? GROUP BY msg_type ORDER BY FIELD(`msg_type`,\"1\",\"2\",\"3\",\"4\",\"5\",\"6\",\"7\")", userId, appId) typeCount := []*message.ResCount{} // 未读消息分类及数量 if query != nil && len(*query) > 0 { for _, v := range *query { typeCount = append(typeCount, &message.ResCount{MsgType: v["msg_type"].(int64), Count: v["count"].(int64)}) } } return typeCount, nil } // 已接收到消息的分类 func (service *MessageService) GetMsgType() (data []*message.MsgTypes, err error) { types := entity.Mysql.SelectBySql("SELECT * FROM `message_column` WHERE msg_type > 0 ORDER BY sequence ASC") if types != nil && len(*types) > 0 { for _, val := range *types { data = append(data, &message.MsgTypes{ MsgType: qutil.Int64All(val["msg_type"]), 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) UpdateMessageReadStatus(msgType int, receiveUserid, appId string) (int, error) { query := map[string]interface{}{ "receive_userid": receiveUserid, "msg_type": msgType, "appid": appId, } b := entity.Mysql.Update("message", query, map[string]interface{}{"isRead": 1}) if !b { return 0, errors.New("修改消息已读出错") } MsgCountZero(receiveUserid, appId, qutil.Int64All(msgType)) return 1, nil }