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/33cn/chain33/common/address"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"gorm.io/gorm"
)

type CourseSignupApi struct {
}

var courseSignupService = service.ServiceGroupApp.CourseServiceGroup.CourseSignupService


// CreateCourseSignup 创建CourseSignup
// @Tags CourseSignup
// @Summary 创建CourseSignup
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body course.CourseSignup true "创建CourseSignup"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /courseSignup/createCourseSignup [post]
func (courseSignupApi *CourseSignupApi) CreateCourseSignup(c *gin.Context) {
	var courseSignup course.CourseSignup
	err := c.ShouldBindJSON(&courseSignup)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := courseSignupService.CreateCourseSignup(courseSignup); err != nil {
        global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

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

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

// UpdateCourseSignup 更新CourseSignup
// @Tags CourseSignup
// @Summary 更新CourseSignup
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body course.CourseSignup true "更新CourseSignup"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /courseSignup/updateCourseSignup [put]
func (courseSignupApi *CourseSignupApi) UpdateCourseSignup(c *gin.Context) {
	var courseSignup course.CourseSignup
	err := c.ShouldBindJSON(&courseSignup)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := courseSignupService.UpdateCourseSignup(courseSignup); err != nil {
        global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindCourseSignup 用id查询CourseSignup
// @Tags CourseSignup
// @Summary 用id查询CourseSignup
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query course.CourseSignup true "用id查询CourseSignup"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /courseSignup/findCourseSignup [get]
func (courseSignupApi *CourseSignupApi) FindCourseSignup(c *gin.Context) {
	var courseSignup course.CourseSignup
	err := c.ShouldBindQuery(&courseSignup)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if recourseSignup, err := courseSignupService.GetCourseSignup(courseSignup.ID); err != nil {
        global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"recourseSignup": recourseSignup}, c)
	}
}

// GetCourseSignupList 分页获取CourseSignup列表
// @Tags CourseSignup
// @Summary 分页获取CourseSignup列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query courseReq.CourseSignupSearch true "分页获取CourseSignup列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /courseSignup/getCourseSignupList [get]
func (courseSignupApi *CourseSignupApi) GetCourseSignupList(c *gin.Context) {
	var pageInfo courseReq.CourseSignupSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := courseSignupService.GetCourseSignupInfoList(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)
    }
}

// SignCourse 创建CourseSignup
// @Tags SignCourse
// @Summary 创建CourseSignup
// @accept application/json
// @Produce application/json
// @Param data body course.CourseSignup true "创建CourseSignup"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /courseSignup/sign [post]
func (courseSignupApi *CourseSignupApi) SignCourse(c *gin.Context) {
	var courseSignup course.CourseSignup
	err := c.ShouldBindJSON(&courseSignup)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := utils.Verify(courseSignup,utils.AddressVerify);err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
		return
	}
	if !address.IsEthAddress(courseSignup.Addr) {
		global.GVA_LOG.Error("地址格式错误！", zap.Error(err),zap.String("addr",courseSignup.Addr))
		response.FailWithMessage(err.Error(), c)
		return
	}
	user,err := courseUsersService.GetCourseUsersByAddr(courseSignup.Addr)
	if err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
		return
	}
	if user.Invitor == "" {
		response.FailWithMessage("无推荐人", c)
		return
	}
	user.Name = courseSignup.Name
	user.Age = courseSignup.Age
	user.Company = courseSignup.Company
	user.Phone = courseSignup.Phone
	user.Photo = courseSignup.Photo
	user.Position = courseSignup.Position

	var uid int
	uid = int(user.ID)
	courseSignup.Uid = &uid

	// 检查是否已报名
	cs,err := courseSignupService.GetCourseSignupByUidAndCid(uint(*courseSignup.Cid),user.ID)
	if err != nil && err != gorm.ErrRecordNotFound {
		global.GVA_LOG.Error("服务异常!", zap.Error(err))
		response.FailWithMessage("服务异常", c)
		return
	}

	if err == nil && !*cs.IsPass{
		response.FailWithMessage("已报名,等待审核", c)
		return
	} else if err == nil && *cs.IsPass {
		response.FailWithMessage("已报名", c)
		return
	}

	if err := courseSignupService.CreateCourseSignup(courseSignup); err != nil {
		global.GVA_LOG.Error("报名失败!", zap.Error(err))
		response.FailWithMessage("报名失败", c)
		return
	} else {
		err = courseUsersService.UpdateCourseUsers(user)
		if err != nil {
			global.GVA_LOG.Error("更新用户信息失败!", zap.Error(err))
			response.FailWithMessage("更新用户信息失败", c)
			return
		}
		response.OkWithMessage("报名成功", c)
	}
}

// GetMyCourse 分页获取CourseSignup列表
// @Tags CourseSignup
// @Summary 分页获取CourseSignup列表
// @accept application/json
// @Produce application/json
// @Param data query courseReq.CourseSignupSearch true "分页获取CourseSignup列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /courseSignup/mySign [get]
func (courseSignupApi *CourseSignupApi) GetMyCourse(c *gin.Context) {
	var pageInfo courseReq.CourseSignupSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	user,err := courseUsersService.GetCourseUsersByAddr(pageInfo.Addr)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	courseInfo,err := courseListService.GetCourseList(uint(*pageInfo.Cid))
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	if list, total, err := courseSignupService.GetUserCourse(pageInfo,user.ID,courseInfo.ID); 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)
	}
}