package data_manage import ( "eta/eta_api/utils" "fmt" "github.com/beego/beego/v2/client/orm" "strings" "time" ) // AiPredictModelClassify 同花顺高频数据-分类 type AiPredictModelClassify struct { AiPredictModelClassifyId int `orm:"column(ai_predict_model_classify_id);pk"` UniqueCode string `description:"唯一编码"` ClassifyName string `description:"分类名称"` ClassifyNameEn string `description:"英文分类名称"` ParentId int `description:"父级ID"` Level int `description:"层级"` LevelPath string `description:"层级路径"` Sort int `description:"排序"` RootId int `description:"顶级分类ID"` SysUserId int `description:"创建人ID"` SysUserRealName string `description:"创建人姓名"` CreateTime time.Time `description:"创建时间"` ModifyTime time.Time `description:"修改时间"` } func (m *AiPredictModelClassify) TableName() string { return "ai_predict_model_classify" } type AiPredictModelClassifyCols struct { PrimaryId string UniqueCode string ClassifyName string ClassifyNameEn string ParentId string Level string LevelPath string Sort string RootId string SysUserId string SysUserRealName string CreateTime string ModifyTime string } func (m *AiPredictModelClassify) Cols() AiPredictModelClassifyCols { return AiPredictModelClassifyCols{ PrimaryId: "ai_predict_model_classify_id", UniqueCode: "unique_code", ClassifyName: "classify_name", ClassifyNameEn: "classify_name_en", ParentId: "parent_id", Level: "level", LevelPath: "level_path", Sort: "sort", RootId: "root_id", SysUserId: "sys_user_id", SysUserRealName: "sys_user_real_name", CreateTime: "create_time", ModifyTime: "modify_time", } } func (m *AiPredictModelClassify) Create() (err error) { o := orm.NewOrmUsingDB("data") id, err := o.Insert(m) if err != nil { return } m.AiPredictModelClassifyId = int(id) return } func (m *AiPredictModelClassify) CreateMulti(items []*AiPredictModelClassify) (err error) { if len(items) == 0 { return } o := orm.NewOrmUsingDB("data") _, err = o.InsertMulti(len(items), items) return } func (m *AiPredictModelClassify) Update(cols []string) (err error) { o := orm.NewOrmUsingDB("data") _, err = o.Update(m, cols...) return } func (m *AiPredictModelClassify) 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.AiPredictModelClassifyId).Exec() return } func (m *AiPredictModelClassify) 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 *AiPredictModelClassify) 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 *AiPredictModelClassify) GetItemById(id int) (item *AiPredictModelClassify, 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 *AiPredictModelClassify) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *AiPredictModelClassify, 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 *AiPredictModelClassify) 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 *AiPredictModelClassify) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*AiPredictModelClassify, 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 *AiPredictModelClassify) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*AiPredictModelClassify, 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 } // AiPredictModelClassifyItem 同花顺高频数据信息 type AiPredictModelClassifyItem 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 []*AiPredictModelClassifyItem `description:"子分类"` } func (m *AiPredictModelClassify) Format2Item() (item *AiPredictModelClassifyItem) { item = new(AiPredictModelClassifyItem) item.ClassifyId = m.AiPredictModelClassifyId 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([]*AiPredictModelClassifyItem, 0) return } type AiPredictModelClassifyAddReq struct { ClassifyName string `description:"分类名称"` ParentId int `description:"父级ID, 第一级传0"` Level int `description:"层级, 第一级传0, 其余传上一级的层级"` } type AiPredictModelClassifyEditReq struct { ClassifyId int `description:"分类ID"` ClassifyName string `description:"分类名称"` } func (m *AiPredictModelClassify) 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 AiPredictModelClassifyRemoveReq struct { ClassifyId int `description:"分类ID"` IndexId int `description:"标的ID"` } type AiPredictModelClassifyListResp struct { AllNodes []*AiPredictModelClassifyListItem `description:"分类节点"` } type AiPredictModelClassifyListItem struct { NodeType int `description:"节点类型: 0-分类; 1-指标"` NodeName string `description:"节点名称"` ClassifyId int `description:"分类ID"` ClassifyName 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 []*AiPredictModelClassifyListItem `description:"子分类"` } type AiPredictModelClassifyMoveReq 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 GetAiPredictModelClassifyById(classifyId int) (item *AiPredictModelClassify, err error) { o := orm.NewOrmUsingDB("data") sql := `SELECT * FROM ai_predict_model_classify WHERE ai_predict_model_classify_id = ?` err = o.Raw(sql, classifyId).QueryRow(&item) return } func GetAiPredictModelClassifyByRootIdLevel(rootId int, orderStr string) (items []*AiPredictModelClassify, err error) { o := orm.NewOrmUsingDB("data") sql := ` SELECT * FROM ai_predict_model_classify WHERE root_id = ? ` if orderStr != "" { sql += orderStr } else { sql += ` order by level desc, sort asc, ai_predict_model_classify_id asc` } _, err = o.Raw(sql, rootId).QueryRows(&items) return } // UpdateAiPredictModelClassifySortByParentId 根据父类id更新排序 func UpdateAiPredictModelClassifySortByParentId(parentId, classifyId, nowSort int, updateSort string) (err error) { o := orm.NewOrmUsingDB("data") sql := ` update ai_predict_model_classify set sort = ` + updateSort + ` WHERE parent_id = ? AND sort > ? ` if classifyId > 0 { sql += ` or ( ai_predict_model_classify_id > ` + fmt.Sprint(classifyId) + ` and sort = ` + fmt.Sprint(nowSort) + `)` } _, err = o.Raw(sql, parentId, nowSort).Exec() return } // GetFirstAiPredictModelClassifyByParentId 获取当前父级分类下,且排序数相同 的排序第一条的数据 func GetFirstAiPredictModelClassifyByParentId(parentId int) (item *AiPredictModelClassify, err error) { o := orm.NewOrmUsingDB("data") sql := ` SELECT * FROM ai_predict_model_classify WHERE parent_id = ? order by sort asc,ai_predict_model_classify_id asc limit 1` err = o.Raw(sql, parentId).QueryRow(&item) return } func UpdateAiPredictModelClassifyChildByParentClassifyId(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 ai_predict_model_classify SET root_id = ?, level = level+? where ai_predict_model_classify_id IN (` + utils.GetOrmInReplace(len(classifyIds)) + `)` _, err = o.Raw(sql, pars).Exec() if err != nil { return } return } // GetAiPredictModelIndexCountByClassifyId 获取目录下(包含子目录)的指标数量 func GetAiPredictModelIndexCountByClassifyId(classifyId int) (count int, err error) { o := orm.NewOrmUsingDB("data") sql := `SELECT COUNT(1) AS count FROM ai_predict_model_index AS a WHERE a.classify_id IN( SELECT t.ai_predict_model_classify_id FROM ( SELECT rd.* FROM (SELECT * FROM ai_predict_model_classify WHERE parent_id IS NOT NULL) rd, (SELECT @pid := ?) pd WHERE FIND_IN_SET(parent_id, @pid) > 0 AND @pid := CONCAT(@pid, ',', ai_predict_model_classify_id) UNION SELECT * FROM ai_predict_model_classify WHERE ai_predict_model_classify_id = @pid )AS t )` err = o.Raw(sql, classifyId).QueryRow(&count) return } // GetAiPredictModelClassifyCountByClassifyId 获取目录下子目录数量 func GetAiPredictModelClassifyCountByClassifyId(chartClassifyId int) (count int, err error) { o := orm.NewOrmUsingDB("data") sql := `SELECT COUNT(1) AS count FROM ( SELECT rd.* FROM (SELECT * FROM ai_predict_model_classify WHERE parent_id IS NOT NULL) rd, (SELECT @pid := ?) pd WHERE FIND_IN_SET(parent_id, @pid) > 0 AND @pid := CONCAT(@pid, ',', ai_predict_model_classify_id) UNION SELECT * FROM ai_predict_model_classify WHERE ai_predict_model_classify_id = @pid )AS t WHERE t.ai_predict_model_classify_id <> ?` err = o.Raw(sql, chartClassifyId, chartClassifyId).QueryRow(&count) return } // RemoveAiPredictModelClassify 删除分类及子分类 func RemoveAiPredictModelClassify(classifyId int) (err error) { o := orm.NewOrmUsingDB("data") sql := `DELETE FROM ai_predict_model_classify WHERE ai_predict_model_classify_id IN( SELECT t.ai_predict_model_classify_id FROM ( SELECT rd.* FROM (SELECT * FROM ai_predict_model_classify WHERE parent_id IS NOT NULL) rd, (SELECT @pid := ?) pd WHERE FIND_IN_SET(parent_id, @pid) > 0 AND @pid := CONCAT(@pid, ',', ai_predict_model_classify_id) UNION SELECT * FROM ai_predict_model_classify WHERE ai_predict_model_classify_id = @pid )AS t )` _, err = o.Raw(sql, classifyId).Exec() return }