package models

// 企业风管期货商品相关

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

// GetErmcpGoodses 企业风管专用获取商品信息的方法
func GetErmcpGoodses(lastUpdateTime string) ([]Goods, error) {
	engine := db.GetEngine()

	goodses := make([]Goods, 0)
	session := engine.Table("GOODS").Where("GOODSSTATUS = 3")
	// if lastID != 0 {
	// 	session = session.And("GOODSID > ?", lastID)
	// }
	// session = session.Asc("GOODSID")
	if len(lastUpdateTime) != 0 {
		session = session.And(fmt.Sprintf("MODIFYTIME > to_date('%s','yyyy-MM-dd hh24:mi:ss')", lastUpdateTime))
	}
	if err := session.Find(&goodses); err != nil {
		return nil, err
	}

	return goodses, nil
}

// GetGoodsLastUpdateTime 获取商品表最后更新(Update)时间
func GetGoodsLastUpdateTime() (*time.Time, error) {
	engine := db.GetEngine()

	var lastUpdateTimes time.Time
	sql := `SELECT VERSIONS_STARTTIME LASTUPDATETIME
			FROM GOODS VERSIONS BETWEEN TIMESTAMP MINVALUE AND MAXVALUE WHERE
			VERSIONS_STARTTIME IS NOT NULL and VERSIONS_OPERATION = 'U' order by LASTUPDATETIME desc`
	has, err := engine.SQL(sql).Get(&lastUpdateTimes)
	if err != nil {
		return nil, err
	}
	if !has {
		return nil, nil
	}

	return &lastUpdateTimes, nil
}
