/**
* @Author: zou.yingbin
* @Create  : 2021/4/15 10:35
* @Modify  : 2021/4/15 10:35
 */

package ermcp3

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

// QueryDeliveryGoods
// @Summary 查询现货商品
// @Produce json
// @Security ApiKeyAuth
// @Param userid query int true "用户id"
// @Param usertype query int false "用户类型 2-机构 7-企业成员"
// @Param excludecfg query int false "排除已配置的业务类型商品 1-套保 2-套利"
// @Success 200 {array} models.ErmcpDeliveryGoods
// @Failure 500 {object} app.Response
// @Router /Ermcp3/QueryDeliveryGoods [get]
// @Tags 企业风险管理v3(app)
func QueryDeliveryGoods(c *gin.Context) {
	a := app.GinUtils{Gin: app.Gin{C: c}}
	req := struct {
		UserId     int64 `form:"userid" binding:"required"` // 用户id
		USERTYPE   int32 `form:"usertype"`                  // 用户类型
		ExcludeCfg int32 `form:"excludecfg"`                // 排除已配置的业务类型商品 1-套保 2-套利
	}{}

	a.DoBindReq(&req)
	m := models.ErmcpDeliveryGoods{AREAUSERID: mtpcache.GetAreaUserId(req.UserId, req.USERTYPE),
		ExcludeCfg: req.ExcludeCfg}
	a.DoGetDataI(&m)
}

// QueryDeliveryGoodsDetail
// @Summary 查询现货商品详情
// @Produce json
// @Security ApiKeyAuth
// @Param userid query int true "用户id"
// @Param usertype query int false "用户类型 2-机构 7-企业成员"
// @Param deliverygoodsid query int false "现货商品id"
// @Success 200 {array} models.ErmcpDeliveryGoodsDetailEx
// @Failure 500 {object} app.Response
// @Router /Ermcp3/QueryDeliveryGoodsDetail [get]
// @Tags 企业风险管理v3(app)
func QueryDeliveryGoodsDetail(c *gin.Context) {
	a := app.GinUtils{Gin: app.Gin{C: c}}
	req := struct {
		UserId          int64 `form:"userid" binding:"required"` // 用户id
		USERTYPE        int32 `form:"usertype"`                  // 用户类型
		DeliveryGoodsId int32 `form:"deliverygoodsid"`           // 现货商品id
	}{}
	a.DoBindReq(&req)
	m := models.ErmcpDeliveryGoodsDetailEx{}
	m.Data = models.ErmcpDeliveryGoods{AREAUSERID: mtpcache.GetAreaUserId(req.UserId, req.USERTYPE),
		DELIVERYGOODSID: req.DeliveryGoodsId}
	a.DoGetDataI(&m)
}

// QueryMiddleGoodsDetail
// @Summary 查询套保品种详情(套保品种/商品详情)
// @Produce json
// @Security ApiKeyAuth
// @Param userid query int true  "用户id"
// @Param usertype query int false "用户类型 2-机构 7-企业成员"
// @Param middlegoodsid query int false  "套保品种id"
// @Success 200 {array} models.Ermcp3MiddleGoodsDetailEx
// @Failure 500 {object} app.Response
// @Router /Ermcp3/QueryMiddleGoodsDetail [get]
// @Tags 企业风险管理v3(app)
func QueryMiddleGoodsDetail(c *gin.Context) {
	a := app.GinUtils{Gin: app.Gin{C: c}}
	req := struct {
		USERID        int64  `form:"userid" binding:"required"` // 所属机构id
		USERTYPE      int32  `form:"usertype"`                  // 用户类型
		MIDDLEGOODSID uint32 `form:"middlegoodsid"`             // 套保品种id
	}{}
	a.DoBindReq(&req)
	m := models.Ermcp3MiddleGoodsDetailEx{}
	m.Ermcp3MiddleGoodsDetail.Mg.AREAUSERID = mtpcache.GetAreaUserId(req.USERID, req.USERTYPE)
	m.Ermcp3MiddleGoodsDetail.Mg.MIDDLEGOODSID = req.MIDDLEGOODSID
	a.DoGetDataI(&m)
}

// QryContractReq 查询合同请求
type QryContractReq struct {
	AreadUserId  int64  `form:"userid" binding:"required"`    // 所属机构Id
	CONTRACTTYPE int32  `form:"contracttype"`                 // 合同类型 1-采购, -1-销售
	QUERYTYPE    int32  `form:"querytype" binding:"required"` // 查询类型
	USERID       int64  `form:"userid"`                       // 用户Id
	USERTYPE     int32  `form:"usertype"`                     // 用户类型
	CONTRACTID   string `form:"contractid"`                   // 合同ID(SpotContractId)
}

// QuerySpotContract
// @Summary 查询现货合同
// @Produce json
// @Security ApiKeyAuth
// @Param userid query int true "用户id"
// @Param usertype query int false "用户类型 2-机构 7-企业成员"
// @Param querytype query int true "查询类型 1-未提交 2-待审核 3-履约中 4-已完成"
// @Param contractid query string false "合同ID(SpotContractId)"
// @Success 200 {array} models.Ermcp3Contract
// @Failure 500 {object} app.Response
// @Router /Ermcp3/QuerySpotContract [get]
// @Tags 企业风险管理v3(app)
func QuerySpotContract(c *gin.Context) {
	a := app.GinUtils{Gin: app.Gin{C: c}}
	req := QryContractReq{}
	a.DoBindReq(&req)
	m := models.Ermcp3Contract{USERID: mtpcache.GetAreaUserId(req.USERID, req.USERTYPE),
		QryType:   req.QUERYTYPE,
		OwnUserId: req.USERID, UserType: req.USERTYPE, SPOTCONTRACTID: req.CONTRACTID}
	a.DoGetDataI(&m)
}

// QuerySpotContractBS
// @Summary 查询合同(采购/销售)
// @Produce json
// @Security ApiKeyAuth
// @Param userid query int true "用户id"
// @Param usertype query int false "用户类型 2-机构 7-企业成员"
// @Param contracttype query int true "合同类型 1-采购, -1-销售"
// @Param querytype query int true "查询类型 1-全部 2-待点价 3-履约结算 4-已完成"
// @Param contractid query string false "合同ID(SpotContractId)"
// @Success 200 {array} models.Ermcp3SellBuyContract
// @Failure 500 {object} app.Response
// @Router /Ermcp3/QuerySpotContractBS [get]
// @Tags 企业风险管理v3(app)
func QuerySpotContractBS(c *gin.Context) {
	a := app.GinUtils{Gin: app.Gin{C: c}}
	req := QryContractReq{}
	a.DoBindReq(&req)
	m := models.Ermcp3SellBuyContract{UserID: mtpcache.GetAreaUserId(req.USERID, req.USERTYPE),
		Contracttype: req.CONTRACTTYPE,
		OwnUserId:    req.USERID, UserType: req.USERTYPE, SpotContractId: req.CONTRACTID, QryType: req.QUERYTYPE}
	a.DoGetDataI(&m)
}

// QueryExposureDetail
// @Summary 查询敞口现货明细
// @Produce json
// @Security ApiKeyAuth
// @Param userid query int true "用户id"
// @Param usertype query int false "用户类型 2-机构 7-企业成员"
// @Param middlegoodsid query int true "套保商品"
// @Success 200 {array} models.Ermcp3ExposureDetail
// @Failure 500 {object} app.Response
// @Router /Ermcp3/QueryExposureDetail [get]
// @Tags 企业风险管理v3(app)
func QueryExposureDetail(c *gin.Context) {
	a := app.GinUtils{Gin: app.Gin{C: c}}
	req := struct {
		USERID        int64 `form:"userid" binding:"required"`        // 用户id
		USERTYPE      int32 `form:"usertype"`                         // 用户类型
		MIDDLEGOODSID int32 `form:"middlegoodsid" binding:"required"` // 套保商品id
	}{}
	a.DoBindReq(&req)
	m := models.Ermcp3ExposureDetail{Areauserid: req.USERID,
		MiddlegoodsId: req.MIDDLEGOODSID}
	a.DoGetDataI(&m)
}

// QueryExposureSpot
// @Summary 查询敞口现货头寸(敞口/现货头寸)
// @Produce json
// @Security ApiKeyAuth
// @Param userid query int true "用户id"
// @Param usertype query int false "用户类型 2-机构 7-企业成员"
// @Success 200 {array} models.Ermcp3AreaSpot
// @Failure 500 {object} app.Response
// @Router /Ermcp3/QueryExposureSpot [get]
// @Tags 企业风险管理v3(app)
func QueryExposureSpot(c *gin.Context) {
	a := app.GinUtils{Gin: app.Gin{C: c}}
	req := struct {
		USERID int64 `form:"userid" binding:"required"` // 用户id
	}{}
	a.DoBindReq(&req)
	// 交易员有敞口, 也是按交易员userid生成的, 不用转换为areauserid
	m := models.Ermcp3AreaSpot{AREAUSERID: req.USERID}
	a.DoGetDataI(&m)
}

// QueryExposureSpotDetail
// @Summary 查询敞口现货头寸明细(敞口/现货头寸/现货明细)
// @Produce json
// @Security ApiKeyAuth
// @Param userid query int true "用户id"
// @Param usertype query int false "用户类型 2-机构 7-企业成员"
// @Param deliverygoodsid query int true "现货品种ID"
// @Success 200 {array} models.Ermcp3AreaSpotDetail
// @Failure 500 {object} app.Response
// @Router /Ermcp3/QueryExposureSpotDetail [get]
// @Tags 企业风险管理v3(app)
func QueryExposureSpotDetail(c *gin.Context) {
	appG := app.GinUtils{Gin: app.Gin{C: c}}
	req := struct {
		USERID          int64 `form:"userid" binding:"required"`          // 用户id
		USERTYPE        int32 `form:"usertype"`                           // 用户类型
		DELIVERYGOODSID int32 `form:"deliverygoodsid" binding:"required"` // 现货商品id
	}{}
	appG.DoBindReq(&req)
	m := models.Ermcp3AreaSpotDetail{UserId: req.USERID,
		DELIVERYGOODSID: req.DELIVERYGOODSID}
	appG.DoGetDataI(&m)
}

// QueryDGFactoryItem
// @Summary 查询品种要素定义项
// @Produce json
// @Security ApiKeyAuth
// @Param userid query int false "用户id"
// @Param usertype query int false "用户类型 2-机构 7-企业成员"
// @Param dgfactoryitemid query int false "选择项ID(SEQ_DGFACTORYITEM)"
// @Param deliverygoodsid query int false "现货品种ID"
// @Param dgfactoryitemtypeid query int false "要素项类型"
// @Param orderindex query int false "顺序"
// @Success 200 {array} models.ErmcpDGFactoryItem
// @Failure 500 {object} app.Response
// @Router /Ermcp3/QueryDGFactoryItem [get]
// @Tags 企业风险管理v3(app)
func QueryDGFactoryItem(c *gin.Context) {
	appG := app.GinUtils{Gin: app.Gin{C: c}}
	req := struct {
		USERID              int64 `form:"userid"`              // 用户id
		USERTYPE            int32 `form:"usertype"`            // 用户类型
		DGFACTORYITEMID     int64 `form:"dgfactoryitemid"`     // 选择项ID(SEQ_DGFACTORYITEM)
		DELIVERYGOODSID     int32 `form:"deliverygoodsid"`     // 现货品种ID
		DGFACTORYITEMTYPEID int64 `form:"dgfactoryitemtypeid"` // 要素项类型
		ORDERINDEX          int32 `form:"orderindex"`          // 顺序
	}{}
	appG.DoBindReq(&req)
	m := models.ErmcpDGFactoryItem{
		AREAUSERID:          mtpcache.GetAreaUserId(req.USERID, req.USERTYPE),
		DGFACTORYITEMID:     req.DGFACTORYITEMID,
		DELIVERYGOODSID:     req.DELIVERYGOODSID,
		DGFACTORYITEMTYPEID: req.DGFACTORYITEMTYPEID,
		ORDERINDEX:          req.ORDERINDEX,
	}
	appG.DoGetDataI(&m)
}

// QueryGoodsbrand
// @Summary 查询商品品牌
// @Produce json
// @Security ApiKeyAuth
// @Param userid query int true "用户id"
// @Param usertype query int false "用户类型 2-机构 7-企业成员"
// @Success 200 {array} models.Ermcp3Brand
// @Failure 500 {object} app.Response
// @Router /Ermcp3/QueryGoodsbrand [get]
// @Tags 企业风险管理v3(app)
func QueryGoodsbrand(c *gin.Context) {
	appG := app.GinUtils{Gin: app.Gin{C: c}}
	req := struct {
		USERID          int64 `form:"userid"`          // 用户id
		USERTYPE        int32 `form:"usertype"`        // 用户类型
		DELIVERYGOODSID int32 `form:"deliverygoodsid"` // 现货品种ID
	}{}
	appG.DoBindReq(&req)
	m := models.Ermcp3Brand{
		AREAUSERID:      mtpcache.GetAreaUserId(req.USERID, req.USERTYPE),
		DELIVERYGOODSID: req.DELIVERYGOODSID,
	}
	appG.DoGetDataI(&m)
}

// QueryGoodsWrstandard
// @Summary 查询商品品类
// @Produce json
// @Security ApiKeyAuth
// @Param userid query int true  "用户id"
// @Param usertype query int false "用户类型 2-机构 7-企业成员"
// @Param deliverygoodsid query int false  "现货商品id"
// @Success 200 {array} models.Ermcp3Wrstandard
// @Failure 500 {object} app.Response
// @Router /Ermcp3/QueryGoodsWrstandard [get]
// @Tags 企业风险管理v3(app)
func QueryGoodsWrstandard(c *gin.Context) {
	appG := app.GinUtils{Gin: app.Gin{C: c}}
	req := struct {
		USERID          int64 `form:"userid" binding:"required"` // 用户id
		USERTYPE        int32 `form:"usertype"`                  // 用户类型
		DELIVERYGOODSID int32 `form:"deliverygoodsid"`           // 现货商品id
	}{}
	appG.DoBindReq(&req)
	m := models.Ermcp3Wrstandard{AREAUSERID: mtpcache.GetAreaUserId(req.USERID, req.USERTYPE),
		DELIVERYGOODSID: req.DELIVERYGOODSID}
	appG.DoGetDataI(&m)
}

// QueryAreaStockApply
// @Summary 查询库存申请(出入库记录|库存审核)
// @Produce json
// @Security ApiKeyAuth
// @Param userid query int true "用户ID"
// @Param usertype query int false "用户类型 2-机构 7-企业成员"
// @Param deliverygoodsid query int false "现货商品ID"
// @Param inouttype query string false "出入库类型(可多项,逗号隔开) 5:采购入库 6:销售出库 7:生产入库 8:生产出库"
// @Param spotcontractid query int false "合同ID"
// @Param wrstandardid query int false "品类ID"
// @Param spotgoodsbrandid query int false "品牌ID"
// @Param warehouseinfoid query int false "仓库ID"
// @Param applystatus query string false "申请状态(可多项,逗号隔开)1:待审核 2:审核通过 3:审核拒绝 4:处理失败 5:已撤回"
// @Success 200 {array} models.Ermcp3AreaStockApply
// @Failure 500 {object} app.Response
// @Router /Ermcp3/QueryAreaStockApply [get]
// @Tags 企业风险管理v3(app)
func QueryAreaStockApply(c *gin.Context) {
	a := app.NewGinUtils(c)
	req := struct {
		USERID           int64  `form:"userid" binding:"required"` // 用户
		WRSTANDARDID     int32  `form:"wrstandardid"`              // 品类
		SPOTGOODSBRANDID int32  `form:"spotgoodsbrandid"`          // 品牌
		SPOTCONTRACTID   string `form:"spotcontractid"`            // 合同id
		WAREHOUSEINFOID  string `form:"warehouseinfoid"`           // 仓库id
		DELIVERYGOODSID  int32  `form:"deliverygoodsid"`           // 现货商品id
		FilterStatus     string `form:"applystatus"`               // 申请状态
		FilterType       string `form:"inouttype"`                 // 出入库类型
		USERTYPE         int32  `form:"usertype"`                  // 用户类型
	}{}
	a.DoBindReq(&req)
	m := models.Ermcp3AreaStockApply{
		USERID:           req.USERID,
		FilterType:       req.FilterType,
		SPOTCONTRACTID:   req.SPOTCONTRACTID,
		WRSTANDARDID:     req.WRSTANDARDID,
		SPOTGOODSBRANDID: req.SPOTGOODSBRANDID,
		DELIVERYGOODSID:  req.DELIVERYGOODSID,
		WAREHOUSEINFOID:  req.WAREHOUSEINFOID,
		FilterStatus:     req.FilterStatus,
	}
	a.DoGetDataI(&m)
}

// QueryAreaStock
// @Summary 查询机构库存(库存管理/当前库存)
// @Produce json
// @Security ApiKeyAuth
// @Param userid query int true "用户ID"
// @Success 200 {array} models.Ermcp3AreaStock
// @Failure 500 {object} app.Response
// @Router /Ermcp3/QueryAreaStock [get]
// @Tags 企业风险管理v3(app)
func QueryAreaStock(c *gin.Context) {
	a := app.GinUtils{Gin: app.Gin{C: c}}
	m := models.Ermcp3AreaStock{}
	a.DoBindReq(&m)
	a.DoGetDataI(&m)
}

// QueryAreaStockApplySum
// @Summary 查询已登记出入库信息(入库登记/已入库信息 | 出库登记/已出库信息)
// @Produce json
// @Security ApiKeyAuth
// @Param spotcontractid query int false "合同ID"
// @Success 200 {array} models.Ermcp3AreaStockApplySum
// @Failure 500 {object} app.Response
// @Router /Ermcp3/QueryAreaStockApplySum [get]
// @Tags 企业风险管理v3(app)
func QueryAreaStockApplySum(c *gin.Context) {
	a := app.GinUtils{Gin: app.Gin{C: c}}
	req := struct {
		SPOTCONTRACTID string `form:"spotcontractid"` // 合同id
	}{}
	a.DoBindReq(&req)
	m := models.Ermcp3AreaStockApplySum{SPOTCONTRACTID: req.SPOTCONTRACTID}
	a.DoGetDataI(&m)
}

// QuerySpotGoodsPrice
// @Summary 查询现货市价(现货市价)
// @Produce json
// @Security ApiKeyAuth
// @Param userid query int true "用户ID"
// @Success 200 {array} models.Ermcp3SpotGoodsPrice
// @Failure 500 {object} app.Response
// @Router /Ermcp3/QuerySpotGoodsPrice [get]
// @Tags 企业风险管理v3(app)
func QuerySpotGoodsPrice(c *gin.Context) {
	req := struct {
		UserId int64 `form:"userid" binding:"required"` // 用户id
	}{}
	a := app.GinUtils{Gin: app.Gin{C: c}}
	a.DoBindReq(&req)
	m := models.Ermcp3SpotGoodsPrice{AREAUSERID: req.UserId}
	a.DoGetDataI(&m)
}

// QuerySpotGoodsPriceLog
// @Summary 查询现货市价详情(现货市价/详情)
// @Produce json
// @Security ApiKeyAuth
// @Param userid query int true "用户ID"
// @Param deliverygoodsid query int true "现货商品ID"
// @Param brandid query int false "品牌ID"
// @Param wrstandardid query int false "品类ID"
// @Success 200 {array} models.Ermcp3SpotGoodsPriceLog
// @Failure 500 {object} app.Response
// @Router /Ermcp3/QuerySpotGoodsPriceLog [get]
// @Tags 企业风险管理v3(app)
func QuerySpotGoodsPriceLog(c *gin.Context) {
	req := struct {
		UserId          int64 `form:"userid" binding:"required"`          // 用户id
		DELIVERYGOODSID int32 `form:"deliverygoodsid" binding:"required"` // 现货商品id
		WRSTANDARDID    int64 `form:"wrstandardid"`                       // 品类id
		BRANDID         int32 `form:"brandid"`                            // 品牌id
	}{}
	a := app.GinUtils{Gin: app.Gin{C: c}}
	a.DoBindReq(&req)
	m := models.Ermcp3SpotGoodsPriceLog{AREAUSERID: req.UserId,
		DELIVERYGOODSID: req.DELIVERYGOODSID, WRSTANDARDID: req.WRSTANDARDID, SPOTGOODSBRANDID: req.BRANDID}
	a.DoGetDataI(&m)
}

// QueryHedgePlan 查询套保计划
// @Summary 查询套保计划
// @Produce json
// @Security ApiKeyAuth
// @Param userid query int true "用户ID"
// @Param usertype query int false "用户类型 2-机构 7-企业成员"
// @Param hedgeplanstatus query string true "套保计划状态(允许多个，逗号隔开) - 0:未提交 1:待审核 2:执行中 3:正常完结 4:审核拒绝 5:异常完结 6:已撤回"
// @Success 200 {array} models.Ermcp3HedgePlan
// @Failure 500 {object} app.Response
// @Router /Ermcp3/QueryHedgePlan [get]
// @Tags 企业风险管理v3(app)
func QueryHedgePlan(c *gin.Context) {
	appG := app.Gin{C: c}
	req := struct {
		UserId          int64  `form:"userid" binding:"required"`          // 用户id
		USERTYPE        int32  `form:"usertype"`                           // 用户类型
		HedgePlanStatus string `form:"hedgeplanstatus" binding:"required"` // 状态
	}{}
	if err := c.ShouldBind(&req); err != nil {
		logger.GetLogger().Errorf("query hedgeplanReq,%v", err)
		appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
		return
	}

	var m = models.Ermcp3HedgePlan{Areauserid: req.UserId, UserType: req.USERTYPE}
	if d, err := m.GetData(req.HedgePlanStatus); err == nil {
		appG.Response(http.StatusOK, e.SUCCESS, d)
	} else {
		logger.GetLogger().Errorf("query fail, %v", err)
		appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
	}
}

// QueryGoodsGroup
// @Summary 查询期货商品组
// @Produce json
// @Security ApiKeyAuth
// @Param userid query int true "用户id"
// @Param usertype query int false "用户类型 2-机构 7-企业成员"
// @Param excludecfg query int false "排除套保品中已关联的商品组 1-排除"
// @Success 200 {array} models.Ermcp3GoodsGroup
// @Failure 500 {object} app.Response
// @Router /Ermcp3/QueryGoodsGroup [get]
// @Tags 企业风险管理v3(app)
func QueryGoodsGroup(c *gin.Context) {
	a := app.GinUtils{Gin: app.Gin{C: c}}
	req := struct {
		USERID     int64 `form:"userid" binding:"required"` // 所属机构id
		USERTYPE   int32 `form:"usertype"`                  // 用户类型
		EXCLUDECFG int32 `form:"excludecfg"`                // 是否排除已配置
	}{}
	a.DoBindReq(&req)
	m := models.Ermcp3GoodsGroup{AreaUserId: mtpcache.GetAreaUserId(req.USERID, req.USERTYPE),
		ExcludeCfg: req.EXCLUDECFG}
	a.DoGetDataI(&m)
}

// QueryRootUserAccount
// @Summary 查询顶级用户信息
// @Produce json
// @Security ApiKeyAuth
// @Param userid query int true "用户ID"
// @Success 200 {array} models.Ermcp3RootUserAccount
// @Failure 500 {object} app.Response
// @Router /Ermcp3/QueryRootUserAccount [get]
// @Tags 企业风险管理v3(app)
func QueryRootUserAccount(c *gin.Context) {
	a := app.GinUtils{Gin: app.Gin{C: c}}
	req := struct {
		USERID int64 `form:"userid" binding:"required"` // 用户id
	}{}
	a.DoBindReq(&req)
	m := models.Ermcp3RootUserAccount{USERID: req.USERID}
	a.DoGetDataI(&m)
}
