package twi

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

type TwiActListService struct {
}

// CreateTwiActList 创建TwiActList记录
// Author [piexlmax](https://github.com/piexlmax)
func (twiActListService *TwiActListService) CreateTwiActList(twiActList twi.TwiActList) (err error) {
	err = global.GVA_DB.Create(&twiActList).Error
	return err
}

// DeleteTwiActList 删除TwiActList记录
// Author [piexlmax](https://github.com/piexlmax)
func (twiActListService *TwiActListService)DeleteTwiActList(twiActList twi.TwiActList) (err error) {
	err = global.GVA_DB.Delete(&twiActList).Error
	return err
}

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

// UpdateTwiActList 更新TwiActList记录
// Author [piexlmax](https://github.com/piexlmax)
func (twiActListService *TwiActListService)UpdateTwiActList(twiActList twi.TwiActList) (err error) {
	err = global.GVA_DB.Save(&twiActList).Error
	return err
}

// GetTwiActList 根据id获取TwiActList记录
// Author [piexlmax](https://github.com/piexlmax)
func (twiActListService *TwiActListService)GetTwiActList(id uint) (twiActList twi.TwiActList, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&twiActList).Error
	return
}

// GetTwiActListInfoList 分页获取TwiActList记录
// Author [piexlmax](https://github.com/piexlmax)
func (twiActListService *TwiActListService)GetTwiActListInfoList(info twiReq.TwiActListSearch) (list []twi.TwiActList, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&twi.TwiActList{})
    var twiActLists []twi.TwiActList
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.StartCreatedAt !=nil && info.EndCreatedAt !=nil {
     db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
    }
    if info.StartTime != nil {
        db = db.Where("start_time > ?",info.StartTime)
    }
    if info.EndTime != nil {
        db = db.Where("end_time < ?",info.EndTime)
    }
    if info.Type != nil {
        db = db.Where("type = ?",info.Type)
    }
    if info.Chain != "" {
        db = db.Where("chain = ?",info.Chain)
    }
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }

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