package app

import (
	"git.echol.cn/loser/ai_proxy/server/global"
	"git.echol.cn/loser/ai_proxy/server/model/app"
	"git.echol.cn/loser/ai_proxy/server/model/common/request"
)

type AiPresetService struct{}

// CreateAiPreset 创建预设
func (s *AiPresetService) CreateAiPreset(preset *app.AiPreset) error {
	return global.GVA_DB.Create(preset).Error
}

// DeleteAiPreset 删除预设
func (s *AiPresetService) DeleteAiPreset(id uint, userID uint) error {
	return global.GVA_DB.Where("id = ? AND user_id = ?", id, userID).Delete(&app.AiPreset{}).Error
}

// UpdateAiPreset 更新预设
func (s *AiPresetService) UpdateAiPreset(preset *app.AiPreset, userID uint) error {
	return global.GVA_DB.Where("user_id = ?", userID).Updates(preset).Error
}

// GetAiPreset 查询预设
func (s *AiPresetService) GetAiPreset(id uint, userID uint) (preset app.AiPreset, err error) {
	err = global.GVA_DB.Where("id = ? AND user_id = ?", id, userID).First(&preset).Error
	return
}

// GetAiPresetList 获取预设列表
func (s *AiPresetService) GetAiPresetList(info request.PageInfo, userID uint) (list []app.AiPreset, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	db := global.GVA_DB.Model(&app.AiPreset{}).Where("user_id = ?", userID)
	err = db.Count(&total).Error
	if err != nil {
		return
	}
	err = db.Limit(limit).Offset(offset).Order("id desc").Find(&list).Error
	return
}
