package data_manage

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

// FactorEdbSeriesMapping 因子指标系列-指标关联表
type FactorEdbSeriesMapping struct {
	FactorEdbSeriesMappingId int       `orm:"column(factor_edb_series_mapping_id);pk"`
	FactorEdbSeriesId        int       `description:"因子指标系列ID"`
	EdbInfoId                int       `description:"指标ID"`
	EdbCode                  string    `description:"指标编码"`
	CreateTime               time.Time `description:"创建时间"`
	ModifyTime               time.Time `description:"修改时间"`
}

func (m *FactorEdbSeriesMapping) TableName() string {
	return "factor_edb_series_mapping"
}

type FactorEdbSeriesMappingCols struct {
	PrimaryId         string
	FactorEdbSeriesId string
	EdbInfoId         string
	EdbCode           string
	CreateTime        string
	ModifyTime        string
}

func (m *FactorEdbSeriesMapping) Cols() FactorEdbSeriesMappingCols {
	return FactorEdbSeriesMappingCols{
		PrimaryId:         "factor_edb_series_mapping_id",
		FactorEdbSeriesId: "factor_edb_series_id",
		EdbInfoId:         "edb_info_id",
		EdbCode:           "edb_code",
		CreateTime:        "create_time",
		ModifyTime:        "modify_time",
	}
}

func (m *FactorEdbSeriesMapping) Create() (err error) {
	o := orm.NewOrmUsingDB("data")
	id, err := o.Insert(m)
	if err != nil {
		return
	}
	m.FactorEdbSeriesMappingId = int(id)
	return
}

func (m *FactorEdbSeriesMapping) CreateMulti(items []*FactorEdbSeriesMapping) (err error) {
	if len(items) == 0 {
		return
	}
	o := orm.NewOrmUsingDB("data")
	_, err = o.InsertMulti(len(items), items)
	return
}

func (m *FactorEdbSeriesMapping) Update(cols []string) (err error) {
	o := orm.NewOrmUsingDB("data")
	_, err = o.Update(m, cols...)
	return
}

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

func (m *FactorEdbSeriesMapping) MultiRemove(ids []int) (err error) {
	if len(ids) == 0 {
		return
	}
	o := orm.NewOrmUsingDB("data")
	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 *FactorEdbSeriesMapping) RemoveByCondition(condition string, pars []interface{}) (err error) {
	if condition == "" {
		return
	}
	o := orm.NewOrmUsingDB("data")
	sql := fmt.Sprintf(`DELETE FROM %s WHERE %s`, m.TableName(), condition)
	_, err = o.Raw(sql, pars).Exec()
	return
}

func (m *FactorEdbSeriesMapping) GetItemById(id int) (item *FactorEdbSeriesMapping, err error) {
	o := orm.NewOrmUsingDB("data")
	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 *FactorEdbSeriesMapping) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *FactorEdbSeriesMapping, err error) {
	o := orm.NewOrmUsingDB("data")
	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 *FactorEdbSeriesMapping) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
	o := orm.NewOrmUsingDB("data")
	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 *FactorEdbSeriesMapping) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*FactorEdbSeriesMapping, err error) {
	o := orm.NewOrmUsingDB("data")
	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 *FactorEdbSeriesMapping) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*FactorEdbSeriesMapping, err error) {
	o := orm.NewOrmUsingDB("data")
	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
}

// FactorEdbSeriesMappingItem 因子指标系列-指标关联信息
type FactorEdbSeriesMappingItem struct {
	SeriesId  int    `description:"因子指标系列ID"`
	EdbInfoId int    `description:"指标ID"`
	EdbCode   string `description:"指标编码"`
	EdbName   string `description:"指标名称"`
	EdbNameEn string `description:"指标名称-英文"`
}

func (m *FactorEdbSeriesMapping) Format2Item() (item *FactorEdbSeriesMappingItem) {
	item = new(FactorEdbSeriesMappingItem)
	item.SeriesId = m.FactorEdbSeriesId
	item.EdbInfoId = m.EdbInfoId
	item.EdbCode = m.EdbCode
	return
}
