package data_manage

import (
	"eta_gn/eta_api/global"
	"eta_gn/eta_api/utils"
	"fmt"
	"strings"
	"time"
)

// BaseFromEdbMapping 同花顺高频数据
type BaseFromEdbMapping struct {
	Id              int       `orm:"column(id);pk" gorm:"primaryKey" `
	BaseFromIndexId int       `description:"源指标ID"`
	BaseIndexCode   string    `description:"源指标编码"`
	EdbInfoId       int       `description:"指标ID"`
	EdbCode         string    `description:"指标编码"`
	Source          int       `description:"指标来源:1-同花顺..."`
	SubSource       int       `description:"子数据来源:0-经济数据库;1-日期序列;2-高频数据"`
	ConvertRule     string    `description:"转换规则"`
	CreateTime      time.Time `description:"创建时间"`
	ModifyTime      time.Time `description:"修改时间"`
}

func (m *BaseFromEdbMapping) TableName() string {
	return "base_from_edb_mapping"
}

type BaseFromEdbMappingCols struct {
	PrimaryId       string
	BaseFromIndexId string
	BaseIndexCode   string
	EdbInfoId       string
	EdbCode         string
	Source          string
	SubSource       string
	ConvertRule     string
	CreateTime      string
	ModifyTime      string
}

func (m *BaseFromEdbMapping) Cols() BaseFromEdbMappingCols {
	return BaseFromEdbMappingCols{
		PrimaryId:       "id",
		BaseFromIndexId: "base_from_index_id",
		BaseIndexCode:   "base_index_code",
		EdbInfoId:       "edb_info_id",
		EdbCode:         "edb_code",
		Source:          "source",
		SubSource:       "sub_source",
		ConvertRule:     "convert_rule",
		CreateTime:      "create_time",
		ModifyTime:      "modify_time",
	}
}

func (m *BaseFromEdbMapping) Create() (err error) {
	err = global.DmSQL["data"].Create(m).Error
	if err != nil {
		return
	}

	return
}

func (m *BaseFromEdbMapping) CreateMulti(items []*BaseFromEdbMapping) (err error) {
	if len(items) == 0 {
		return
	}
	err = global.DmSQL["data"].CreateInBatches(items, utils.MultiAddNum).Error

	return
}

func (m *BaseFromEdbMapping) Update(cols []string) (err error) {
	err = global.DmSQL["data"].Select(cols).Updates(m).Error

	return
}

func (m *BaseFromEdbMapping) Remove() (err error) {
	sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
	err = global.DmSQL["data"].Exec(sql, m.Id).Error

	return
}

func (m *BaseFromEdbMapping) MultiRemove(ids []int) (err error) {
	if len(ids) == 0 {
		return
	}
	sql := fmt.Sprintf(`DELETE FROM %s WHERE %s IN (%s)`, m.TableName(), m.Cols().PrimaryId, utils.GetOrmInReplace(len(ids)))
	err = global.DmSQL["data"].Exec(sql, ids).Error

	return
}

func (m *BaseFromEdbMapping) RemoveByCondition(condition string, pars []interface{}) (err error) {
	if condition == "" {
		return
	}
	sql := fmt.Sprintf(`DELETE FROM %s WHERE %s`, m.TableName(), condition)
	err = global.DmSQL["data"].Exec(sql, pars...).Error

	return
}

func (m *BaseFromEdbMapping) GetItemById(id int) (item *BaseFromEdbMapping, err error) {
	sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
	err = global.DmSQL["data"].Raw(sql, id).First(&item).Error

	return
}

func (m *BaseFromEdbMapping) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *BaseFromEdbMapping, err error) {
	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 = global.DmSQL["data"].Raw(sql, pars...).First(&item).Error

	return
}

func (m *BaseFromEdbMapping) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
	sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
	err = global.DmSQL["data"].Raw(sql, pars...).Scan(&count).Error

	return
}

func (m *BaseFromEdbMapping) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*BaseFromEdbMapping, err error) {
	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 = global.DmSQL["data"].Raw(sql, pars...).First(&items).Error

	return
}

func (m *BaseFromEdbMapping) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*BaseFromEdbMapping, err error) {
	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)
	pars = append(pars, startSize)
	pars = append(pars, pageSize)
	err = global.DmSQL["data"].Raw(sql, pars...).Scan(&items).Error

	return
}