package common import ( "app.yhyue.com/moapp/MessageCenter/entity" "app.yhyue.com/moapp/MessageCenter/rpc/type/message" 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, "删除消息成功" } // 未读消息合计 func (service *MessageService) CountUnread(userId string) int64 { var count int64 var msgTypes []string for _, v := range entity.MessageColumn { msgTypes = append(msgTypes, fmt.Sprintf(`"%s"`, qutil.InterfaceToStr(v["group_id"]))) } 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 GROUP BY group_id ORDER BY FIELD(`group_id`,%s)", strings.Join(msgTypes, ",")), userId) if query != nil && len(*query) > 0 { for _, v := range *query { count += qutil.Int64All(v["count"]) key := fmt.Sprintf(MsgCountKey, userId, qutil.IntAll(v["group_id"])) redis.Put(redisModule, key, qutil.Int64All(v["count"]), -1) } } return 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 }