package data_manage

import (
	"errors"
	"eta/eta_task/utils"
	"fmt"
	"github.com/beego/beego/v2/client/orm"
	"strconv"
	"time"
)

func GetEdbDataTableName(source int) (tableName string) {
	switch source {
	case utils.DATA_SOURCE_THS:
		tableName = "edb_data_ths"
	case utils.DATA_SOURCE_WIND:
		tableName = "edb_data_wind"
	case utils.DATA_SOURCE_PB, utils.DATA_SOURCE_PB_FINANCE:
		tableName = "edb_data_pb"
	case utils.DATA_SOURCE_CALCULATE:
		tableName = "edb_data_calculate"
	case utils.DATA_SOURCE_CALCULATE_LJZZY:
		tableName = "edb_data_calculate_ljzzy"
	case utils.DATA_SOURCE_CALCULATE_TBZ:
		tableName = "edb_data_calculate_tbz"
	case utils.DATA_SOURCE_CALCULATE_TCZ:
		tableName = "edb_data_calculate_tcz"
	case utils.DATA_SOURCE_CALCULATE_NSZYDPJJS:
		tableName = "edb_data_calculate_nszydpjjs"
	case utils.DATA_SOURCE_MANUAL:
		tableName = "edb_data_manual"
	case utils.DATA_SOURCE_LZ:
		tableName = "edb_data_lz"
	case utils.DATA_SOURCE_YS:
		tableName = "edb_data_ys"
	case utils.DATA_SOURCE_CALCULATE_HBZ:
		tableName = "edb_data_calculate_hbz"
	case utils.DATA_SOURCE_CALCULATE_HCZ:
		tableName = "edb_data_calculate_hcz"
	case utils.DATA_SOURCE_CALCULATE_BP:
		tableName = "edb_data_calculate_bp"
	case utils.DATA_SOURCE_GL:
		tableName = "edb_data_gl"
	case utils.DATA_SOURCE_ZZ:
		tableName = "edb_data_zz"
	case utils.DATA_SOURCE_DL:
		tableName = "edb_data_dl"
	case utils.DATA_SOURCE_SH:
		tableName = "edb_data_sh"
	case utils.DATA_SOURCE_CFFEX:
		tableName = "edb_data_cffex"
	case utils.DATA_SOURCE_SHFE:
		tableName = "edb_data_ine"
	case utils.DATA_SOURCE_GIE:
		tableName = "edb_data_gie"
	case utils.DATA_SOURCE_CALCULATE_ZJPJ:
		tableName = "edb_data_calculate_zjpj"
	case utils.DATA_SOURCE_CALCULATE_TIME_SHIFT:
		tableName = "edb_data_calculate_time_shift"
	case utils.DATA_SOURCE_CALCULATE_LJZTBPJ:
		tableName = "edb_data_calculate_ljztbpj"
	case utils.DATA_SOURCE_LT:
		tableName = "edb_data_lt"
	case utils.DATA_SOURCE_COAL:
		tableName = "edb_data_coal"
	case utils.DATA_SOURCE_GOOGLE_TRAVEL:
		tableName = "edb_data_google_travel"
	case utils.DATA_SOURCE_PREDICT_CALCULATE:
		tableName = "edb_data_predict_calculate"
	case utils.DATA_SOURCE_PREDICT_CALCULATE_TBZ:
		tableName = "edb_data_predict_calculate_tbz"
	case utils.DATA_SOURCE_PREDICT_CALCULATE_TCZ:
		tableName = "edb_data_predict_calculate_tcz"
	case utils.DATA_SOURCE_MYSTEEL_CHEMICAL:
		tableName = "edb_data_mysteel_chemical"
	case utils.DATA_SOURCE_CALCULATE_CJJX:
		tableName = "edb_data_calculate_cjjx"
	case utils.DATA_SOURCE_EIA_STEO:
		tableName = "edb_data_eia_steo"
	case utils.DATA_SOURCE_PREDICT_CALCULATE_LJZZY:
		tableName = "edb_data_predict_calculate_ljzzy"
	case utils.DATA_SOURCE_PREDICT_CALCULATE_TIME_SHIFT:
		tableName = "edb_data_predict_calculate_time_shift"
	case utils.DATA_SOURCE_PREDICT_CALCULATE_ZJPJ:
		tableName = "edb_data_predict_calculate_zjpj"
	case utils.DATA_SOURCE_PREDICT_CALCULATE_LJZTBPJ:
		tableName = "edb_data_predict_calculate_ljztbpj"
	case utils.DATA_SOURCE_PREDICT_CALCULATE_NHCC:
		tableName = "edb_data_predict_calculate_nhcc"
	case utils.DATA_SOURCE_PREDICT_CALCULATE_CJJX:
		tableName = "edb_data_predict_calculate_cjjx"
	case utils.DATA_SOURCE_PREDICT_CALCULATE_HBZ:
		tableName = "edb_data_predict_calculate_hbz"
	case utils.DATA_SOURCE_PREDICT_CALCULATE_HCZ:
		tableName = "edb_data_predict_calculate_hcz"
	case utils.DATA_SOURCE_PREDICT_CALCULATE_BP:
		tableName = "edb_data_predict_calculate_bp"
	default:
		tableName = ""
		errors.New("无效的渠道:" + strconv.Itoa(source))
		return
	}
	return
}

func GetEdbInfoCalculateTableName(source int) (tableName string) {
	switch source {
	case utils.DATA_SOURCE_CALCULATE:
		tableName = "edb_info_calculate"
	case utils.DATA_SOURCE_CALCULATE_LJZZY:
		tableName = "edb_info_calculate_ljzzy"
	case utils.DATA_SOURCE_CALCULATE_TBZ:
		tableName = "edb_info_calculate_tbz"
	case utils.DATA_SOURCE_CALCULATE_TCZ:
		tableName = "edb_info_calculate_tcz"
	case utils.DATA_SOURCE_CALCULATE_NSZYDPJJS:
		tableName = "edb_info_calculate_nszydpjjs"
	case utils.DATA_SOURCE_CALCULATE_HBZ:
		tableName = "edb_info_calculate_hbz"
	case utils.DATA_SOURCE_CALCULATE_HCZ:
		tableName = "edb_info_calculate_hcz"
	case utils.DATA_SOURCE_CALCULATE_BP:
		tableName = "edb_info_calculate_bp"
	default:
		tableName = ""
		errors.New("无效的渠道:" + strconv.Itoa(source))
		return
	}
	return
}

func GetAddSql(edbInfoId, edbCode, dataTime, timestampStr, value string) (addSql string) {
	nowStr := time.Now().Format(utils.FormatDateTime)
	addSql += "("
	addSql += edbInfoId + "," + "'" + edbCode + "'" + "," + "'" + dataTime + "'" + "," + value + "," + "'" + nowStr + "'" +
		"," + "'" + nowStr + "'" + "," + "1"
	addSql += "," + "'" + timestampStr + "'"
	addSql += "),"
	return
}

func GetEdbDataBaseByCodeAndDate(source int, edbCode string, startDate string) (count int, err error) {
	o := orm.NewOrm()
	tableName := GetEdbDataTableName(source)
	sql := ` SELECT COUNT(1) AS count FROM %s WHERE edb_code=? AND data_time=? `
	sql = fmt.Sprintf(sql, tableName)
	err = o.Raw(sql, edbCode, startDate).QueryRow(&count)
	return
}

type EdbDataBase struct {
	EdbDataId     int `orm:"column(edb_data_id);pk"`
	EdbInfoId     int
	EdbCode       string
	DataTime      string
	Value         string
	Status        int
	CreateTime    time.Time
	ModifyTime    time.Time
	DataTimestamp int64
}