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"
	"gorm.io/gorm"
)

type ProblemInfoService struct {
}

// CreateProblemInfo 创建ProblemInfo记录
// Author [piexlmax](https://github.com/piexlmax)
func (problemInfoService *ProblemInfoService) CreateProblemInfo(problemInfo autocode.ProblemInfo) (err error) {
	err = global.GVA_DB.Create(&problemInfo).Error
	return err
}

// DeleteProblemInfo 删除ProblemInfo记录
// Author [piexlmax](https://github.com/piexlmax)
func (problemInfoService *ProblemInfoService) DeleteProblemInfo(problemInfo autocode.ProblemInfo) (err error) {
	err = global.GVA_DB.Delete(&problemInfo).Error
	return err
}

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

// UpdateProblemInfo 更新ProblemInfo记录
// Author [piexlmax](https://github.com/piexlmax)
func (problemInfoService *ProblemInfoService) UpdateProblemInfo(problemInfo autocode.ProblemInfo) (err error) {
	err = global.GVA_DB.Where("id=?", problemInfo.ID).Save(problemInfo).Error
	return err
}

func (problemInfoService *ProblemInfoService) UpdateProblemInfoCount(id uint) (err error) {
	err = global.GVA_DB.Model(&autocode.ProblemInfo{}).Where("id=?", id).Update("count", gorm.Expr("count+1")).Error
	return err
}

// GetProblemInfo 根据id获取ProblemInfo记录
// Author [piexlmax](https://github.com/piexlmax)
func (problemInfoService *ProblemInfoService) GetProblemInfo(id uint) (err error, problemInfo autocode.ProblemInfo) {
	err = global.GVA_DB.Where("id = ?", id).First(&problemInfo).Error
	return
}

// GetProblemInfoInfoList 分页获取ProblemInfo记录
// Author [piexlmax](https://github.com/piexlmax)
func (problemInfoService *ProblemInfoService) GetProblemInfoInfoList(info autoCodeReq.ProblemInfoSearch) (err error, infoList []autocode.ProblemInfo, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&autocode.ProblemInfo{})
	if info.SiteType != "" {
		db.Where("site_type=?", info.SiteType)
	}
	if info.UnitId != nil && *info.UnitId != 0 {
		db.Where("unit_id=?", info.UnitId)
	}
	if info.Status != "" {
		db.Where("status=?", info.Status)
	}
	if info.Oper != 0 {
		db.Where("oper=?", info.Oper)
	}
	if !info.CreatedAtStart.IsZero() && !info.CreatedAtEnd.IsZero() {
		db.Where("created_at between ? and ?", info.CreatedAtStart, info.CreatedAtEnd)
	}
	var problemInfos []autocode.ProblemInfo
	// 如果有条件搜索 下方会自动创建搜索语句
	err = db.Count(&total).Error
	err = db.Limit(limit).Offset(offset).Order("created_at desc").Find(&problemInfos).Error
	return err, problemInfos, total
}
