package edbmonitor import ( "eta/eta_api/utils" "fmt" "time" "github.com/beego/beego/v2/client/orm" ) type EdbMonitorClassify struct { ClassifyId int `orm:"column(classify_id);pk"` ClassifyName string `description:"分类名称"` Level int `description:"分类层级"` ParentId int `description:"父分类ID"` RootId int `description:"根分类ID"` Sort int `description:"排序"` CreateTime time.Time `description:"创建时间"` } func (e *EdbMonitorClassify) Insert() (id int64, err error) { o := orm.NewOrmUsingDB("data") var edbMonitorIds []int if e.ParentId != 0 { sql := `SELECT edb_monitor_id FROM edb_monitor_info WHERE edb_monitor_classify_id = ?` _, err = o.Raw(sql, e.ParentId).QueryRows(&edbMonitorIds) if err != nil { return } } tx, err := o.Begin() if err != nil { return } defer func() { if err != nil { tx.Rollback() } else { tx.Commit() } }() id, err = tx.Insert(e) if len(edbMonitorIds) > 0 { sql := `UPDATE edb_monitor_info SET edb_monitor_classify_id = ? WHERE edb_monitor_id IN (` + utils.GetOrmInReplace(len(edbMonitorIds)) + `)` _, err = tx.Raw(sql, id, edbMonitorIds).Exec() if err != nil { return } } return } func (e *EdbMonitorClassify) Update(cols []string, classifyId []int) (err error) { o := orm.NewOrmUsingDB("data") var edbMonitorIds []int if e.ParentId != 0 { sql := `SELECT edb_monitor_id FROM edb_monitor_info WHERE edb_monitor_classify_id = ?` _, err = o.Raw(sql, e.ParentId).QueryRows(&edbMonitorIds) if err != nil { return } } tx, err := o.Begin() if err != nil { return } defer func() { if err != nil { tx.Rollback() } else { tx.Commit() } }() _, err = tx.Update(e, cols...) if err != nil { return } if len(classifyId) > 0 { sql := `UPDATE edb_monitor_classify SET root_id = ? WHERE classify_id IN (` + utils.GetOrmInReplace(len(classifyId)) + `)` _, err = tx.Raw(sql, e.RootId, classifyId).Exec() if err != nil { return } } if len(edbMonitorIds) > 0 { sql := `UPDATE edb_monitor_info SET edb_monitor_classify_id = ? WHERE edb_monitor_id IN (` + utils.GetOrmInReplace(len(edbMonitorIds)) + `)` _, err = tx.Raw(sql, e.ClassifyId, edbMonitorIds).Exec() if err != nil { return } } return } func DeleteEdbMonitorClassifyById(id int) (err error) { o := orm.NewOrmUsingDB("data") _, err = o.Raw("DELETE FROM edb_monitor_classify WHERE classify_id =?", id).Exec() return } func DeleteEdbMonitorClassifyByIdList(ids []int) (err error) { if len(ids) == 0 { return } o := orm.NewOrmUsingDB("data") sql := "DELETE FROM edb_monitor_classify WHERE classify_id IN (" + utils.GetOrmInReplace(len(ids)) + ")" _, err = o.Raw(sql, ids).Exec() return } func GetEdbMonitorClassifyList() (items []*EdbMonitorClassify, err error) { o := orm.NewOrmUsingDB("data") sql := "SELECT * FROM edb_monitor_classify ORDER BY sort ASC" _, err = o.Raw(sql).QueryRows(&items) return } func GetEdbMonitorClassifyById(id int) (item *EdbMonitorClassify, err error) { o := orm.NewOrmUsingDB("data") sql := "SELECT * FROM edb_monitor_classify WHERE classify_id =?" err = o.Raw(sql, id).QueryRow(&item) return } func GetEdbMonitorClassifyMaxSortByParentId(parentId int) (sort int, err error) { o := orm.NewOrmUsingDB("data") sql := "SELECT MAX(sort) FROM edb_monitor_classify WHERE parent_id =?" err = o.Raw(sql, parentId).QueryRow(&sort) return } func GetChildEdbMonitorClassifyIdById(id int) (classifyId []int, err error) { o := orm.NewOrmUsingDB("data") sql := "SELECT classify_id FROM edb_monitor_classify WHERE parent_id =?" _, err = o.Raw(sql, id).QueryRows(&classifyId) return } func GetChildEdbMonitorClassifyById(id int) (items []*EdbMonitorClassify, err error) { o := orm.NewOrmUsingDB("data") sql := "SELECT * FROM edb_monitor_classify WHERE parent_id =?" _, err = o.Raw(sql, id).QueryRows(&items) return } func GetChildEdbMonitorClassifyByRootId(rootId int) (items []*EdbMonitorClassify, err error) { o := orm.NewOrmUsingDB("data") sql := "SELECT * FROM edb_monitor_classify WHERE root_id =?" _, err = o.Raw(sql, rootId).QueryRows(&items) return } func GetEdbMonitorClassifyCountByIdList(ids []int) (count int, err error) { if len(ids) == 0 { return } o := orm.NewOrmUsingDB("data") sql := `SELECT COUNT(*) FROM edb_monitor_classify WHERE classify_id IN (` + utils.GetOrmInReplace(len(ids)) + `)` err = o.Raw(sql, ids).QueryRow(&count) return } // GetEdbMonitorClassifyByParentIdAndName 根据父级ID和名称获取分类 func GetEdbMonitorClassifyByParentIdAndName(parentId int, classifyName string, classifyId int) (item *EdbMonitorClassify, err error) { o := orm.NewOrmUsingDB("data") sql := `SELECT * FROM edb_monitor_classify WHERE parent_id = ? AND classify_name = ? AND classify_id <> ? LIMIT 1` err = o.Raw(sql, parentId, classifyName, classifyId).QueryRow(&item) return } // UpdateEdbMonitorClassifySortByParentId 根据分类父类id更新排序 func UpdateEdbMonitorClassifySortByParentId(parentId, classifyId, nowSort int, updateSort string) (err error) { o := orm.NewOrmUsingDB("data") sql := ` update edb_monitor_classify set sort = ` + updateSort + ` WHERE parent_id=? and sort > ? ` if classifyId > 0 { sql += ` or ( classify_id > ` + fmt.Sprint(classifyId) + ` and sort= ` + fmt.Sprint(nowSort) + `)` } _, err = o.Raw(sql, parentId, nowSort).Exec() return } // GetFirstEdbMonitorClassifyByParentId 获取当前父级图表分类下的排序第一条的数据 func GetFirstEdbMonitorClassifyByParentId(parentId int) (item *EdbMonitorClassify, err error) { o := orm.NewOrmUsingDB("data") sql := ` SELECT * FROM edb_monitor_classify WHERE parent_id=? order by sort asc,classify_id asc limit 1` err = o.Raw(sql, parentId).QueryRow(&item) return }