package data_manage

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

// BaseFromNationalStatisticsData 国统局-指标
type BaseFromNationalStatisticsData struct {
	BaseFromNationalStatisticsDataId  int       `orm:"column(base_from_national_statistics_data_id);pk"`
	BaseFromNationalStatisticsIndexId int       `description:"指标ID"`
	IndexCode                         string    `description:"指标编码"`
	DataTime                          time.Time `description:"数据日期"`
	Value                             float64   `description:"数据值"`
	CreateTime                        time.Time `description:"创建时间"`
	ModifyTime                        time.Time `description:"更新时间"`
}

func (m *BaseFromNationalStatisticsData) TableName() string {
	return "base_from_national_statistics_data"
}

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

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

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

func (m *BaseFromNationalStatisticsData) Del() (err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `DELETE FROM base_from_national_statistics_data WHERE base_from_national_statistics_data_id = ? LIMIT 1`
	_, err = o.Raw(sql, m.BaseFromNationalStatisticsDataId).Exec()
	return
}

func (m *BaseFromNationalStatisticsData) GetItemById(id int) (err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM base_from_national_statistics_data WHERE base_from_national_statistics_data_id = ? LIMIT 1`
	err = o.Raw(sql, id).QueryRow(&m)
	return
}

func (m *BaseFromNationalStatisticsData) GetItemByCondition(condition string, pars []interface{}) (err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM base_from_national_statistics_data WHERE 1=1 `
	sql += condition
	sql += ` LIMIT 1`
	err = o.Raw(sql, pars).QueryRow(&m)
	return
}

func (m *BaseFromNationalStatisticsData) 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 *BaseFromNationalStatisticsData) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*BaseFromNationalStatisticsData, err error) {
	o := orm.NewOrmUsingDB("data")
	fields := strings.Join(fieldArr, ",")
	if len(fieldArr) == 0 {
		fields = `*`
	}
	order := `ORDER BY create_time DESC`
	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
}
