package airDrop

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

type VoiceAddrService struct {
}

// CreateVoiceAddr 创建VoiceAddr记录
// Author [piexlmax](https://github.com/piexlmax)
func (voiceAddrService *VoiceAddrService) CreateVoiceAddr(voiceAddr airDrop.VoiceAddr) (err error) {
	err = global.GVA_DB.Create(&voiceAddr).Error
	return err
}

// DeleteVoiceAddr 删除VoiceAddr记录
// Author [piexlmax](https://github.com/piexlmax)
func (voiceAddrService *VoiceAddrService)DeleteVoiceAddr(voiceAddr airDrop.VoiceAddr) (err error) {
	err = global.GVA_DB.Delete(&voiceAddr).Error
	return err
}

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

// UpdateVoiceAddr 更新VoiceAddr记录
// Author [piexlmax](https://github.com/piexlmax)
func (voiceAddrService *VoiceAddrService)UpdateVoiceAddr(voiceAddr airDrop.VoiceAddr) (err error) {
	err = global.GVA_DB.Save(&voiceAddr).Error
	return err
}

// GetVoiceAddr 根据id获取VoiceAddr记录
// Author [piexlmax](https://github.com/piexlmax)
func (voiceAddrService *VoiceAddrService)GetVoiceAddr(id uint) (voiceAddr airDrop.VoiceAddr, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&voiceAddr).Error
	return
}

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

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