/**
* @Author: zou.yingbin
* @Create  : 2021/1/5 15:15
* @Modify  : 2021/1/5 15:15
 */

// 企业风险管理
package models

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

// ErmcpSpotContractModel 现货合同结构（对应现货合同菜单)
type ErmcpSpotContractModel struct {
	SPOTCONTRACTID    string  `json:"spotcontractid"  xorm:"'SPOTCONTRACTID'"`       // 现货合同ID(602+Unix秒时间戳(10位)+xxxxxx)
	CONTRACTNO        string  `json:"contractno"  xorm:"'CONTRACTNO'"`               // 现货合同编号
	CONTRACTTYPE      int32   `json:"contracttype"  xorm:"'CONTRACTTYPE'"`           // 现货合同类型 - 1:采购 -1:销售
	USERID            int64   `json:"userid"  xorm:"'USERID'"`                       // 机构ID
	BUYUSERID         int64   `json:"buyuserid"  xorm:"'BUYUSERID'"`                 // 采购方ID
	BUYUSERName       string  `json:"buyusername"  xorm:"'BUYUSERName'"`             // 采购方名称
	SELLUSERID        int64   `json:"selluserid"  xorm:"'SELLUSERID'"`               // 销售方ID
	SELLUSERNAME      string  `json:"sellusername"  xorm:"'SELLUSERNAME'"`           // 销售方名称
	DELIVERYGOODSID   int32   `json:"deliverygoodsid"  xorm:"'DELIVERYGOODSID'"`     // 现货品种ID
	WRSTANDARDID      int32   `json:"wrstandardid"  xorm:"'wrstandardid'"`           // 现货商品ID
	WRSTANDARDNAME    string  `json:"wrstandardname"  xorm:"'WRSTANDARDNAME'"`       // 现货商品名称
	WRSTANDARDCODE    string  `json:"wrstandardcode"  xorm:"'WRSTANDARDCODE'"`       // 现货商品代码
	DELIVERYGOODSCODE string  `json:"deliverygoodscode"  xorm:"'DELIVERYGOODSCODE'"` // 现货品种代码
	DELIVERYGOODSNAME string  `json:"deliverygoodsname"  xorm:"'DELIVERYGOODSNAME'"` // 现货品种名称
	PRODUCTTYPE       int32   `json:"producttype"  xorm:"'PRODUCTTYPE'"`             // 产品类型 - 1:标准仓单 2:等标 3:非标
	CONVERTFACTOR     float64 `json:"convertfactor"  xorm:"'CONVERTFACTOR'"`         // 标仓系数
	SPOTGOODSDESC     string  `json:"spotgoodsdesc"  xorm:"'SPOTGOODSDESC'"`         // 商品型号
	PRICETYPE         int32   `json:"pricetype"  xorm:"'PRICETYPE'"`                 // 定价类型 - 1:一口价 2:点价 3:暂定价
	QTY               float64 `json:"qty"  xorm:"'QTY'"`                             // 数量
	PRICE             float64 `json:"price"  xorm:"'PRICE'"`                         // 价格\暂定价 [1:一口价、3:暂定价]
	AMOUNT            float64 `json:"amount"  xorm:"'AMOUNT'"`                       // 金额 [1:一口价、3:暂定价]
	DELIVERYSTARTDATE string  `json:"deliverystartdate"  xorm:"'DELIVERYSTARTDATE'"` // 交收期(开始)
	DELIVERYENDDATE   string  `json:"deliveryenddate"  xorm:"'DELIVERYENDDATE'"`     // 交收期(结束)
	GOODSID           int32   `json:"goodsid"  xorm:"'GOODSID'"`                     // 点价合约ID - 0:为现货，其它为期货商品合约ID [2:点价 3:暂定价]
	GOODSCODE         string  `json:"goodscode"  xorm:"'GOODSCODE'"`                 // 点价合约代码
	GOODSNAME         string  `json:"goodsname"  xorm:"'GOODSNAME'"`                 // 点价商品名称
	PRICEMOVE         float64 `json:"pricemove"  xorm:"'PRICEMOVE'"`                 // 升贴水 [2:点价 3:暂定价]
	STARTDATE         string  `json:"startdate"  xorm:"'STARTDATE'"`                 // 点价开始日期 [2:点价 3:暂定价]
	ENDDATE           string  `json:"enddate"  xorm:"'ENDDATE'"`                     // 点价结束日期 [2:点价 3:暂定价]
	MARGIN            float64 `json:"margin"  xorm:"'MARGIN'"`                       // 当前保证金
	CONTRACTMARGIN    float64 `json:"contractmargin"  xorm:"'CONTRACTMARGIN'"`       // 合同保证金
	EnumdicName       string  `json:"enumdicname"  xorm:"'ENUMDICNAME'"`             // 单位名称
	CONTRACCTSTATUS   int32   `json:"contracctstatus"  xorm:"'CONTRACTSTATUS'"`      // 合同状态 - 0:未提交 1:待审核 2:执行中 3:正常完结 4:审核拒绝 5:异常完结 6:已撤回
	Remark            string  `json:"remark"  xorm:"'Remark'"`                       // 备注
	AUDITREMARK       string  `json:"auditremark"  xorm:"'AUDITREMARK'"`             // 审核意见
	AUDITTIME         string  `json:"audittime"  xorm:"'AUDITTIME'"`                 // 审核时间
	CREATETIME        string  `json:"createtime"  xorm:"'CREATETIME'"`               // 创建时间
	UPDATETIME        string  `json:"updatetime"  xorm:"'UPDATETIME'"`               // 更新时间
	SPOTGOODSMODELID  int32   `json:"spotgoodsmodelid"  xorm:"'SPOTGOODSMODELID'"`   // 现货型号ID
	SPOTGOODSBRANDID  int32   `json:"spotgoodsbrandid"  xorm:"'SPOTGOODSBRANDID'"`   // 现货品牌ID
	BRANDNAME         string  `json:"brandname"  xorm:"'brandname'"`                 // 品牌名称
	MODELNAME         string  `json:"modelname"  xorm:"'modelname'"`                 // 型号名称
	ATTACHMENT        string  `json:"attachment"  xorm:"'ATTACHMENT'"`               // 附件
}

func (r *ErmcpSpotContractModel) buildSql(nQueryType int32) string {
	str := "SELECT to_char(t.SPOTCONTRACTID) SPOTCONTRACTID," +
		"       t.CONTRACTNO," +
		"       t.CONTRACTTYPE," +
		"       t.USERID," +
		"       t.BUYUSERID," +
		"       t.SELLUSERID," +
		"       t.DELIVERYGOODSID," +
		"       t.wrstandardid," +
		"       t.PRODUCTTYPE," +
		"       t.CONVERTFACTOR," +
		"       t.SPOTGOODSDESC," +
		"       t.PRICETYPE," +
		"       t.QTY," +
		"       t.PRICE," +
		"       t.AMOUNT," +
		"       to_char(t.DELIVERYSTARTDATE, 'yyyy-mm-dd hh24:mi:ss') DELIVERYSTARTDATE," +
		"       to_char(t.DELIVERYENDDATE, 'yyyy-mm-dd hh24:mi:ss') DELIVERYENDDATE," +
		"       t.GOODSID," +
		"       t.PRICEMOVE," +
		"       to_char(t.STARTDATE, 'yyyy-mm-dd hh24:mi:ss') STARTDATE," +
		"       to_char(t.ENDDATE, 'yyyy-mm-dd hh24:mi:ss') ENDDATE," +
		"       t.MARGIN," +
		"       t.contractmargin," +
		"       t.remark," +
		"       t.auditremark," +
		"       to_char(t.contractattachment) attachment," +
		"       t.contractstatus," +
		"       t.SPOTGOODSMODELID," +
		"       t.SPOTGOODSBRANDID," +
		"       to_char(t.audittime, 'yyyy-mm-dd hh24:mi:ss') audittime," +
		"       to_char(t.createtime, 'yyyy-mm-dd hh24:mi:ss') createtime," +
		"       to_char(t.updatetime, 'yyyy-mm-dd hh24:mi:ss') updatetime," +
		"       u1.accountname BuyUserName," +
		"       u2.accountname SellUserName," +
		"       g.deliverygoodscode," +
		"       g.deliverygoodsname," +
		"       g2.goodscode," +
		"       g2.goodsname," +
		"       gb.brandname," +
		"       gm.modelname," +
		"       e.Enumdicname," +
		"       w.wrstandardname," +
		"       w.wrstandardcode" +
		"  FROM ERMCP_SPOTCONTRACT t" +
		"  left join useraccount u1" +
		"    on t.buyuserid = u1.userid" +
		"  left join useraccount u2" +
		"    on t.selluserid = u2.userid" +
		"  left join deliverygoods g" +
		"    on t.deliverygoodsid = g.deliverygoodsid" +
		"  left join goods g2" +
		"    on t.goodsid = g2.goodsid" +
		"    left join spotgoodsbrand gb on t.spotgoodsbrandid=gb.brandid" +
		"    left join spotgoodsmodel gm on t.spotgoodsmodelid=gm.modelid" +
		"  left join wrstandard w" +
		"    on t.wrstandardid = w.wrstandardid" +
		"  left join enumdicitem e" +
		"    on w.unitid = e.enumitemname" +
		"   and e.enumdiccode = 'goodsunit'" +
		" where t.contractstatus in (%v)" +
		"   and t.userid = %v"

	// 查询类型 1-未提交 2-待审核 3-履约中 4-已完成
	// 合同状态 - 0:未提交 1:待审核 2:执行中 3:正常完结 4:审核拒绝 5:异常完结 6:已撤回
	var status string
	switch nQueryType {
	case 1: // 未提交
		status = "0,4,6"
		str += " order by t.updatetime desc"
	case 2: // 待审核
		status = "1"
		str += " order by t.updatetime desc"
	case 3: // 履约中
		status = "2"
		str += " order by t.audittime desc"
	case 4: // 已完成
		status = "3,5"
		str += " order by t.updatetime desc"
	}

	return fmt.Sprintf(str, status, r.USERID)
}

// GetData 从数据库中查询现货合同
func (r *ErmcpSpotContractModel) GetData(nQueryType int32) ([]ErmcpSpotContractModel, error) {
	sData := make([]ErmcpSpotContractModel, 0)
	e := db.GetEngine()
	s := e.SQL(r.buildSql(nQueryType))
	if err := s.Find(&sData); err != nil {
		logger.GetLogger().Errorf("ermcp query fail:%v", err)
		return sData, err
	}
	return sData, nil
}

// ErmcpModel 风险管理合同(对应采购和销售菜单)
type ErmcpModel struct {
	UserID             int64   `json:"userid"  xorm:"'UserID'"`                         // 用户ID
	SpotContractId     string  `json:"spotcontractid"  xorm:"'SPOTCONTRACTID'"`         // 合同ID
	AccountName        string  `json:"accountname"  xorm:"'ACCOUNTNAME'"`               // 账户名称
	DeliveryGoodsId    int     `json:"deliverygoodsid"  xorm:"'DELIVERYGOODSID'"`       // 现货商品ID
	DeliveryGoodsCode  string  `json:"deliverygoodscode"  xorm:"'DELIVERYGOODSCODE'"`   // 现货品种代码
	DeliveryGoodsName  string  `json:"deliverygoodsname"  xorm:"'DELIVERYGOODSNAME'"`   // 现货品种名称
	SpotGoodsdesc      string  `json:"spotgoodsdesc"  xorm:"'SPOTGOODSDESC'"`           // 商品型号(商品规格)
	GoodsId            int     `json:"goodsid"  xorm:"'GOODSID'"`                       // 点价商品ID
	GOODSNAME          string  `json:"goodsname"  xorm:"'GOODSNAME'"`                   // 点价商品名称
	Goodscode          string  `json:"goodscode"  xorm:"'GOODSCODE'"`                   // 点价商品代码
	Pricemove          float64 `json:"pricemove"  xorm:"'PRICEMOVE'"`                   // 升贴水
	Qty                float64 `json:"qty"  xorm:"'QTY'"`                               // 合同量
	UnpricedQty        float64 `json:"unpricedqty"  xorm:"'UNPRICEDQTY'"`               // 未定价量
	PricedQty          float64 `json:"pricedqty"  xorm:"'PRICEDQTY'"`                   // 已定价量
	UnsureQty          float64 `json:"unsureqty"  xorm:"'UNSUREQTY'"`                   // 未确定量
	PayAmount          float64 `json:"payamount"  xorm:"'PAYAMOUNT'"`                   // 已收付额(收款或付款)
	UnpayAmount        float64 `json:"unpayamount"`                                     // 应收付款额(应支付或应收款)
	PrePayAmount       float64 `json:"prepayamount"`                                    // 预收付额
	InvoiceAmount      float64 `json:"invoiceamount"  xorm:"'INVOICEAMOUNT'"`           // 已开票额
	DaikaiAmount       float64 `json:"daikaiamount"  xorm:"'DAIKAIAMOUNT'"`             // 应收(开)票额
	PreInvoiceAmount   float64 `json:"preinvoiceamount"`                                // 预收(开)票额
	StartDate          string  `json:"startdate"  xorm:"'STARTDATE'"`                   // 点价开始日
	EndDate            string  `json:"enddate"  xorm:"'ENDDATE'"`                       // 点价结束日
	DeliveryStartDate  string  `json:"deliverystartdate"  xorm:"'DELIVERYSTARTDATE'"`   // 交割开始日
	DeliveryendDate    string  `json:"deliveryenddate"  xorm:"'DELIVERYENDDATE'"`       // 交割结束日
	Convertfactor      float64 `json:"convertfactor"  xorm:"'CONVERTFACTOR'"`           // 标仓系数
	EnumdicName        string  `json:"enumdicname"  xorm:"'ENUMDICNAME'"`               // 单位名称
	Contracctstatus    uint    `json:"contracctstatus"  xorm:"'CONTRACTSTATUS'"`        // 合同状态- 0:未提交 1:待审核 2:执行中 3:正常完结 4:审核拒绝 5:异常完结 6:已撤回
	PriceType          int     `json:"pricetype"  xorm:"'PRICETYPE'"`                   // 定价类型 - 1:一口价 2:点价 3:暂定价
	ProductType        int     `json:"producttype"  xorm:"'PRODUCTTYPE'"`               // 产品类型 - 1:标准仓单 2:等标 3:非标
	Contracttype       int32   `json:"contracttype"  xorm:"'CONTRACTTYPE'"`             // 合同类型 1-采购, -1-销售
	Pricedamount       float64 `json:"pricedamount"  xorm:"'PRICEDAMOUNT'"`             // 已定价额
	PricedAvg          float64 `json:"pricedavg"  xorm:"'PRICEDAVG'"`                   // 已点均价
	Margin             float64 `json:"margin"  xorm:"'MARGIN'"`                         // 保证金
	CONTRACTMARGIN     float64 `json:"contractmargin"  xorm:"'CONTRACTMARGIN'"`         // 合同保证金
	Remark             string  `json:"remark"  xorm:"'Remark'"`                         // 备注
	ATTACHMENT         string  `json:"attachment"  xorm:"'ATTACHMENT'"`                 // 附件
	ReckonRealQty      float64 `json:"reckonrealqty"  xorm:"'ReckonRealQty'"`           // 已确定量
	ReckonOtherAmount  float64 `json:"reckonotheramount"  xorm:"'ReckonOtherAmount'"`   // 其它费用
	ReckonAdjustAmount float64 `json:"reckonadjustamount"  xorm:"'ReckonAdjustAmount'"` // 调整金额
	Price              float64 `json:"price"  xorm:"'Price'"`                           // 价格
	LoanAmount         float64 `json:"loanamount"  xorm:"'LoanAmount'"`                 // 贷款总额=已定价额+调整金额
	Contractno         string  `json:"contractno"  xorm:"'Contractno'"`                 // 合同编号
	TotalAmount        float64 `json:"totalamount"  xorm:"'-'"`                         // 合计总额
	ReckonedAmount     float64 `json:"reckonedamount"  xorm:"'ReckonedAmount'"`         // 实际已收付额(已确定额,已收付总额-已退款总额)
	BUYUSERID          int64   `json:"-"  xorm:"'BUYUSERID'"`                           // 采购方ID
	SELLUSERID         int64   `json:"-"  xorm:"'SELLUSERID'"`                          // 销售方ID
	AUDITTIME          string  `json:"audittime"  xorm:"'AUDITTIME'"`                   // 审核时间
	CREATETIME         string  `json:"createtime"  xorm:"'CREATETIME'"`                 // 创建时间
	SPOTGOODSMODELID   int32   `json:"spotgoodsmodelid"  xorm:"'SPOTGOODSMODELID'"`     // 现货型号ID
	SPOTGOODSBRANDID   int32   `json:"spotgoodsbrandid"  xorm:"'SPOTGOODSBRANDID'"`     // 现货品牌ID
	BRANDNAME          string  `json:"brandname"  xorm:"'brandname'"`                   // 品牌名称
	MODELNAME          string  `json:"modelname"  xorm:"'modelname'"`                   // 型号名称
	WRSTANDARDID       int64   `json:"wrstandardid"  xorm:"'WRSTANDARDID'"`             // 现货商品ID
	WRSTANDARDNAME     string  `json:"wrstandardname"  xorm:"'WRSTANDARDNAME'"`         // 现货商品名称
	WRSTANDARDCODE     string  `json:"wrstandardcode"  xorm:"'WRSTANDARDCODE'"`         // 现货商品代码
}

func (r *ErmcpModel) calc() {
	// #94293 公式调整：合计总额不再加保证金
	r.TotalAmount = r.LoanAmount + r.ReckonOtherAmount
	r.UnpayAmount = r.TotalAmount - r.ReckonedAmount
	r.PrePayAmount = r.ReckonedAmount - r.TotalAmount
	r.PreInvoiceAmount = r.InvoiceAmount - r.TotalAmount
	if r.Contracttype == 1 {
		//采购合同, 取销售方名称
		r.AccountName = mtpcache.GetUserNameByUserId(r.SELLUSERID)
	} else {
		//销售合同, 取采购方名称
		r.AccountName = mtpcache.GetUserNameByUserId(r.BUYUSERID)
	}
}

// buildSql 组装查询的sql
func (r *ErmcpModel) buildSql(nContractType, nQueryType int32) string {
	str := "select t.contractno," +
		"       to_char(t.SpotContractId) SpotContractId," +
		"       t.deliverygoodsid," +
		"       t.spotgoodsdesc," +
		"       t.goodsid," +
		"       t.pricemove," +
		"       t.userid," +
		"       t.BUYUSERID," +
		"       t.SELLUSERID," +
		"       t.qty," +
		"       t.qty - t.pricedqty unpricedqty," +
		"       t.pricedqty," +
		"       (case" +
		"         when t.pricetype = 3 then" +
		"          t.qty - t.RECKONREALQTY" +
		"         else" +
		"          t.pricedqty - t.ReckonRealQty" +
		"       end) unsureqty," +
		"       t.reckonedamount PayAmount," +
		"       t.invoiceamount," +
		"       (t.pricedamount + t.RECKONADJUSTAMOUNT + t.RECKONOTHERAMOUNT -" +
		"       t.invoiceamount) as daikaiAmount," +
		"       to_char(t.startdate, 'yyyy-mm-dd hh24:mi:ss') startdate," +
		"       to_char(t.enddate, 'yyyy-mm-dd hh24:mi:ss') enddate," +
		"       to_char(t.deliverystartdate, 'yyyy-mm-dd hh24:mi:ss') deliverystartdate," +
		"       to_char(t.deliveryenddate, 'yyyy-mm-dd hh24:mi:ss') deliveryenddate," +
		"       t.convertfactor," +
		"       t.contractstatus," +
		"       t.pricetype," +
		"       t.producttype," +
		"       t.contracttype," +
		"       t.remark," +
		"       to_char(t.contractattachment) attachment," +
		"       t.margin," +
		"       t.contractmargin," +
		"       t.SPOTGOODSMODELID," +
		"       t.SPOTGOODSBRANDID," +
		"       u.accountname," +
		"       g.deliverygoodscode," +
		"       g.deliverygoodsname," +
		"       wr.wrstandardid," +
		"       wr.wrstandardname," +
		"       wr.wrstandardcode," +
		"       g2.goodscode," +
		"       g2.goodsname," +
		"       gb.brandname," +
		"       gm.modelname," +
		"       e.enumdicname," +
		"       t.contracttype," +
		"       t.pricedamount," +
		"       case when t.pricedqty > 0 then (t.pricedamount)/t.pricedqty else 0 end pricedAvg," +
		"       t.ReckonRealQty," +
		"       t.ReckonOtherAmount," +
		"       t.ReckonAdjustAmount," +
		"       t.Price," +
		"       t.ReckonedAmount," +
		"       to_char(t.audittime,'yyyy-mm-dd hh24:mi:ss') audittime," +
		"       to_char(t.createtime,'yyyy-mm-dd hh24:mi:ss') createtime," +
		"       t.pricedamount + t.ReckonAdjustAmount as LoanAmount" +
		"  from ermcp_spotcontract t" +
		"  left join useraccount u" +
		"    on t.userid = u.userid" +
		"  left join deliverygoods g" +
		"    on t.deliverygoodsid = g.deliverygoodsid" +
		"  left join goods g2" +
		"    on t.goodsid = g2.goodsid" +
		"  left join spotgoodsbrand gb on t.spotgoodsbrandid=gb.brandid" +
		"  left join spotgoodsmodel gm on t.spotgoodsmodelid=gm.modelid" +
		"  left join wrstandard wr" +
		"    on t.wrstandardid = wr.wrstandardid" +
		"  left join enumdicitem e" +
		"    on wr.unitid = e.enumitemname" +
		"   and e.enumdiccode = 'goodsunit'" +
		" where t.contractstatus in (%v)" +
		"   and t.contracttype in (%v) and t.userid=%v"

	if len(r.SpotContractId) > 0 {
		str += fmt.Sprintf(" and t.SpotContractId=%v", r.SpotContractId)
	}

	var orderBy string
	var status string
	if 1 == nQueryType {
		// 全部
		status = "2,3"
		orderBy = " order by t.audittime desc"
	} else if 2 == nQueryType {
		// 待点价
		status = "2"
		str = str + " and t.qty - t.pricedqty > 0 and t.pricetype !=1 "
		orderBy = " order by unpricedqty desc, t.audittime desc"
	} else {
		// 履约
		status = "2"
		orderBy = " order by t.audittime desc"
	}

	str += orderBy
	sqlId := fmt.Sprintf(str, status, nContractType, r.UserID)

	return sqlId
}

// GetData 从数据库中查询合同
func (r *ErmcpModel) GetData(contractType, nQueryType int32) ([]ErmcpModel, error) {
	sData := make([]ErmcpModel, 0)
	e := db.GetEngine()
	s := e.SQL(r.buildSql(contractType, nQueryType))
	if err := s.Find(&sData); err != nil {
		logger.GetLogger().Errorf("ermcp query fail:%v", err)
		return sData, err
	}
	for i := range sData {
		sData[i].calc()
	}
	return sData, nil
}

// ErmcpTradeConfigTMP 交易模板配置
type ErmcpTradeConfigTMP struct {
	TRADECONFIGTMPID   int64  `json:"tradeconfigtmpid"  xorm:"'TRADECONFIGTMPID'"`     // 交易配置模板ID(SEQ_TRADECONFIGTMP)
	TRADECONFIGTMPTYPE int32  `json:"tradeconfigtmptype"  xorm:"'TRADECONFIGTMPTYPE'"` // 交易配置模板类型 - 1:保证金 2:手续费 3:汇率
	TRADECONFIGTMPNAME string `json:"tradeconfigtmpname"  xorm:"'TRADECONFIGTMPNAME'"` // 交易配置模板名称
	REMARK             string `json:"remark"  xorm:"'REMARK'"`                         // 备注
	UPDATETIME         string `json:"updatetime"  xorm:"'UPDATETIME'"`                 // 更新时间
	MODIFIERID         int32  `json:"modifierid"  xorm:"'MODIFIERID'"`                 // 修改人
	AREAUSERID         int64  `json:"areauserid"  xorm:"'AREAUSERID'"`                 // 所属机构
	BASICCURRENCYID    int64  `json:"basiccurrencyid"  xorm:"'BASICCURRENCYID'"`       // 基本币种ID[汇率] - 目标币种
	MAINACCOUNTID      int64  `json:"mainaccountid"  xorm:"'MAINACCOUNTID'"`           // 主账户ID
}

func (r *ErmcpTradeConfigTMP) calc() {
}

func (r *ErmcpTradeConfigTMP) buildSql() string {
	var sqlId utils.SQLVal = "select t.areauserid," +
		"       t.tradeconfigtmpid," +
		"       t.tradeconfigtmptype," +
		"       t.tradeconfigtmpname," +
		"       t.basiccurrencyid," +
		"       t.mainaccountid," +
		"       t.modifierid," +
		"       t.remark," +
		"       to_char(t.updatetime, 'yyyy-mm-dd hh24:mi:ss') updatetime" +
		"  from TRADECONFIGTMP t" +
		"  where 1=1"
	if r.AREAUSERID > 0 {
		sqlId.And("t.AREAUSERID", r.AREAUSERID)
	}
	return sqlId.String()
}

// GetDataEx 获取交易配置模板
func (r *ErmcpTradeConfigTMP) GetDataEx() (interface{}, error) {
	sData := make([]ErmcpTradeConfigTMP, 0)
	err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
	for i := range sData {
		sData[i].calc()
	}
	return sData, err
}
