package daily_base_column

import (
	"github.com/beego/beego/v2/client/orm"
	"hongze/hongze_open_api/utils"
)

type TickerDataItem struct {
	Date           string  `json:"date"`
	Ticker         string  `json:"ticker"`
	BaseColumnName string  `json:"base_column_name"`
	TickerValue    float64 `json:"ticker_value"`
	LastValue      float64 `json:"last_value"`
	DdValue        float64 `json:"dd_value"`
	WwValue        float64 `json:"ww_value"`
	MmValue        float64 `json:"mm_value"`
}

// GetDataByBaseColumnTickers17 获取数据指标
func GetDataByBaseColumnTickers17(tickers []string) (list []*TickerDataItem, err error) {
	lenNum := len(tickers)
	if lenNum <= 0 {
		return
	}
	sql := `SELECT
 ifnull(max(doo.date), max(dd.date)) AS date,
 dbc.base_column_ticker AS ticker,
 dbc.base_column_name,
 ifnull(
  (
   SELECT

   VALUE

   FROM
    daily_data
   WHERE
    ticker = dbc.base_column_ticker
   AND date = ifnull(max(doo.date), max(dd.date))
  ),
  (
   SELECT

   VALUE

   FROM
    daily_original
   WHERE
    ticker = dbc.base_column_ticker
   AND date = ifnull(max(doo.date), max(dd.date))
  )
 ) AS ticker_value,
 ifnull(
  (
   SELECT

   VALUE

   FROM
    daily_data
   WHERE
    ticker = dbc.base_column_ticker
   AND date < ifnull(max(doo.date), max(dd.date))
   ORDER BY
    date DESC
   LIMIT 1
  ),
  (
   SELECT

   VALUE

   FROM
    daily_original
   WHERE
    ticker = dbc.base_column_ticker
   AND date < ifnull(max(doo.date), max(dd.date))
   ORDER BY
    date DESC
   LIMIT 1
  )
 ) AS last_value
FROM
 daily_base_column AS dbc
LEFT JOIN daily_data AS dd ON dbc.base_column_ticker = dd.ticker
LEFT JOIN daily_original AS doo ON dbc.base_column_ticker = doo.ticker
WHERE
 dbc.base_column_ticker IN (` + utils.GetOrmInReplace(lenNum) + `)
GROUP BY
 dbc.base_column_ticker`
	o := orm.NewOrm()
	_, err = o.Raw(sql, tickers).QueryRows(&list)
	return
}

// GetDataByBaseColumnTickers 获取数据指标
func GetDataByBaseColumnTickers(tickers []string) (list []*TickerDataItem, err error) {
	lenNum := len(tickers)
	if lenNum <= 0 {
		return
	}
	sql := `select ifnull(max(doo.date),max(dd.date)) as date,dbc.base_column_ticker as ticker,dbc.base_column_name ,
            ifnull((select value from daily_data where ticker = dbc.base_column_ticker and date <= ifnull(max(doo.date),max(dd.date)) and value is not null order by date desc limit 1),(select value from daily_original where ticker = dbc.base_column_ticker and date <= ifnull(max(doo.date),max(dd.date)) and value is not null order by date desc limit 1)) as ticker_value,
            ifnull((select value from daily_data where ticker = concat(dbc.base_column_ticker,'_dd') and date <= ifnull(max(doo.date),max(dd.date)) and value is not null order by date desc limit 1),(select value from daily_original where ticker = concat(dbc.base_column_ticker,'_dd') and date <= ifnull(max(doo.date),max(dd.date))and value is not null order by date desc limit 1)) as dd_value,
            ifnull((select value from daily_data where ticker = concat(dbc.base_column_ticker,'_ww') and date <= ifnull(max(doo.date),max(dd.date)) and value is not null order by date desc limit 1),(select value from daily_original where ticker = concat(dbc.base_column_ticker,'_ww') and date <= ifnull(max(doo.date),max(dd.date)) and value is not null order by date desc limit 1)) as ww_value,
            ifnull((select value from daily_data where ticker = concat(dbc.base_column_ticker,'_mm') and date <= ifnull(max(doo.date),max(dd.date)) and value is not null order by date desc limit 1),(select value from daily_original where ticker = concat(dbc.base_column_ticker,'_mm') and date <= ifnull(max(doo.date),max(dd.date)) and value is not null order by date desc limit 1)) as mm_value
       from
			 daily_base_column AS dbc
LEFT JOIN daily_data AS dd ON dbc.base_column_ticker = dd.ticker
LEFT JOIN daily_original AS doo ON dbc.base_column_ticker = doo.ticker
WHERE
 dbc.base_column_ticker IN (` + utils.GetOrmInReplace(lenNum) + `)
GROUP BY
 dbc.base_column_ticker`
	o := orm.NewOrm()
	_, err = o.Raw(sql, tickers).QueryRows(&list)
	return
}
