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

// CreateProblemReply 创建ProblemReply记录
// Author [piexlmax](https://github.com/piexlmax)
func (problemReplyService *ProblemReplyService) CreateProblemReply(problemReply autocode.ProblemReply) (err error) {
	err = global.GVA_DB.Create(&problemReply).Error
	return err
}

// DeleteProblemReply 删除ProblemReply记录
// Author [piexlmax](https://github.com/piexlmax)
func (problemReplyService *ProblemReplyService) DeleteProblemReply(problemReply autocode.ProblemReply) (err error) {
	err = global.GVA_DB.Delete(&problemReply).Error
	return err
}

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

// UpdateProblemReply 更新ProblemReply记录
// Author [piexlmax](https://github.com/piexlmax)
func (problemReplyService *ProblemReplyService) UpdateProblemReply(problemReply autocode.ProblemReply) (err error) {
	err = global.GVA_DB.Save(&problemReply).Error
	return err
}

// GetProblemReply 根据id获取ProblemReply记录
// Author [piexlmax](https://github.com/piexlmax)
func (problemReplyService *ProblemReplyService) GetProblemReply(id uint) (err error, problemReply autocode.ProblemReply) {
	err = global.GVA_DB.Where("id = ?", id).First(&problemReply).Error
	return
}

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