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 AiPresetBindingService struct{}

// CreateAiPresetBinding 创建绑定
func (s *AiPresetBindingService) CreateAiPresetBinding(binding *app.AiPresetBinding) error {
	return global.GVA_DB.Create(binding).Error
}

// DeleteAiPresetBinding 删除绑定
func (s *AiPresetBindingService) DeleteAiPresetBinding(id uint, userID uint) error {
	return global.GVA_DB.Where("id = ? AND user_id = ?", id, userID).Delete(&app.AiPresetBinding{}).Error
}

// UpdateAiPresetBinding 更新绑定
func (s *AiPresetBindingService) UpdateAiPresetBinding(binding *app.AiPresetBinding, userID uint) error {
	return global.GVA_DB.Where("user_id = ?", userID).Updates(binding).Error
}

// GetAiPresetBinding 查询绑定
func (s *AiPresetBindingService) GetAiPresetBinding(id uint, userID uint) (binding app.AiPresetBinding, err error) {
	err = global.GVA_DB.Preload("Preset").Preload("Provider").Where("id = ? AND user_id = ?", id, userID).First(&binding).Error
	return
}

// GetAiPresetBindingList 获取绑定列表
func (s *AiPresetBindingService) GetAiPresetBindingList(info request.PageInfo, userID uint) (list []app.AiPresetBinding, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	db := global.GVA_DB.Model(&app.AiPresetBinding{}).Preload("Preset").Preload("Provider").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
}
