package controller

import (
	"github.com/gin-gonic/gin"
	"icloudapp.cn/tools/entity"
	"net/http"
)

/*
{
	"code": 10000, // 程序中的错误码
	"msg": xx,     // 提示信息
	"data": {},    // 数据
}
*/

type ResponseData struct {
	Code entity.ResCode `json:"code"`
	Msg  interface{}    `json:"msg"`
	Body interface{}    `json:"data,omitempty"`
}

type ResponseBodyData struct {
	Code entity.ResCode `json:"code"`
	Msg  interface{}    `json:"msg"`
	Body interface{}    `json:"body:omitempty"`
}

// Response error
func ResponseError(c *gin.Context, code entity.ResCode) {
	c.JSON(http.StatusOK, &ResponseData{
		Code: code,
		Msg:  code.Msg(),
		Body: nil,
	})
}

// Response error with msg
func ResponseErrorWithMsg(c *gin.Context, code entity.ResCode, msg interface{}) {
	c.JSON(http.StatusOK, &ResponseData{
		Code: code,
		Msg:  msg,
		Body: nil,
	})
}

// Response success
func ResponseSuccess(c *gin.Context, data interface{}) {
	c.JSON(http.StatusOK, &ResponseData{
		Code: entity.CodeSuccess,
		Msg:  entity.CodeSuccess.Msg(),
		Body: data,
	})
}

// Response {code, msg, body}
func ResponseNormal(c *gin.Context, code entity.ResCode, msg interface{}, body interface{}) {
	c.JSON(http.StatusOK, &ResponseData{
		Code: code,
		Msg:  msg,
		Body: body,
	})
}

func ResponseBody(c *gin.Context, code entity.ResCode, msg interface{}, body interface{}) {
	c.JSON(http.StatusOK, &ResponseBodyData{
		Code: code,
		Msg:  msg,
		Body: body,
	})
}