package backstage_api

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

// 写统一接口处理小红书，抖音，微博，快手，B站，大众点评，知乎，

var Dianping = dianpingApi{}

type dianpingApi struct {
}

// FindAll 查询所有
func (dianping *dianpingApi) FindAll(r *ghttp.Request) {
	var apiReq *backstage_model.DianpingFind
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, http.StatusBadRequest, err.Error())
	}
	res, num, err := backstage_service.Dianping.FindByConditionDianping(apiReq)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	var result []*backstage_model.ResponseRedFind
	err = gconv.Structs(res, &result)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	response.JsonExit(r, http.StatusOK, "数据返回成功", g.Map{
		"result": result, "num": num,
	})
}

// RemoveAccount  下架
func (dianping *dianpingApi) RemoveAccount(r *ghttp.Request) {
	var apiReq *backstage_model.RemoveAccountDianping
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, http.StatusBadRequest, err.Error())
	}
	err := backstage_service.Dianping.RemoveAccount(apiReq)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	response.JsonExit(r, http.StatusOK, "0")
}

// FindAllById 单个查询所有信息
func (bilibili *dianpingApi) FindAllById(r *ghttp.Request) {
	// 只需要接收一个id即可
	var apiReq *backstage_model.RemoveAccountDianping
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, http.StatusBadRequest, err.Error())
	}
	res, err := backstage_service.Dianping.FindAllById(apiReq)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	var result *backstage_model.ResponsePlatformDianpingAccountInfo
	err = gconv.Structs(res, &result)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	// 查找失败原因及失败时间
	reason, err := backstage_service.Account.FindFailReason(6, apiReq.PlatformAccountId)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	response.JsonExit(r, http.StatusOK, "数据返回成功", g.Map{
		"result": result,
		"reason": reason,
	})
}
