package task

import (
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"log-server/global"
	"log-server/model/common/response"
	"log-server/model/task"
	"log-server/model/task/request"
	taskRes "log-server/model/task/response"
	"log-server/utils"
	"strconv"
)

type TaskConfApi struct {

}

func (b *TaskConfApi) UploadFile(c *gin.Context) {

	_, header, err := c.Request.FormFile("file")
	if err != nil {
		global.GVA_LOG.Error("接收文件失败!", zap.Error(err))
		response.FailWithMessage("接收文件失败", c)
		return
	}
	taskID := c.PostForm("task_id")
	user   := c.PostForm("responsible_person")
	if taskID == "" {
		response.FailWithMessage("taskId不能为空", c)
		return
	}
	if user == "" {
		response.FailWithMessage("负责人不能为空", c)
		return
	}
	var fileRes taskRes.UploadFileResponse
	taskId, _ := strconv.Atoi(taskID)
	fileRes, err = taskConfService.UploadFile(taskId, user,  header) // 文件上传后拿到文件路径
	if err != nil {
		global.GVA_LOG.Error("修改数据库链接失败!", zap.Error(err))
		response.FailWithMessage("修改数据库链接失败", c)
		return
	}
	response.OkWithDetailed(taskRes.InfoResponse{fileRes}, "上传成功", c)
}

func (b *TaskConfApi) DownloadFile(c *gin.Context) {
	taskIdStr := c.Query("taskId")
	md5String := c.Query("md5String")
	if taskIdStr == "" {
		response.FailWithMessage("taskId不能为空", c)
		return
	}
	taskId, _ := strconv.Atoi(taskIdStr)
	downloadFile, err := taskConfService.DownloadFile(taskId, md5String)
	if err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败，无对应脚本", c)
	} else {
		response.OkWithDetailed(taskRes.DownloadInfoResponse{downloadFile}, "查询成功", c)
	}

}


//获取所有游戏脚本列表
func (b *TaskConfApi) GetScriptList(c *gin.Context)  {
	//获取前端传值并校验
	var pageInfo task.SearchAccountTypeParams
	_ = c.ShouldBindJSON(&pageInfo)
	//页面信息校验
	if err := utils.Verify(pageInfo.PageInfo, utils.PageInfoVerify); err != nil{
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := taskConfService.GetScriptList(pageInfo.GameScriptResponse, pageInfo.PageInfo, pageInfo.OrderKey, pageInfo.Desc); 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)
	}
}

//更改脚本状态
func (b *TaskConfApi) StatusChange(c *gin.Context) {
	var paramsInfo request.UpdateScriptStatusRequest
	_ = c.ShouldBindJSON(&paramsInfo)
	if paramsInfo.State == 0 {
		response.FailWithMessage("状态值不能为空", c)
		return
	}
	if paramsInfo.Id == 0 {
		response.FailWithMessage("Id不能为空", c)
		return
	}
	if err := taskConfService.UpdateScriptStatus(paramsInfo); err != nil {
		global.GVA_LOG.Error("修改失败!", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
	} else {
		response.OkWithMessage("修改成功", c)
	}
}

//编辑加付费提醒额度
func (b *TaskConfApi) AddFeeNotice(c *gin.Context) {
	var taskConf task.TaskConf
	_ = c.ShouldBindJSON(&taskConf)
	if taskConf.TaskId == 0 {
		response.FailWithMessage("Id不能为空", c)
		return
	}
	if err := utils.Verify(taskConf, utils.TaskConfVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := taskConfService.JudgeOperate(taskConf); err != nil {
		global.GVA_LOG.Error("编辑失败!", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
	} else {
		response.OkWithMessage("编辑成功", c)
	}
}