package models

import (
	"eta/eta_index_lib/utils"
	"fmt"
	"github.com/beego/beego/v2/client/orm"
	"strings"
	"time"
)

// EdbDataThsHf 同花顺高频指标数据
type EdbDataThsHf struct {
	EdbDataId     int       `orm:"column(edb_data_id);pk"`
	EdbInfoId     int       `description:"指标ID"`
	EdbCode       string    `description:"指标编码"`
	DataTime      time.Time `description:"数据日期"`
	Value         float64   `description:"数据值"`
	CreateTime    time.Time `description:"创建时间"`
	ModifyTime    time.Time `description:"修改时间"`
	DataTimestamp int64     `description:"数据日期时间戳"`
}

func (m *EdbDataThsHf) TableName() string {
	return "edb_data_ths_hf"
}

type EdbDataThsHfCols struct {
	PrimaryId     string
	EdbInfoId     string
	EdbCode       string
	DataTime      string
	Value         string
	CreateTime    string
	ModifyTime    string
	DataTimestamp string
}

func (m *EdbDataThsHf) Cols() EdbDataThsHfCols {
	return EdbDataThsHfCols{
		PrimaryId:     "edb_data_id",
		EdbInfoId:     "edb_info_id",
		EdbCode:       "edb_code",
		DataTime:      "data_time",
		Value:         "value",
		CreateTime:    "create_time",
		ModifyTime:    "modify_time",
		DataTimestamp: "data_timestamp",
	}
}

func (m *EdbDataThsHf) Create() (err error) {
	o := orm.NewOrm()
	id, err := o.Insert(m)
	if err != nil {
		return
	}
	m.EdbDataId = int(id)
	return
}

func (m *EdbDataThsHf) CreateMulti(items []*EdbDataThsHf) (err error) {
	if len(items) == 0 {
		return
	}
	o := orm.NewOrm()
	_, err = o.InsertMulti(200, items)
	return
}

func (m *EdbDataThsHf) Update(cols []string) (err error) {
	o := orm.NewOrm()
	_, err = o.Update(m, cols...)
	return
}

func (m *EdbDataThsHf) Remove() (err error) {
	o := orm.NewOrm()
	sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
	_, err = o.Raw(sql, m.EdbDataId).Exec()
	return
}

func (m *EdbDataThsHf) MultiRemove(ids []int) (err error) {
	if len(ids) == 0 {
		return
	}
	o := orm.NewOrm()
	sql := fmt.Sprintf(`DELETE FROM %s WHERE %s IN (%s)`, m.TableName(), m.Cols().PrimaryId, utils.GetOrmInReplace(len(ids)))
	_, err = o.Raw(sql, ids).Exec()
	return
}

func (m *EdbDataThsHf) RemoveByCondition(condition string, pars []interface{}) (err error) {
	if condition == "" {
		return
	}
	o := orm.NewOrm()
	sql := fmt.Sprintf(`DELETE FROM %s WHERE %s`, m.TableName(), condition)
	_, err = o.Raw(sql, pars).Exec()
	return
}

func (m *EdbDataThsHf) GetItemById(id int) (item *EdbDataThsHf, err error) {
	o := orm.NewOrm()
	sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
	err = o.Raw(sql, id).QueryRow(&item)
	return
}

func (m *EdbDataThsHf) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *EdbDataThsHf, err error) {
	o := orm.NewOrm()
	order := ``
	if orderRule != "" {
		order = ` ORDER BY ` + orderRule
	}
	sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s %s LIMIT 1`, m.TableName(), condition, order)
	err = o.Raw(sql, pars).QueryRow(&item)
	return
}

func (m *EdbDataThsHf) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
	o := orm.NewOrm()
	sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
	err = o.Raw(sql, pars).QueryRow(&count)
	return
}

func (m *EdbDataThsHf) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*EdbDataThsHf, err error) {
	o := orm.NewOrm()
	fields := strings.Join(fieldArr, ",")
	if len(fieldArr) == 0 {
		fields = `*`
	}
	order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
	if orderRule != "" {
		order = ` ORDER BY ` + orderRule
	}
	sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}

func (m *EdbDataThsHf) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*EdbDataThsHf, err error) {
	o := orm.NewOrm()
	fields := strings.Join(fieldArr, ",")
	if len(fieldArr) == 0 {
		fields = `*`
	}
	order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
	if orderRule != "" {
		order = ` ORDER BY ` + orderRule
	}
	sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	return
}

// MultiUpdateValue 批量更新数据值
func (m *EdbDataThsHf) MultiUpdateValue(items []*EdbDataThsHf) (err error) {
	if len(items) == 0 {
		return
	}

	o := orm.NewOrm()
	sql := fmt.Sprintf("UPDATE %s SET %s = ?, %s = NOW() WHERE %s = ?", m.TableName(), m.Cols().Value, m.Cols().ModifyTime, m.Cols().PrimaryId)
	p, e := o.Raw(sql).Prepare()
	if e != nil {
		err = fmt.Errorf("update sql prepare err: %v", e)
		return
	}
	defer func() {
		_ = p.Close()
	}()
	for _, v := range items {
		_, err = p.Exec(v.Value, v.EdbDataId)
		if err != nil {
			return
		}
	}
	return
}
