/**
* @Author: zou.yingbin
* @Create  : 2021/1/27 9:50
* @Modify  : 2021/1/27 9:50
 */

package models

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

// ErmcpGGConvertCfg 期货品种系数表
type ErmcpGGConvertCfg struct {
	Srcgoodsgroupid  int32   `json:"srcgoodsgroupid"  xorm:"'Srcgoodsgroupid'"`   // 源品种ID
	Destgoodsgroupid int32   `json:"destgoodsgroupid"  xorm:"'Destgoodsgroupid'"` // 目标品种ID
	Convertratio     float64 `json:"convertratio"  xorm:"'Convertratio'"`         // 折算系数
	SrcExchangeCode  string  `json:"srcexchangecode"  xorm:"'SrcExchangeCode'"`   // 源期货交易所
	SrcName          string  `json:"srcname"  xorm:"'SrcName'"`                   // 源品种名称
	SrcCode          string  `json:"srccode"  xorm:"'SrcCode'"`                   // 源品种代码
	SrcUnitid        uint32  `json:"srcunitid"  xorm:"'SrcUnitid'"`               // 源品种单位ID
	SrcUnitidName    string  `json:"srcunitidname"  xorm:"'-'"`                   // 源品种单位名称
	DstExchangeCode  string  `json:"dstexchangecode"  xorm:"'DstExchangeCode'"`   // 目标期货交易所
	DstName          string  `json:"dstname"  xorm:"'DstName'"`                   // 目标品种名称
	DstCode          string  `json:"dstcode"  xorm:"'DstCode'"`                   // 目录品种代码
	DstUnitid        uint32  `json:"dstunitid"  xorm:"'DstUnitid'"`               // 目标品种单位ID
	DstUnitidName    string  `json:"dstunitidname"`                               // 目标品种单位名称
	MODIFYTIME       string  `json:"modifytime"  xorm:"'MODIFYTIME'"`             // 修改时间
}

// Calc 获取品种单位名称
func (r *ErmcpGGConvertCfg) Calc() {
	r.SrcUnitidName = mtpcache.GetEnumDicitemName(int32(r.SrcUnitid))
	r.DstUnitidName = mtpcache.GetEnumDicitemName(int32(r.DstUnitid))
}

// GetData 获取数据
func (r *ErmcpGGConvertCfg) GetData() ([]interface{}, error) {
	sData := make([]ErmcpGGConvertCfg, 0)
	if err := db.GetEngine().SQL(r.buildSql()).Find(&sData); err != nil {
		return nil, err
	}
	iData := make([]interface{}, 0)
	for i := range sData {
		sData[i].Calc()
		iData = append(iData, &sData[i])
	}
	return iData, nil
}

// GetData2 获取数据
func (r *ErmcpGGConvertCfg) GetData2() ([]ErmcpGGConvertCfg, error) {
	sData := make([]ErmcpGGConvertCfg, 0)
	if err := db.GetEngine().SQL(r.buildSql()).Find(&sData); err != nil {
		return nil, err
	}
	for i := range sData {
		sData[i].Calc()
	}
	return sData, nil
}

func (r *ErmcpGGConvertCfg) buildSql() string {
	sqlId := "select t.srcgoodsgroupid," +
		"       t.destgoodsgroupid," +
		"       t.convertratio," +
		"       to_char(t.modifytime,'yyyy-mm-dd hh24:mi:ss')  modifytime," +
		"       ex1.exexchangecode srcExchangeCode," +
		"       g1.goodsgroupname  srcName," +
		"       g1.outergroupcode  srcCode," +
		"       g1.goodunitid      srcUnitid," +
		"       ex2.exexchangecode dstExchangeCode," +
		"       g2.goodsgroupname  dstName," +
		"       g2.outergroupcode  dstCode," +
		"       g2.goodunitid      dstUnitid" +
		"  from ERMCP_GGConvertconfig t" +
		"  left join goodsgroup g1" +
		"    on t.srcgoodsgroupid = g1.goodsgroupid" +
		"  left join goodsgroup g2" +
		"    on t.destgoodsgroupid = g2.goodsgroupid" +
		"  left join externalexchange ex1" +
		"    on g1.exexchangeid = ex1.autoid" +
		"  left join externalexchange ex2" +
		"    on g2.exexchangeid = ex2.autoid" +
		" where 1 = 1"
	if r.Destgoodsgroupid > 0 {
		sqlId = sqlId + fmt.Sprintf("and t.Destgoodsgroupid=%v", r.Destgoodsgroupid)
	}
	if r.Srcgoodsgroupid > 0 {
		sqlId = sqlId + fmt.Sprintf("and t.Srcgoodsgroupid=%v", r.Srcgoodsgroupid)
	}
	sqlId += " order by t.destgoodsgroupid, t.modifytime desc"
	return sqlId
}

// ErmcpAvalidGoodsGroupModel 可选择的源期货品种
type ErmcpAvalidGoodsGroupModel struct {
	Goodsgroupid   int32  `json:"goodsgroupid"  xorm:"'Goodsgroupid'"`     // 期货品种id
	Goodsgroupname string `json:"goodsgroupname"  xorm:"'Goodsgroupname'"` // 期货品种名称
	Outergroupcode string `json:"outergroupcode"  xorm:"'Outergroupcode'"` // 期货品种代码
	Goodunitid     int32  `json:"goodunitid"  xorm:"'Goodunitid'"`         // 品种单位id
	GoodunitName   string `json:"goodunitname"  xorm:"'GoodunitName'"`     // 品种单位名称
	AreaUserId     int64  `json:"-"`                                       // 所属机构id
}

// GetDataEx 获取数据
func (r *ErmcpAvalidGoodsGroupModel) GetDataEx() (interface{}, error) {
	sData := make([]ErmcpAvalidGoodsGroupModel, 0)
	err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
	return sData, err
}

func (r *ErmcpAvalidGoodsGroupModel) buildSql() string {
	sqlId := "select distinct g.groupcategroyid," +
		"       g.goodsgroupid," +
		"       g.goodsgroupname," +
		"       g.outergroupcode," +
		"       g.goodunitid" +
		"  from ERMCP_GGConvertconfig t" +
		"  left join goodsgroup g" +
		"    on t.srcgoodsgroupid = g.goodsgroupid" +
		" where g.groupcategroyid not in" +
		"       (select g.groupcategroyid" +
		"          from erms_middlegoods t" +
		"          left join goodsgroup g" +
		"            on t.goodsgroupid = g.goodsgroupid" +
		"         where t.areauserid = %v" +
		"           and t.goodsgroupid is not null)"
	sqlId = fmt.Sprintf(sqlId, r.AreaUserId)
	return sqlId
}
