package course

import (
	"courseSign/server/global"
	"courseSign/server/model/course"
	"courseSign/server/model/common/request"
    courseReq "courseSign/server/model/course/request"
)

type CourseBannersService struct {
}

// CreateCourseBanners 创建CourseBanners记录
// Author [piexlmax](https://github.com/piexlmax)
func (courseBannersService *CourseBannersService) CreateCourseBanners(courseBanners course.CourseBanners) (err error) {
	err = global.GVA_DB.Create(&courseBanners).Error
	return err
}

// DeleteCourseBanners 删除CourseBanners记录
// Author [piexlmax](https://github.com/piexlmax)
func (courseBannersService *CourseBannersService)DeleteCourseBanners(courseBanners course.CourseBanners) (err error) {
	err = global.GVA_DB.Delete(&courseBanners).Error
	return err
}

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

// UpdateCourseBanners 更新CourseBanners记录
// Author [piexlmax](https://github.com/piexlmax)
func (courseBannersService *CourseBannersService)UpdateCourseBanners(courseBanners course.CourseBanners) (err error) {
	err = global.GVA_DB.Save(&courseBanners).Error
	return err
}

// GetCourseBanners 根据id获取CourseBanners记录
// Author [piexlmax](https://github.com/piexlmax)
func (courseBannersService *CourseBannersService)GetCourseBanners(id uint) (courseBanners course.CourseBanners, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&courseBanners).Error
	return
}

// GetCourseBannersInfoList 分页获取CourseBanners记录
// Author [piexlmax](https://github.com/piexlmax)
func (courseBannersService *CourseBannersService)GetCourseBannersInfoList(info courseReq.CourseBannersSearch) (list []course.CourseBanners, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&course.CourseBanners{})
    var courseBannerss []course.CourseBanners
    // 如果有条件搜索 下方会自动创建搜索语句
    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(&courseBannerss).Error
	return  courseBannerss, total, err
}
