package data_manage

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

// CCFStockClassify CCF化纤-装置分类
type CCFStockClassify struct {
	CcfStockClassifyId int       `orm:"column(ccf_stock_classify_id);pk"`
	ClassifyName       string    `description:"分类名称"`
	CreateTime         time.Time `description:"创建时间"`
	ModifyTime         time.Time `description:"修改时间"`
}

func (m *CCFStockClassify) TableName() string {
	return "ccf_stock_classify"
}

type CCFStockClassifyCols struct {
	CcfStockClassifyId string
	ClassifyName       string
	CreateTime         string
	ModifyTime         string
}

func (m *CCFStockClassify) Cols() CCFStockClassifyCols {
	return CCFStockClassifyCols{
		CcfStockClassifyId: "ccf_stock_classify_id",
		ClassifyName:       "classify_name",
		CreateTime:         "create_time",
		ModifyTime:         "modify_time",
	}
}

func (m *CCFStockClassify) PrimaryId() string {
	return m.Cols().CcfStockClassifyId
}

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

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

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

func (m *CCFStockClassify) Del() (err error) {
	o := orm.NewOrmUsingDB("data")
	sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
	_, err = o.Raw(sql, m.CcfStockClassifyId).Exec()
	return
}

func (m *CCFStockClassify) MultiDel(menuIds []int) (err error) {
	if len(menuIds) == 0 {
		return
	}
	o := orm.NewOrmUsingDB("data")
	sql := fmt.Sprintf(`DELETE FROM %s WHERE %s IN (%s)`, m.TableName(), m.PrimaryId(), utils.GetOrmInReplace(len(menuIds)))
	_, err = o.Raw(sql, menuIds).Exec()
	return
}

func (m *CCFStockClassify) GetItemById(id int) (item *CCFStockClassify, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
	err = o.Raw(sql, id).QueryRow(&item)
	return
}

func (m *CCFStockClassify) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *CCFStockClassify, err error) {
	o := orm.NewOrmUsingDB("data")
	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 = o.Raw(sql, pars).QueryRow(&item)
	return
}

func (m *CCFStockClassify) 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 *CCFStockClassify) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*CCFStockClassify, err error) {
	o := orm.NewOrmUsingDB("data")
	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 = o.Raw(sql, pars).QueryRows(&items)
	return
}

func (m *CCFStockClassify) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*CCFStockClassify, err error) {
	o := orm.NewOrmUsingDB("data")
	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)
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	return
}

// CCFStockClassifyItem CCF化纤-装置分类
type CCFStockClassifyItem struct {
	ClassifyId   int    `description:"分类ID"`
	ClassifyName string `description:"分类名称"`
	CreateTime   string `description:"创建时间"`
	ModifyTime   string `description:"修改时间"`
}

func (m *CCFStockClassify) Format2Item() (item *CCFStockClassifyItem) {
	item = new(CCFStockClassifyItem)
	item.ClassifyId = m.CcfStockClassifyId
	item.ClassifyName = m.ClassifyName
	item.CreateTime = utils.TimeTransferString(utils.FormatDateTime, m.CreateTime)
	item.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, m.ModifyTime)
	return
}
