package twi

import (
	"courseSign/server/global"
	"courseSign/server/model/common/request"
	"courseSign/server/model/twi"
	twiReq "courseSign/server/model/twi/request"
)

type TwiActFinishService struct {
}

// CreateTwiActFinish 创建TwiActFinish记录
// Author [piexlmax](https://github.com/piexlmax)
func (twiActFinishService *TwiActFinishService) CreateTwiActFinish(twiActFinish twi.TwiActFinish) (err error) {
	err = global.GVA_DB.Create(&twiActFinish).Error
	return err
}

// DeleteTwiActFinish 删除TwiActFinish记录
// Author [piexlmax](https://github.com/piexlmax)
func (twiActFinishService *TwiActFinishService)DeleteTwiActFinish(twiActFinish twi.TwiActFinish) (err error) {
	err = global.GVA_DB.Delete(&twiActFinish).Error
	return err
}

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

// UpdateTwiActFinish 更新TwiActFinish记录
// Author [piexlmax](https://github.com/piexlmax)
func (twiActFinishService *TwiActFinishService)UpdateTwiActFinish(twiActFinish twi.TwiActFinish) (err error) {
	err = global.GVA_DB.Save(&twiActFinish).Error
	return err
}

// GetTwiActFinish 根据id获取TwiActFinish记录
// Author [piexlmax](https://github.com/piexlmax)
func (twiActFinishService *TwiActFinishService)GetTwiActFinish(id uint) (twiActFinish twi.TwiActFinish, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&twiActFinish).Error
	return
}

// GetTwiActFinishInfoList 分页获取TwiActFinish记录
// Author [piexlmax](https://github.com/piexlmax)
func (twiActFinishService *TwiActFinishService)GetTwiActFinishInfoList(info twiReq.TwiActFinishSearch) (list []twi.TwiActFinish, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&twi.TwiActFinish{})
    var twiActFinishs []twi.TwiActFinish
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.StartCreatedAt !=nil && info.EndCreatedAt !=nil {
     db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
    }
    // if info.Addr != "" {
	// 	db = db.Where("address = ?",info.Address)
	// }
    // if info.TwiUserName != "" {
    //     db = db.Where("twi_username = ?",info.TwiUsername)
    // }
    if info.RewardType != nil {
        db = db.Where("reward_type = ?",info.RewardType)
    }
    if info.RewardStatus != nil {
        db = db.Where("reward_status = ?",info.RewardStatus)
    }
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }

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