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

// CreateAssignment 创建Assignment记录
// Author [piexlmax](https://github.com/piexlmax)
func (assignmentService *AssignmentService) CreateAssignment(assignment autocode.Assignment) (err error) {
	err = global.GVA_DB.Create(&assignment).Error
	return err
}

// DeleteAssignment 删除Assignment记录
// Author [piexlmax](https://github.com/piexlmax)
func (assignmentService *AssignmentService) DeleteAssignment(assignment autocode.Assignment) (err error) {
	err = global.GVA_DB.Delete(&assignment).Error
	return err
}

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

// UpdateAssignment 更新Assignment记录
// Author [piexlmax](https://github.com/piexlmax)
func (assignmentService *AssignmentService) UpdateAssignment(assignment autocode.Assignment) (err error) {
	err = global.GVA_DB.Save(&assignment).Error
	return err
}

// GetAssignment 根据id获取Assignment记录
// Author [piexlmax](https://github.com/piexlmax)
func (assignmentService *AssignmentService) GetAssignment(id uint) (err error, assignment autocode.Assignment) {
	err = global.GVA_DB.Where("id = ?", id).First(&assignment).Error
	return
}

// GetAssignmentInfoList 分页获取Assignment记录
// Author [piexlmax](https://github.com/piexlmax)
func (assignmentService *AssignmentService) GetAssignmentInfoList(info autoCodeReq.AssignmentSearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&autocode.Assignment{})
	var assignments []autocode.Assignment
	// 如果有条件搜索 下方会自动创建搜索语句
	err = db.Count(&total).Error
	err = db.Limit(limit).Offset(offset).Find(&assignments).Error
	return err, assignments, total
}
