package data_manage

import (
	"eta/eta_mobile/utils"
	"github.com/beego/beego/v2/client/orm"
	"github.com/rdlucklib/rdluck_tools/paging"
	"time"
)

// BaseFromBusinessIndex
// @Description: 外部指标（商家系统）表
type BaseFromBusinessIndex struct {
	BaseFromBusinessIndexId int64     `orm:"column(base_from_business_index_id);pk"`
	IndexCode               string    `description:"指标编码"`
	IndexName               string    `description:"指标名称"`
	Unit                    string    `description:"单位"`
	Frequency               string    `description:"频度"`
	Source                  int       `description:"数据来源"`
	SourceName              string    `description:"数据来源名称"`
	StartDate               time.Time `description:"开始日期"`
	EndDate                 time.Time `description:"结束日期"`
	Remark                  string    `description:"备注字段"`
	BaseModifyTime          time.Time `description:"基础信息(名称，单位，频度)变更时间"`
	DataUpdateTime          time.Time `description:"最近一次数据发生变化的时间"`
	CreateTime              time.Time `description:"创建时间"`
	ModifyTime              time.Time `description:"修改时间"`
}

var BaseFromBusinessIndexCols = struct {
	BaseFromBusinessIndexId string
	IndexCode               string
	IndexName               string
	Unit                    string
	Frequency               string
	Source                  string
	SourceName              string
	StartDate               string
	EndDate                 string
	Remark                  string
	BaseModifyTime          string
	DataUpdateTime          string
	CreateTime              string
	ModifyTime              string
}{
	BaseFromBusinessIndexId: "base_from_business_index_id",
	IndexCode:               "index_code",
	IndexName:               "index_name",
	Unit:                    "unit",
	Frequency:               "frequency",
	Source:                  "source",
	SourceName:              "source_name",
	StartDate:               "start_date",
	EndDate:                 "end_date",
	Remark:                  "remark",
	BaseModifyTime:          "base_modify_time",
	DataUpdateTime:          "data_update_time",
	CreateTime:              "create_time",
	ModifyTime:              "modify_time",
}

// EdbBusinessSource
// @Description: 自有数据（商家）指标来源
type EdbBusinessSource struct {
	EdbBusinessSourceId int64     `orm:"column(edb_business_source_id);pk"`
	SourceName          string    `description:"来源名称"` // 来源名称
	CreateTime          time.Time `description:"创建时间"` // 创建时间
}

// GetEdbBusinessSourceItem
// @Description: 根据来源名称获取来源信息
// @author: Roc
// @receiver m
// @datetime 2024-04-25 18:09:03
// @param sourceName string
// @return item *EdbBusinessSource
// @return err error
func (m *EdbBusinessSource) GetEdbBusinessSourceItem(sourceName string) (item *EdbBusinessSource, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM edb_business_source WHERE source_name = ? `
	err = o.Raw(sql, sourceName).QueryRow(&item)
	return
}

// GetAllList
// @Description: 获取所有来源列表
// @author: Roc
// @receiver m
// @datetime 2024-05-06 09:21:42
// @return items []*EdbBusinessSource
// @return err error
func (m *EdbBusinessSource) GetAllList() (items []*EdbBusinessSource, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM edb_business_source order by  edb_business_source_id desc `
	_, err = o.Raw(sql).QueryRows(&items)

	return
}

// GetListCount
// @Description: 根据条件获取列表页的总数量
// @author: Roc
// @receiver m
// @datetime 2024-05-06 17:15:28
// @param condition string
// @param pars []interface{}
// @return count int
// @return err error
func (m *BaseFromBusinessIndex) GetListCount(condition string, pars []interface{}) (count int, err error) {
	sql := ` select count(1) as count FROM base_from_business_index as a WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	o := orm.NewOrmUsingDB("data")
	err = o.Raw(sql, pars).QueryRow(&count)

	return
}

// GetList
// @Description: 根据条件获取列表页的数据
// @author: Roc
// @receiver m
// @datetime 2024-05-06 17:15:42
// @param condition string
// @param pars []interface{}
// @param startSize int
// @param pageSize int
// @return items []*BaseFromBusinessIndex
// @return err error
func (m *BaseFromBusinessIndex) GetList(condition string, pars []interface{}, startSize, pageSize int) (items []*BaseFromBusinessIndex, err error) {
	sql := `select * FROM base_from_business_index AS a  WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	sql += ` order by a.base_from_business_index_id desc limit ?,? `
	o := orm.NewOrmUsingDB("data")
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)

	return
}

// BusinessIndexListResp
// @Description: 外部（商家）指标列表返回
type BusinessIndexListResp struct {
	List   []*BaseFromBusinessIndexItem
	Paging *paging.PagingItem `description:"分页数据"`
}

// BaseFromBusinessIndexItem
// @Description: 外部指标（商家系统）结构
type BaseFromBusinessIndexItem struct {
	BaseFromBusinessIndexId int64  `orm:"column(base_from_business_index_id);pk"`
	IndexCode               string `description:"指标编码"`
	IndexName               string `description:"指标名称"`
	Unit                    string `description:"单位"`
	Frequency               string `description:"频度"`
	Source                  int    `description:"数据来源"`
	SourceName              string `description:"数据来源名称"`
	StartDate               string `description:"开始日期"`
	EndDate                 string `description:"结束日期"`
	Remark                  string `description:"备注字段"`
	BaseModifyTime          string `description:"基础信息(名称，单位，频度)变更时间"`
	DataUpdateTime          string `description:"最近一次数据发生变化的时间"`
	CreateTime              string `description:"创建时间"`
	ModifyTime              string `description:"修改时间"`
	EdbInfoId               int    `description:"eta指标库的id"`
	EdbUniqueCode           string `description:"指标库唯一编码"`
	EdbClassifyId           int    `description:"指标库分类ID"`
	EdbExist                int    `description:"指标库是否已添加：0-否；1-是"`
}

func (m *BaseFromBusinessIndex) GetPageItemsByCondition(condition string, pars []interface{}, orderRule string, startSize, pageSize int) (items []*BaseFromBusinessIndexItem, err error) {
	sql := `select a.*,b.edb_info_id,b.unique_code as edb_unique_code,b.classify_id as edb_classify_id FROM base_from_business_index AS a LEFT JOIN edb_info b on a.index_code=b.edb_code AND b.source=? WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	if orderRule == `` {
		sql += ` order by a.base_from_business_index_id desc`
	} else {
		sql += ` order by ` + orderRule
	}
	sql += ` limit ?,? `
	o := orm.NewOrmUsingDB("data")
	_, err = o.Raw(sql, utils.DATA_SOURCE_BUSINESS, pars, startSize, pageSize).QueryRows(&items)

	return
}

// GeItemsByCondition
// @Description: 根据条件获取指定数量的指标
// @author: Roc
// @receiver m
// @datetime 2024-05-06 17:16:02
// @param condition string
// @param pars []interface{}
// @param limitSize int
// @return items []*BaseFromBusinessIndexItem
// @return err error
func (m *BaseFromBusinessIndex) GeItemsByCondition(condition string, pars []interface{}, limitSize int) (items []*BaseFromBusinessIndexItem, err error) {
	if pars == nil {
		pars = make([]interface{}, 0)
	}

	sql := `select a.*,b.edb_info_id,b.unique_code as edb_unique_code,b.classify_id as edb_classify_id FROM base_from_business_index AS a LEFT JOIN edb_info b on a.index_code=b.edb_code AND b.source=? WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	sql += ` order by a.base_from_business_index_id desc `

	if limitSize > 0 {
		sql += " limit ? "
		pars = append(pars, limitSize)
	}

	o := orm.NewOrmUsingDB("data")
	_, err = o.Raw(sql, utils.DATA_SOURCE_BUSINESS, pars).QueryRows(&items)

	return
}

// GetDetailByEdbCode
// @Description: 根据edbCode获取指标详情
// @author: Roc
// @receiver m
// @datetime 2024-05-07 11:00:07
// @param edbCode string
// @return item *BaseFromBusinessIndex
// @return err error
func (m *BaseFromBusinessIndex) GetDetailByEdbCode(edbCode string) (item *BaseFromBusinessIndex, err error) {
	sql := `select *  FROM base_from_business_index  WHERE index_code = ? `
	o := orm.NewOrmUsingDB("data")
	err = o.Raw(sql, edbCode).QueryRow(&item)

	return
}

// BusinessIndexDataListResp
// @Description: 外部（商家）指标数据列表返回
type BusinessIndexDataListResp struct {
	List   []*BaseFromBusinessIndexDataItem
	Paging *paging.PagingItem `description:"分页数据"`
}

// BaseFromBusinessIndexDataItem
// @Description: 外部指标（商家系统）数据结构
type BaseFromBusinessIndexDataItem struct {
	ID        string
	EdbDataId int `orm:"column(edb_data_id);pk"`
	IndexCode string
	DataTime  string
	Value     float64
}
