/**
* @Author: zou.yingbin
* @Create  : 2021/2/25 13:32
* @Modify  : 2021/2/25 13:32
 */

package models

import (
	"fmt"
	"mtp2_if/db"
)

// ErmcpPendingAudit 待审核信息
type ErmcpPendingAudit struct {
	UserQty         int64 `json:"userqty"  xorm:"'UserQty'"`                 // 待审核数量: 用户
	SpotContractQty int64 `json:"spotcontractqty"  xorm:"'SpotContractQty'"` // 待审核数量: 现货合同
	HedgePlanQty    int64 `json:"hedgeplanqty"  xorm:"'HedgePlanQty'"`       // 待审核数量: 套保计划
	BusinessDjQty   int64 `json:"businessdjqty"  xorm:"'BusinessDjQty'"`     // 待审核数量: 点价
	BusinessJsQty   int64 `json:"businessjsqty"  xorm:"'BusinessJsQty'"`     // 待审核数量: 结算
	BusinessKxQty   int64 `json:"businesskxqty"  xorm:"'BusinessKxQty'"`     // 待审核数量: 款项
	BusinessFpQty   int64 `json:"businessfpqty"  xorm:"'BusinessFpQty'"`     // 待审核数量: 发票
	AreaStockQty    int64 `json:"areastockqty"  xorm:"'AreaStockQty'"`       // 待审核数量: 库存申请

	UserId int64 `json:"-"` // 所属用户id
}

// ErmcpPendingAuditQty 待审核数量类型和计数
type ErmcpPendingAuditQty struct {
	CntType int64 `json:"CntType"  xorm:"'CntType'"` // 类型 1-用户 2-合同 3-套保 4-点价申请 5-结算 6-款项 7-发票
	CntQty  int64 `json:"CntQty"  xorm:"'CntQty'"`   // 数量
}

func (r *ErmcpPendingAudit) buildSql() string {
	var sqlId = "select 1 as cnttype, count(1) cntqty from wskh_userinfo t where t.memberareaid = %v and t.userstate in(2,4)" +
		" union all " +
		"select 2, count(1) SpotContractQty from ermcp_spotcontract t where t.userid = %v and t.contractstatus = 1" +
		" union all " +
		"select 3, count(1) HedgePlanQty from ermcp_hedgeplan t where t.areauserid = %v and t.hedgeplanstatus = 1" +
		" union all " +
		"select 4, count(1) BusinessDjQty from ermcp_contractoperateapply t where t.userid = %v and t.applystatus = 1 and t.operateapplytype=1" +
		" union all " +
		"select 5, count(1) BusinessJsQty from ermcp_contractoperateapply t where t.userid = %v and t.applystatus = 1 and t.operateapplytype=2" +
		" union all " +
		"select 6, count(1) BusinessKxQty from ermcp_contractoperateapply t where t.userid = %v and t.applystatus = 1 and t.operateapplytype=3" +
		" union all " +
		"select 7, count(1) BusinessFpQty from ermcp_contractoperateapply t where t.userid = %v and t.applystatus = 1 and t.operateapplytype=4" +
		" union all " +
		"select 8, count(1) AreaStockQty from ermcp_contractoperateapply t where t.userid = %v and t.operateapplytype in(5,6,7,8) and t.applystatus = 1"
	sqlId = fmt.Sprintf(sqlId, r.UserId, r.UserId, r.UserId, r.UserId, r.UserId, r.UserId, r.UserId, r.UserId)
	return sqlId
}

// GetDataEx 获取小红点计数信息
func (r *ErmcpPendingAudit) GetDataEx() (interface{}, error) {
	rspData := make([]ErmcpPendingAudit, 0)
	sData := make([]ErmcpPendingAuditQty, 0)
	err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
	if err == nil {
		var v ErmcpPendingAudit
		for _, d := range sData {
			// 类型 1-用户 2-合同 3-套保 4-点价申请 5-结算 6-款项 7-发票
			switch d.CntType {
			case 1:
				v.UserQty = d.CntQty
			case 2:
				v.SpotContractQty = d.CntQty
			case 3:
				v.HedgePlanQty = d.CntQty
			case 4:
				v.BusinessDjQty = d.CntQty
			case 5:
				v.BusinessJsQty = d.CntQty
			case 6:
				v.BusinessKxQty = d.CntQty
			case 7:
				v.BusinessFpQty = d.CntQty
			case 8:
				v.AreaStockQty = d.CntQty
			}
		}
		rspData = append(rspData, v)
	}
	return rspData, err
}
