package services

import (
	errorcode "ai_scheduler/internal/data/error"
	"net/http"

	"github.com/gofiber/fiber/v2"
	"github.com/gofiber/fiber/v2/log"
)

// 响应数据
func handRes(c *fiber.Ctx, _err error, rsp interface{}) error {
	var (
		err *errorcode.BusinessErr
	)
	if _err == nil {
		err = errorcode.Success
	} else {
		if e, ok := _err.(*errorcode.BusinessErr); ok {
			err = e
		} else {
			log.Error(c.UserContext(), "系统错误 error: ", _err)
			err = errorcode.NewBusinessErr(http.StatusInternalServerError, _err.Error())
		}
	}

	body := fiber.Map{
		"code": err.Code(),
		"msg":  err.Error(),
		"data": rsp,
	}

	log.Info(c.UserContext(), c.Path(), "请求参数=", string(c.BodyRaw()), "响应=", body)
	return c.JSON(body)
}
