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

// CreateAiProvider 创建提供商
func (s *AiProviderService) CreateAiProvider(provider *app.AiProvider) error {
	return global.GVA_DB.Create(provider).Error
}

// DeleteAiProvider 删除提供商
func (s *AiProviderService) DeleteAiProvider(id uint, userID uint) error {
	return global.GVA_DB.Where("id = ? AND user_id = ?", id, userID).Delete(&app.AiProvider{}).Error
}

// UpdateAiProvider 更新提供商
func (s *AiProviderService) UpdateAiProvider(provider *app.AiProvider, userID uint) error {
	return global.GVA_DB.Where("user_id = ?", userID).Updates(provider).Error
}

// GetAiProvider 查询提供商
func (s *AiProviderService) GetAiProvider(id uint, userID uint) (provider app.AiProvider, err error) {
	err = global.GVA_DB.Where("id = ? AND user_id = ?", id, userID).First(&provider).Error
	return
}

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