75 lines
2.3 KiB
Go
75 lines
2.3 KiB
Go
package bot
|
|
|
|
import (
|
|
"context"
|
|
"git.echol.cn/loser/lckt/global"
|
|
"git.echol.cn/loser/lckt/model/bot"
|
|
botReq "git.echol.cn/loser/lckt/model/bot/request"
|
|
)
|
|
|
|
type BotService struct{}
|
|
|
|
// CreateBot 创建机器人记录
|
|
// Author [yourname](https://github.com/yourname)
|
|
func (btService *BotService) CreateBot(ctx context.Context, bt *bot.Bot) (err error) {
|
|
err = global.GVA_DB.Create(bt).Error
|
|
return err
|
|
}
|
|
|
|
// DeleteBot 删除机器人记录
|
|
// Author [yourname](https://github.com/yourname)
|
|
func (btService *BotService) DeleteBot(ctx context.Context, ID string) (err error) {
|
|
err = global.GVA_DB.Delete(&bot.Bot{}, "id = ?", ID).Error
|
|
return err
|
|
}
|
|
|
|
// DeleteBotByIds 批量删除机器人记录
|
|
// Author [yourname](https://github.com/yourname)
|
|
func (btService *BotService) DeleteBotByIds(ctx context.Context, IDs []string) (err error) {
|
|
err = global.GVA_DB.Delete(&[]bot.Bot{}, "id in ?", IDs).Error
|
|
return err
|
|
}
|
|
|
|
// UpdateBot 更新机器人记录
|
|
// Author [yourname](https://github.com/yourname)
|
|
func (btService *BotService) UpdateBot(ctx context.Context, bt bot.Bot) (err error) {
|
|
err = global.GVA_DB.Model(&bot.Bot{}).Where("id = ?", bt.ID).Updates(&bt).Error
|
|
return err
|
|
}
|
|
|
|
// GetBot 根据ID获取机器人记录
|
|
// Author [yourname](https://github.com/yourname)
|
|
func (btService *BotService) GetBot(ctx context.Context, ID string) (bt bot.Bot, err error) {
|
|
err = global.GVA_DB.Where("id = ?", ID).First(&bt).Error
|
|
return
|
|
}
|
|
|
|
// GetBotInfoList 分页获取机器人记录
|
|
// Author [yourname](https://github.com/yourname)
|
|
func (btService *BotService) GetBotInfoList(ctx context.Context, info botReq.BotSearch) (list []bot.Bot, total int64, err error) {
|
|
limit := info.PageSize
|
|
offset := info.PageSize * (info.Page - 1)
|
|
// 创建db
|
|
db := global.GVA_DB.Model(&bot.Bot{})
|
|
var bts []bot.Bot
|
|
// 如果有条件搜索 下方会自动创建搜索语句
|
|
if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
|
|
db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
|
|
}
|
|
err = db.Count(&total).Error
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
if limit != 0 {
|
|
db = db.Limit(limit).Offset(offset)
|
|
}
|
|
|
|
err = db.Find(&bts).Error
|
|
return bts, total, err
|
|
}
|
|
func (btService *BotService) GetBotPublic(keyWord botReq.FindKey) (bt bot.Bot, err error) {
|
|
err = global.GVA_DB.Where("keyword Like ?", "%"+keyWord.KeyWord+"%").First(&bt).Error
|
|
return
|
|
}
|