/**
* @Author: zou.yingbin
* @Create  : 2020/12/2 9:17
* @Modify  : 2020/12/10 10:45
 */

// 查询待审核的合同
package models

import (
	"fmt"
	"mtp2_if/db"
	"mtp2_if/logger"
)

// 待审核合同(普通合同)
type AuditContractModel struct {
	SpotContractId    string `json:"spotcontractid"  xorm:"'SPOTCONTRACTID'" binding:"required"`       // 合同ID
	MatchCustomerName string `json:"matchcustomername"  xorm:"'MATCHCUSTOMERNAME'" binding:"required"` // 销售方ID
	MatchAccountId    string `json:"matchaccountid"  xorm:"'MATCHACCOUNTID'"`                          // 业务员ID
	AccountId         string `json:"accountid"  xorm:"'ACCOUNTID'"`                                    // 交易员ID
	CustomerName      string `json:"customername"  xorm:"'CUSTOMERNAME'"`                              // 采购方ID
	ApplyStatus       uint32 `json:"applystatus"  xorm:"'APPLYSTATUS'"`                                // 状态
	EnumdicName       string `json:"enumdicname"  xorm:"'ENUMDICNAME'"`                                // 单位名称
	DetailJSON        string `json:"detailjson"  xorm:"'DETAILJSON'"`                                  // 合同明细
}

// 组装查询普通待审核合同Sql
func (r *AuditContractModel) buildSql(accountIDs string, contractType int32) string {
	str := "select to_char(s.spotcontractid) spotcontractid," +
		"       u.customername," +
		"       s.accountid," +
		"       u2.customername matchcustomername," +
		"       s.customeraccountid matchaccountid," +
		"       s.signdate," +
		"       s.applystatus," +
		"       to_char(s.DETAILJSON) DETAILJSON" +
		"  from ERMS3_SpotContractApply s" +
		"  left join userinfo u" +
		"    on u.userid = s.areauserid" +
		"  left join userinfo u2" +
		"    on u2.userid = s.customeruserid" +
		" where s.contracttype = %v" +
		"   and s.applystatus in(0,2)" +
		"   and s.accountid in(%v)"

	return fmt.Sprintf(str, contractType, accountIDs)
}

// 获取待审核的合同
func (r *AuditContractModel) GetData(accountIDs string, contractType int32) ([]AuditContractModel, error) {
	sAC := make([]AuditContractModel, 0)
	e := db.GetEngine()
	s := e.SQL(r.buildSql(accountIDs, contractType))
	if err := s.Find(&sAC); err != nil {
		logger.GetLogger().Errorf("query pending contract fail:%v", err)
		return sAC, err
	}
	return sAC, nil
}

// 待审核合同(回购)
type AutditContractHGModel struct {
	SpotContractId    string  `json:"spotcontractid"  xorm:"'SPOTCONTRACTID'" binding:"required"`       // 合同ID
	MatchCustomerName string  `json:"matchcustomername"  xorm:"'MATCHCUSTOMERNAME'" binding:"required"` // 销售方ID
	MatchAccountId    string  `json:"matchaccountid"  xorm:"'MATCHACCOUNTID'"`                          // 业务员ID
	AccountId         string  `json:"accountid"  xorm:"'ACCOUNTID'"`                                    // 交易员ID
	CustomerName      string  `json:"customername"  xorm:"'CUSTOMERNAME'"`                              // 采购方ID
	WrstandardName    string  `json:"wrstandardname"  xorm:"'WRSTANDARDNAME'"`                          // 商品名称
	Wrstandardcode    string  `json:"wrstandardcode"  xorm:"'WRSTANDARDCODE'"`                          // 商品代码
	ApplyStatus       uint32  `json:"applystatus"  xorm:"'APPLYSTATUS'"`                                // 状态
	EnumdicName       string  `json:"enumdicname"  xorm:"'ENUMDICNAME'"`                                // 单位名称
	ContractQty       float64 `json:"contractqty"  xorm:"'CONTRACTQTY'"`                                // 合同量
	SignDate          string  `json:"signdate"  xorm:"'SIGNDATE'"`                                      // 签订日期
}

func (r *AutditContractHGModel) buildSql(accountIDs string) string {
	sqlId := "select to_char(t.wrrcontractid) spotcontractid," +
		"       t.accountid," +
		"       t.contractqty," +
		"       to_char(t.signdate, 'yyyy-mm-dd') signdate," +
		"       t.applystatus," +
		"       w.wrstandardname," +
		"       w.wrstandardcode," +
		"       u.accountname       customername," +
		"       u2.accountname      matchcustomername," +
		"       t.customeraccountid matchaccountid," +
		"       ec.enumdicname" +
		"  from erms2_wrrapply t" +
		"  left join wrstandard w" +
		"    on t.wrstandardid = w.wrstandardid" +
		"  left join useraccount u" +
		"    on t.areauserid = u.userid" +
		"  left join useraccount u2" +
		"    on t.customeruserid = u2.userid" +
		"  left join enumdicitem ec" +
		"    on w.unitid = ec.enumitemname" +
		"   and ec.enumdiccode = 'goodsunit'" +
		" where t.accountid in(%v)" +
		"   and t.applystatus in (0, 2)"

	return fmt.Sprintf(sqlId, accountIDs)
}

// 从数据库获取待审核合同
func (r *AutditContractHGModel) GetData(accountIDs string) ([]AutditContractHGModel, error) {
	sAC := make([]AutditContractHGModel, 0)
	e := db.GetEngine()
	s := e.SQL(r.buildSql(accountIDs))
	if err := s.Find(&sAC); err != nil {
		logger.GetLogger().Errorf("query hg pending contract fail:%v", err)
		return sAC, err
	}
	return sAC, nil
}

// 待审核基差贸易业务
type PendingAuditBizModel struct {
	BizID     string `json:"bizid"  xorm:"'BIZID'"`         // 业务ID
	AccountID string `json:"accountid"  xorm:"'ACCOUNTID'"` // 现货账户
	Type      int32  `json:"type"  xorm:"'TYPE'"`           // 业务类型,1-期现套利,2-仓单回购,3-现货贸易
	BizName   string `json:"bizname"  xorm:"'BIZNAME'"`     // 业务名称
	AreaName  string `json:"areaname"  xorm:"'AREANAME'"`   // 所属部门
	Status    int32  `json:"status"  xorm:"'STATUS'"`       // 状态,0:待审核 1:审核通过 2:审核中 3:审核失败 4已撤销 5:审核拒绝
}

func (r *PendingAuditBizModel) buildSql(accIDs string) string {
	sqlId := "select t.*, u.accountname AreaName" +
		"  from (select to_char(spottradeid)   BizID," +
		"               spottradename BizName," +
		"               to_char(spotaccountid) AccountID," +
		"               3               as Type," +
		"               areauserid    userId," +
		"               applystatus   Status" +
		"          from erms2_spottradeapply" +
		"        union all" +
		"        select to_char(wrrcontractid)   BizID," +
		"               wrrcontractname BizName," +
		"               to_char(accountid) accountid," +
		"               2                 as Type," +
		"               areauserid      userId," +
		"               applystatus     Status" +
		"          from erms2_wrrapply t" +
		"        union all" +
		"        select to_char(a.asapplyid)   BizID," +
		"               a.asname      BizName," +
		"               to_char(b.accountid) accountid," +
		"               1             as Type," +
		"               a.userid," +
		"               a.applystatus Status" +
		"          from erms2_asapply a" +
		"         inner join erms2_asaccount b" +
		"            on a.asapplyid = b.asapplyid" +
		"           and b.taaccountbiztype = 1) t" +
		"  left join useraccount u" +
		"    on t.userId = u.userid" +
		" where t.Status in (0, 2, 5) and t.accountid in(%v)"

	return fmt.Sprintf(sqlId, accIDs)
}

// 从数据库获取待审核基差贸易业务
func (r *PendingAuditBizModel) GetData(accIDs string) ([]PendingAuditBizModel, error) {
	sRet := make([]PendingAuditBizModel, 0)
	e := db.GetEngine()
	s := e.SQL(r.buildSql(accIDs))
	if err := s.Find(&sRet); err != nil {
		logger.GetLogger().Errorf("query pending biz fail: %v", err)
		return nil, err
	}

	return sRet, nil
}
