package course

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

type CourseUsersService struct {
}

// CreateCourseUsers 创建CourseUsers记录
// Author [piexlmax](https://github.com/piexlmax)
func (courseUsersService *CourseUsersService) CreateCourseUsers(courseUsers course.CourseUsers) (err error) {
	err = global.GVA_DB.Create(&courseUsers).Error
	return err
}

// DeleteCourseUsers 删除CourseUsers记录
// Author [piexlmax](https://github.com/piexlmax)
func (courseUsersService *CourseUsersService)DeleteCourseUsers(courseUsers course.CourseUsers) (err error) {
	err = global.GVA_DB.Delete(&courseUsers).Error
	return err
}

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

// UpdateCourseUsers 更新CourseUsers记录
// Author [piexlmax](https://github.com/piexlmax)
func (courseUsersService *CourseUsersService)UpdateCourseUsers(courseUsers course.CourseUsers) (err error) {
	err = global.GVA_DB.Save(&courseUsers).Error
	return err
}

// GetCourseUsers 根据id获取CourseUsers记录
// Author [piexlmax](https://github.com/piexlmax)
func (courseUsersService *CourseUsersService)GetCourseUsers(id uint) (courseUsers course.CourseUsers, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&courseUsers).Error
	return
}

// GetCourseUsersByAddr 根据id获取CourseUsers记录
// Author [piexlmax](https://github.com/piexlmax)
func (courseUsersService *CourseUsersService)GetCourseUsersByAddr(addr string) (courseUsers course.CourseUsers, err error) {
	err = global.GVA_DB.Where("address = ?", addr).First(&courseUsers).Error
	return
}

// GetCourseUsersInfoList 分页获取CourseUsers记录
// Author [piexlmax](https://github.com/piexlmax)
func (courseUsersService *CourseUsersService)GetCourseUsersInfoList(info courseReq.CourseUsersSearch) (list []course.CourseUsers, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&course.CourseUsers{})
    var courseUserss []course.CourseUsers
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.StartCreatedAt !=nil && info.EndCreatedAt !=nil {
     db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
    }
    if info.Address != "" {
        db = db.Where("address = ?",info.Address)
    }
    if info.Name != "" {
        db = db.Where("name = ?",info.Name)
    }
    if info.Sex != nil {
        db = db.Where("sex = ?",info.Sex)
    }
    if info.Phone != "" {
        db = db.Where("phone = ?",info.Phone)
    }
    if info.Invitor != "" {
        db = db.Where("invitor = ?",info.Invitor)
    }
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }

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