/**
* @Author: zou.yingbin
* @Create  : 2021/4/12 10:07
* @Modify  : 2021/4/12 10:07
 */

package models

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

// ErmcpSpotGoodsPrice 现货市价
type ErmcpSpotGoodsPrice struct {
	WRSTANDARDID     int64   `json:"wrstandardid"  xorm:"'WRSTANDARDID'"`         // 现货商品ID
	SPOTGOODSMODELID int32   `json:"spotgoodsmodelid"  xorm:"'SPOTGOODSMODELID'"` // 现货品类ID(通用则为0)
	SPOTGOODSBRANDID int32   `json:"spotgoodsbrandid"  xorm:"'SPOTGOODSBRANDID'"` // 现货品牌ID(通用则为0, 不为0则须先有品类ID)
	CURRENCYID       int64   `json:"currencyid"  xorm:"'CURRENCYID'"`             // 报价货币ID
	SPOTGOODSPRICE   float64 `json:"spotgoodsprice"  xorm:"'SPOTGOODSPRICE'"`     // 现货价格
	TRADEDATE        string  `json:"tradedate"  xorm:"'TRADEDATE'"`               // 交易日(yyyyMMdd)
	OPERATESRC       int32   `json:"operatesrc"  xorm:"'OPERATESRC'"`             // 最后操作来源 - 1:管理端 2:终端
	OPERATEID        int64   `json:"operateid"  xorm:"'OPERATEID'"`               // 最后操作人
	OPERATETIME      string  `json:"operatetime"  xorm:"'OPERATETIME'"`           // 最后操作时间
	ISVALID          int32   `json:"isvalid"  xorm:"'ISVALID'"`                   // 是否有效 - 0:无效 1:有效
	AREAUSERID       int64   `json:"areauserid"  xorm:"'AREAUSERID'"`             // 所属机构id
	WRSTANDARDCODE   string  `json:"wrstandardcode"  xorm:"'WRSTANDARDCODE'"`     // 现货商品代码
	WRSTANDARDNAME   string  `json:"wrstandardname"  xorm:"'WRSTANDARDNAME'"`     // 现货商品名称
	UNITID           int32   `json:"unitid"  xorm:"'UNITID'"`                     // 现货商品单位id
	BRANDNAME        string  `json:"brandname"  xorm:"'BRANDNAME'"`               // 品牌名称
	MODELNAME        string  `json:"modelname"  xorm:"'MODELNAME'"`               // 品类名称
	GMUNITID         int32   `json:"gmunitid"  xorm:"'GMUNITID'"`                 // 品类单位id

	OPERATORNAME  string // 操作人名称
	ENUMDICNAME   string `json:"enumdicname"`   // 现货商品单位名称
	GBENUMDICNAME string `json:"gbenumdicname"` // 品类单位名称
}

func (r *ErmcpSpotGoodsPrice) calc() {
	if r.OPERATESRC == 1 {
		r.OPERATORNAME = mtpcache.GetSystemmangerLoginCode(r.OPERATEID)
	} else {
		r.OPERATORNAME = mtpcache.GetLoginCodeByLoginId(r.OPERATEID)
	}
	r.ENUMDICNAME = mtpcache.GetEnumDicitemName(r.UNITID)
	r.GBENUMDICNAME = mtpcache.GetEnumDicitemName(r.GMUNITID)
}

func (r *ErmcpSpotGoodsPrice) buildSql() string {
	var sqlId utils.SQLVal = "SELECT t.WRSTANDARDID," +
		"       t.SPOTGOODSMODELID," +
		"       t.SPOTGOODSBRANDID," +
		"       t.CURRENCYID," +
		"       t.SPOTGOODSPRICE," +
		"       t.TRADEDATE," +
		"       t.OPERATESRC," +
		"       t.OPERATEID," +
		"       to_char(t.OPERATETIME, 'yyyy-mm-dd hh24:mi:ss') OPERATETIME," +
		"       t.ISVALID," +
		"       w.areauserid," +
		"       w.wrstandardcode," +
		"       w.wrstandardname," +
		"       w.unitid," +
		"       gb.brandname," +
		"       gm.modelname," +
		"       gm.unitid gmunitid" +
		"  FROM ERMCP_SPOTGOODSPRICE t" +
		"  left join wrstandard w" +
		"    on t.wrstandardid = w.wrstandardid" +
		"  left join spotgoodsbrand gb" +
		"    on t.spotgoodsbrandid = gb.brandid" +
		"  left join spotgoodsmodel gm" +
		"    on t.spotgoodsmodelid = gm.modelid" +
		" WHERE 1 = 1"
	sqlId.And("w.areauserid", r.AREAUSERID)
	return sqlId.String()
}

// GetDataEx 获取现货市价
func (r *ErmcpSpotGoodsPrice) GetDataEx() (interface{}, error) {
	sData := make([]ErmcpSpotGoodsPrice, 0)
	err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
	if err == nil {
		for i := range sData {
			sData[i].calc()
		}
	}
	return sData, err
}

// ErmcpSpotGoodsPriceLog 现货市价操作日志
type ErmcpSpotGoodsPriceLog struct {
	LOGID            int64   `json:"logid"  xorm:"'LOGID'"`                       // 日志id
	WRSTANDARDID     int64   `json:"wrstandardid"  xorm:"'WRSTANDARDID'"`         // 现货商品ID
	SPOTGOODSMODELID int32   `json:"spotgoodsmodelid"  xorm:"'SPOTGOODSMODELID'"` // 现货品类ID(通用则为0)
	SPOTGOODSBRANDID int32   `json:"spotgoodsbrandid"  xorm:"'SPOTGOODSBRANDID'"` // 现货品牌ID(通用则为0, 不为0则须先有品类ID)
	CURRENCYID       int64   `json:"currencyid"  xorm:"'CURRENCYID'"`             // 报价货币ID
	SPOTGOODSPRICE   float64 `json:"spotgoodsprice"  xorm:"'SPOTGOODSPRICE'"`     // 现货价格
	TRADEDATE        string  `json:"tradedate"  xorm:"'TRADEDATE'"`               // 交易日(yyyyMMdd)
	OPERATESRC       int32   `json:"operatesrc"  xorm:"'OPERATESRC'"`             // 最后操作来源 - 1:管理端 2:终端
	OPERATEID        int64   `json:"operateid"  xorm:"'OPERATEID'"`               // 最后操作人
	OPERATETIME      string  `json:"operatetime"  xorm:"'OPERATETIME'"`           // 最后操作时间
	AREAUSERID       int64   `json:"areauserid"  xorm:"'AREAUSERID'"`             // 所属机构id
	WRSTANDARDCODE   string  `json:"wrstandardcode"  xorm:"'WRSTANDARDCODE'"`     // 现货商品代码
	WRSTANDARDNAME   string  `json:"wrstandardname"  xorm:"'WRSTANDARDNAME'"`     // 现货商品名称
	UNITID           int32   `json:"unitid"  xorm:"'UNITID'"`                     // 现货商品单位id
	BRANDNAME        string  `json:"brandname"  xorm:"'BRANDNAME'"`               // 品牌名称
	MODELNAME        string  `json:"modelname"  xorm:"'MODELNAME'"`               // 品类名称
	GMUNITID         int32   `json:"gmunitid"  xorm:"'GMUNITID'"`                 // 品类单位id

	OPERATORNAME  string // 操作人名称
	ENUMDICNAME   string `json:"enumdicname"`   // 现货商品单位名称
	GBENUMDICNAME string `json:"gbenumdicname"` // 品类单位名称
}

func (r *ErmcpSpotGoodsPriceLog) calc() {
	if r.OPERATESRC == 1 {
		r.OPERATORNAME = mtpcache.GetSystemmangerLoginCode(r.OPERATEID)
	} else {
		r.OPERATORNAME = mtpcache.GetLoginCodeByLoginId(r.OPERATEID)
	}
	r.ENUMDICNAME = mtpcache.GetEnumDicitemName(r.UNITID)
	r.GBENUMDICNAME = mtpcache.GetEnumDicitemName(r.GMUNITID)
}

func (r *ErmcpSpotGoodsPriceLog) buildSql() string {
	var sqlId utils.SQLVal = "SELECT t.WRSTANDARDID," +
		"       t.SPOTGOODSMODELID," +
		"       t.SPOTGOODSBRANDID," +
		"       t.CURRENCYID," +
		"       t.SPOTGOODSPRICE," +
		"       t.TRADEDATE," +
		"       t.OPERATESRC," +
		"       t.OPERATEID," +
		"       to_char(t.OPERATETIME, 'yyyy-mm-dd hh24:mi:ss') OPERATETIME," +
		"       t.LOGID," +
		"       w.areauserid," +
		"       w.wrstandardcode," +
		"       w.wrstandardname," +
		"       w.unitid," +
		"       gb.brandname," +
		"       gm.modelname," +
		"       gm.unitid gmunitid" +
		"  FROM ERMCP_SPOTGOODSPRICELOG t" +
		"  left join wrstandard w" +
		"    on t.wrstandardid = w.wrstandardid" +
		"  left join spotgoodsbrand gb" +
		"    on t.spotgoodsbrandid = gb.brandid" +
		"  left join spotgoodsmodel gm" +
		"    on t.spotgoodsmodelid = gm.modelid" +
		" WHERE 1 = 1"
	sqlId.And("w.areauserid", r.AREAUSERID)
	sqlId.And("t.wrstandardid", r.WRSTANDARDID)
	return sqlId.String()
}

// GetDataEx 获取现货市价信息日志
func (r *ErmcpSpotGoodsPriceLog) GetDataEx() (interface{}, error) {
	sData := make([]ErmcpSpotGoodsPrice, 0)
	err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
	if err == nil {
		for i := range sData {
			sData[i].calc()
		}
	}
	return sData, err
}
