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

package ermcp

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

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

//敞口相关查询

//实时敞口查询请求
type RealtimeExposureReq struct {
	AreaUserID uint32 `form:"userid" binding:"required"` // 用户id
}

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

// QueryUserInfo 查询实时敞口
// @Summary 查询实时敞口
// @Produce json
// @Security ApiKeyAuth
// @Param userid 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.GetDataEx(); 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:"userid" binding:"required"`  //所属机构ID
	LastNum    int32 `form:"lastNum" binding:"required"` //查询最近多少条记录
}

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

// QueryUserInfo 查询历史敞口
// @Summary 查询历史敞口(菜单:敞口/历史敞口)
// @Produce json
// @Security ApiKeyAuth
// @Param userid query int true "用户id"
// @Param lastNum query int true "每个品种记录数"
// @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)
	}
}

// 实时敞口/期货明细 请求
type RealtimeExposurePositionReq struct {
	AreaUserId    int64 `form:"userid" json:"areaUserId"`           // 所属机构ID
	MiddleGoodsId int32 `form:"middleGoodsId" json:"middlegoodsid"` // 套保商品
}

// 实时敞口/期货明细 应答
type RealtimeExposurePositionRsp models.ErmcpExposurePostion

// @Summary 查询实时敞口期货头寸明细(菜单:实时敞口/期货明细)
// @Produce json
// @Security ApiKeyAuth
// @Param userid query int true "用户id"
// @Param middleGoodsId query int true "套保商品ID"
// @Success 200 {array} RealtimeExposurePositionRsp
// @Failure 500 {object} app.Response
// @Router /Ermcp/QueryRealtimeExposurePosition [get]
// @Tags 企业风险管理(app)
func QueryRealtimeExposurePosition(c *gin.Context) {
	appG := app.GinUtils{Gin: app.Gin{C: c}}
	var req RealtimeExposurePositionReq
	appG.DoBindReq(&req)
	m := models.ErmcpExposurePostion{AREAUSERID: req.AreaUserId, MIDDLEGOODSID: req.MiddleGoodsId}
	appG.DoGetDataEx(&m)
}

// 敞口/期货头寸 请求
type ExposureHedgePositionReq struct {
	AreaUserId int64 `form:"userid" json:"areaUserId"` // 用户ID
}

// 敞口/期货头寸 应答
type ExposureHedgePositionRsp models.ErmcpHedgePosition

// @Summary 查询敞口期货头寸(菜单:敞口/期货头寸)
// @Produce json
// @Security ApiKeyAuth
// @Param userid query int true "用户id"
// @Success 200 {array} ExposureHedgePositionRsp
// @Failure 500 {object} app.Response
// @Router /Ermcp/QueryExposureHedgePosition [get]
// @Tags 企业风险管理(app)
func QueryExposureHedgePosition(c *gin.Context) {
	appG := app.Gin{C: c}
	var req ExposureHedgePositionReq
	if err := c.ShouldBind(&req); err != nil {
		appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
		return
	}
	if mtpcache.IsAreaUserId(req.AreaUserId) {
		var m = models.ErmcpHedgePosition{RELATEDUSERID: 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)
		}
	} else {
		var m = models.ErmcpTradePosition{USERID: req.AreaUserId}
		if d, err := m.GetData(); err == nil {
			sData := make([]models.ErmcpHedgePosition, 0)
			for _, v := range d {
				hedgePos := models.ErmcpHedgePosition{
					RELATEDUSERID:   v.USERID,
					YDBUYPOSITION:   int32(v.BUYPOSITIONQTY),
					CURBUYPOSITION:  int32(v.BUYCURPOSITIONQTY),
					YDSELLPOSITION:  int32(v.SELLPOSITIONQTY),
					CURSELLPOSITION: int32(v.SELLCURPOSITIONQTY),
					HEDGEGOODSID:    v.GOODSID,
					GOODSID:         v.GOODSID,
					GOODSCODE:       v.GOODSCODE,
					GOODSNAME:       v.GOODSNAME,
				}
				hedgePos.Calc()
				sData = append(sData, hedgePos)
			}
			appG.Response(http.StatusOK, e.SUCCESS, sData)
		} else {
			appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
		}
	}
}

// 期货明细请求
type ExposureHedgePosDetailReq struct {
	AreaUserId int32 `form:"userid" json:"areaUserId"` // 所属机构ID
	GoodsId    int32 `form:"goodsId" json:"goodsId"`   // 套保商品
}

// 期货明细应答
type ExposureHedgePosDetailRsp models.ErmcpHedgePositionDetail

// QueryExposureHedgePositionDetail 查询敞口期货头寸期货明细
// @Summary 查询敞口期货头寸期货明细(菜单:敞口/期货头寸/期货明细)
// @Produce json
// @Security ApiKeyAuth
// @Param userid query int true "所属机构ID"
// @Param goodsId query int true "商品id"
// @Success 200 {array} ExposureHedgePosDetailRsp
// @Failure 500 {object} app.Response
// @Router /Ermcp/QueryExposureHedgePositionDetail [get]
// @Tags 企业风险管理(app)
func QueryExposureHedgePositionDetail(c *gin.Context) {
	appG := app.GinUtils{Gin: app.Gin{C: c}}
	var req ExposureHedgePosDetailReq
	appG.DoBindReq(&req)
	m := models.ErmcpHedgePositionDetail{AREAUSERID: req.AreaUserId, HEDGEGOODSID: req.GoodsId}
	appG.DoGetDataEx(&m)
}

// QueryExposureGoods
// @Summary 查询敞口主力合约
// @Produce json
// @Security ApiKeyAuth
// @Param middlegoodsid query int true "套保商品id"
// @Success 200 {array} models.ErmcpMiddlegoodsRelateGoods
// @Failure 500 {object} app.Response
// @Router /Ermcp/QueryExposureGoods [get]
// @Tags 企业风险管理(app)
func QueryExposureGoods(c *gin.Context) {
	a := app.GinUtils{Gin: app.Gin{C: c}}
	req := struct {
		MIDDLEGOODSID int32 `form:"middlegoodsid" binding:"required"` // 用户id
	}{}
	a.DoBindReq(&req)
	m := models.ErmcpMiddlegoodsRelateGoods{MIDDLEGOODSID: req.MIDDLEGOODSID}
	a.DoGetDataI(&m)
}
