package course

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

type CourseActionUsersService struct {
}

// CreateCourseActionUsers 创建CourseActionUsers记录
// Author [piexlmax](https://github.com/piexlmax)
func (courseActionUsersService *CourseActionUsersService) CreateCourseActionUsers(courseActionUsers course.CourseActionUsers) (err error) {
	err = global.GVA_DB.Create(&courseActionUsers).Error
	return err
}

// DeleteCourseActionUsers 删除CourseActionUsers记录
// Author [piexlmax](https://github.com/piexlmax)
func (courseActionUsersService *CourseActionUsersService)DeleteCourseActionUsers(courseActionUsers course.CourseActionUsers) (err error) {
	err = global.GVA_DB.Delete(&courseActionUsers).Error
	return err
}

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

// UpdateCourseActionUsers 更新CourseActionUsers记录
// Author [piexlmax](https://github.com/piexlmax)
func (courseActionUsersService *CourseActionUsersService)UpdateCourseActionUsers(courseActionUsers course.CourseActionUsers) (err error) {
	err = global.GVA_DB.Save(&courseActionUsers).Error
	return err
}

// GetCourseActionUsers 根据id获取CourseActionUsers记录
// Author [piexlmax](https://github.com/piexlmax)
func (courseActionUsersService *CourseActionUsersService)GetCourseActionUsers(id uint) (courseActionUsers course.CourseActionUsers, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&courseActionUsers).Error
	return
}


// GetCourseActionUsers 根据id获取CourseActionUsers记录
// Author [piexlmax](https://github.com/piexlmax)
func (courseActionUsersService *CourseActionUsersService)GetCourseActionUsersByUidAndAid(aid int,uid int) (courseActionUsers course.CourseActionUsers, err error) {
	err = global.GVA_DB.Where("aid = ? and uid = ?", aid,uid).First(&courseActionUsers).Error
	return
}

// GetCourseActionUsersInfoList 分页获取CourseActionUsers记录
// Author [piexlmax](https://github.com/piexlmax)
func (courseActionUsersService *CourseActionUsersService)GetCourseActionUsersInfoList(info courseReq.CourseActionUsersSearch) (list []course.CourseActionUsers, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&course.CourseActionUsers{})
    var courseActionUserss []course.CourseActionUsers
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.StartCreatedAt !=nil && info.EndCreatedAt !=nil {
     db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
    }
    if info.Uid != nil {
        db = db.Where("uid = ?",info.Uid)
    }
    if info.Cid != nil {
        db = db.Where("cid = ?",info.Cid)
    }
    if info.Aid != nil {
        db = db.Where("aid = ?",info.Aid)
    }
    if info.IsSign != nil {
        db = db.Where("is_sign = ?",info.IsSign)
    }
    if info.SignDate != nil {
        db = db.Where("sign_date = ?",info.SignDate)
    }
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }
	err = db.Limit(limit).Offset(offset).Find(&courseActionUserss).Error

	for _,v := range courseActionUserss {
		var courseInfo course.CourseList
		err = global.GVA_DB.Model(&course.CourseList{}).Where("id = ?",v.Cid).First(&courseInfo).Error
		if err != nil {
			return
		}
		var actionInfo course.CourseAction
		err = global.GVA_DB.Model(&course.CourseAction{}).Where("id = ?",v.Aid).First(&actionInfo).Error
		if err != nil {
			return
		}
		v.CourseName = courseInfo.CourseName
		v.ActionName = actionInfo.ActionName
		v.ActionStart = actionInfo.ActionStart
		v.ActionEnd = actionInfo.ActionEnd
		v.IsShow = actionInfo.IsShow
	}
	return  courseActionUserss, total, err
}
