/**
* @Author: zou.yingbin
* @Create  : 2021/1/11 17:58
* @Modify  : 2021/1/11 17:58
 */

package ermcp

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

//点价数据
type DjData struct {
	PricedPrice *float64 `json:"pricedPrice,omitempty"` //点价价格(非必填)
	PricedQty   *float64 `json:"pricedQty,omitempty"`   //点价数量(非必填)
	Amount      *float64 `json:"amount,omitempty"`      //点价金额=(点价价格+升贴水)*点价数量
}

//计算点价金额
func (r *DjData) calc(priceMove float64) {
	if r.PricedPrice != nil && r.PricedQty != nil {
		r.Amount = new(float64)
		*r.Amount = (*r.PricedPrice + priceMove) * (*r.PricedQty)
	}
}

//结点登记数据
type ReckonData struct {
	ReckonRealQty      *float64 `json:"reckonrealqty,omitempty"`      //结算实际数量(非必填)
	ReckonOsAmount     *float64 `json:"reckonosamount,omitempty"`     //结算溢短金额(非必填)
	ReckonOtherAmount  *float64 `json:"reckonotheramount,omitempty"`  //结算其他费用(非必填)
	ReckonAdjustAmount *float64 `json:"reckonadjustamount,omitempty"` //结算调整金额(非必填)
	AddMargin          *float64 `json:"addmargin,omitempty"`          //追加保证金(非必填)
	DecMargin          *float64 `json:"decmargin,omitempty"`          //减少保证金(非必填)
}

//款项操作
type KxData struct {
	PayAmount    *float64 `json:"payamount,omitempty"`    //收付款(非必填)
	DeductAmount *float64 `json:"deductamount,omitempty"` //退款(非必填)
	KxType       int32    `json:"kxtype,omitempty"`       // 款项类型 1-收付款(PayAmount字段有值) 2-退款(DeductAmount字段有值) 3-收付款/退款(2个字段都有)
}

// 获取款项类型,1-收付款(PayAmount字段有值) 2-退款(DeductAmount字段有值) 3-收付款/退款(2个字段都有)
func (r *KxData) CalcKxType() {
	if r.PayAmount != nil && r.DeductAmount != nil {
		r.KxType = 3
	} else if r.PayAmount != nil {
		r.KxType = 1
	} else if r.DeductAmount != nil {
		r.KxType = 2
	}
}

//开票操作数据
type KpData struct {
	InvoiceAmount *float64 //已开收票金额(销售为开票,采购为收票)
}

// 查询合同操作请求
type QryOPApplyReq struct {
	UserId         int64  `form:"UserId" binding:"required"`    //用户ID
	RelatedId      string `form:"relatedid" binding:"required"` //现货合同ID, 不填则查所有
	OperateapplyId string `form:"operateapplyid"`               //操作申请id
	Applystatus    string `form:"applystatus"`                  // 申请状态 1:待审核 2:审核通过 3:审核拒绝 4:处理失败 5:已撤回
	USERTYPE       int32  `form:"usertype"`                     // 用户类型 2-机构 7-企业成员
}

// 查询业务管理/点价应答
type QryBusinessDjRsp struct {
	models.ErmcpOPApplyModel
	DjData
}

// QueryBusinessDj  企业风险管理合同操作查询
// @Summary 查询业务管理(点价)(对应菜单:业务管理/点价)
// @Produce json
// @Security ApiKeyAuth
// @Param UserId query int true "用户ID"
// @Param usertype query int false "用户类型 2-机构 7-企业成员"
// @Param relatedid query string false "现货合同ID, 不填则查所有"
// @Param applystatus query string false "申请状态(逗号隔开) 1:待审核 2:审核通过 3:审核拒绝 4:处理失败 5:已撤回"
// @Success 200 {array} QryBusinessDjRsp
// @Failure 500 {object} app.Response
// @Router /Ermcp/QueryBusinessDj [get]
// @Tags 企业风险管理(app)
func QueryBusinessDj(c *gin.Context) {
	appG := app.Gin{C: c}
	var req QryOPApplyReq
	_ = c.ShouldBind(&req)
	m := models.ErmcpOPApplyModel{RELATEDID: req.RelatedId, USERID: req.UserId, FilterAppStatus: req.Applystatus, UserType: req.USERTYPE}
	if d, err := m.GetData(1); err == nil {
		//构建应答数据
		sData := make([]QryBusinessDjRsp, 0)
		for _, v := range d {
			var rsp QryBusinessDjRsp
			rsp.ErmcpOPApplyModel = v
			if len(rsp.DETAILJSON) > 0 {
				if err := json.Unmarshal([]byte(rsp.DETAILJSON), &rsp.DjData); err == nil {
					rsp.DjData.calc(rsp.PRICEMOVE)
				}
				sData = append(sData, rsp)
			}
		}
		appG.Response(http.StatusOK, e.SUCCESS, sData)
	} else {
		appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
	}
}

// 查询业务管理/结算响应
type QryBussinessJsRsp struct {
	models.ErmcpOPApplyModel
	ReckonData
}

// QueryBusinessJs  企业风险管理合同操作查询
// @Summary 查询业务管理(结算)(对应菜单:业务管理/结算)
// @Produce json
// @Security ApiKeyAuth
// @Param UserId query int true "用户ID"
// @Param usertype query int false "用户类型 2-机构 7-企业成员"
// @Param relatedid query string false "现货合同ID, 不填则查所有"
// @Param operateapplyid query string false "操作申请id"
// @Param applystatus query string false "申请状态(逗号隔开) 1:待审核 2:审核通过 3:审核拒绝 4:处理失败 5:已撤回"
// @Success 200 {array} QryBussinessJsRsp
// @Failure 500 {object} app.Response
// @Router /Ermcp/QueryBusinessJs [get]
// @Tags 企业风险管理(app)
func QueryBusinessJs(c *gin.Context) {
	appG := app.Gin{C: c}
	var req QryOPApplyReq
	_ = c.ShouldBind(&req)
	m := models.ErmcpOPApplyModel{RELATEDID: req.RelatedId, USERID: req.UserId,
		OPERATEAPPLYID: req.OperateapplyId, FilterAppStatus: req.Applystatus, UserType: req.USERTYPE}
	if d, err := m.GetData(2); err == nil {
		//构建应答数据
		sData := make([]QryBussinessJsRsp, 0)
		for _, v := range d {
			var rsp QryBussinessJsRsp
			rsp.ErmcpOPApplyModel = v
			if len(rsp.DETAILJSON) > 0 {
				_ = json.Unmarshal([]byte(rsp.DETAILJSON), &rsp.ReckonData)
			}
			sData = append(sData, rsp)
		}
		appG.Response(http.StatusOK, e.SUCCESS, sData)
	} else {
		appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
	}
}

// 查询业务管理/结算响应(拆分字段)
type QryBussinessJsExRsp struct {
	models.ErmcpOPApplyModel
	ReckonType  string  `json:"reckontype"`  // 结算类型
	ReckonValue float64 `json:"reckonvalue"` // 值
}

// QueryBusinessJsEx
// @Summary 查询业务管理(结算)(对应菜单:业务管理/结算[拆分记录])
// @Produce json
// @Security ApiKeyAuth
// @Param UserId query int true "用户ID"
// @Param usertype query int false "用户类型 2-机构 7-企业成员"
// @Param relatedid query string false "现货合同ID, 不填则查所有"
// @Param applystatus query string false "申请状态(逗号隔开) 1:待审核 2:审核通过 3:审核拒绝 4:处理失败 5:已撤回"
// @Success 200 {array} QryBussinessJsExRsp
// @Failure 500 {object} app.Response
// @Router /Ermcp/QueryBusinessJsEx [get]
// @Tags 企业风险管理(app)
func QueryBusinessJsEx(c *gin.Context) {
	appG := app.Gin{C: c}
	var req QryOPApplyReq
	_ = c.ShouldBind(&req)
	m := models.ErmcpOPApplyModel{RELATEDID: req.RelatedId, USERID: req.UserId, FilterAppStatus: req.Applystatus, UserType: req.USERTYPE}
	if d, err := m.GetData(2); err == nil {
		//构建应答数据
		sData := make([]QryBussinessJsExRsp, 0)
		for _, v := range d {
			var rsp QryBussinessJsExRsp
			var rd ReckonData
			rsp.ErmcpOPApplyModel = v
			if len(rsp.DETAILJSON) > 0 {
				if err := json.Unmarshal([]byte(rsp.DETAILJSON), &rd); err == nil {
					if rd.ReckonRealQty != nil {
						rsp.ReckonType = "交收量"
						rsp.ReckonValue = *rd.ReckonRealQty
						sData = append(sData, rsp)
					}
					if rd.ReckonOtherAmount != nil {
						rsp.ReckonType = "其它费用"
						rsp.ReckonValue = *rd.ReckonOtherAmount
						sData = append(sData, rsp)
					}
					if rd.ReckonAdjustAmount != nil {
						rsp.ReckonType = "调整金额"
						rsp.ReckonValue = *rd.ReckonAdjustAmount
						sData = append(sData, rsp)
					}
					if rd.AddMargin != nil {
						rsp.ReckonType = "追加保证金"
						rsp.ReckonValue = *rd.AddMargin
						sData = append(sData, rsp)
					}
					if rd.DecMargin != nil {
						rsp.ReckonType = "退还保证金"
						rsp.ReckonValue = *rd.DecMargin
						sData = append(sData, rsp)
					}
					if rd.ReckonOsAmount != nil {
						rsp.ReckonType = "溢短金额"
						rsp.ReckonValue = *rd.ReckonOsAmount
						sData = append(sData, rsp)
					}
				}
			}
		}
		appG.Response(http.StatusOK, e.SUCCESS, sData)
	} else {
		appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
	}
}

// 查询业务管理/款项响应
type QryBussinessKxRsp struct {
	models.ErmcpOPApplyModel
	KxData
}

// QueryBusinessKx  企业风险管理合同操作查询
// @Summary 查询财务管理(款项)(对应菜单:财务管理/款项)
// @Produce json
// @Security ApiKeyAuth
// @Param UserId query int true "用户ID"
// @Param usertype query int false "用户类型 2-机构 7-企业成员"
// @Param relatedid query string false "现货合同ID, 不填则查所有"
// @Param applystatus query string false "申请状态(逗号隔开) 1:待审核 2:审核通过 3:审核拒绝 4:处理失败 5:已撤回"
// @Success 200 {array} QryBussinessKxRsp
// @Failure 500 {object} app.Response
// @Router /Ermcp/QueryBusinessKx [get]
// @Tags 企业风险管理(app)
func QueryBusinessKx(c *gin.Context) {
	appG := app.Gin{C: c}
	var req QryOPApplyReq
	_ = c.ShouldBind(&req)
	m := models.ErmcpOPApplyModel{RELATEDID: req.RelatedId, USERID: req.UserId, FilterAppStatus: req.Applystatus, UserType: req.USERTYPE}
	if d, err := m.GetData(3); err == nil {
		//构建应答数据
		sData := make([]QryBussinessKxRsp, 0)
		for _, v := range d {
			var rsp QryBussinessKxRsp
			rsp.ErmcpOPApplyModel = v
			if len(rsp.DETAILJSON) > 0 {
				if err := json.Unmarshal([]byte(rsp.DETAILJSON), &rsp.KxData); err == nil {
					rsp.KxData.CalcKxType()
					sData = append(sData, rsp)
				}
			}
		}
		appG.Response(http.StatusOK, e.SUCCESS, sData)
	} else {
		appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
	}
}

// 查询业务管理/款项响应
type QryBussinessFpRsp struct {
	models.ErmcpOPApplyModel
	KpData
}

// QueryBusinessFp  企业风险管理合同操作查询
// @Summary 查询财务管理(发票)(对应菜单:财务管理/发票)
// @Produce json
// @Security ApiKeyAuth
// @Param UserId query int true "用户ID"
// @Param usertype query int false "用户类型 2-机构 7-企业成员"
// @Param relatedid query string false "现货合同ID, 不填则查所有"
// @Param applystatus query string false "申请状态(逗号隔开) 1:待审核 2:审核通过 3:审核拒绝 4:处理失败 5:已撤回"
// @Success 200 {array} QryBussinessFpRsp
// @Failure 500 {object} app.Response
// @Router /Ermcp/QueryBusinessFp [get]
// @Tags 企业风险管理(app)
func QueryBusinessFp(c *gin.Context) {
	appG := app.Gin{C: c}
	var req QryOPApplyReq
	_ = c.ShouldBind(&req)
	m := models.ErmcpOPApplyModel{RELATEDID: req.RelatedId, USERID: req.UserId, FilterAppStatus: req.Applystatus, UserType: req.USERTYPE}
	if d, err := m.GetData(4); err == nil {
		//构建应答数据
		sData := make([]QryBussinessFpRsp, 0)
		for _, v := range d {
			var rsp QryBussinessFpRsp
			rsp.ErmcpOPApplyModel = v
			if len(rsp.DETAILJSON) > 0 {
				_ = json.Unmarshal([]byte(rsp.DETAILJSON), &rsp.KpData)
			}
			sData = append(sData, rsp)
		}
		appG.Response(http.StatusOK, e.SUCCESS, sData)
	} else {
		appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
	}
}
