123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 |
- package data_manage
- import (
- "eta/eta_api/utils"
- "fmt"
- "github.com/beego/beego/v2/client/orm"
- "strings"
- "time"
- )
- // BaseFromThsHfClassify 同花顺高频数据-分类
- type BaseFromThsHfClassify struct {
- BaseFromThsHfClassifyId int `orm:"column(base_from_ths_hf_classify_id);pk"`
- ClassifyName string `description:"分类名称"`
- ClassifyNameEn string `description:"英文分类名称"`
- ParentId int `description:"父级ID"`
- SysUserId int `description:"创建人ID"`
- SysUserRealName string `description:"创建人姓名"`
- Level int `description:"层级"`
- Sort int `description:"排序"`
- RootId int `description:"顶级分类ID"`
- LevelPath string `description:"层级路径"`
- UniqueCode string `description:"唯一编码"`
- CreateTime time.Time `description:"创建时间"`
- ModifyTime time.Time `description:"修改时间"`
- }
- func (m *BaseFromThsHfClassify) TableName() string {
- return "base_from_ths_hf_classify"
- }
- type BaseFromThsHfClassifyCols struct {
- PrimaryId string
- ClassifyName string
- ClassifyNameEn string
- ParentId string
- SysUserId string
- SysUserRealName string
- Level string
- Sort string
- RootId string
- LevelPath string
- UniqueCode string
- CreateTime string
- ModifyTime string
- }
- func (m *BaseFromThsHfClassify) Cols() BaseFromThsHfClassifyCols {
- return BaseFromThsHfClassifyCols{
- PrimaryId: "base_from_ths_hf_classify_id",
- ClassifyName: "classify_name",
- ClassifyNameEn: "classify_name_en",
- ParentId: "parent_id",
- SysUserId: "sys_user_id",
- SysUserRealName: "sys_user_real_name",
- Level: "level",
- Sort: "sort",
- RootId: "root_id",
- LevelPath: "level_path",
- UniqueCode: "unique_code",
- CreateTime: "create_time",
- ModifyTime: "modify_time",
- }
- }
- func (m *BaseFromThsHfClassify) Create() (err error) {
- o := orm.NewOrmUsingDB("data")
- id, err := o.Insert(m)
- if err != nil {
- return
- }
- m.BaseFromThsHfClassifyId = int(id)
- return
- }
- func (m *BaseFromThsHfClassify) CreateMulti(items []*BaseFromThsHfClassify) (err error) {
- if len(items) == 0 {
- return
- }
- o := orm.NewOrmUsingDB("data")
- _, err = o.InsertMulti(len(items), items)
- return
- }
- func (m *BaseFromThsHfClassify) Update(cols []string) (err error) {
- o := orm.NewOrmUsingDB("data")
- _, err = o.Update(m, cols...)
- return
- }
- func (m *BaseFromThsHfClassify) Remove() (err error) {
- o := orm.NewOrmUsingDB("data")
- sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
- _, err = o.Raw(sql, m.BaseFromThsHfClassifyId).Exec()
- return
- }
- func (m *BaseFromThsHfClassify) MultiRemove(ids []int) (err error) {
- if len(ids) == 0 {
- return
- }
- o := orm.NewOrmUsingDB("data")
- sql := fmt.Sprintf(`DELETE FROM %s WHERE %s IN (%s)`, m.TableName(), m.Cols().PrimaryId, utils.GetOrmInReplace(len(ids)))
- _, err = o.Raw(sql, ids).Exec()
- return
- }
- func (m *BaseFromThsHfClassify) RemoveByCondition(condition string, pars []interface{}) (err error) {
- if condition == "" {
- return
- }
- o := orm.NewOrmUsingDB("data")
- sql := fmt.Sprintf(`DELETE FROM %s WHERE %s`, m.TableName(), condition)
- _, err = o.Raw(sql, pars).Exec()
- return
- }
- func (m *BaseFromThsHfClassify) GetItemById(id int) (item *BaseFromThsHfClassify, err error) {
- o := orm.NewOrmUsingDB("data")
- sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
- err = o.Raw(sql, id).QueryRow(&item)
- return
- }
- func (m *BaseFromThsHfClassify) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *BaseFromThsHfClassify, 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 *BaseFromThsHfClassify) 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 *BaseFromThsHfClassify) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*BaseFromThsHfClassify, 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 *BaseFromThsHfClassify) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*BaseFromThsHfClassify, 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
- }
- // BaseFromThsHfClassifyItem 同花顺高频数据信息
- type BaseFromThsHfClassifyItem struct {
- ClassifyId int `description:"分类ID"`
- ClassifyName string `description:"分类名称"`
- ClassifyNameEn string `description:"英文分类名称"`
- ParentId int `description:"父级ID"`
- Level int `description:"层级"`
- Sort int `description:"排序"`
- LevelPath string `description:"层级路径"`
- UniqueCode string `description:"唯一编码"`
- Children []*BaseFromThsHfClassifyItem `description:"子分类"`
- }
- func (m *BaseFromThsHfClassify) Format2Item() (item *BaseFromThsHfClassifyItem) {
- item = new(BaseFromThsHfClassifyItem)
- item.ClassifyId = m.BaseFromThsHfClassifyId
- item.ClassifyName = m.ClassifyName
- item.ClassifyNameEn = m.ClassifyNameEn
- item.ParentId = m.ParentId
- item.Level = m.Level
- item.Sort = m.Sort
- item.LevelPath = m.LevelPath
- item.UniqueCode = m.UniqueCode
- item.Children = make([]*BaseFromThsHfClassifyItem, 0)
- return
- }
- type ThsHfClassifyAddReq struct {
- ClassifyName string `description:"分类名称"`
- ParentId int `description:"父级ID, 第一级传0"`
- Level int `description:"层级, 第一级传0, 其余传上一级的层级"`
- }
- type ThsHfClassifyEditReq struct {
- ClassifyId int `description:"分类ID"`
- ClassifyName string `description:"分类名称"`
- }
- func (m *BaseFromThsHfClassify) GetSortMax(parentId int) (sort int, err error) {
- o := orm.NewOrmUsingDB("data")
- sql := fmt.Sprintf(`SELECT Max(%s) FROM %s WHERE %s = ?`, m.Cols().Sort, m.TableName(), m.Cols().ParentId)
- err = o.Raw(sql, parentId).QueryRow(&sort)
- return
- }
- type ThsHfClassifyRemoveReq struct {
- ClassifyId int `description:"分类ID"`
- }
- type BaseFromThsHfClassifyListItem struct {
- ItemType int `description:"类型: 0-分类; 1-指标"`
- ClassifyId int `description:"分类ID"`
- ClassifyName string `description:"分类名称"`
- ClassifyNameEn string `description:"英文分类名称"`
- IndexId int `description:"指标ID"`
- IndexCode string `description:"指标编码"`
- IndexName string `description:"指标名称"`
- ParentId int `description:"父级ID"`
- Level int `description:"层级"`
- Sort int `description:"排序"`
- UniqueCode string `description:"唯一编码, 指标的话用indexCode"`
- Children []*BaseFromThsHfClassifyListItem `description:"子分类"`
- }
- type BaseFromThsHfClassifyMoveReq struct {
- ClassifyId int `description:"分类ID"`
- ParentClassifyId int `description:"父级分类ID"`
- PrevClassifyId int `description:"上一个兄弟节点分类ID"`
- NextClassifyId int `description:"下一个兄弟节点分类ID"`
- ItemId int `description:"指标ID, 如果指标ID有值,则移动对象为指标,否则认为移动对象为分类"`
- PrevItemId int `description:"上一个指标ID"`
- NextItemId int `description:"下一个指标ID"`
- }
- func GetThsHfClassifyById(classifyId int) (item *BaseFromThsHfClassify, err error) {
- o := orm.NewOrmUsingDB("data")
- sql := `SELECT * FROM base_from_ths_hf_classify WHERE base_from_ths_hf_classify_id = ?`
- err = o.Raw(sql, classifyId).QueryRow(&item)
- return
- }
- func GetThsHfClassifyByRootIdLevel(rootId int, orderStr string) (items []*BaseFromThsHfClassify, err error) {
- o := orm.NewOrmUsingDB("data")
- sql := ` SELECT * FROM base_from_ths_hf_classify WHERE root_id = ? `
- if orderStr != "" {
- sql += orderStr
- } else {
- sql += ` order by level desc, sort asc, base_from_ths_hf_classify_id asc`
- }
- _, err = o.Raw(sql, rootId).QueryRows(&items)
- return
- }
- // UpdateThsHfClassifySortByParentId 根据父类id更新排序
- func UpdateThsHfClassifySortByParentId(parentId, classifyId, nowSort int, updateSort string) (err error) {
- o := orm.NewOrmUsingDB("data")
- sql := ` update base_from_ths_hf_classify set sort = ` + updateSort + ` WHERE parent_id = ? AND sort > ? `
- if classifyId > 0 {
- sql += ` or ( base_from_ths_hf_classify_id > ` + fmt.Sprint(classifyId) + ` and sort = ` + fmt.Sprint(nowSort) + `)`
- }
- _, err = o.Raw(sql, parentId, nowSort).Exec()
- return
- }
- // GetFirstThsHfClassifyByParentId 获取当前父级分类下,且排序数相同 的排序第一条的数据
- func GetFirstThsHfClassifyByParentId(parentId int) (item *BaseFromThsHfClassify, err error) {
- o := orm.NewOrmUsingDB("data")
- sql := ` SELECT * FROM base_from_ths_hf_classify WHERE parent_id = ? order by sort asc,base_from_ths_hf_classify_id asc limit 1`
- err = o.Raw(sql, parentId).QueryRow(&item)
- return
- }
- func UpdateThsHfClassifyChildByParentClassifyId(classifyIds []int, rootId int, levelStep int) (err error) {
- o := orm.NewOrmUsingDB("data")
- var pars []interface{}
- pars = append(pars, rootId, levelStep)
- pars = append(pars, classifyIds)
- // 更新相关联的二级分类的parentId,和classify_name_second
- sql := `update base_from_ths_hf_classify SET root_id = ?, level = level+? where base_from_ths_hf_classify_id IN (` + utils.GetOrmInReplace(len(classifyIds)) + `)`
- _, err = o.Raw(sql, pars).Exec()
- if err != nil {
- return
- }
- return
- }
|