package market

import (
	"mtp2_if/global/app"
	"mtp2_if/global/e"
	"mtp2_if/logger"
	"mtp2_if/models"
	"net/http"

	"github.com/gin-gonic/gin"
)

// 通用市场

// QueryMarketRunReq 查询市场运行信息请求参数
type QueryMarketRunReq struct {
	MarketID int `form:"marketID"`
}

// QueryMarketRun 查询市场运行信息
// @Summary 查询市场运行信息
// @Produce json
// @Security ApiKeyAuth
// @Param marketID query int false "市场ID，不传返回所有"
// @Success 200 {object} models.Marketrun
// @Failure 500 {object} app.Response
// @Router /Market/QueryMarketRun [get]
// @Tags 通用市场
func QueryMarketRun(c *gin.Context) {
	appG := app.Gin{C: c}

	// 获取请求参数
	var req QueryMarketRunReq
	if err := appG.C.ShouldBindQuery(&req); err != nil {
		logger.GetLogger().Errorf("QueryMarketRun failed: %s", err.Error())
		appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
		return
	}

	marketRuns, err := models.GetMarketRuns(req.MarketID)
	if err != nil {
		// 查询失败
		logger.GetLogger().Errorf("QueryMarketRun failed: %s", err.Error())
		appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
		return
	}

	// 查询成功返回
	logger.GetLogger().Debugln("QueryMarketRun successed: %v", marketRuns)
	appG.Response(http.StatusOK, e.SUCCESS, marketRuns)
}

// QueryMarketsByLoginIDReq 获取登录账号有权限的市场信息请求参数
type QueryMarketsByLoginIDReq struct {
	LoginID int `form:"loginID" binding:"required"`
}

// QueryMarketsByLoginID 获取登录账号有权限的市场信息
// @Summary 获取登录账号有权限的市场信息
// @Produce json
// @Security ApiKeyAuth
// @Param loginID query int true "登录账号"
// @Success 200 {object} models.Market
// @Failure 500 {object} app.Response
// @Router /Market/QueryMarketsByLoginID [get]
// @Tags 通用市场
func QueryMarketsByLoginID(c *gin.Context) {
	appG := app.Gin{C: c}

	// 获取请求参数
	var req QueryMarketsByLoginIDReq
	if err := appG.C.ShouldBindQuery(&req); err != nil {
		logger.GetLogger().Errorf("QueryMarketsByLoginID failed: %s", err.Error())
		appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
		return
	}

	markets, err := models.GetMarketsByLoginID(req.LoginID)
	if err != nil {
		// 查询失败
		logger.GetLogger().Errorf("QueryMarketsByLoginID failed: %s", err.Error())
		appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
		return
	}

	// 查询成功返回
	logger.GetLogger().Debugln("QueryMarketsByLoginID successed: %v", markets)
	appG.Response(http.StatusOK, e.SUCCESS, markets)
}

// QueryGoodsesByLoginIDReq 获取登录账号有权限的商品信息请求参数
type QueryGoodsesByLoginIDReq struct {
	LoginID   int    `form:"loginID" binding:"required"`
	MarketIDs string `form:"marketIDs"`
}

// QueryGoodsesByLoginID 获取登录账号有权限的商品信息
// @Summary 获取登录账号有权限的商品信息
// @Produce json
// @Security ApiKeyAuth
// @Param loginID query int true "登录账号"
// @Param marketIDs query string false "市场ID列表，格式：1,2,3"
// @Success 200 {object} models.Goods
// @Failure 500 {object} app.Response
// @Router /Market/QueryGoodsesByLoginID [get]
// @Tags 通用市场
func QueryGoodsesByLoginID(c *gin.Context) {
	appG := app.Gin{C: c}

	// 获取请求参数
	var req QueryGoodsesByLoginIDReq
	if err := appG.C.ShouldBindQuery(&req); err != nil {
		logger.GetLogger().Errorf("QueryGoodsesByLoginID failed: %s", err.Error())
		appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
		return
	}

	goodses, err := models.GetGoodsByLoginID(req.LoginID, req.MarketIDs)
	if err != nil {
		// 查询失败
		logger.GetLogger().Errorf("QueryGoodsesByLoginID failed: %s", err.Error())
		appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
		return
	}

	// 查询成功返回
	logger.GetLogger().Debugln("QueryGoodsesByLoginID successed: %v", goodses)
	appG.Response(http.StatusOK, e.SUCCESS, goodses)
}
