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/app/utils"
	"youngmini_server/library/response"
)

var Order = orderApi{}

type orderApi struct {
}

// FindAll 查询所有(订单列表查询所有)
func (order *orderApi) FindAll(r *ghttp.Request) {
	var apiReq *backstage_model.OrderFindAll
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, http.StatusBadRequest, err.Error())
	}
	result, num, err := backstage_service.Order.FindAll(apiReq)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	response.JsonExit(r, http.StatusOK, "数据返回成功", g.Map{
		"result": result, "num": num,
	})
}

// FindAddress 查询其他招募信息
func (order *orderApi) FindAddress(r *ghttp.Request) {
	var apiReq *backstage_model.OrderFindAddress
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, http.StatusBadRequest, err.Error())
	}
	requirement, err := backstage_service.Order.FindAddress(apiReq)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	response.JsonExit(r, http.StatusOK, "数据返回成功", g.Map{
		"requirement": requirement,
	})
}

// ExecutiveInfo 执行信息，（发货信息，初稿审核，作品审核，质检链接，数据链接，）
func (order *orderApi) ExecutiveInfo(r *ghttp.Request) {
	var apiReq *backstage_model.OrderFindAddress
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, http.StatusBadRequest, err.Error())
	}
	deliveryInfo, draftReview, articleReview, quality, dataQuality, err := backstage_service.Order.ExecutiveInfo(apiReq)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	response.JsonExit(r, http.StatusOK, "数据返回成功", g.Map{
		"deliveryInfo":  deliveryInfo,
		"draftReview":   draftReview,
		"articleReview": articleReview,
		"quality":       quality,
		"dataQuality":   dataQuality,
	})
}

// RegistrationInfo  报名信息，（任务信息，商品信息，任务等级信息）
func (order *orderApi) RegistrationInfo(r *ghttp.Request) {
	var apiReq *backstage_model.OrderFindAddress
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, http.StatusBadRequest, err.Error())
	}
	taskBaseInfo, product, taskRecruitTalentLevel, productPhoto, err := backstage_service.Order.RegistrationInfo(apiReq)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	response.JsonExit(r, http.StatusOK, "数据返回成功", g.Map{
		"taskBaseInfo":           taskBaseInfo,
		"product":                product,
		"taskRecruitTalentLevel": taskRecruitTalentLevel,
		"productPhoto":           productPhoto,
	})
}

// DraftFee 稿费明细接口
func (order *orderApi) DraftFee(r *ghttp.Request) {
	var apiReq *backstage_model.OrderFindAddress
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, http.StatusBadRequest, err.Error())
	}
	orderDraftFee, err := backstage_service.Order.DraftFee(apiReq)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	response.JsonExit(r, http.StatusOK, "数据返回成功", g.Map{
		"orderDraftFee": orderDraftFee,
	})
}

// AlterListStatus 修改订单状态
func (order *orderApi) AlterListStatus(r *ghttp.Request) {
	var apiReq *backstage_model.AlterListStatus
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, http.StatusBadRequest, err.Error())
	}
	err := backstage_service.Order.AlterListStatus(apiReq)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	response.JsonExit(r, http.StatusOK, "数据返回成功")
}

// AddDelivery 添加订单发货信息
func (order *orderApi) AddDelivery(r *ghttp.Request) {
	var apiReq *backstage_model.AddDelivery
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, http.StatusBadRequest, err.Error())
	}
	err := backstage_service.Order.AddDelivery(apiReq)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	response.JsonExit(r, http.StatusOK, "数据返回成功")
}

// AddDraftReview 添加初稿审核信息
func (order *orderApi) AddDraftReview(r *ghttp.Request) {
	var apiReq *backstage_model.AddDraftReview
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, http.StatusBadRequest, err.Error())
	}
	err := backstage_service.Order.AddDraftReview(apiReq)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	response.JsonExit(r, http.StatusOK, "数据返回成功")
}

// AddAuditReview 添加作品审核信息
func (order *orderApi) AddAuditReview(r *ghttp.Request) {
	var apiReq *backstage_model.AddAuditReview
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, http.StatusBadRequest, err.Error())
	}
	err := backstage_service.Order.AddAuditReview(apiReq)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	response.JsonExit(r, http.StatusOK, "数据返回成功")
}

// AddDataQuality 添加数据质检审核信息
func (order *orderApi) AddDataQuality(r *ghttp.Request) {
	var apiReq *backstage_model.AddDataQuality
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, http.StatusBadRequest, err.Error())
	}
	err := backstage_service.Order.AddDataQuality(apiReq)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	response.JsonExit(r, http.StatusOK, "数据返回成功")
}

// AddLinkQuality 添加链接质检信息
func (order *orderApi) AddLinkQuality(r *ghttp.Request) {
	var apiReq *backstage_model.AddLinkQuality
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, http.StatusBadRequest, err.Error())
	}
	err := backstage_service.Order.AddLinkQuality(apiReq)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	response.JsonExit(r, http.StatusOK, "数据返回成功")
}

// FindOrderProcedureDetail 查询对应订单所需要的流程
func (order *orderApi) FindOrderProcedureDetail(r *ghttp.Request) {
	var apiReq *backstage_model.AcquiredOrderId
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, http.StatusBadRequest, err.Error())
	}
	flow, err := utils.OrderProcedureManager.GetOrderWorkFlow(apiReq.OrderId)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	response.JsonExit(r, http.StatusOK, "数据返回成功", g.Map{
		"flow": flow,
	})
}

// ChargesDetail 查询一个订单下面所对应的扣款明细
func (order *orderApi) ChargesDetail(r *ghttp.Request) {
	var apiReq *backstage_model.AcquiredOrderId
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, http.StatusBadRequest, err.Error())
	}
	result, err := backstage_service.Order.ChargesDetail(apiReq)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	response.JsonExit(r, http.StatusOK, "数据返回成功", g.Map{
		"result": result,
	})
}

// QueryTaskRecruitTalentLevel 查询招募等级表
func (order *orderApi) QueryTaskRecruitTalentLevel(r *ghttp.Request) {
	var apiReq *backstage_model.AcquiredOrderId
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, http.StatusBadRequest, err.Error())
	}
	result, err := backstage_service.Order.QueryTaskRecruitTalentLevel(apiReq)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	response.JsonExit(r, http.StatusOK, "数据返回成功", g.Map{
		"result": result,
	})
}

// AddBobocoinIncomeRecord 订单完成后需要在收入里面加一条信息
func (order *orderApi) AddBobocoinIncomeRecord(r *ghttp.Request) {
	var apiReq *backstage_model.RequestBobocoinIncomeRecord
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, http.StatusBadRequest, err.Error())
	}
	err := backstage_service.Order.AddBobocoinIncomeRecord(apiReq)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	response.JsonExit(r, http.StatusOK, "操作成功")
}
