package mobile

import (
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
	"gin-vue-admin/model/response"
	"gin-vue-admin/service"
	"gin-vue-admin/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

// @Router /ai/apiOwn [post]
func ApiOwn(c *gin.Context) {
	var req request.ApiOwnReq
	_ = c.ShouldBindJSON(&req)
	var err error

	data := ""
	if req.ApiMethod == "GET" {
		postData := make(map[string]string)
		data, err = utils.Get(req.ApiUrl, postData, "", 1)
	} else {
		data = utils.Post(req.ApiUrl, req.ApiData, "", 1)
	}

	reply := new(interface{})
	utils.UnserislizeJson(data, reply)

	//用户调用api日志
	apiLog := model.ApiLog{
		UserId: getUserID(c),
		Status: 1,
		Api:    req.ApiUrl,
		Req:    string(utils.SerializeJson(req)),
		Reply:  string(utils.SerializeJson(reply)),
	}
	service.CreateApiLog(apiLog)

	if data == "" {
		global.GVA_LOG.Error("失败!", zap.Any("err", err))
		response.FailWithMessage("失败", c)
	} else {
		response.OkWithData(reply, c)
	}
	return
}

// @Router /ai/apiField [get]
func ApiField(c *gin.Context) {
	var req request.ApiFieldReq
	_ = c.ShouldBindJSON(&req)
	title := make(map[string]string, 0)

	title["t1"] = "视频学习"
	title["t2"] = "病例学习"
	title["t3"] = "智能诊断"
	title["t4"] = "看病例"
	title["t5"] = "买商品"

	response.OkWithData(title, c)
	return
}
