175 lines
4.5 KiB
Go
175 lines
4.5 KiB
Go
package service
|
|
|
|
import (
|
|
"StuAcaWorksAI/dao"
|
|
"StuAcaWorksAI/proto"
|
|
"StuAcaWorksAI/worker"
|
|
"fmt"
|
|
"log"
|
|
"regexp"
|
|
"time"
|
|
)
|
|
|
|
func SetToolRedisList(key string, value string, expire int) (code int, message string) {
|
|
if expire == 0 {
|
|
if worker.PushRedisList(key, value) {
|
|
return proto.SuccessCode, "success"
|
|
} else {
|
|
return proto.OperationFailed, "push redis list failed"
|
|
}
|
|
} else if expire > 0 {
|
|
if worker.PushRedisListWithExpire(key, value, time.Duration(expire)) {
|
|
return proto.SuccessCode, "success"
|
|
} else {
|
|
return proto.OperationFailed, "push redis list with expire failed"
|
|
}
|
|
} else {
|
|
return proto.ParameterError, "expire time can not be negative"
|
|
}
|
|
}
|
|
|
|
func SetToolRedisSet(key string, value string, expire int) (code int, message string) {
|
|
if expire == 0 {
|
|
if worker.SetRedis(key, value) {
|
|
return proto.SuccessCode, "success"
|
|
} else {
|
|
return proto.OperationFailed, "set redis failed"
|
|
}
|
|
} else if expire > 0 {
|
|
if worker.SetRedisWithExpire(key, value, time.Duration(expire)) {
|
|
return proto.SuccessCode, "success"
|
|
} else {
|
|
return proto.OperationFailed, "set redis with expire failed"
|
|
}
|
|
} else {
|
|
return proto.ParameterError, "expire time can not be negative"
|
|
}
|
|
}
|
|
|
|
func SetToolRedisKV(key string, value string, expire int) (code int, message string) {
|
|
if expire == 0 {
|
|
if worker.SetRedis(key, value) {
|
|
return proto.SuccessCode, "success"
|
|
} else {
|
|
return proto.OperationFailed, "set redis failed"
|
|
}
|
|
} else if expire > 0 {
|
|
if worker.SetRedisWithExpire(key, value, time.Duration(expire)) {
|
|
return proto.SuccessCode, "success"
|
|
} else {
|
|
return proto.OperationFailed, "set redis with expire failed"
|
|
}
|
|
} else {
|
|
return proto.ParameterError, "expire time can not be negative"
|
|
}
|
|
}
|
|
|
|
func GetToolRedis(key string) (code int, message string) {
|
|
val := worker.GetRedis(key)
|
|
if val == "" {
|
|
return proto.OperationFailed, "get redis failed"
|
|
} else {
|
|
return proto.SuccessCode, val
|
|
}
|
|
}
|
|
|
|
func GetAllRedis() (code int, msg string, data []worker.RedisInfo) {
|
|
data, err := worker.GetAllRedisInfo()
|
|
if err != nil {
|
|
return proto.OperationFailed, err.Error(), nil
|
|
}
|
|
return proto.SuccessCode, "success", data
|
|
}
|
|
|
|
func SendEmail(email, subject, body string) {
|
|
//捕获异常
|
|
defer func() {
|
|
if err := recover(); err != nil {
|
|
fmt.Errorf("tool send mail error: %s", err)
|
|
}
|
|
}()
|
|
// TODO
|
|
// 发送邮件
|
|
// 邮件内容
|
|
// 邮件标题
|
|
// 收件人
|
|
// 发送邮件
|
|
// 发送邮件通知
|
|
// 发送邮件通知
|
|
var em worker.MyEmail
|
|
em.SmtpPassword = "nihzazdkmucnbhid"
|
|
em.SmtpHost = "pop.qq.com:587"
|
|
em.SmtpUserName = "354425203@qq.com"
|
|
em.SmtpPort = 587
|
|
em.ImapPort = 993
|
|
err := em.Send(subject, body, []string{email})
|
|
if err != nil {
|
|
fmt.Println("send mail error:", err)
|
|
}
|
|
}
|
|
|
|
// 地址校验
|
|
func CheckEmail(email string) bool {
|
|
//正则表达式判断是否是邮箱
|
|
pattern := `^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+$`
|
|
reg := regexp.MustCompile(pattern)
|
|
return reg.MatchString(email)
|
|
}
|
|
|
|
// 获取基础信息统计信息
|
|
func GetBaseDashboardInfo() proto.DashBoardStatisticsSt {
|
|
var res proto.DashBoardStatisticsSt
|
|
var err error
|
|
res.SessionNum, res.MessageCount, res.TodayMessageCount, err = dao.FindBaseSessionMessageStatisticsInfo()
|
|
if err != nil {
|
|
log.Println("get base dashboard info error:", err)
|
|
}
|
|
// 获取用户数
|
|
res.UserCount = dao.FindUserNum()
|
|
return res
|
|
}
|
|
|
|
// 获取最近7天的消息、会话数量
|
|
func GetRecent7DaysMessageSessionCount() (proto.DashBoardStatisticsWeekSt, error) {
|
|
var res proto.DashBoardStatisticsWeekSt
|
|
SessionCounts, err := dao.FindSessionCountByDate()
|
|
if err != nil {
|
|
log.Println("get recent 7 days session count error:", err)
|
|
return res, err
|
|
}
|
|
for i := 0; i < len(SessionCounts); i++ {
|
|
res.SessionCount[i] = SessionCounts[i]
|
|
}
|
|
MessageCounts, err := dao.FindMessageCountByDate()
|
|
if err != nil {
|
|
log.Println("get recent 7 days message count error:", err)
|
|
return res, err
|
|
}
|
|
for i := 0; i < len(MessageCounts); i++ {
|
|
res.MessageCount[i] = MessageCounts[i]
|
|
}
|
|
return res, nil
|
|
}
|
|
|
|
// 获取模型使用统计
|
|
func GetModelUsageStatistics() ([]proto.MessageModelIDCountSt, error) {
|
|
m, err := dao.GetAIStreamModelMsgCount()
|
|
if err != nil {
|
|
log.Println("get model usage statistics error:", err)
|
|
return nil, err
|
|
}
|
|
var res []proto.MessageModelIDCountSt
|
|
for _, v := range m {
|
|
model := dao.FindModelByModelID(v.ModelID)
|
|
if len(model) == 0 {
|
|
continue
|
|
}
|
|
res = append(res, proto.MessageModelIDCountSt{
|
|
ModelID: v.ModelID,
|
|
ModelName: v.ModelName,
|
|
Count: v.Count,
|
|
})
|
|
}
|
|
return res, nil
|
|
}
|