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 CourseListService struct {
}

// CreateCourseList 创建CourseList记录
// Author [piexlmax](https://github.com/piexlmax)
func (courseListService *CourseListService) CreateCourseList(courseList course.CourseList) (err error) {
	err = global.GVA_DB.Create(&courseList).Error
	return err
}

// DeleteCourseList 删除CourseList记录
// Author [piexlmax](https://github.com/piexlmax)
func (courseListService *CourseListService)DeleteCourseList(courseList course.CourseList) (err error) {
	err = global.GVA_DB.Delete(&courseList).Error
	return err
}

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

// UpdateCourseList 更新CourseList记录
// Author [piexlmax](https://github.com/piexlmax)
func (courseListService *CourseListService)UpdateCourseList(courseList course.CourseList) (err error) {
	err = global.GVA_DB.Save(&courseList).Error
	return err
}

// GetCourseList 根据id获取CourseList记录
// Author [piexlmax](https://github.com/piexlmax)
func (courseListService *CourseListService)GetCourseList(id uint) (courseList course.CourseList, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&courseList).Error
	return
}

// GetCourseListInfoList 分页获取CourseList记录
// Author [piexlmax](https://github.com/piexlmax)
func (courseListService *CourseListService)GetCourseListInfoList(info courseReq.CourseListSearch) (list []course.CourseList, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&course.CourseList{})
    var courseLists []course.CourseList
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.StartCreatedAt !=nil && info.EndCreatedAt !=nil {
     db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
    }
    if info.CourseName != "" {
        db = db.Where("course_name = ?",info.CourseName)
    }
    if info.IsShow != nil {
        db = db.Where("is_show = ?",info.IsShow)
    }
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }

	err = db.Limit(limit).Offset(offset).Find(&courseLists).Error
	if info.Address != "" {
		var cu course.CourseUsers
		err = global.GVA_DB.Model(&course.CourseUsers{}).Where("address = ?",info.Address).First(&cu).Error
		if err != nil {
			return
		}
		for _, v := range courseLists {
			var cs course.CourseSignup
			err = global.GVA_DB.Model(&course.CourseSignup{}).Where("uid = ? and cid = ?",cu.ID,v.ID).First(&cs).Error
			if err != nil && err != gorm.ErrRecordNotFound{
				return
			} else if err != nil && err == gorm.ErrRecordNotFound {
				err = nil
				continue
			}
			if err == nil && *cs.IsPass{
				v.IsSign = true
				v.IsPass = true
			} else if err == nil && !*cs.IsPass {
				v.IsSign = true
				v.IsPass = false
			}

		}
	}
	return  courseLists, total, err
}

// GetMyCourseList 分页获取CourseList记录
// Author [piexlmax](https://github.com/piexlmax)
func (courseListService *CourseListService)GetMyCourseList(info courseReq.CourseListSearch) (list []course.CourseList, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&course.CourseList{})
	var courseLists []course.CourseList
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt !=nil && info.EndCreatedAt !=nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	if info.CourseName != "" {
		db = db.Where("course_name = ?",info.CourseName)
	}
	if info.IsShow != nil {
		db = db.Where("is_show = ?",info.IsShow)
	}
	err = db.Count(&total).Error
	if err!=nil {
		return
	}

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