package twi

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

type ActBannerService struct {
}

// CreateActBanner 创建ActBanner记录
// Author [piexlmax](https://github.com/piexlmax)
func (actBannerService *ActBannerService) CreateActBanner(actBanner twi.ActBanner) (err error) {
	err = global.GVA_DB.Create(&actBanner).Error
	return err
}

// DeleteActBanner 删除ActBanner记录
// Author [piexlmax](https://github.com/piexlmax)
func (actBannerService *ActBannerService)DeleteActBanner(actBanner twi.ActBanner) (err error) {
	err = global.GVA_DB.Delete(&actBanner).Error
	return err
}

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

// UpdateActBanner 更新ActBanner记录
// Author [piexlmax](https://github.com/piexlmax)
func (actBannerService *ActBannerService)UpdateActBanner(actBanner twi.ActBanner) (err error) {
	err = global.GVA_DB.Save(&actBanner).Error
	return err
}

// GetActBanner 根据id获取ActBanner记录
// Author [piexlmax](https://github.com/piexlmax)
func (actBannerService *ActBannerService)GetActBanner(id uint) (actBanner twi.ActBanner, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&actBanner).Error
	return
}

// GetActBannerInfoList 分页获取ActBanner记录
// Author [piexlmax](https://github.com/piexlmax)
func (actBannerService *ActBannerService)GetActBannerInfoList(info twiReq.ActBannerSearch) (list []twi.ActBanner, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&twi.ActBanner{})
    var actBanners []twi.ActBanner
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.StartCreatedAt !=nil && info.EndCreatedAt !=nil {
     db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
    }
    if info.ActName != "" {
        db = db.Where("act_name LIKE ?","%"+ info.ActName+"%")
    }
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }

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