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 }