Files
EdgeAPI/internal/db/models/message_recipient_dao.go

229 lines
6.3 KiB
Go
Raw Normal View History

2021-04-05 20:48:33 +08:00
package models
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
2021-04-12 19:19:15 +08:00
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
2021-04-05 20:48:33 +08:00
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types"
"regexp"
2021-04-05 20:48:33 +08:00
)
const (
MessageRecipientStateEnabled = 1 // 已启用
MessageRecipientStateDisabled = 0 // 已禁用
)
type MessageRecipientDAO dbs.DAO
func NewMessageRecipientDAO() *MessageRecipientDAO {
return dbs.NewDAO(&MessageRecipientDAO{
DAOObject: dbs.DAOObject{
DB: Tea.Env,
Table: "edgeMessageRecipients",
Model: new(MessageRecipient),
PkName: "id",
},
}).(*MessageRecipientDAO)
}
var SharedMessageRecipientDAO *MessageRecipientDAO
func init() {
dbs.OnReady(func() {
SharedMessageRecipientDAO = NewMessageRecipientDAO()
})
}
2021-04-12 19:19:15 +08:00
// EnableMessageRecipient 启用条目
2021-04-05 20:48:33 +08:00
func (this *MessageRecipientDAO) EnableMessageRecipient(tx *dbs.Tx, id int64) error {
_, err := this.Query(tx).
Pk(id).
Set("state", MessageRecipientStateEnabled).
Update()
return err
}
2021-04-12 19:19:15 +08:00
// DisableMessageRecipient 禁用条目
2021-04-05 20:48:33 +08:00
func (this *MessageRecipientDAO) DisableMessageRecipient(tx *dbs.Tx, id int64) error {
_, err := this.Query(tx).
Pk(id).
Set("state", MessageRecipientStateDisabled).
Update()
return err
}
2021-04-12 19:19:15 +08:00
// FindEnabledMessageRecipient 查找启用中的条目
func (this *MessageRecipientDAO) FindEnabledMessageRecipient(tx *dbs.Tx, recipientId int64, cacheMap maps.Map,
) (*MessageRecipient, error) {
if cacheMap == nil {
cacheMap = maps.Map{}
}
var cacheKey = this.Table + ":record:" + types.String(recipientId)
var cache = cacheMap.Get(cacheKey)
if cache != nil {
return cache.(*MessageRecipient), nil
}
2021-04-05 20:48:33 +08:00
result, err := this.Query(tx).
Pk(recipientId).
2021-04-05 20:48:33 +08:00
Attr("state", MessageRecipientStateEnabled).
Find()
if result == nil {
return nil, err
}
cacheMap[cacheKey] = result
2021-04-05 20:48:33 +08:00
return result.(*MessageRecipient), err
}
2021-04-12 19:19:15 +08:00
// CreateRecipient 创建接收人
func (this *MessageRecipientDAO) CreateRecipient(tx *dbs.Tx, adminId int64, instanceId int64, user string, groupIds []int64, description string, timeFrom string, timeTo string) (int64, error) {
2021-04-05 20:48:33 +08:00
op := NewMessageRecipientOperator()
op.AdminId = adminId
op.InstanceId = instanceId
op.User = user
op.Description = description
// 分组
if len(groupIds) == 0 {
groupIds = []int64{}
}
groupIdsJSON, err := json.Marshal(groupIds)
if err != nil {
return 0, err
}
op.GroupIds = groupIdsJSON
// 判断格式
var timeReg = regexp.MustCompile(`^\d+:\d+:\d+$`)
if timeReg.MatchString(timeFrom) {
op.TimeFrom = timeFrom
}
if timeReg.MatchString(timeTo) {
op.TimeTo = timeTo
}
2021-04-05 20:48:33 +08:00
op.IsOn = true
op.State = MessageRecipientStateEnabled
return this.SaveInt64(tx, op)
}
2021-04-12 19:19:15 +08:00
// UpdateRecipient 修改接收人
func (this *MessageRecipientDAO) UpdateRecipient(tx *dbs.Tx, recipientId int64, adminId int64, instanceId int64, user string, groupIds []int64, description string, timeFrom string, timeTo string, isOn bool) error {
2021-04-05 20:48:33 +08:00
if recipientId <= 0 {
return errors.New("invalid recipientId")
}
op := NewMessageRecipientOperator()
op.Id = recipientId
op.AdminId = adminId
op.InstanceId = instanceId
op.User = user
// 分组
if len(groupIds) == 0 {
groupIds = []int64{}
}
groupIdsJSON, err := json.Marshal(groupIds)
if err != nil {
return err
}
op.GroupIds = groupIdsJSON
op.Description = description
// 判断格式
var timeReg = regexp.MustCompile(`^\d+:\d+:\d+$`)
if timeReg.MatchString(timeFrom) {
op.TimeFrom = timeFrom
} else {
op.TimeFrom = dbs.SQL("NULL")
}
if timeReg.MatchString(timeTo) {
op.TimeTo = timeTo
} else {
op.TimeTo = dbs.SQL("NULL")
}
2021-04-05 20:48:33 +08:00
op.IsOn = isOn
return this.Save(tx, op)
}
2021-04-12 19:19:15 +08:00
// CountAllEnabledRecipients 计算接收人数量
2021-04-05 20:48:33 +08:00
func (this *MessageRecipientDAO) CountAllEnabledRecipients(tx *dbs.Tx, adminId int64, groupId int64, mediaType string, keyword string) (int64, error) {
query := this.Query(tx)
if adminId > 0 {
query.Attr("adminId", adminId)
}
if groupId > 0 {
2021-04-12 19:19:15 +08:00
query.Where("JSON_CONTAINS(groupIds, :groupId)").
Param("groupId", numberutils.FormatInt64(groupId))
2021-04-05 20:48:33 +08:00
}
if len(mediaType) > 0 {
query.Where("instanceId IN (SELECT id FROM "+SharedMessageMediaInstanceDAO.Table+" WHERE state=1 AND mediaType=:mediaType)").
Param("mediaType", mediaType)
}
if len(keyword) > 0 {
query.Where("(`user` LIKE :keyword OR description LIKE :keyword)").
Param("keyword", "%"+keyword+"%")
}
return query.
State(MessageRecipientStateEnabled).
Where("adminId IN (SELECT id FROM " + SharedAdminDAO.Table + " WHERE state=1)").
Where("instanceId IN (SELECT id FROM " + SharedMessageMediaInstanceDAO.Table + " WHERE state=1)").
Count()
}
2021-04-12 19:19:15 +08:00
// ListAllEnabledRecipients 列出单页接收人
2021-04-05 20:48:33 +08:00
func (this *MessageRecipientDAO) ListAllEnabledRecipients(tx *dbs.Tx, adminId int64, groupId int64, mediaType string, keyword string, offset int64, size int64) (result []*MessageRecipient, err error) {
query := this.Query(tx)
if adminId > 0 {
query.Attr("adminId", adminId)
}
if groupId > 0 {
2021-04-12 19:19:15 +08:00
query.Where("JSON_CONTAINS(groupIds, :groupId)").
Param("groupId", numberutils.FormatInt64(groupId))
2021-04-05 20:48:33 +08:00
}
if len(mediaType) > 0 {
query.Where("instanceId IN (SELECT id FROM "+SharedMessageMediaInstanceDAO.Table+" WHERE state=1 AND mediaType=:mediaType)").
Param("mediaType", mediaType)
}
if len(keyword) > 0 {
query.Where("(`user` LIKE :keyword OR description LIKE :keyword)").
Param("keyword", "%"+keyword+"%")
}
_, err = query.
State(MessageRecipientStateEnabled).
Where("adminId IN (SELECT id FROM " + SharedAdminDAO.Table + " WHERE state=1)").
Where("instanceId IN (SELECT id FROM " + SharedMessageMediaInstanceDAO.Table + " WHERE state=1)").
DescPk().
Offset(offset).
Limit(size).
Slice(&result).
FindAll()
return
}
2021-04-12 19:19:15 +08:00
// FindAllEnabledAndOnRecipientIdsWithGroup 查找某个分组下的所有可用接收人ID
func (this *MessageRecipientDAO) FindAllEnabledAndOnRecipientIdsWithGroup(tx *dbs.Tx, groupId int64) ([]int64, error) {
ones, err := this.Query(tx).
Where("JSON_CONTAINS(groupIds, :groupId)").
Param("groupId", numberutils.FormatInt64(groupId)).
State(MessageRecipientStateEnabled).
ResultPk().
FindAll()
if err != nil {
return nil, err
}
result := []int64{}
for _, one := range ones {
result = append(result, int64(one.(*MessageRecipient).Id))
}
return result, nil
}