package data_manage import ( "eta/eta_api/utils" "fmt" "time" "github.com/beego/beego/v2/client/orm" ) // BaseFromClarksonsClassify 卓创红期原始数据分类表 type BaseFromClarksonsClassify struct { BaseFromClassifyId int `orm:"column(base_from_clarksons_classify_id);pk"` ClassifyName string `description:"分类名称"` ParentId int `description:"父级id"` Level int `description:"层级"` Sort int `description:"排序字段"` ModifyTime time.Time `description:"修改时间"` CreateTime time.Time `description:"创建时间"` } type BaseFromClarksonsClassifyItem struct { BaseFromClassifyId int `orm:"column(base_from_clarksons_classify_id);pk"` ClassifyName string `description:"分类名称"` ParentId int `description:"父级id"` Level int `description:"层级"` Sort int `description:"排序字段"` UniqueCode string `description:"唯一code"` ModifyTime time.Time `description:"修改时间"` CreateTime time.Time `description:"创建时间"` ClassifyNameEn string `description:"英文分类名称"` Children []*BaseFromClarksonsClassifyItem `description:"子分类"` } type BaseFromClarksonsClassifyMaxSort struct { BaseFromClassifyId int `description:"分类id"` MaxSort int `description:"最大排序"` } func (t *BaseFromClarksonsClassify) Add() (insertId int64, err error) { o := orm.NewOrmUsingDB("data") insertId, err = o.Insert(t) return } func (t *BaseFromClarksonsClassify) Update(cols []string) (err error) { o := orm.NewOrmUsingDB("data") _, err = o.Update(t, cols...) return } func BatchAddClarksonsClassify(items []*BaseFromClarksonsClassify) (err error) { o := orm.NewOrmUsingDB("data") _, err = o.InsertMulti(len(items), items) return } // 获取所有分类 func GetClarksonsClassifyAll() (items []*BaseFromClarksonsClassifyItem, err error) { o := orm.NewOrmUsingDB("data") sql := ` SELECT * FROM base_from_clarksons_classify ORDER BY sort ASC, base_from_clarksons_classify_id ASC` _, err = o.Raw(sql).QueryRows(&items) return } // GetChildClarksonsClassifyListById 获取子分类列表 func GetChildClarksonsClassifyListById(classifyId int) (items []*BaseFromClarksonsClassifyItem, err error) { o := orm.NewOrmUsingDB("data") sql := ` SELECT * FROM base_from_clarksons_classify WHERE parent_id=? ` _, err = o.Raw(sql, classifyId).QueryRows(&items) return } // GetChildClarksonsClassifyIdsById 获取子分类的id集合 func GetChildClarksonsClassifyIdsById(classifyId int) (items []int, err error) { o := orm.NewOrmUsingDB("data") sql := ` SELECT base_from_clarksons_classify_id FROM base_from_clarksons_classify WHERE parent_id=? ` _, err = o.Raw(sql, classifyId).QueryRows(&items) return } // GetChildClarksonsClassifyMaxSortById 获取子分类最大排序 func GetChildClarksonsClassifyMaxSortById(classifyId int) (sort int, err error) { o := orm.NewOrmUsingDB("data") sql := ` SELECT MAX(sort) AS sort FROM base_from_clarksons_classify WHERE parent_id=? ` err = o.Raw(sql, classifyId).QueryRow(&sort) return } // GetClarksonsClassifyCountById 获取分类数量 func GetClarksonsClassifyCountById(classifyId int) (count int, err error) { o := orm.NewOrmUsingDB("data") sql := ` SELECT COUNT(*) AS count FROM base_from_clarksons_classify WHERE base_from_clarksons_classify_id=? ` err = o.Raw(sql, classifyId).QueryRow(&count) return } // GetClarksonsClassifyById 通过分类id获取分类 func GetClarksonsClassifyById(classifyId int) (item *BaseFromClarksonsClassify, err error) { o := orm.NewOrmUsingDB("data") sql := ` SELECT * FROM base_from_clarksons_classify WHERE base_from_clarksons_classify_id=? ` err = o.Raw(sql, classifyId).QueryRow(&item) return } // GetClarksonsChildClassifyById 通过分类id获取子分类 func GetClarksonsChildClassifyIdsById(classifyId int) (items []int, err error) { o := orm.NewOrmUsingDB("data") sql := ` SELECT base_from_clarksons_classify_id FROM base_from_clarksons_classify WHERE parent_id=? ` _, err = o.Raw(sql, classifyId).QueryRows(&items) return } // GetClarksonsClassifyListByIds 通过分类id获取分类列表 func GetClarksonsClassifyListByIds(classifyIds []int) (items []*BaseFromClarksonsClassify, err error) { if len(classifyIds) == 0 { return } o := orm.NewOrmUsingDB("data") sql := ` SELECT * FROM base_from_clarksons_classify WHERE base_from_clarksons_classify_id IN (` + utils.GetOrmInReplace(len(classifyIds)) + `)` _, err = o.Raw(sql, classifyIds).QueryRows(&items) return } // GetClarksonsClassifyCountByName 通过分类名称获取分类 func GetClarksonsClassifyCountByName(classifyName string) (count int, err error) { o := orm.NewOrmUsingDB("data") sql := ` SELECT COUNT(*) AS count FROM base_from_clarksons_classify WHERE 1=1` sql += ` AND classify_name=? ` err = o.Raw(sql, classifyName).QueryRow(&count) return } func GetBaseFromClarksonsClassifyEnCount(classifyNameEn string, parentId int) (count int, err error) { o := orm.NewOrmUsingDB("data") sql := `SELECT COUNT(1) AS count FROM base_from_clarksons_classify WHERE classify_name_en=? AND parent_id=? ` err = o.Raw(sql, classifyNameEn, parentId).QueryRow(&count) return } func GetBaseFromClarksonsClassifyCount(classifyName string, parentId int) (count int, err error) { o := orm.NewOrmUsingDB("data") sql := `SELECT COUNT(1) AS count FROM base_from_clarksons_classify WHERE classify_name=? AND parent_id=? ` err = o.Raw(sql, classifyName, parentId).QueryRow(&count) return } func DeleteClarksonsClassifyById(classifyId int) (err error) { o := orm.NewOrmUsingDB("data") sql := ` DELETE FROM base_from_clarksons_classify WHERE base_from_clarksons_classify_id=? ` _, err = o.Raw(sql, classifyId).Exec() return } // BatchDeleteClarksonsClassifyById 批量删除分类 func BatchDeleteClarksonsClassifyById(classifyId []int) (err error) { if len(classifyId) == 0 { return } o := orm.NewOrmUsingDB("data") to, err := o.Begin() if err != nil { return } defer func() { if err != nil { _ = to.Rollback() } else { _ = to.Commit() } }() sql := ` DELETE FROM base_from_clarksons_classify WHERE base_from_clarksons_classify_id IN (` + utils.GetOrmInReplace(len(classifyId)) + `) ` _, err = o.Raw(sql, classifyId).Exec() return } // DeleteClarksonsClassifyByClassifyId 根据分类id删除对应的指标分类 func DeleteClarksonsClassifyByClassifyId(classifyIdList []int) (err error) { o := orm.NewOrmUsingDB("data") num := len(classifyIdList) if num <= 0 { return } //删除分类 sql := `DELETE FROM base_from_clarksons_classify WHERE base_from_clarksons_classify_id IN (` + utils.GetOrmInReplace(num) + `) ` _, err = o.Raw(sql, classifyIdList).Exec() return } // GetClarksonsIndexClassifyMinSort 获取最小不等于0的排序 func GetClarksonsIndexClassifyMinSort(parentId int) (sort int, err error) { o := orm.NewOrmUsingDB("data") sql := `SELECT MIN(sort) FROM base_from_clarksons_classify WHERE parent_id=? AND sort <> 0 ` err = o.Raw(sql, parentId).QueryRow(&sort) return } // MoveUpClarksonsIndexClassifyBySort 往上移动 func MoveUpClarksonsIndexClassifyBySort(parentId, nextSort, currentSort int) (err error) { o := orm.NewOrmUsingDB("data") sql := `update base_from_clarksons_classify set sort = sort + 1 where parent_id=? and sort >= ? and sort< ?` _, err = o.Raw(sql, parentId, nextSort, currentSort).Exec() return } // MoveDownClarksonsIndexClassifyBySort 往下移动 func MoveDownClarksonsIndexClassifyBySort(parentId, prevSort, currentSort int) (err error) { o := orm.NewOrmUsingDB("data") sql := `update base_from_clarksons_classify set sort = sort - 1 where parent_id=? and sort <= ? and sort> ? ` _, err = o.Raw(sql, parentId, prevSort, currentSort).Exec() return } // UpdateClarksonsClassifySortByParentId 根据父类id更新排序 func UpdateClarksonsClassifySortByParentId(parentId, classifyId, nowSort int, updateSort string) (err error) { o := orm.NewOrmUsingDB("data") sql := ` update base_from_clarksons_classify set sort = ` + updateSort + ` WHERE parent_id=? and sort > ? ` if classifyId > 0 { sql += ` or ( base_from_clarksons_classify_id > ` + fmt.Sprint(classifyId) + ` and sort= ` + fmt.Sprint(nowSort) + `)` } _, err = o.Raw(sql, parentId, nowSort).Exec() return } // GetFirstClarksonsClassifyByParentId 获取当前父级分类下的排序第一条的数据 func GetFirstClarksonsClassifyByParentId(parentId int) (item *ChartClassify, err error) { o := orm.NewOrmUsingDB("data") sql := ` SELECT * FROM base_from_clarksons_classify WHERE parent_id=? order by sort asc,base_from_clarksons_classify_id asc limit 1` err = o.Raw(sql, parentId).QueryRow(&item) return }