123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- 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"
- "errors"
- "github.com/zeromicro/go-zero/core/logx"
- "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"]), strconv.Itoa(int((*msg)["msg_type"].(int64))), data.Appid)
- 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, int64) {
- query := map[string]interface{}{
- "receive_userid": userId,
- "isRead": 0,
- "isdel": 1,
- "appid": appId,
- }
- count := entity.Mysql.Count("message", query)
- logx.Info("未读消息数量:", count)
- 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) {
- orm := entity.Engine.NewSession()
- defer orm.Close()
- types := entity.Mysql.Find("message_column", map[string]interface{}{}, "", "sequence ASC", -1, -1)
- 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, strconv.Itoa(msgType), appId)
- return 1, nil
- }
|