/**
* @Author: zou.yingbin
* @Create  : 2021/4/16 18:40
* @Modify  : 2021/4/16 18:40
 */

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"
	"strconv"
	"strings"
	"time"
)

// 查询日期
type QueryDate string

// IsNumberic 判断是否为数字, queryType 1:8位日期 2:6位日期
func (v QueryDate) IsNumberic(queryType int32) bool {
	if queryType == 1 {
		// 日报表 日期长度YYYYMMDD
		if len(v) != 8 {
			return false
		}
	} else if queryType == 2 {
		// 月报表 日期长度YYYYMM
		if len(v) != 6 {
			return false
		}
	}
	// 判断是否为数字
	if _, err := strconv.ParseInt(string(v), 10, 32); err != nil {
		return false
	}
	return true
}

// QryReportReq 查报表请求
// @Param userid query int true "用户ID"
// @Param querytype query int true "查询类型 1-报表 2-报表明细"
// @Param cycletype query int true "周期类型 - 0:日 1:月 2:季 3:年 4:周 5:全报表【原值】"
// @Param cycletime query string true "周期时间 日(YYYYMMDD) 月(YYYYMM) 季(YYYYQ) 年(YYYY) 周(YYYYMMDD周内任意一天) 全(0)【原值】"
type QryReportReq struct {
	USERID    int64  `form:"userid" binding:"required"`                // 用户id
	QUERYTYPE int32  `form:"querytype" binding:"required,min=1,max=2"` // 查询类型
	CYCLETYPE int32  `form:"cycletype"`                                // 周期类型
	CYCLETIME string `form:"cycletime" binding:"required"`             // 周期时间

	TRADEDATE string // 交易日
	BEGINDATE string // 开始交易日
	ENDDATE   string // 结束交易日
}

// Check 参数检查
func (r *QryReportReq) Check() bool {
	// 兼容性处理, 月报表日期如果是yyyymmdd, 只取yyyymm
	if r.CYCLETYPE == 1 && len(r.CYCLETIME) > 6 {
		r.CYCLETIME = r.CYCLETIME[:6]
	}
	// 检查日期格式
	switch r.CYCLETYPE {
	case 0, 4:
		// 日/周报表 日期格式为yyyymmdd
		return QueryDate(r.CYCLETIME).IsNumberic(1)
	case 1:
		// 月 日期格式为yyyymm
		return QueryDate(r.CYCLETIME).IsNumberic(2)
	case 2, 3, 5:
		// nothing to do
		return true
	default:
		logger.GetLogger().Errorf("cycletype[%v] invalid", r.CYCLETYPE)
		return false
	}
}

// CovertRequest 转换查询请求, 日期格式等
func (r *QryReportReq) CovertRequest() {

	// 日报表 -> 给TRADEDATE赋值
	if r.CYCLETYPE == 0 {
		r.TRADEDATE = r.CYCLETIME
	}

	// 周报表(YYYYMMDD) -> 周报表(YYYYIW)
	if r.QUERYTYPE == 1 && r.CYCLETYPE == 4 {
		r.BEGINDATE, r.ENDDATE = getDateScope(r.CYCLETYPE, r.CYCLETIME)
		r.CYCLETIME = mtpcache.GetWeekIW(r.CYCLETIME)
	}

	// 月报表 -> 获取起止日期
	if r.QUERYTYPE == 1 && r.CYCLETYPE == 1 {
		r.BEGINDATE, r.ENDDATE = getDateScope(r.CYCLETYPE, r.CYCLETIME)
	}

	// 周报表明细 -> 日报表明细([开始交易日,结束交易日])
	if r.QUERYTYPE == 2 && r.CYCLETYPE == 4 {
		r.BEGINDATE, r.ENDDATE = getDateScope(r.CYCLETYPE, r.CYCLETIME)
		r.CYCLETYPE = 0
	}

	// 月报表明细 -> 日报表明细([开始交易日,结束交易日])
	if r.QUERYTYPE == 2 && r.CYCLETYPE == 1 {
		r.BEGINDATE, r.ENDDATE = getDateScope(r.CYCLETYPE, r.CYCLETIME)
		r.CYCLETYPE = 0
	}
}

// getDateScope 获取日期范围
func getDateScope(cycletype int32, cycletime string) (beginDate, endDate string) {
	switch cycletype {
	case 1: // 月(yyyymm)
		if len(cycletime) == 6 {
			year, _ := strconv.ParseInt(cycletime[0:4], 10, 32)
			month, _ := strconv.ParseInt(cycletime[4:], 10, 32)
			if year >= 1970 && year <= 9999 && month >= 1 && month <= 12 {
				// 计算日期范围
				beginDate = cycletime + "01"
				d := time.Date(int(year), time.Month(month), 1, 0, 0, 0, 0, time.Now().Location())
				last := d.AddDate(0, 1, -1)
				endDate = last.Format("20060102")
			} else {
				beginDate = cycletime + "01"
				endDate = cycletime + "31"
			}
		}
	case 2: // 季, 暂不实现
	case 3: // 年, 暂不实现
	case 4: // 周
		if w, ok := mtpcache.GetWeekInfo(cycletime); ok {
			beginDate = w.BeginDate
			endDate = w.EndDate
		} else {
			// 转换失败 : 一般来说不会转换失败
			logger.GetLogger().Errorf("covert cycletime to week fail:%v", cycletime)
		}
	}
	return
}

// QryReportDaySpotDetail 查询现货日报表详情(菜单:报表查询/现货报表/现货日报表详情) - 作废
func QryReportDaySpotDetail(c *gin.Context) {
	a := app.GinUtils{Gin: app.Gin{C: c}}
	req := struct {
		USERID           int64  `form:"userid" binding:"required"`          // 用户id
		TRADEDATE        string `form:"tradedate" binding:"required"`       // 交易日
		DELIVERYGOODSID  int32  `form:"deliverygoodsid" binding:"required"` // 现货商品id
		SPOTGOODSBRANDID int32  `form:"spotgoodsbrandid"`                   // 品牌id
		WRSTANDARDID     int32  `form:"wrstandardid"`                       // 品类id
	}{}
	a.DoBindReq(&req)
	m := models.Ermcp3ReportOPLog{USERID: req.USERID, DELIVERYGOODSID: req.DELIVERYGOODSID,
		TRADEDATE: req.TRADEDATE, SPOTGOODSBRANDID: req.SPOTGOODSBRANDID, WRSTANDARDID: req.WRSTANDARDID,
		LogTypeFilter: "2,3,24,25,26,27"}
	a.DoGetDataI(&m)
}

// QryReportDayFinanceKx 查询财务日报表款项(菜单:报表查询/财务报表/日报表/款项) - 作废
func QryReportDayFinanceKx(c *gin.Context) {
	a := app.GinUtils{Gin: app.Gin{C: c}}
	req := struct {
		USERID    int64  `form:"userid" binding:"required"`    // 用户id
		TRADEDATE string `form:"tradedate" binding:"required"` // 交易日
	}{}
	a.DoBindReq(&req)
	m := models.Ermcp3ReportOPLog{USERID: req.USERID, TRADEDATE: req.TRADEDATE, LogTypeFilter: "8,9,10"}
	a.DoGetDataI(&m)
}

// QryReportDayFinanceFp 查询财务日报表发票(菜单:报表查询/财务报表/日报表/发票) - 作废
func QryReportDayFinanceFp(c *gin.Context) {
	a := app.GinUtils{Gin: app.Gin{C: c}}
	req := struct {
		USERID    int64  `form:"userid" binding:"required"`    // 用户id
		TRADEDATE string `form:"tradedate" binding:"required"` // 交易日
	}{}
	a.DoBindReq(&req)
	m := models.Ermcp3ReportOPLog{USERID: req.USERID, TRADEDATE: req.TRADEDATE, LogTypeFilter: "11, 12"}
	a.DoGetDataI(&m)
}

// QryReportDaySpot 查询现货日报表(菜单:报表查询/现货报表/现货日报表) - 作废
func QryReportDaySpot(c *gin.Context) {
	a := app.GinUtils{Gin: app.Gin{C: c}}
	m := models.Ermcp3ReckonAreaSpotSub{}
	a.DoBindReq(&m)
	a.DoGetDataI(&m)
}

// QryReportMonthSpot 查询现货月报表(菜单:报表查询/现货报表/现货月报表) - 作废
func QryReportMonthSpot(c *gin.Context) {
	a := app.GinUtils{Gin: app.Gin{C: c}}
	m := models.Ermcp3ReportAreaSpotSub{}
	a.DoBindReq(&m)
	a.DoGetDataI(&m)
}

// QryReportMonthSpotDetail 查询现货月报表详情(菜单:报表查询/现货月报表/现货月报表详情) - 作废
func QryReportMonthSpotDetail(c *gin.Context) {
	a := app.GinUtils{Gin: app.Gin{C: c}}
	req := models.Ermcp3ReportAreaSpotSub{}
	a.DoBindReq(&req)
	if req.CYCLETYPE != 1 || len(req.CYCLETIME) != 6 {
		// 目前仅支持月报表明细, 月报表的日期应是6位,如 202101
		a.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
		return
	}
	t1 := req.CYCLETIME + "01"
	t2 := req.CYCLETIME + "31"
	// 月报表明细 = 01~31 日报表
	m := models.Ermcp3ReckonAreaSpotSub{
		AREAUSERID: req.AREAUSERID, DELIVERYGOODSID: req.DELIVERYGOODSID, ACCOUNTID: req.ACCOUNTID,
		WRSTANDARDID: req.WRSTANDARDID, SPOTGOODSBRANDID: req.SPOTGOODSBRANDID, BeginDate: t1, EndDate: t2}
	a.DoGetDataI(&m)
}

// QryAreaExpourseReport
// @Summary 查询敞口报表
// @Produce json
// @Security ApiKeyAuth
// @Param userid query int true "用户ID"
// @Param querytype query int true "查询类型 1-报表, 这个接口只查报表;明细的字段完全不同,另有接口"
// @Param cycletype query int true "周期类型 - 0:日 1:月 2:季 3:年 4:周 5:全报表【原值】"
// @Param cycletime query string true "周期时间 日(YYYYMMDD) 月(YYYYMM)  季(YYYYQ) 年(YYYY) 周(YYYYMMDD周内任意一天) 全(0)【原值】"
// @Success 200 {array} models.Ermcp3ExpourseReport
// @Failure 500 {object} app.Response
// @Router /Ermcp3/QryAreaExpourseReport [get]
// @Tags 企业风险管理v3(app)
func QryAreaExpourseReport(c *gin.Context) {
	a := app.GinUtils{Gin: app.Gin{C: c}}
	req := QryReportReq{}
	a.DoBindReq(&req)
	a.CheckParam(&req)
	req.CovertRequest()
	m := models.Ermcp3ExpourseReport{AREAUSERID: req.USERID, QueryType: req.QUERYTYPE,
		CYCLETYPE: req.CYCLETYPE, CYCLETIME: req.CYCLETIME, RECKONDATE: req.TRADEDATE,
		BeginDate: req.BEGINDATE, EndDate: req.ENDDATE}
	a.DoGetDataI(&m)
}

// QryAreaExpourseContractDetail
// @Summary 查询敞口合同明细(敞口报表/现货合同明细)
// @Produce json
// @Security ApiKeyAuth
// @Param userid query int true "用户ID"
// @Param middlegoodsid query int true "套保商品id"
// @Param cycletype query int true "周期类型 - 0:日 1:月 2:季 3:年 4:周 5:全报表【原值】"
// @Param cycletime query string true "周期时间 日(YYYYMMDD) 月(YYYYMM)  季(YYYYQ) 年(YYYY) 周(YYYYMMDD周内任意一天) 全(0)【原值】"
// @Success 200 {array} models.Ermcp3ExposureContractDetail
// @Failure 500 {object} app.Response
// @Router /Ermcp3/QryAreaExpourseContractDetail [get]
// @Tags 企业风险管理v3(app)
func QryAreaExpourseContractDetail(c *gin.Context) {
	a := app.GinUtils{Gin: app.Gin{C: c}}
	tmp := struct {
		USERID        int64  `form:"userid" binding:"required"`        // 用户id
		MIDDLEGOODSID int32  `form:"middlegoodsid" binding:"required"` // 套保商品id
		CYCLETYPE     int32  `form:"cycletype"`
		CYCLETIME     string `form:"cycletime"`
	}{}
	a.DoBindReq(&tmp)
	req := QryReportReq{USERID: tmp.USERID, QUERYTYPE: 2, CYCLETYPE: tmp.CYCLETYPE, CYCLETIME: tmp.CYCLETIME}
	a.CheckParam(&req)
	req.CovertRequest()
	m := models.Ermcp3ExposureContractDetail{USERID: req.USERID,
		MIDDLEGOODSID: tmp.MIDDLEGOODSID, TRADEDATE: req.TRADEDATE, BeginDate: req.BEGINDATE, EndDate: req.ENDDATE}
	a.DoGetDataI(&m)
}

// QryAreaExpourseHedgeplanDetail
// @Summary 查询敞口套保计划明细(敞口报表/套保计划明细)
// @Produce json
// @Security ApiKeyAuth
// @Param userid query int true "用户ID"
// @Param middlegoodsid query int true "套保商品id"
// @Param cycletype query int true "周期类型 - 0:日 1:月 2:季 3:年 4:周 5:全报表【原值】"
// @Param cycletime query string true "周期时间 日(YYYYMMDD) 月(YYYYMM)  季(YYYYQ) 年(YYYY) 周(YYYYMMDD周内任意一天) 全(0)【原值】"
// @Success 200 {array} models.Ermcp3ExposureHedgeplanDetail
// @Failure 500 {object} app.Response
// @Router /Ermcp3/QryAreaExpourseHedgeplanDetail [get]
// @Tags 企业风险管理v3(app)
func QryAreaExpourseHedgeplanDetail(c *gin.Context) {
	a := app.GinUtils{Gin: app.Gin{C: c}}
	tmp := struct {
		USERID        int64  `form:"userid" binding:"required"`        // 用户id
		MIDDLEGOODSID int32  `form:"middlegoodsid" binding:"required"` // 套保商品id
		CYCLETYPE     int32  `form:"cycletype"`
		CYCLETIME     string `form:"cycletime"`
	}{}
	a.DoBindReq(&tmp)
	req := QryReportReq{USERID: tmp.USERID, QUERYTYPE: 2, CYCLETYPE: tmp.CYCLETYPE, CYCLETIME: tmp.CYCLETIME}
	a.CheckParam(&req)
	req.CovertRequest()
	m := models.Ermcp3ExposureHedgeplanDetail{USERID: req.USERID,
		MIDDLEGOODSID: tmp.MIDDLEGOODSID, TRADEDATE: req.TRADEDATE, BeginDate: req.BEGINDATE, EndDate: req.ENDDATE}
	a.DoGetDataI(&m)
}

// QryAreaExpourseFutuDetail
// @Summary 查询敞口期货明细(敞口报表/期货明细)
// @Produce json
// @Security ApiKeyAuth
// @Param userid query int true "用户ID"
// @Param middlegoodsid query int true "套保商品id"
// @Param cycletype query int true "周期类型 - 0:日 1:月 2:季 3:年 4:周 5:全报表【原值】"
// @Param cycletime query string true "周期时间 日(YYYYMMDD) 月(YYYYMM)  季(YYYYQ) 年(YYYY) 周(YYYYMMDD周内任意一天) 全(0)【原值】"
// @Success 200 {array} models.Ermcp3ExposureFutuDetail
// @Failure 500 {object} app.Response
// @Router /Ermcp3/QryAreaExpourseFutuDetail [get]
// @Tags 企业风险管理v3(app)
func QryAreaExpourseFutuDetail(c *gin.Context) {
	a := app.GinUtils{Gin: app.Gin{C: c}}
	tmp := struct {
		USERID        int64  `form:"userid" binding:"required"`        // 用户id
		MIDDLEGOODSID int32  `form:"middlegoodsid" binding:"required"` // 套保商品id
		CYCLETYPE     int32  `form:"cycletype"`
		CYCLETIME     string `form:"cycletime"`
	}{}
	a.DoBindReq(&tmp)
	req := QryReportReq{USERID: tmp.USERID, QUERYTYPE: 2, CYCLETYPE: tmp.CYCLETYPE, CYCLETIME: tmp.CYCLETIME}
	a.CheckParam(&req)
	req.CovertRequest()
	m := models.Ermcp3ExposureFutuDetail{USERID: req.USERID,
		MIDDLEGOODSID: tmp.MIDDLEGOODSID, RECKONDATE: req.TRADEDATE, BeginDate: req.BEGINDATE, EndDate: req.ENDDATE}
	a.DoGetDataI(&m)
}

// QryAreaExpourseParamChLogDetail
// @Summary 查询敞口参数调整明细(敞口报表/参数调整明细)
// @Produce json
// @Security ApiKeyAuth
// @Param userid query int true "用户ID"
// @Param middlegoodsid query int true "套保商品id"
// @Param cycletype query int true "周期类型 - 0:日 1:月 2:季 3:年 4:周 5:全报表【原值】"
// @Param cycletime query string true "周期时间 日(YYYYMMDD) 月(YYYYMM)  季(YYYYQ) 年(YYYY) 周(YYYYMMDD周内任意一天) 全(0)【原值】"
// @Success 200 {array} models.Ermcp3ExposureFutuDetail
// @Failure 500 {object} app.Response
// @Router /Ermcp3/QryAreaExpourseParamChLogDetail [get]
// @Tags 企业风险管理v3(app)
func QryAreaExpourseParamChLogDetail(c *gin.Context) {
	a := app.GinUtils{Gin: app.Gin{C: c}}
	tmp := struct {
		USERID        int64  `form:"userid" binding:"required"`        // 用户id
		MIDDLEGOODSID int64  `form:"middlegoodsid" binding:"required"` // 套保商品id
		CYCLETYPE     int32  `form:"cycletype"`
		CYCLETIME     string `form:"cycletime"`
	}{}
	a.DoBindReq(&tmp)
	req := QryReportReq{USERID: tmp.USERID, QUERYTYPE: 2, CYCLETYPE: tmp.CYCLETYPE, CYCLETIME: tmp.CYCLETIME}
	a.CheckParam(&req)
	req.CovertRequest()
	m := models.Ermcp3ExposureParamChLog{AREAUSERID: mtpcache.GetAreaUserId(req.USERID, 0),
		MIDDLEGOODSID: tmp.MIDDLEGOODSID, TRADEDATE: req.TRADEDATE, BeginDate: req.BEGINDATE, EndDate: req.ENDDATE}
	a.DoGetDataI(&m)
}

// QryAreaSpotplReport
// @Summary 查询现货报表
// @Produce json
// @Security ApiKeyAuth
// @Param userid query int true "用户ID"
// @Param querytype query int true "查询类型 1-报表 2-报表明细"
// @Param cycletype query int true "周期类型 - 0:日 1:月 2:季 3:年 4:周 5:全报表【原值】"
// @Param cycletime query string true "周期时间 日(YYYYMMDD) 月(YYYYMM)  季(YYYYQ) 年(YYYY) 周(YYYYMMDD周内任意一天) 全(0)【原值】"
// @Param deliverygoodsid query int false "现货品种id(明细必填)"
// @Param currencyid query int false "币种id(明细必填)"
// @Success 200 {array} models.Ermcp3AreaSpotPLReport
// @Failure 500 {object} app.Response
// @Router /Ermcp3/QryAreaSpotplReport [get]
// @Tags 企业风险管理v3(app)
func QryAreaSpotplReport(c *gin.Context) {
	a := app.GinUtils{Gin: app.Gin{C: c}}
	req := QryReportReq{}
	a.DoBindReq(&req)
	a.CheckParam(&req)
	req2 := struct {
		DELIVERYGOODSID int64 `form:"deliverygoodsid"` // 品种
		WRSTANDARDID    int64 `form:"wrstandardid"`    // 品类
		CURRENCYID      int32 `form:"currencyid"`      // 币种
	}{}
	if req.QUERYTYPE == 2 {
		a.DoBindReq(&req2)
		a.CheckParamF(func() bool {
			if req2.DELIVERYGOODSID <= 0 || req2.CURRENCYID <= 0 {
				return false
			}
			return true
		})
	}
	req.CovertRequest()
	m := models.Ermcp3AreaSpotPLReport{AREAUSERID: req.USERID, QueryType: req.QUERYTYPE, RECKONDATE: req.TRADEDATE,
		CYCLETYPE: req.CYCLETYPE, CYCLETIME: req.CYCLETIME, BeginDate: req.BEGINDATE, EndDate: req.ENDDATE,
		DELIVERYGOODSID: req2.DELIVERYGOODSID, WRSTANDARDID: req2.WRSTANDARDID, CURRENCYID: req2.CURRENCYID}
	a.DoGetDataI(&m)
}

// QryFinanceReport
// @Summary 查询财务报表
// @Produce json
// @Security ApiKeyAuth
// @Param userid query int true "用户ID"
// @Param querytype query int true "查询类型 1-报表 2-报表明细"
// @Param cycletype query int true "周期类型 - 0:日 1:月 2:季 3:年 4:周 5:全报表【原值】"
// @Param cycletime query string true "周期时间 日(YYYYMMDD) 月(YYYYMM)  季(YYYYQ) 年(YYYY) 周(YYYYMMDD周内任意一天) 全(0)【原值】"
// @Param currencyid query int false "币种id(报表明细必填)"
// @Param sumfields query string false "汇总维度(逗号隔开) 1-品种 2-品类 3-品牌"
// @Success 200 {array} models.Ermcp3FinanceReport
// @Failure 500 {object} app.Response
// @Router /Ermcp3/QryFinanceReport [get]
// @Tags 企业风险管理v3(app)
func QryFinanceReport(c *gin.Context) {
	a := app.GinUtils{Gin: app.Gin{C: c}}
	req := QryReportReq{}
	a.DoBindReq(&req)
	a.CheckParam(&req)
	req2 := struct {
		CURRENCYID int32  `form:"currencyid"` // 币种id
		SUMFIELDS  string `form:"sumfields"`  // 汇总维度
	}{}
	a.DoBindReq(&req2)
	a.CheckParamF(func() bool {
		if req.QUERYTYPE == 2 {
			return req2.CURRENCYID > 0
		}
		tmp := req2.SUMFIELDS
		req2.SUMFIELDS = strings.TrimSpace(req2.SUMFIELDS)
		for _, v := range req2.SUMFIELDS {
			if v != ',' {
				if v < '1' || v > '3' {
					logger.GetLogger().Errorf("QryFinanceReport sumfield[%v] check fail", tmp)
					return false
				}
			}
		}
		return true
	})
	req.CovertRequest()
	m := models.Ermcp3FinanceReport{AREAUSERID: req.USERID, QueryType: req.QUERYTYPE, RECKONDATE: req.TRADEDATE, SumFields: req2.SUMFIELDS,
		CYCLETYPE: req.CYCLETYPE, CYCLETIME: req.CYCLETIME, BeginDate: req.BEGINDATE, EndDate: req.ENDDATE, CURRENCYID: req2.CURRENCYID}
	a.DoGetDataI(&m)
}

// QryAreaStockReport
// @Summary 查询库存报表
// @Produce json
// @Security ApiKeyAuth
// @Param userid query int true "用户ID"
// @Param querytype query int true "查询类型 1-报表 2-报表明细"
// @Param cycletype query int true "周期类型 - 0:日 1:月 2:季 3:年 4:周 5:全报表【原值】"
// @Param cycletime query string true "周期时间 日(YYYYMMDD) 月(YYYYMM) 季(YYYYQ) 年(YYYY) 周(YYYYMMDD周内任意一天) 全(0)【原值】"
// @Param sumfields query string false "汇总维度(逗号隔开) 1-品种 2-品类 3-品牌 4-仓库"
// @Param deliverygoodsid query int false "品种ID"
// @Param wrstandardid query int false "品类ID"
// @Param spotgoodsbrandid query int false "品牌ID"
// @Param warehouseinfoid query int false "仓库ID"
// @Success 200 {array} models.Ermcp3AreaStockReportEx
// @Failure 500 {object} app.Response
// @Router /Ermcp3/QryAreaStockReport [get]
// @Tags 企业风险管理v3(app)
func QryAreaStockReport(c *gin.Context) {
	a := app.GinUtils{Gin: app.Gin{C: c}}
	req := QryReportReq{}
	req2 := struct {
		SUMFIELDS        string `form:"sumfields"`        // 汇总维度
		DELIVERYGOODSID  int32  `form:"deliverygoodsid"`  // 现货品种id
		WRSTANDARDID     int32  `form:"wrstandardid"`     // 品类ID
		SPOTGOODSBRANDID int32  `form:"spotgoodsbrandid"` // 品牌id
		WAREHOUSEINFOID  string `form:"warehouseinfoid"`  // 仓库id
	}{}
	a.DoBindReq(&req)
	a.CheckParam(&req)
	a.DoBindReq(&req2)
	a.CheckParamF(func() bool {
		tmp := req2.SUMFIELDS
		req2.SUMFIELDS = strings.TrimSpace(req2.SUMFIELDS)
		for _, v := range req2.SUMFIELDS {
			if v != ',' {
				if v < '1' || v > '4' {
					logger.GetLogger().Errorf("sumfield[%v] check fail", tmp)
					return false
				}
			}
		}
		return true
	})
	req.CovertRequest()
	m := models.Ermcp3AreaStockReportEx{USERID: req.USERID, RECKONDATE: req.TRADEDATE,
		CYCLETYPE: req.CYCLETYPE, CYCLETIME: req.CYCLETIME, BeginDate: req.BEGINDATE, EndDate: req.ENDDATE,
		DELIVERYGOODSID: req2.DELIVERYGOODSID, WRSTANDARDID: req2.WRSTANDARDID,
		SPOTGOODSBRANDID: req2.SPOTGOODSBRANDID, WAREHOUSEINFOID: req2.WAREHOUSEINFOID, SumFields: req2.SUMFIELDS}
	a.DoGetDataI(&m)
}

// QueryAreaStockReportDetail
// 查询库存报表明细 - 作废
func QueryAreaStockReportDetail(c *gin.Context) {
	a := app.GinUtils{Gin: app.Gin{C: c}}
	req := struct {
		USERID           int64  `form:"userid" binding:"required"`     // 机构ID
		DELIVERYGOODSID  int32  `form:"deliverygoodsid"`               // 现货品种id
		WRSTANDARDID     int32  `form:"wrstandardid"`                  // 品类ID
		SPOTGOODSBRANDID int32  `form:"spotgoodsbrandid"`              // 品牌id
		WAREHOUSEINFOID  string `form:"warehouseinfoid"`               // 现货仓库ID
		QUERYTYPE        int32  `form:"querytype" binding:"required"`  // 查询类型
		QUERYDATE        string `form:"querydate" binding:"required"`  // 查询日期
		DETAILTYPE       int32  `form:"detailtype" binding:"required"` // 明细类型
	}{}
	a.DoBindReq(&req)
	if QueryDate(req.QUERYDATE).IsNumberic(req.QUERYTYPE) {
		var beginDate, endDate string
		if req.QUERYTYPE == 1 {
			beginDate = req.QUERYDATE
			endDate = beginDate
		} else if req.QUERYTYPE == 2 {
			beginDate = req.QUERYDATE + "01"
			endDate = req.QUERYDATE + "31"
		}
		m := models.Ermcp3AreaStockApply{USERID: req.USERID, WRSTANDARDID: req.WRSTANDARDID,
			SPOTGOODSBRANDID: req.SPOTGOODSBRANDID, DELIVERYGOODSID: req.DELIVERYGOODSID,
			WAREHOUSEINFOID: req.WAREHOUSEINFOID, BeginDate: beginDate, EndDate: endDate, APPLYSTATUS: 2}
		// 出入库类型(可多项,逗号隔开) 1:采购入库 2:销售出库 3:生产入库 4:生产出库"
		if req.DETAILTYPE == 1 {
			m.FilterType = "1,3"
		} else if req.DETAILTYPE == 2 {
			m.FilterType = "2,4"
		}
		a.DoGetDataI(&m)
	} else {
		a.Gin.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
	}
}

// QryAreaSumPL
// @Summary 查询汇总损益表
// @Produce json
// @Security ApiKeyAuth
// @Param userid query int true "用户ID"
// @Param querytype query int true "查询类型 1-报表 2-报表明细"
// @Param cycletype query int true "周期类型 - 0:日 1:月 2:季 3:年 4:周 5:全报表【原值】"
// @Param cycletime query string true "周期时间 日(YYYYMMDD) 月(YYYYMM)  季(YYYYQ) 年(YYYY) 周(YYYYMMDD周内任意一天) 全(0)【原值】"
// @Param currencyid query int false "币种id(明细必填)"
// @Success 200 {array} models.Ermcp3ArealSumPL
// @Failure 500 {object} app.Response
// @Router /Ermcp3/QryAreaSumPL [get]
// @Tags 企业风险管理v3(app)
func QryAreaSumPL(c *gin.Context) {
	a := app.GinUtils{Gin: app.Gin{C: c}}
	req := QryReportReq{}
	a.DoBindReq(&req)
	a.CheckParam(&req)
	req2 := struct {
		CURRENCYID int32 `form:"currencyid"` // 币种id
	}{}
	a.DoBindReq(&req2)
	a.CheckParamF(func() bool {
		if req.QUERYTYPE == 2 {
			return req2.CURRENCYID > 0
		}
		return true
	})
	req.CovertRequest()
	m := models.Ermcp3ArealSumPL{AREAUSERID: req.USERID, QueryType: req.QUERYTYPE, RECKONDATE: req.TRADEDATE,
		CYCLETYPE: req.CYCLETYPE, CYCLETIME: req.CYCLETIME, BeginDate: req.BEGINDATE, EndDate: req.ENDDATE, CURRENCYID: req2.CURRENCYID}
	a.DoGetDataI(&m)
}

// QryTaFutureDataReport
// @Summary 查询期货报表
// @Produce json
// @Security ApiKeyAuth
// @Param userid query int true "用户ID"
// @Param querytype query int true "查询类型 1-报表 2-报表明细"
// @Param cycletype query int true "周期类型 - 0:日 1:月 2:季 3:年 4:周 5:全报表【原值】"
// @Param cycletime query string true "周期时间 日(YYYYMMDD) 月(YYYYMM)  季(YYYYQ) 年(YYYY) 周(YYYYMMDD周内任意一天) 全(0)【原值】"
// @Param currencyid query int false "币种id(查明细必填)"
// @Param goodsgroupid query int false "品种id(查明细必填)"
// @Param goodsid query int false "商品id(查明细必填)"
// @Param buyorsell query int false "买卖方向(查明细必填)"
// @Success 200 {array} models.Ermcp3TaFutuReDataReport
// @Failure 500 {object} app.Response
// @Router /Ermcp3/QryTaFutureDataReport [get]
// @Tags 企业风险管理v3(app)
func QryTaFutureDataReport(c *gin.Context) {
	a := app.GinUtils{Gin: app.Gin{C: c}}
	req := QryReportReq{}
	a.DoBindReq(&req)
	a.CheckParam(&req)
	req2 := struct {
		CURRENCYID   int32 `form:"currencyid"`   // 币种id
		GOODSGROUPID int32 `form:"goodsgroupid"` // 品种id
		GOODSID      int32 `form:"goodsid"`      // 商品id
		BUYORSELL    int32 `form:"buyorsell"`    // 买卖方向
	}{}
	a.DoBindReq(&req2)
	a.CheckParamF(func() bool {
		if req.QUERYTYPE == 2 {
			// 检查报表明细必填字段
			if req2.CURRENCYID == 0 || req2.GOODSGROUPID == 0 || req2.GOODSID == 0 || req2.BUYORSELL == 0 {
				return false
			}
		}
		return true
	})
	req.CovertRequest()
	m := models.Ermcp3TaFutuReDataReport{USERID: req.USERID, QueryType: req.QUERYTYPE, RECKONDATE: req.TRADEDATE,
		CYCLETYPE: req.CYCLETYPE, CYCLETIME: req.CYCLETIME, BeginDate: req.BEGINDATE, EndDate: req.ENDDATE,
		CURRENCYID: req2.CURRENCYID, GOODSGROUPID: req2.GOODSGROUPID, GOODSID: req2.GOODSID, BUYORSELL: req2.BUYORSELL}
	a.DoGetDataI(&m)
}
