package airDrop

import (
	"courseSign/server/global"
	"courseSign/server/model/airDrop"
	"courseSign/server/model/common/request"
    airDropReq "courseSign/server/model/airDrop/request"
)

type VoiceVerifyService struct {
}

// CreateVoiceVerify 创建VoiceVerify记录
// Author [piexlmax](https://github.com/piexlmax)
func (voiceVerifyService *VoiceVerifyService) CreateVoiceVerify(voiceVerify airDrop.VoiceVerify) (err error) {
	err = global.GVA_DB.Create(&voiceVerify).Error
	return err
}

// DeleteVoiceVerify 删除VoiceVerify记录
// Author [piexlmax](https://github.com/piexlmax)
func (voiceVerifyService *VoiceVerifyService)DeleteVoiceVerify(voiceVerify airDrop.VoiceVerify) (err error) {
	err = global.GVA_DB.Delete(&voiceVerify).Error
	return err
}

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

// UpdateVoiceVerify 更新VoiceVerify记录
// Author [piexlmax](https://github.com/piexlmax)
func (voiceVerifyService *VoiceVerifyService)UpdateVoiceVerify(voiceVerify airDrop.VoiceVerify) (err error) {
	err = global.GVA_DB.Save(&voiceVerify).Error
	return err
}

// GetVoiceVerify 根据id获取VoiceVerify记录
// Author [piexlmax](https://github.com/piexlmax)
func (voiceVerifyService *VoiceVerifyService)GetVoiceVerify(id uint) (voiceVerify airDrop.VoiceVerify, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&voiceVerify).Error
	return
}

// GetVoiceVerifyInfoList 分页获取VoiceVerify记录
// Author [piexlmax](https://github.com/piexlmax)
func (voiceVerifyService *VoiceVerifyService)GetVoiceVerifyInfoList(info airDropReq.VoiceVerifySearch) (list []airDrop.VoiceVerify, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&airDrop.VoiceVerify{})
    var voiceVerifys []airDrop.VoiceVerify
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.StartCreatedAt !=nil && info.EndCreatedAt !=nil {
     db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
    }
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }

	err = db.Limit(limit).Offset(offset).Find(&voiceVerifys).Error
	return  voiceVerifys, total, err
}
