package course

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

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 k,v := range courseActionUserss {
		var courseInfo course.CourseList
		err = global.GVA_DB.Model(&course.CourseList{}).Where("id = ?",v.Cid).First(&courseInfo).Error
		if err != nil && err != gorm.ErrRecordNotFound{
			return
		} else if err != nil && err == gorm.ErrRecordNotFound {
			continue
		}
		var actionInfo course.CourseAction
		err = global.GVA_DB.Model(&course.CourseAction{}).Where("id = ?",v.Aid).First(&actionInfo).Error
		if err != nil && err != gorm.ErrRecordNotFound{
			return
		} else if err != nil && err == gorm.ErrRecordNotFound {
			continue
		}
		var user course.CourseUsers
		err = global.GVA_DB.Model(&course.CourseUsers{}).Where("id = ?",v.Uid).First(&user).Error
		if err != nil && err != gorm.ErrRecordNotFound{
			return
		} else if err != nil && err == gorm.ErrRecordNotFound {
			continue
		}
		courseActionUserss[k].Addr = user.Address
		courseActionUserss[k].CourseName = courseInfo.CourseName
		courseActionUserss[k].ActionName = actionInfo.ActionName
		courseActionUserss[k].ActionStart = actionInfo.ActionStart
		courseActionUserss[k].ActionEnd = actionInfo.ActionEnd
		courseActionUserss[k].IsShow = actionInfo.IsShow
	}
	return  courseActionUserss, total, err
}


// GetMyCourseActionList 分页获取CourseActionUsers记录
// Author [piexlmax](https://github.com/piexlmax)
func (courseActionUsersService *CourseActionUsersService)GetMyCourseActionList(info courseReq.CourseActionUsersSearch,uid uint) (list []course.CourseActionUsers, total int64, err error) {
	var courseSignup []course.CourseSignup
	err = global.GVA_DB.Model(&course.CourseSignup{}).Where("uid = ? and is_pass = ?",uid,true).Find(&courseSignup).Error
	if err != nil {
		return
	}
	var courseActionUserss []course.CourseActionUsers

	for k,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
		}
		courseActionUserss[k].CourseName = courseInfo.CourseName
		courseActionUserss[k].ActionName = actionInfo.ActionName
		courseActionUserss[k].ActionStart = actionInfo.ActionStart
		courseActionUserss[k].ActionEnd = actionInfo.ActionEnd
		courseActionUserss[k].IsShow = actionInfo.IsShow
	}
	return  courseActionUserss, total, err
}