/**
* @Author: zou.yingbin
* @Create  : 2021/3/15 10:27
* @Modify  : 2021/3/15 10:27
 */

package models

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

// ErmcpGoodsBrand 品牌
type ErmcpGoodsBrand struct {
	BRANDID         int32  `json:"brandid"  xorm:"'BRANDID'"`                 // BrandID(SEQ_SPOTGOODSBRAND)
	BRANDNAME       string `json:"brandname"  xorm:"'BRANDNAME'"`             // 品牌名称
	WRSTANDARDID    int64  `json:"wrstandardid"  xorm:"'WRSTANDARDID'"`       // 现货商品ID
	DELIVERYGOODSID int32  `json:"deliverygoodsid"  xorm:"'DELIVERYGOODSID'"` // 现货品种ID
	AREAUSERID      int64  `json:"areauserid"  xorm:"'AREAUSERID'"`           // 所属机构
	ISVALID         int32  `json:"isvalid"  xorm:"'ISVALID'"`                 // 是否有效 - 0:无效 1:有效
	CREATORSRC      int32  `json:"creatorsrc"  xorm:"'CREATORSRC'"`           // 创建人来源 - 1:管理端 2:终端
	CREATORID       int64  `json:"creatorid"  xorm:"'CREATORID'"`             // 创建人
	CREATETIME      string `json:"createtime"  xorm:"'CREATETIME'"`           // 创建时间
	UPDATORSRC      int32  `json:"updatorsrc"  xorm:"'UPDATORSRC'"`           // 更新人来源 - 1:管理端 2:终端
	UPDATORID       int64  `json:"updatorid"  xorm:"'UPDATORID'"`             // 更新人
	UPDATETIME      string `json:"updatetime"  xorm:"'UPDATETIME'"`           // 更新时间
}

func (r *ErmcpGoodsBrand) buildSql() string {
	var sqlId utils.SQLVal = "SELECT t.BRANDID," +
		"       t.BRANDNAME," +
		"       t.WRSTANDARDID," +
		"       t.DELIVERYGOODSID," +
		"       t.AREAUSERID," +
		"       t.ISVALID," +
		"       t.CREATORSRC," +
		"       t.CREATORID," +
		"       to_char(t.CREATETIME, 'yyyy-mm-dd hh24:mi:ss') CREATETIME," +
		"       t.UPDATORSRC," +
		"       t.UPDATORID," +
		"       to_char(t.UPDATETIME, 'yyyy-mm-dd hh24:mi:ss') UPDATETIME" +
		"  FROM SPOTGOODSBRAND t" +
		" WHERE 1 = 1"
	sqlId.And("t.AREAUSERID", r.AREAUSERID)
	if r.WRSTANDARDID > 0 {
		sqlId.And("t.WRSTANDARDID", r.WRSTANDARDID)
	}
	return sqlId.String()
}

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

// ErmcpGoodsModel 商品型号
type ErmcpGoodsModel struct {
	MODELID         int32   `json:"modelid"  xorm:"'MODELID'"`                 // ModelID(SEQ_SPOTGOODSMODEL)
	MODELNAME       string  `json:"modelname"  xorm:"'MODELNAME'"`             // 型号名称
	WRSTANDARDID    int64   `json:"wrstandardid"  xorm:"'WRSTANDARDID'"`       // 现货商品ID
	DELIVERYGOODSID int32   `json:"deliverygoodsid"  xorm:"'DELIVERYGOODSID'"` // 现货品种ID
	AREAUSERID      int64   `json:"areauserid"  xorm:"'AREAUSERID'"`           // 所属机构
	ISVALID         int32   `json:"isvalid"  xorm:"'ISVALID'"`                 // 是否有效 - 0:无效 1:有效
	CREATORSRC      int32   `json:"creatorsrc"  xorm:"'CREATORSRC'"`           // 创建人来源 - 1:管理端 2:终端
	CREATORID       int64   `json:"creatorid"  xorm:"'CREATORID'"`             // 创建人
	CREATETIME      string  `json:"createtime"  xorm:"'CREATETIME'"`           // 创建时间
	UPDATORSRC      int32   `json:"updatorsrc"  xorm:"'UPDATORSRC'"`           // 更新人来源 - 1:管理端 2:终端
	UPDATORID       int64   `json:"updatorid"  xorm:"'UPDATORID'"`             // 更新人
	UPDATETIME      string  `json:"updatetime"  xorm:"'UPDATETIME'"`           // 更新时间
	CONVERTFACTOR   float64 `json:"convertfactor"  xorm:"'CONVERTFACTOR'"`     // 标仓系数
}

func (r *ErmcpGoodsModel) buildSql() string {
	var sqlId utils.SQLVal = "SELECT t.MODELID," +
		"       t.MODELNAME," +
		"       t.WRSTANDARDID," +
		"       t.DELIVERYGOODSID," +
		"       t.AREAUSERID," +
		"       t.ISVALID," +
		"       t.CREATORSRC," +
		"       t.CREATORID," +
		"       t.CONVERTFACTOR," +
		"       to_char(t.CREATETIME, 'yyyy-mm-dd hh24:mi:ss') CREATETIME," +
		"       t.UPDATORSRC," +
		"       t.UPDATORID," +
		"       to_char(t.UPDATETIME, 'yyyy-mm-dd hh24:mi:ss') UPDATETIME" +
		"  FROM SPOTGOODSMODEL t" +
		" WHERE 1 = 1"
	sqlId.And("t.AREAUSERID", r.AREAUSERID)
	if r.WRSTANDARDID > 0 {
		sqlId.And("t.WRSTANDARDID", r.WRSTANDARDID)
	}
	return sqlId.String()
}

// GetDataEx 商品型号
func (r *ErmcpGoodsModel) GetDataEx() (interface{}, error) {
	sData := make([]ErmcpGoodsModel, 0)
	err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
	return sData, err
}
