/**
* @Author: zou.yingbin
* @Create  : 2021/1/18 9:26
* @Modify  : 2021/1/18 9:26
 */

package ermcp

import (
	"github.com/gin-gonic/gin"
	"mtp2_if/global/app"
	"mtp2_if/global/e"
	"mtp2_if/logger"
	"mtp2_if/models"
	"net/http"
)

//敞口相关查询

//实时敞口查询请求
type RealtimeExposureReq struct {
	AreaUserID uint32 `form:"AreaUserID" binding:"required"` //所属机构ID
}

//实时敞口查询应答
type RealtimeExposureRsp models.ErmcpRealExposureModel

// QueryUserInfo 查询实时敞口
// @Summary 查询实时敞口
// @Produce json
// @Security ApiKeyAuth
// @Param AreaUserID query int true "所属机构ID"
// @Success 200 {array} RealtimeExposureRsp
// @Failure 500 {object} app.Response
// @Router /Ermcp/QueryRealtimeExposure [get]
// @Tags 企业风险管理(app)
func QueryRealtimeExposure(c *gin.Context) {
	appG := app.Gin{C: c}
	var req RealtimeExposureReq
	if err := c.ShouldBind(&req); err != nil {
		logger.GetLogger().Errorf("QueryRealtimeExposure fail, %v", err)
		appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
		return
	}

	m := models.ErmcpRealExposureModel{AreaUserID: req.AreaUserID}
	if d, err := m.GetData(); err == nil {
		appG.Response(http.StatusOK, e.SUCCESS, d)
	} else {
		appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
	}
}

/***********************敞口明细*************************/

//敞口现货明细请求
type ExposureDetailReq struct {
	AreaUserId    uint32 `form:"areaUserId" json:"areaUserId"`       // 所属机构ID
	MiddleGoodsId int32  `form:"middleGoodsId" json:"middlegoodsid"` // 套保商品
}

//敞口现货明细响应
type ExposureDetailRsp models.ErmcpExposureDetailModel

// 查询敞口现货明细
// @Summary 查询敞口现货明细
// @Produce json
// @Security ApiKeyAuth
// @Param areaUserId query int true "所属机构ID"
// @Param middleGoodsId query int true "套保商品"
// @Success 200 {array} ExposureDetailRsp
// @Failure 500 {object} app.Response
// @Router /Ermcp/QueryExposureDetail [get]
// @Tags 企业风险管理(app)
func QueryExposureDetail(c *gin.Context) {
	appG := app.Gin{C: c}
	var req ExposureDetailReq
	if err := c.ShouldBind(&req); err != nil {
		appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
		return
	}

	var m = models.ErmcpExposureDetailModel{Areauserid: req.AreaUserId, MiddlegoodsId: req.MiddleGoodsId}
	if d, err := m.GetData(); err == nil {
		appG.Response(http.StatusOK, e.SUCCESS, d)
	} else {
		appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
	}
}

//////////////////////////////////////////////

// 敞口/现货头寸请求
type ExposureSpotReq struct {
	AreaUserID int32 `form:"areaUserId" binding:"required"` //所属机构ID
}

// 敞口/现货头寸应答
type ExposureSpotRsp models.AreaSpotModel

// QueryExposureSpot 查询敞口现货头寸
// @Summary 查询敞口现货头寸(敞口/现货头寸)
// @Produce json
// @Security ApiKeyAuth
// @Param areaUserId query int true "所属机构ID"
// @Success 200 {array} ExposureSpotRsp
// @Failure 500 {object} app.Response
// @Router /Ermcp/QueryExposureSpot [get]
// @Tags 企业风险管理(app)
func QueryExposureSpot(c *gin.Context) {
	appG := app.Gin{C: c}
	var req ExposureSpotReq
	if err := c.ShouldBind(&req); err != nil {
		appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
		return
	}
	var m = models.AreaSpotModel{AREAUSERID: req.AreaUserID}
	if d, err := m.GetData(); err == nil {
		appG.Response(http.StatusOK, e.SUCCESS, d)
	} else {
		appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
	}
}

////////////////////////////////////////////

// 敞口/现货头寸/现货明细请求
type ExposureSpotDetailReq struct {
	AreaUserID   int `form:"areaUserId" binding:"required"`   //所属机构ID
	WrstandardId int32 `form:"wrstandardId" binding:"required"` // 现货商品ID
}

// 敞口/现货头寸/现货明细应答
type ExposureSpotDetailRsp models.ErmcpAreaSpotDetailModel

// @Summary 查询敞口现货头寸明细(敞口/现货头寸/现货明细)
// @Produce json
// @Security ApiKeyAuth
// @Param areaUserId query int true "所属机构ID"
// @Param wrstandardId query int true "现货品种ID"
// @Success 200 {array} ExposureSpotDetailRsp
// @Failure 500 {object} app.Response
// @Router /Ermcp/QueryExposureSpotDetail [get]
// @Tags 企业风险管理(app)
func QueryExposureSpotDetail(c *gin.Context) {
	appG := app.GinUtils{Gin:app.Gin{C: c}}
	var req ExposureSpotDetailReq
	appG.DoBindReq(&req)
	m := models.ErmcpAreaSpotDetailModel{UserId: req.AreaUserID, WrstandardId: req.WrstandardId}
	// m必须传指针类型,否则无法进行类型转换
	appG.DoGetData(&m)
}

///////////////////////////////////////////

// 敞口/历史敞口请求
type HisExposureReq struct {
	AreaUserID int32 `form:"areaUserId" binding:"required"` //所属机构ID
	LastNum    int32 `form:"lastNum" binding:"required"`    //查询最近多少条记录
}

// 敞口/历史敞口应答
type HisExposureRsp models.ErmcpHisExposure

// QueryUserInfo 查询历史敞口
// @Summary 查询历史敞口(菜单:敞口/历史敞口)
// @Produce json
// @Security ApiKeyAuth
// @Param areaUserId query int true "所属机构ID"
// @Param lastNum query int true "最近多少条记录, -1:为查全部"
// @Success 200 {array} HisExposureRsp
// @Failure 500 {object} app.Response
// @Router /Ermcp/QueryHisExposure [get]
// @Tags 企业风险管理(app)
func QueryHisExposure(c *gin.Context) {
	appG := app.Gin{C: c}
	var req HisExposureReq
	if err := c.ShouldBind(&req); err != nil {
		appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
		return
	}
	var m = models.ErmcpHisExposure{AREAUSERID: req.AreaUserID, LastNum: req.LastNum}
	if d, err := m.GetData(); err == nil {
		appG.Response(http.StatusOK, e.SUCCESS, d)
	} else {
		appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
	}
}
