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

var courseActionService = service.ServiceGroupApp.CourseServiceGroup.CourseActionService


// CreateCourseAction 创建CourseAction
// @Tags CourseAction
// @Summary 创建CourseAction
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body course.CourseAction true "创建CourseAction"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /courseAction/createCourseAction [post]
func (courseActionApi *CourseActionApi) CreateCourseAction(c *gin.Context) {
	var courseAction course.CourseAction
	err := c.ShouldBindJSON(&courseAction)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
    verify := utils.Rules{
        "ActionName":{utils.NotEmpty()},
        "ActionStart":{utils.NotEmpty()},
        "ActionEnd":{utils.NotEmpty()},
    }
	if err := utils.Verify(courseAction, verify); err != nil {
    		response.FailWithMessage(err.Error(), c)
    		return
    	}
	if err := courseActionService.CreateCourseAction(courseAction); err != nil {
        global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

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

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

// UpdateCourseAction 更新CourseAction
// @Tags CourseAction
// @Summary 更新CourseAction
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body course.CourseAction true "更新CourseAction"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /courseAction/updateCourseAction [put]
func (courseActionApi *CourseActionApi) UpdateCourseAction(c *gin.Context) {
	var courseAction course.CourseAction
	err := c.ShouldBindJSON(&courseAction)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
      verify := utils.Rules{
          "ActionName":{utils.NotEmpty()},
          "ActionStart":{utils.NotEmpty()},
          "ActionEnd":{utils.NotEmpty()},
      }
    if err := utils.Verify(courseAction, verify); err != nil {
      	response.FailWithMessage(err.Error(), c)
      	return
     }
	if err := courseActionService.UpdateCourseAction(courseAction); err != nil {
        global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindCourseAction 用id查询CourseAction
// @Tags CourseAction
// @Summary 用id查询CourseAction
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query course.CourseAction true "用id查询CourseAction"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /courseAction/findCourseAction [get]
func (courseActionApi *CourseActionApi) FindCourseAction(c *gin.Context) {
	var courseAction course.CourseAction
	err := c.ShouldBindQuery(&courseAction)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if recourseAction, err := courseActionService.GetCourseAction(courseAction.ID); err != nil {
        global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"recourseAction": recourseAction}, c)
	}
}

// GetCourseActionList 分页获取CourseAction列表
// @Tags CourseAction
// @Summary 分页获取CourseAction列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query courseReq.CourseActionSearch true "分页获取CourseAction列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /courseAction/getCourseActionList [get]
func (courseActionApi *CourseActionApi) GetCourseActionList(c *gin.Context) {
	var pageInfo courseReq.CourseActionSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := courseActionService.GetCourseActionInfoList(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)
    }
}


// FindCourseActionWeb 用id查询CourseAction
// @Tags CourseAction
// @Summary 用id查询CourseAction
// @accept application/json
// @Produce application/json
// @Param data query course.CourseAction true "用id查询CourseAction"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /courseAction/findAction [get]
func (courseActionApi *CourseActionApi) FindCourseActionWeb(c *gin.Context) {
	var courseAction course.CourseAction
	err := c.ShouldBindQuery(&courseAction)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if recourseAction, err := courseActionService.GetCourseActionByIdAddr(courseAction.ID,courseAction.Address); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"recourseAction": recourseAction}, c)
	}
}


// GetCourseActionListWeb 分页获取CourseAction列表
// @Tags CourseAction
// @Summary 分页获取CourseAction列表
// @accept application/json
// @Produce application/json
// @Param data query courseReq.CourseActionSearch true "分页获取CourseAction列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /courseAction/getActionList [get]
func (courseActionApi *CourseActionApi) GetCourseActionListWeb(c *gin.Context) {
	var pageInfo courseReq.CourseActionSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := courseActionService.GetCourseActionInfoList(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)
	}
}