package autocode

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/autocode"
	autocodeReq "github.com/flipped-aurora/gin-vue-admin/server/model/autocode/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/service"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type AssignmentApi struct {
}

var assignmentService = service.ServiceGroupApp.AutoCodeServiceGroup.AssignmentService
var fileUploadAndDownloadService = service.ServiceGroupApp.ExampleServiceGroup.FileUploadAndDownloadService

// CreateAssignment 创建Assignment
// @Tags Assignment
// @Summary 创建Assignment
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body autocode.Assignment true "创建Assignment"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /assignment/createAssignment [post]
func (assignmentApi *AssignmentApi) CreateAssignment(c *gin.Context) {
	var assignment autocode.Assignment
	_ = c.ShouldBindJSON(&assignment)
	if err := assignmentService.CreateAssignment(assignment); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Any("err", err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteAssignment 删除Assignment
// @Tags Assignment
// @Summary 删除Assignment
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body autocode.Assignment true "删除Assignment"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /assignment/deleteAssignment [delete]
func (assignmentApi *AssignmentApi) DeleteAssignment(c *gin.Context) {
	var assignment autocode.Assignment
	_ = c.ShouldBindJSON(&assignment)
	if err := assignmentService.DeleteAssignment(assignment); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Any("err", err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteAssignmentByIds 批量删除Assignment
// @Tags Assignment
// @Summary 批量删除Assignment
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除Assignment"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /assignment/deleteAssignmentByIds [delete]
func (assignmentApi *AssignmentApi) DeleteAssignmentByIds(c *gin.Context) {
	var IDS request.IdsReq
	_ = c.ShouldBindJSON(&IDS)
	if err := assignmentService.DeleteAssignmentByIds(IDS); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Any("err", err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateAssignment 更新Assignment
// @Tags Assignment
// @Summary 更新Assignment
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body autocode.Assignment true "更新Assignment"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /assignment/updateAssignment [put]
func (assignmentApi *AssignmentApi) UpdateAssignment(c *gin.Context) {
	var assignment autocode.Assignment
	_ = c.ShouldBindJSON(&assignment)
	if err := assignmentService.UpdateAssignment(assignment); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Any("err", err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindAssignment 用id查询Assignment
// @Tags Assignment
// @Summary 用id查询Assignment
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query autocode.Assignment true "用id查询Assignment"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /assignment/findAssignment [get]
func (assignmentApi *AssignmentApi) FindAssignment(c *gin.Context) {
	var assignment autocode.Assignment
	_ = c.ShouldBindQuery(&assignment)
	if err, reassignment := assignmentService.GetAssignment(assignment.ID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Any("err", err))
		response.FailWithMessage("查询失败", c)
	} else {
		_, files := fileUploadAndDownloadService.GetFileRecordInfoByList(reassignment.ID)
		reassignment.Files = files
		response.OkWithData(gin.H{"reassignment": reassignment}, c)
	}
}

// GetAssignmentList 分页获取Assignment列表
// @Tags Assignment
// @Summary 分页获取Assignment列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query autocodeReq.AssignmentSearch true "分页获取Assignment列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /assignment/getAssignmentList [get]
func (assignmentApi *AssignmentApi) GetAssignmentList(c *gin.Context) {
	var pageInfo autocodeReq.AssignmentSearch
	_ = c.ShouldBindQuery(&pageInfo)
	if err, list, total := assignmentService.GetAssignmentInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}
