/**
* @Author: zou.yingbin
* @Create  : 2021/3/10 15:30
* @Modify  : 2021/3/10 15:30
 */

package ermcp

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

// 查询日期
type QueryDate string

// IsNumberic 判断是否为数字,
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
}

// QryAreaStockApplyReq 查询库存申请请求
type QryAreaStockApplyReq struct {
	USERID           int64  `form:"userid" binding:"required"` // 用户id
	INOUTTYPE        string `form:"inouttype"`                 // 出入库类型 - 1:采购入库 2:销售出库 3:生产入库 4:生产出库
	SPOTCONTRACTID   string `form:"spotcontractid"`            // 合同ID
	WRSTANDARDID     string `form:"wrstandardid"`              // 现货商品ID
	SPOTGOODSBRANDID int32  `form:"spotgoodsbrandid"`          // 品牌ID
	SPOTGOODSMODELID int32  `form:"spotgoodsmodelid"`          // 型号ID
	WAREHOUSEINFOID  string `form:"warehouseinfoid"`           // 仓库ID
	APPLYSTATUS      string `form:"applystatus"`               // 申请状态 - 1:待审核 2:审核通过 3:审核拒绝 4:处理失败 5:已撤回
}

// QryAreaStockApplyRsp 查询库存申请响应
type QryAreaStockApplyRsp models.ErmcpAreaStockApply

// QueryAreaStockApply
func QueryAreaStockApply(c *gin.Context) {
	a := app.NewGinUtils(c)
	req := QryAreaStockApplyReq{}
	a.DoBindReq(&req)
	m := models.ErmcpAreaStockApply{
		USERID:           req.USERID,
		FilterType:       req.INOUTTYPE,
		SPOTCONTRACTID:   req.SPOTCONTRACTID,
		WRSTANDARDID:     req.WRSTANDARDID,
		SPOTGOODSBRANDID: req.SPOTGOODSBRANDID,
		SPOTGOODSMODELID: req.SPOTGOODSMODELID,
		WAREHOUSEINFOID:  req.WAREHOUSEINFOID,
		FilterStatus:     req.APPLYSTATUS,
	}
	a.DoGetDataEx(&m)
}

// QueryAreaStockApplySum 出入库汇总请求
type QueryAreaStockApplySumReq struct {
	SPOTCONTRACTID string `form:"spotcontractid"` // 合同ID
}

// QueryAreaStockApplySumRsp 出入库汇总响应
type QueryAreaStockApplySumRsp models.ErmcpAreaStockApplySum

// QueryAreaStockApplySum
func QueryAreaStockApplySum(c *gin.Context) {
	a := app.GinUtils{Gin: app.Gin{C: c}}
	req := QueryAreaStockApplySumReq{}
	a.DoBindReq(&req)
	m := models.ErmcpAreaStockApplySum{SPOTCONTRACTID: req.SPOTCONTRACTID}
	a.DoGetDataEx(&m)
}

// QueryAreaStockReq 查询库存量请求
type QueryAreaStockReq struct {
	USERID int64 `form:"userid" binding:"required"` // 用户id
}

// QueryAreaStock
func QueryAreaStock(c *gin.Context) {
	a := app.GinUtils{Gin: app.Gin{C: c}}
	req := QueryAreaStockReq{}
	a.DoBindReq(&req)
	m := models.ErmcpAreaStock{USERID: req.USERID}
	a.DoGetDataEx(&m)
}

type QueryAreaStockReportReq struct {
	USERID           int64  `form:"userid" binding:"required"`    // 用户id
	DETAILTYPE       int32  `form:"detailtype"`                   // 明细类型 1:入库明细(采购入库+生产入库) 2:出库明细(销售出库+生产出库)
	WRSTANDARDID     string `form:"wrstandardid"`                 // 现货商品ID
	SPOTGOODSBRANDID int32  `form:"spotgoodsbrandid"`             // 品牌ID
	SPOTGOODSMODELID int32  `form:"spotgoodsmodelid"`             // 型号ID
	WAREHOUSEINFOID  string `form:"warehouseinfoid"`              // 仓库ID
	QUERYTYPE        int32  `form:"querytype" binding:"required"` // 查询类型 1-日报表(或明细) 2-月报表(或明细)
	QUERYDATE        string `form:"querydate" binding:"required"` // 查询日期(格式 日报表YYYYMMDD, 月报表YYYYMM)
}

// QueryAreaStockReport
func QueryAreaStockReport(c *gin.Context) {
	a := app.GinUtils{Gin: app.Gin{C: c}}
	req := QueryAreaStockReportReq{}
	a.DoBindReq(&req)
	if QueryDate(req.QUERYDATE).IsNumberic(req.QUERYTYPE) {
		m := models.ErmcpAreaStockReport{USERID: req.USERID, WRSTANDARDID: req.WRSTANDARDID,
			SPOTGOODSBRANDID: req.SPOTGOODSBRANDID, SPOTGOODSMODELID: req.SPOTGOODSMODELID,
			WAREHOUSEINFOID: req.WAREHOUSEINFOID, ReportDate: req.QUERYDATE, ReportType: req.QUERYTYPE}
		a.DoGetDataEx(&m)
	} else {
		a.Gin.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
	}
}

// QueryAreaStockReportDetail
func QueryAreaStockReportDetail(c *gin.Context) {
	a := app.GinUtils{Gin: app.Gin{C: c}}
	req := QueryAreaStockReportReq{}
	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.ErmcpAreaStockApply{USERID: req.USERID, WRSTANDARDID: req.WRSTANDARDID,
			SPOTGOODSBRANDID: req.SPOTGOODSBRANDID, SPOTGOODSMODELID: req.SPOTGOODSMODELID,
			WAREHOUSEINFOID: req.WAREHOUSEINFOID, BeginDate: beginDate, EndDate: endDate, APPLYSTATUS: 2}
		// 出入库类型(可多项,逗号隔开) 1:采购入库 2:销售出库 3:生产入库 4:生产出库"
		if req.DETAILTYPE == 1 {
			m.FilterStatus = "1,3"
		} else if req.DETAILTYPE == 2 {
			m.FilterStatus = "2,4"
		}
		a.DoGetDataEx(&m)
	} else {
		a.Gin.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
	}
}
