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 Zhihu = zhihuApi{}

type zhihuApi struct {
}

// FindAll 查询所有
func (zhihu *zhihuApi) FindAll(r *ghttp.Request) {
	var apiReq *backstage_model.ZhihuFind
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, http.StatusBadRequest, err.Error())
	}
	res, num, err := backstage_service.Zhihu.FindByConditionZhihu(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 (zhihu *zhihuApi) RemoveAccount(r *ghttp.Request) {
	var apiReq *backstage_model.RemoveAccountZhihu
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, http.StatusBadRequest, err.Error())
	}
	err := backstage_service.Zhihu.RemoveAccount(apiReq)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	response.JsonExit(r, http.StatusOK, "0")
}

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