/**
* @Author: zou.yingbin
* @Create  : 2021/1/6 10:12
* @Modify  : 2021/1/6 10:12
 */

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 QrySpotContractReq struct {
	UserId    int64 `form:"userId" binding:"required"`    //用户ID
	QueryType int32 `form:"QueryType" binding:"required"` // 查询类型 1-未提交 2-待审核 3-履约中 4-已完成
}

// 查询现货合同应答
type QrySpotContractRsp models.ErmcpSpotContractModel

// QuerySpotContract  企业风险管理合同查询
func QuerySpotContract(c *gin.Context) {
	appG := app.Gin{C: c}
	var req QrySpotContractReq
	if err := c.ShouldBind(&req); err != nil {
		logger.GetLogger().Errorf("parse query req: %v", err)
		appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
		return
	}

	var m = models.ErmcpSpotContractModel{USERID: req.UserId}
	if d, err := m.GetData(req.QueryType); err == nil {
		appG.Response(http.StatusOK, e.SUCCESS, d)
	} else {
		appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
	}
}

// 查询合同请求结构
type QryErmcpReq struct {
	UserId       int64  `form:"userId" binding:"required"`       //用户ID
	ContractType int32  `form:"contracttype" binding:"required"` // 合同类型
	QueryType    int32  `form:"querytype" binding:"required"`    // 查询类型 1-全部 2-待点价 3-履约结算
	ContractId   string `form:"contractid"`                      // 合同id(SpotContractId)
}

// 查询合同应答
type QryErmcpRsp models.ErmcpModel

// QueryContract  企业风险管理合同查询
func QueryContract(c *gin.Context) {
	appG := app.Gin{C: c}
	var req QryErmcpReq
	if err := c.ShouldBind(&req); err != nil {
		logger.GetLogger().Errorf("parse query req: %v", err)
		appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
		return
	}

	// 参数检查
	if req.ContractType != 1 && req.ContractType != -1 {
		logger.GetLogger().Errorf("ContractType should in(1, -1)")
		appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
		return
	}

	if req.QueryType != 1 && req.QueryType != 2 && req.QueryType != 3 {
		logger.GetLogger().Errorf("QueryType should in(1, 2, 3)")
		appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
		return
	}

	var m = models.ErmcpModel{UserID: req.UserId, SpotContractId: req.ContractId}
	if d, err := m.GetData(req.ContractType, req.QueryType); err == nil {
		appG.Response(http.StatusOK, e.SUCCESS, d)
	} else {
		appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
	}
}

// QueryTradeConfigTMP
// @Summary 查询交易模板配置
// @Produce json
// @Security ApiKeyAuth
// @Param areauserid query int false "所属机构id"
// @Success 200 {array} models.ErmcpTradeConfigTMP
// @Failure 500 {object} app.Response
// @Router /Ermcp/QueryTradeConfigTMP [get]
// @Tags 企业风险管理(app)
func QueryTradeConfigTMP(c *gin.Context) {
	a := app.GinUtils{Gin: app.Gin{C: c}}
	req := struct {
		Areauserid int64 `form:"areauserid"` // 机构id
	}{}
	a.DoBindReq(&req)
	// 现在表里id都为1, 改为固定查arearuserid=1的
	req.Areauserid = 1
	m := models.ErmcpTradeConfigTMP{AREAUSERID: req.Areauserid}
	a.DoGetDataI(&m)
}
