package autocode

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/autocode"
	autoCodeReq "github.com/flipped-aurora/gin-vue-admin/server/model/autocode/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
)

type ContentService struct {
}

// CreateContent 创建Content记录
// Author [piexlmax](https://github.com/piexlmax)
func (contentService *ContentService) CreateContent(content autocode.Content) (err error) {
	err = global.GVA_DB.Create(&content).Error
	return err
}

// DeleteContent 删除Content记录
// Author [piexlmax](https://github.com/piexlmax)
func (contentService *ContentService) DeleteContent(content autocode.Content) (err error) {
	err = global.GVA_DB.Delete(&content).Error
	return err
}

// DeleteContentByIds 批量删除Content记录
// Author [piexlmax](https://github.com/piexlmax)
func (contentService *ContentService) DeleteContentByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]autocode.Content{}, "id in ?", ids.Ids).Error
	return err
}

// UpdateContent 更新Content记录
// Author [piexlmax](https://github.com/piexlmax)
func (contentService *ContentService) UpdateContent(content autocode.Content) (err error) {
	err = global.GVA_DB.Save(&content).Error
	return err
}

// GetContent 根据id获取Content记录
// Author [piexlmax](https://github.com/piexlmax)
func (contentService *ContentService) GetContent(id uint) (err error, content autocode.Content) {
	err = global.GVA_DB.Where("id = ?", id).First(&content).Error
	return
}

// GetContentInfoList 分页获取Content记录
// Author [piexlmax](https://github.com/piexlmax)
func (contentService *ContentService) GetContentInfoList(info autoCodeReq.ContentSearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&autocode.Content{})
	if info.Type != "" {
		db.Where("type = ?", info.Type)
	}
	var contents []autocode.Content
	// 如果有条件搜索 下方会自动创建搜索语句
	err = db.Count(&total).Error
	err = db.Limit(limit).Offset(offset).Find(&contents).Error
	return err, contents, total
}
