go-wxhelper/service/friend.go

89 lines
2.4 KiB
Go
Raw Normal View History

package service
import (
"go-wechat/client"
"go-wechat/entity"
"go-wechat/vo"
"log"
"strings"
)
// GetAllFriend
// @description: 取出所有好友
// @return friends
// @return groups
// @return err
func GetAllFriend() (friends, groups []vo.FriendItem, err error) {
var records []vo.FriendItem
err = client.MySQL.
Table("t_friend AS tf").
//Joins("LEFT JOIN t_message AS tm ON tf.wxid = tm.from_user").
//Select("tf.*", "MAX(tm.create_at) AS last_active").
Select("tf.*").
//Group("tf.wxid").
Order("tf.last_active DESC").
Find(&records).Error
if err != nil {
return
}
for _, record := range records {
if strings.HasSuffix(record.Wxid, "@chatroom") {
groups = append(groups, record)
} else {
friends = append(friends, record)
}
}
return
}
// GetAllEnableAI
// @description: 取出所有启用了AI的好友或群组
// @return []entity.Friend
func GetAllEnableAI() (records []entity.Friend, err error) {
err = client.MySQL.Where("enable_ai = ?", 1).Find(&records).Error
return
}
// GetAllEnableChatRank
// @description: 取出所有启用了聊天排行榜的群组
// @return records
// @return err
func GetAllEnableChatRank() (records []entity.Friend, err error) {
err = client.MySQL.Where("enable_chat_rank = ?", 1).Where("wxid LIKE '%@chatroom'").Find(&records).Error
return
}
// CheckIsEnableCommand
// @description: 检查用户是否启用了指令
// @param userId
// @return flag
func CheckIsEnableCommand(userId string) (flag bool) {
var coo int64
client.MySQL.Model(&entity.Friend{}).Where("enable_command = 1").Where("wxid = ?", userId).Count(&coo)
return coo > 0
}
// updateLastActive
// @description: 更新最后活跃时间
// @param msg
func updateLastActive(msg entity.Message) {
var err error
// 如果是群,更新群成员最后活跃时间
if strings.HasSuffix(msg.FromUser, "@chatroom") {
err = client.MySQL.Model(&entity.GroupUser{}).
Where("group_id = ?", msg.FromUser).
Where("wxid = ?", msg.GroupUser).
Update("last_active", msg.CreateTime).Error
if err != nil {
log.Printf("更新群成员最后活跃时间失败, 错误信息: %v", err)
}
}
// 更新群或者好友活跃时间
err = client.MySQL.Model(&entity.Friend{}).
Where("wxid = ?", msg.FromUser).
Update("last_active", msg.CreateTime).Error
if err != nil {
log.Printf("更新群或者好友活跃时间失败, 错误信息: %v", err)
}
}