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

// CreateProblemType 创建ProblemType记录
// Author [piexlmax](https://github.com/piexlmax)
func (problemTypeService *ProblemTypeService) CreateProblemType(problemType autocode.ProblemType) (err error) {
	err = global.GVA_DB.Create(&problemType).Error
	return err
}

// DeleteProblemType 删除ProblemType记录
// Author [piexlmax](https://github.com/piexlmax)
func (problemTypeService *ProblemTypeService) DeleteProblemType(problemType autocode.ProblemType) (err error) {
	err = global.GVA_DB.Delete(&problemType).Error
	return err
}

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

// UpdateProblemType 更新ProblemType记录
// Author [piexlmax](https://github.com/piexlmax)
func (problemTypeService *ProblemTypeService) UpdateProblemType(problemType autocode.ProblemType) (err error) {
	err = global.GVA_DB.Save(&problemType).Error
	return err
}

// GetProblemType 根据id获取ProblemType记录
// Author [piexlmax](https://github.com/piexlmax)
func (problemTypeService *ProblemTypeService) GetProblemType(id uint) (err error, problemType autocode.ProblemType) {
	err = global.GVA_DB.Where("id = ?", id).First(&problemType).Error
	return
}

func (problemTypeService *ProblemTypeService) GetProblemTypeIds(id []string) (err error, problemType []autocode.ProblemType) {
	var problemTypes []autocode.ProblemType
	err = global.GVA_DB.Where("id in ?", id).Find(&problemTypes).Error
	return err, problemTypes
}

// GetProblemTypeInfoList 分页获取ProblemType记录
// Author [piexlmax](https://github.com/piexlmax)
func (problemTypeService *ProblemTypeService) GetProblemTypeInfoList(info autoCodeReq.ProblemTypeSearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&autocode.ProblemType{})
	if info.SiteType != nil {
		db.Where("site_type = ?", info.SiteType)
	}
	if info.Problem != "" {
		db.Where("problem like ?", "%"+info.Problem+"%")
	}
	var problemTypes []autocode.ProblemType
	// 如果有条件搜索 下方会自动创建搜索语句
	err = db.Count(&total).Error
	err = db.Limit(limit).Offset(offset).Find(&problemTypes).Error
	return err, problemTypes, total
}

func (problemTypeService *ProblemTypeService) GetProblemTypeInfoListBySiteType(siteType *int) (err error, list interface{}, total int64) {
	// 创建db
	db := global.GVA_DB.Model(&autocode.ProblemType{})
	db.Where("site_type = ?", siteType)
	var problemTypes []autocode.ProblemType
	// 如果有条件搜索 下方会自动创建搜索语句
	err = db.Count(&total).Error
	err = db.Find(&problemTypes).Error
	return err, problemTypes, total
}

func (problemTypeService *ProblemTypeService) GetProblemTypeInfoListAll() (err error, list []autocode.ProblemType) {
	// 创建db
	db := global.GVA_DB.Model(&autocode.ProblemType{})
	var problemTypes []autocode.ProblemType
	// 如果有条件搜索 下方会自动创建搜索语句
	err = db.Find(&problemTypes).Order("site_type").Error
	return err, problemTypes
}
