package service import ( "app.yhyue.com/moapp/MessageCenter/entity" "app.yhyue.com/moapp/MessageCenter/rpc/message" "app.yhyue.com/moapp/MessageCenter/util" qutil "app.yhyue.com/moapp/jybase/common" "errors" "github.com/tal-tech/go-zero/core/logx" "log" "strconv" ) type MessageService struct { } // 修改消息阅读状态 func (service *MessageService) ChangeReadStatus(data *message.ChangeReadStatusRequest) (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) { sql := "SELECT * FROM message receive_userid=? and isdel=1 and appid=? and msg_type=?" valueList := []interface{}{userId, appId, msgType} if isRead != -1 { sql = sql + " and isRead=?" valueList = append(valueList, isRead) } query := map[string]interface{}{ "receive_userid": userId, "isdel": 1, "appid": appId, "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) ReceiveMsgType(userId string, appId string) ([]int64, error) { orm := entity.Engine.NewSession() defer orm.Close() m := []entity.Message{} err := orm.Distinct("msg_type").Where("receive_userid=? and isdel=1 and appid=?", userId, appId).Find(&m) if err != nil { return nil, err } msgTypeList := []int64{} for _, v := range m { msgTypeList = append(msgTypeList, int64(v.MsgType)) } return msgTypeList, 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 }