package data_manage

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

// BaseFromNationalStatisticsClassify 国统局-指标分类
type BaseFromNationalStatisticsClassify struct {
	BaseFromNationalStatisticsClassifyId int       `orm:"column(base_from_national_statistics_classify_id);pk"`
	ClassifyName                         string    `description:"分类名称"`
	Id                                   string    `description:"分类ID(字符串)"`
	Dbcode                               string    `description:"dbcode"`
	Wdcode                               string    `description:"wdcode"`
	Pid                                  string    `description:"父级分类ID"`
	IsParent                             int       `description:"是否存在子分类: 0-否; 1-是"`
	CreateTime                           time.Time `description:"创建时间"`
	ModifyTime                           time.Time `description:"更新时间"`
}

func (m *BaseFromNationalStatisticsClassify) TableName() string {
	return "base_from_national_statistics_classify"
}

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

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

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

func (m *BaseFromNationalStatisticsClassify) Del() (err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `DELETE FROM base_from_national_statistics_classify WHERE base_from_national_statistics_classify_id = ? LIMIT 1`
	_, err = o.Raw(sql, m.BaseFromNationalStatisticsClassifyId).Exec()
	return
}

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

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

func (m *BaseFromNationalStatisticsClassify) 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 *BaseFromNationalStatisticsClassify) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*BaseFromNationalStatisticsClassify, 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
}

type BaseFromNationalStatisticsClassifyItem struct {
	ClassifyId   int                                       `description:"分类ID"`
	ClassifyName string                                    `description:"分类名称"`
	ParentId     int                                       `description:"父级分类ID"`
	ParentIds    []int                                     `description:"父级分类ID集合-用于前端的定位"`
	Dbcode       string                                    `description:"数据库编码"`
	IsParent     int                                       `description:"是否存在子分类: 0-否; 1-是"`
	Children     []*BaseFromNationalStatisticsClassifyItem `description:"子分类"`
}
