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

// CreateCourseAction 创建CourseAction记录
// Author [piexlmax](https://github.com/piexlmax)
func (courseActionService *CourseActionService) CreateCourseAction(courseAction course.CourseAction) (err error) {
	err = global.GVA_DB.Create(&courseAction).Error
	return err
}

// DeleteCourseAction 删除CourseAction记录
// Author [piexlmax](https://github.com/piexlmax)
func (courseActionService *CourseActionService)DeleteCourseAction(courseAction course.CourseAction) (err error) {
	err = global.GVA_DB.Delete(&courseAction).Error
	return err
}

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

// UpdateCourseAction 更新CourseAction记录
// Author [piexlmax](https://github.com/piexlmax)
func (courseActionService *CourseActionService)UpdateCourseAction(courseAction course.CourseAction) (err error) {
	err = global.GVA_DB.Save(&courseAction).Error
	return err
}

// GetCourseAction 根据id获取CourseAction记录
// Author [piexlmax](https://github.com/piexlmax)
func (courseActionService *CourseActionService)GetCourseAction(id uint) (courseAction course.CourseAction, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&courseAction).Error
	return
}

// GetCourseAction 根据id获取CourseAction记录
// Author [piexlmax](https://github.com/piexlmax)
func (courseActionService *CourseActionService)GetCourseActionByIdAddr(id uint,addr string) (courseAction course.CourseAction, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&courseAction).Error
	if addr != "" {
		var user course.CourseUsers
		err = global.GVA_DB.Model(&course.CourseUsers{}).Where("address = ?",addr).First(&user).Error
		if err != nil {
			return
		}
		var cas course.CourseActionUsers
		err = global.GVA_DB.Model(&course.CourseActionUsers{}).Where("aid = ? and uid = ?",id,user.ID).First(&cas).Error
		if err != nil && err != gorm.ErrRecordNotFound {
			return
		} else if err != nil && err == gorm.ErrRecordNotFound {
			err = nil
			return
		}

		if err == nil {
			courseAction.IsSign = true
		}
	}
	return
}

// GetCourseActionInfoList 分页获取CourseAction记录
// Author [piexlmax](https://github.com/piexlmax)
func (courseActionService *CourseActionService)GetCourseActionInfoList(info courseReq.CourseActionSearch) (list []course.CourseAction, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&course.CourseAction{})
    var courseActions []course.CourseAction
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.StartCreatedAt !=nil && info.EndCreatedAt !=nil {
     db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
    }
    if info.ActionName != "" {
        db = db.Where("action_name = ?",info.ActionName)
    }
    if info.ActionStart != nil {
        db = db.Where("action_start = ?",info.ActionStart)
    }
    if info.ActionEnd != nil {
        db = db.Where("action_end = ?",info.ActionEnd)
    }
    if info.IsShow != nil {
        db = db.Where("is_show = ?",info.IsShow)
    }
    if info.Cid != nil {
    	db = db.Where("cid = ?",info.Cid)
	}
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }

	err = db.Limit(limit).Order("action_start").Offset(offset).Find(&courseActions).Error
	if info.Address != "" {
		var user course.CourseUsers
		err = global.GVA_DB.Model(&course.CourseUsers{}).Where("address = ?",info.Address).First(&user).Error
		if err != nil {
			return
		}
		for k, v := range courseActions {
			var cau course.CourseActionUsers
			err = global.GVA_DB.Model(&course.CourseActionUsers{}).Where("uid = ? and cid = ? and aid = ?",user.ID,v.Cid,v.ID).First(&cau).Error
			if err != nil && err != gorm.ErrRecordNotFound {
				return
			} else if err != nil && err == gorm.ErrRecordNotFound {
				err = nil
				continue
			}
			courseActions[k].IsSign = true
			courseActions[k].SignDate = cau.SignDate
		}
	}

	return  courseActions, total, err
}
