package course

import (
	"courseSign/server/global"
	"courseSign/server/model/common/request"
	"courseSign/server/model/common/response"
	"courseSign/server/model/course"
	courseReq "courseSign/server/model/course/request"
	"courseSign/server/service"
	"courseSign/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type CourseListApi struct {
}

var courseListService = service.ServiceGroupApp.CourseServiceGroup.CourseListService


// CreateCourseList 创建CourseList
// @Tags CourseList
// @Summary 创建CourseList
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body course.CourseList true "创建CourseList"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /courseList/createCourseList [post]
func (courseListApi *CourseListApi) CreateCourseList(c *gin.Context) {
	var courseList course.CourseList
	err := c.ShouldBindJSON(&courseList)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
    verify := utils.Rules{
        "CourseName":{utils.NotEmpty()},
        "CourseDesc":{utils.NotEmpty()},
        "CourseBanner":{utils.NotEmpty()},
        "IsShow":{utils.NotEmpty()},
    }
	if err := utils.Verify(courseList, verify); err != nil {
    		response.FailWithMessage(err.Error(), c)
    		return
    	}
	if err := courseListService.CreateCourseList(courseList); err != nil {
        global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteCourseList 删除CourseList
// @Tags CourseList
// @Summary 删除CourseList
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body course.CourseList true "删除CourseList"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /courseList/deleteCourseList [delete]
func (courseListApi *CourseListApi) DeleteCourseList(c *gin.Context) {
	var courseList course.CourseList
	err := c.ShouldBindJSON(&courseList)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := courseListService.DeleteCourseList(courseList); err != nil {
        global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteCourseListByIds 批量删除CourseList
// @Tags CourseList
// @Summary 批量删除CourseList
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除CourseList"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /courseList/deleteCourseListByIds [delete]
func (courseListApi *CourseListApi) DeleteCourseListByIds(c *gin.Context) {
	var IDS request.IdsReq
    err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := courseListService.DeleteCourseListByIds(IDS); err != nil {
        global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateCourseList 更新CourseList
// @Tags CourseList
// @Summary 更新CourseList
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body course.CourseList true "更新CourseList"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /courseList/updateCourseList [put]
func (courseListApi *CourseListApi) UpdateCourseList(c *gin.Context) {
	var courseList course.CourseList
	err := c.ShouldBindJSON(&courseList)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
      verify := utils.Rules{
          "CourseName":{utils.NotEmpty()},
          "CourseDesc":{utils.NotEmpty()},
          "CourseBanner":{utils.NotEmpty()},
          "IsShow":{utils.NotEmpty()},
      }
    if err := utils.Verify(courseList, verify); err != nil {
      	response.FailWithMessage(err.Error(), c)
      	return
     }
	if err := courseListService.UpdateCourseList(courseList); err != nil {
        global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindCourseList 用id查询CourseList
// @Tags CourseList
// @Summary 用id查询CourseList
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query course.CourseList true "用id查询CourseList"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /courseList/findCourseList [get]
func (courseListApi *CourseListApi) FindCourseList(c *gin.Context) {
	var courseList course.CourseList
	err := c.ShouldBindQuery(&courseList)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if recourseList, err := courseListService.GetCourseList(courseList.ID); err != nil {
        global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"recourseList": recourseList}, c)
	}
}

// GetCourseListList 分页获取CourseList列表
// @Tags CourseList
// @Summary 分页获取CourseList列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query courseReq.CourseListSearch true "分页获取CourseList列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /courseList/getCourses [get]
func (courseListApi *CourseListApi) GetCourseListList(c *gin.Context) {
	var pageInfo courseReq.CourseListSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := courseListService.GetCourseListInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}

// GetCourseListListWeb 分页获取CourseList列表
// @Tags CourseList
// @Summary 分页获取CourseList列表
// @accept application/json
// @Produce application/json
// @Param data query courseReq.CourseListSearch true "分页获取CourseList列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /courseList/getCourses [get]
func (courseListApi *CourseListApi) GetCourseListListWeb(c *gin.Context) {
	var pageInfo courseReq.CourseListSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := courseListService.GetCourseListInfoList(pageInfo); err != nil {
	    global.GVA_LOG.Error("获取失败!", zap.Error(err))
        response.FailWithMessage("获取失败", c)
    } else {
        response.OkWithDetailed(response.PageResult{
            List:     list,
            Total:    total,
            Page:     pageInfo.Page,
            PageSize: pageInfo.PageSize,
        }, "获取成功", c)
    }
}

// GetCourseInfoWeb 用id查询CourseList
// @Tags CourseList
// @Summary 用id查询CourseList
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query course.CourseList true "用id查询CourseList"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /courseList/getCourseInfo [get]
func (courseListApi *CourseListApi) GetCourseInfoWeb(c *gin.Context) {
	var courseList course.CourseList
	err := c.ShouldBindQuery(&courseList)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if recourseList, err := courseListService.GetCourseList(courseList.ID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"course": recourseList}, c)
	}
}