package backstage_api

import (
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
	"net/http"
	"youngmini_server/app/model/backstage_model"
	"youngmini_server/app/service/backstage_service"
	"youngmini_server/library/response"
)

var Task = taskApi{}

type taskApi struct {
}

// Creat 创建任务
func (task *taskApi) Creat(r *ghttp.Request) {
	var apiReq *backstage_model.RequestAllCreatParam
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, http.StatusBadRequest, err.Error())
	}
	err := backstage_service.Task.CreatTask(apiReq)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	response.JsonExit(r, http.StatusOK, "任务创建成功")
}

// FindAll 任务信息查找所有
func (task *taskApi) FindAll(r *ghttp.Request) {
	var apiReq *backstage_model.TaskFindAll
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, http.StatusBadRequest, err.Error())
	}
	taskList, num, err := backstage_service.Task.FindConditionAll(apiReq)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	// 没有数据返回错误请求
	if taskList == nil {
		response.JsonExit(r, http.StatusBadRequest, "没有数据返回")
	}
	response.JsonExit(r, http.StatusOK, "任务创建成功", g.Map{
		"taskList": taskList,
		"num":      num,
	})
}

// AlterTaskStatus 修改任务状态
func (task *taskApi) AlterTaskStatus(r *ghttp.Request) {
	var apiReq *backstage_model.RequestStatus
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, http.StatusBadRequest, err.Error())
	}
	err := backstage_service.Task.AlterTaskStatus(apiReq)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	response.JsonExit(r, http.StatusOK, "状态修改成功")
}

// FindSingleTaskInfo 查询单个任务信息
func (task *taskApi) FindSingleTaskInfo(r *ghttp.Request) {
	var apiReq *backstage_model.RequestTaskInfo
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, http.StatusBadRequest, err.Error())
	}
	result, err := backstage_service.Task.TaskInfoShow(apiReq)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	response.JsonExit(r, http.StatusOK, "状态修改成功", g.Map{
		"result": result,
	})
}

// UpdateBrand 修改品牌信息
func (task *taskApi) UpdateBrand(r *ghttp.Request) {
	var apiReq *backstage_model.RequestBrand
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, http.StatusBadRequest, err.Error())
	}
	err := backstage_service.Task.UpdateBrand(apiReq)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	response.JsonExit(r, http.StatusOK, "品牌修改成功")
}

// UpdateProduct 修改商品信息
func (task *taskApi) UpdateProduct(r *ghttp.Request) {
	var apiReq *backstage_model.RequestProduct
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, http.StatusBadRequest, err.Error())
	}
	err := backstage_service.Task.UpdateProduct(apiReq)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	response.JsonExit(r, http.StatusOK, "商品信息修改成功")
}

// UpdateProductPhoto 修改商品图片信息
func (task *taskApi) UpdateProductPhoto(r *ghttp.Request) {
	var apiReq *backstage_model.RequestProductPhoto
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, http.StatusBadRequest, err.Error())
	}
	err := backstage_service.Task.UpdateProductPhoto(apiReq)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	response.JsonExit(r, http.StatusOK, "修改商品图片信息")
}

// AddProductPhoto 增加图片信息
func (task *taskApi) AddProductPhoto(r *ghttp.Request) {
	var apiReq *backstage_model.RequestProductPhoto
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, http.StatusBadRequest, err.Error())
	}
	err := backstage_service.Task.AddProductPhoto(apiReq)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	response.JsonExit(r, http.StatusOK, "增加图片信息成功")
}

// DeleteProductPhoto 删除图片信息
func (task *taskApi) DeleteProductPhoto(r *ghttp.Request) {
	var apiReq *backstage_model.RequestProductPhoto
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, http.StatusBadRequest, err.Error())
	}
	err := backstage_service.Task.DeleteProductPhoto(apiReq)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	response.JsonExit(r, http.StatusOK, "删除图片信息成功")
}

// UpdateTaskBaseInfo 修改任务基础信息
func (task *taskApi) UpdateTaskBaseInfo(r *ghttp.Request) {
	var apiReq *backstage_model.RequestTaskBaseInfo
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, http.StatusBadRequest, err.Error())
	}
	err := backstage_service.Task.UpdateTaskBaseInfo(apiReq)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	response.JsonExit(r, http.StatusOK, "任务基础信息修改成功")
}

// UpdateProcedure 修改任务配置总流程
func (task *taskApi) UpdateProcedure(r *ghttp.Request) {
	var apiReq *backstage_model.RequestTaskProcedureDecisionCondition
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, http.StatusBadRequest, err.Error())
	}
	err := backstage_service.Task.UpdateProcedure(apiReq)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	response.JsonExit(r, http.StatusOK, "任务配置总流程修改成功")
}

// UpdateDraftInfo 修改流程初稿配置
func (task *taskApi) UpdateDraftInfo(r *ghttp.Request) {
	var apiReq *backstage_model.RequestTaskProcedureDraftInfo
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, http.StatusBadRequest, err.Error())
	}
	err := backstage_service.Task.UpdateDraftInfo(apiReq)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	response.JsonExit(r, http.StatusOK, "流程初稿配置修改成功")
}

// UpdateArticleInfo 修改流程作品配置
func (task *taskApi) UpdateArticleInfo(r *ghttp.Request) {
	var apiReq *backstage_model.RequestTaskProcedureArticleInfo
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, http.StatusBadRequest, err.Error())
	}
	err := backstage_service.Task.UpdateArticleInfo(apiReq)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	response.JsonExit(r, http.StatusOK, "流程作品配置修改成功")
}

// UpdateQualityInfo 修改质检链接配置
func (task *taskApi) UpdateQualityInfo(r *ghttp.Request) {
	var apiReq *backstage_model.RequestTaskProcedureQualityDataTestInfo
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, http.StatusBadRequest, err.Error())
	}
	err := backstage_service.Task.UpdateQualityInfo(apiReq)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	response.JsonExit(r, http.StatusOK, "质检链接配置修改成功")
}

// UpdateReward 修改达人任务奖励配置
func (task *taskApi) UpdateReward(r *ghttp.Request) {
	var apiReq *backstage_model.RequestTaskReward
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, http.StatusBadRequest, err.Error())
	}
	err := backstage_service.Task.UpdateReward(apiReq)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	response.JsonExit(r, http.StatusOK, "达人任务奖励配置修改成功")
}

// UpdateRecruitRequirement 修改招募要求配置
func (task *taskApi) UpdateRecruitRequirement(r *ghttp.Request) {
	var apiReq *backstage_model.RequestTaskRecruitRequirements
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, http.StatusBadRequest, err.Error())
	}
	err := backstage_service.Task.UpdateRecruitRequirement(apiReq)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	response.JsonExit(r, http.StatusOK, "招募要求配置修改成功")
}

// UpdateRecruitLevel 修改合作方式，所需达人数量及其对应稿费
func (task *taskApi) UpdateRecruitLevel(r *ghttp.Request) {
	var apiReq *backstage_model.RequestTaskRecruitTalentLevel
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, http.StatusBadRequest, err.Error())
	}
	err := backstage_service.Task.UpdateRecruitLevel(apiReq)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	response.JsonExit(r, http.StatusOK, "合作方式，所需达人数量及其对应稿费修改成功")
}

// AddRecruitLevel 增加合作方式，所需达人数量及其对应稿费
func (task *taskApi) AddRecruitLevel(r *ghttp.Request) {
	var apiReq *backstage_model.RequestTaskRecruitTalentLevel
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, http.StatusBadRequest, err.Error())
	}
	err := backstage_service.Task.AddRecruitLevel(apiReq)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	response.JsonExit(r, http.StatusOK, "合作方式，所需达人数量及其对应稿费修改成功")
}

// DeleteRecruitLevel 删除合作方式，所需达人数量及其对应稿费
func (task *taskApi) DeleteRecruitLevel(r *ghttp.Request) {
	var apiReq *backstage_model.RequestTaskRecruitTalentLevel
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, http.StatusBadRequest, err.Error())
	}
	err := backstage_service.Task.DeleteRecruitLevel(apiReq)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	response.JsonExit(r, http.StatusOK, "合作方式，所需达人数量及其对应稿费修改成功")
}

// UpdatePerformHelp 修改任务执行帮助
func (task *taskApi) UpdatePerformHelp(r *ghttp.Request) {
	var apiReq *backstage_model.RequestTaskPerformHelpInfo
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, http.StatusBadRequest, err.Error())
	}
	err := backstage_service.Task.UpdatePerformHelp(apiReq)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	response.JsonExit(r, http.StatusOK, "任务执行帮助修改成功")
}

// FindSendAddress 发货信息查询
func (task *taskApi) FindSendAddress(r *ghttp.Request) {
	var apiReq *backstage_model.SendProductAddress
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, http.StatusBadRequest, err.Error())
	}
	result, num, err := backstage_service.Task.FindSendAddress(apiReq)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	response.JsonExit(r, http.StatusOK, "地址查询成功", g.Map{
		"result": result,
		"num":    num,
	})
}

// FindRecruit 招募进度数据统计
func (task *taskApi) FindRecruit(r *ghttp.Request) {
	var apiReq *backstage_model.RequestTaskInfo
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, http.StatusBadRequest, err.Error())
	}
	result, err := backstage_service.Task.FindRecruit(apiReq)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	response.JsonExit(r, http.StatusOK, "招募进度数据统计查询成功", g.Map{
		"result": result,
	})
}

// FindBIStatistics 招募进度数据统计
func (task *taskApi) FindBIStatistics(r *ghttp.Request) {
	var apiReq *backstage_model.RequestTaskInfo
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, http.StatusBadRequest, err.Error())
	}
	result, err := backstage_service.Task.FindBIStatistics(apiReq)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	response.JsonExit(r, http.StatusOK, "BI数据统计查询成功", g.Map{
		"result": result,
	})
}

// DeleteTask 删除任务
func (task *taskApi) DeleteTask(r *ghttp.Request) {
	taskId := r.GetRequestInt("task_id", 0)
	if taskId == 0 {
		response.JsonExit(r, http.StatusBadRequest, "输入要删除的任务id")
	}

	err := backstage_service.Task.DeleteTask(taskId)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, "删除任务失败:" + err.Error())
	}

	response.JsonExit(r, http.StatusOK, "删除任务成功", nil)
}
