ai_predict_model_classify.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. package data_manage
  2. import (
  3. "eta/eta_api/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "strings"
  7. "time"
  8. )
  9. // AiPredictModelClassify 同花顺高频数据-分类
  10. type AiPredictModelClassify struct {
  11. AiPredictModelClassifyId int `orm:"column(ai_predict_model_classify_id);pk"`
  12. UniqueCode string `description:"唯一编码"`
  13. ClassifyName string `description:"分类名称"`
  14. ClassifyNameEn string `description:"英文分类名称"`
  15. ParentId int `description:"父级ID"`
  16. Level int `description:"层级"`
  17. LevelPath string `description:"层级路径"`
  18. Sort int `description:"排序"`
  19. RootId int `description:"顶级分类ID"`
  20. SysUserId int `description:"创建人ID"`
  21. SysUserRealName string `description:"创建人姓名"`
  22. CreateTime time.Time `description:"创建时间"`
  23. ModifyTime time.Time `description:"修改时间"`
  24. }
  25. func (m *AiPredictModelClassify) TableName() string {
  26. return "ai_predict_model_classify"
  27. }
  28. type AiPredictModelClassifyCols struct {
  29. PrimaryId string
  30. UniqueCode string
  31. ClassifyName string
  32. ClassifyNameEn string
  33. ParentId string
  34. Level string
  35. LevelPath string
  36. Sort string
  37. RootId string
  38. SysUserId string
  39. SysUserRealName string
  40. CreateTime string
  41. ModifyTime string
  42. }
  43. func (m *AiPredictModelClassify) Cols() AiPredictModelClassifyCols {
  44. return AiPredictModelClassifyCols{
  45. PrimaryId: "ai_predict_model_classify_id",
  46. UniqueCode: "unique_code",
  47. ClassifyName: "classify_name",
  48. ClassifyNameEn: "classify_name_en",
  49. ParentId: "parent_id",
  50. Level: "level",
  51. LevelPath: "level_path",
  52. Sort: "sort",
  53. RootId: "root_id",
  54. SysUserId: "sys_user_id",
  55. SysUserRealName: "sys_user_real_name",
  56. CreateTime: "create_time",
  57. ModifyTime: "modify_time",
  58. }
  59. }
  60. func (m *AiPredictModelClassify) Create() (err error) {
  61. o := orm.NewOrmUsingDB("data")
  62. id, err := o.Insert(m)
  63. if err != nil {
  64. return
  65. }
  66. m.AiPredictModelClassifyId = int(id)
  67. return
  68. }
  69. func (m *AiPredictModelClassify) CreateMulti(items []*AiPredictModelClassify) (err error) {
  70. if len(items) == 0 {
  71. return
  72. }
  73. o := orm.NewOrmUsingDB("data")
  74. _, err = o.InsertMulti(len(items), items)
  75. return
  76. }
  77. func (m *AiPredictModelClassify) Update(cols []string) (err error) {
  78. o := orm.NewOrmUsingDB("data")
  79. _, err = o.Update(m, cols...)
  80. return
  81. }
  82. func (m *AiPredictModelClassify) Remove() (err error) {
  83. o := orm.NewOrmUsingDB("data")
  84. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
  85. _, err = o.Raw(sql, m.AiPredictModelClassifyId).Exec()
  86. return
  87. }
  88. func (m *AiPredictModelClassify) MultiRemove(ids []int) (err error) {
  89. if len(ids) == 0 {
  90. return
  91. }
  92. o := orm.NewOrmUsingDB("data")
  93. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s IN (%s)`, m.TableName(), m.Cols().PrimaryId, utils.GetOrmInReplace(len(ids)))
  94. _, err = o.Raw(sql, ids).Exec()
  95. return
  96. }
  97. func (m *AiPredictModelClassify) RemoveByCondition(condition string, pars []interface{}) (err error) {
  98. if condition == "" {
  99. return
  100. }
  101. o := orm.NewOrmUsingDB("data")
  102. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s`, m.TableName(), condition)
  103. _, err = o.Raw(sql, pars).Exec()
  104. return
  105. }
  106. func (m *AiPredictModelClassify) GetItemById(id int) (item *AiPredictModelClassify, err error) {
  107. o := orm.NewOrmUsingDB("data")
  108. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
  109. err = o.Raw(sql, id).QueryRow(&item)
  110. return
  111. }
  112. func (m *AiPredictModelClassify) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *AiPredictModelClassify, err error) {
  113. o := orm.NewOrmUsingDB("data")
  114. order := ``
  115. if orderRule != "" {
  116. order = ` ORDER BY ` + orderRule
  117. }
  118. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s %s LIMIT 1`, m.TableName(), condition, order)
  119. err = o.Raw(sql, pars).QueryRow(&item)
  120. return
  121. }
  122. func (m *AiPredictModelClassify) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  123. o := orm.NewOrmUsingDB("data")
  124. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  125. err = o.Raw(sql, pars).QueryRow(&count)
  126. return
  127. }
  128. func (m *AiPredictModelClassify) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*AiPredictModelClassify, err error) {
  129. o := orm.NewOrmUsingDB("data")
  130. fields := strings.Join(fieldArr, ",")
  131. if len(fieldArr) == 0 {
  132. fields = `*`
  133. }
  134. order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
  135. if orderRule != "" {
  136. order = ` ORDER BY ` + orderRule
  137. }
  138. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  139. _, err = o.Raw(sql, pars).QueryRows(&items)
  140. return
  141. }
  142. func (m *AiPredictModelClassify) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*AiPredictModelClassify, err error) {
  143. o := orm.NewOrmUsingDB("data")
  144. fields := strings.Join(fieldArr, ",")
  145. if len(fieldArr) == 0 {
  146. fields = `*`
  147. }
  148. order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
  149. if orderRule != "" {
  150. order = ` ORDER BY ` + orderRule
  151. }
  152. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
  153. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  154. return
  155. }
  156. // AiPredictModelClassifyItem 同花顺高频数据信息
  157. type AiPredictModelClassifyItem struct {
  158. ClassifyId int `description:"分类ID"`
  159. ClassifyName string `description:"分类名称"`
  160. ClassifyNameEn string `description:"英文分类名称"`
  161. ParentId int `description:"父级ID"`
  162. Level int `description:"层级"`
  163. Sort int `description:"排序"`
  164. LevelPath string `description:"层级路径"`
  165. UniqueCode string `description:"唯一编码"`
  166. Children []*AiPredictModelClassifyItem `description:"子分类"`
  167. }
  168. func (m *AiPredictModelClassify) Format2Item() (item *AiPredictModelClassifyItem) {
  169. item = new(AiPredictModelClassifyItem)
  170. item.ClassifyId = m.AiPredictModelClassifyId
  171. item.ClassifyName = m.ClassifyName
  172. item.ClassifyNameEn = m.ClassifyNameEn
  173. item.ParentId = m.ParentId
  174. item.Level = m.Level
  175. item.Sort = m.Sort
  176. item.LevelPath = m.LevelPath
  177. item.UniqueCode = m.UniqueCode
  178. item.Children = make([]*AiPredictModelClassifyItem, 0)
  179. return
  180. }
  181. type AiPredictModelClassifyAddReq struct {
  182. ClassifyName string `description:"分类名称"`
  183. ParentId int `description:"父级ID, 第一级传0"`
  184. Level int `description:"层级, 第一级传0, 其余传上一级的层级"`
  185. }
  186. type AiPredictModelClassifyEditReq struct {
  187. ClassifyId int `description:"分类ID"`
  188. ClassifyName string `description:"分类名称"`
  189. }
  190. func (m *AiPredictModelClassify) GetSortMax(parentId int) (sort int, err error) {
  191. o := orm.NewOrmUsingDB("data")
  192. sql := fmt.Sprintf(`SELECT MAX(%s) FROM %s WHERE %s = ?`, m.Cols().Sort, m.TableName(), m.Cols().ParentId)
  193. err = o.Raw(sql, parentId).QueryRow(&sort)
  194. return
  195. }
  196. type AiPredictModelClassifyRemoveReq struct {
  197. ClassifyId int `description:"分类ID"`
  198. IndexId int `description:"标的ID"`
  199. }
  200. type AiPredictModelClassifyListResp struct {
  201. AllNodes []*AiPredictModelClassifyListItem `description:"分类节点"`
  202. }
  203. type AiPredictModelClassifyListItem struct {
  204. NodeType int `description:"节点类型: 0-分类; 1-指标"`
  205. NodeName string `description:"节点名称"`
  206. ClassifyId int `description:"分类ID"`
  207. ClassifyName string `description:"分类名称"`
  208. IndexId int `description:"指标ID"`
  209. IndexCode string `description:"指标编码"`
  210. IndexName string `description:"指标名称"`
  211. ParentId int `description:"父级ID"`
  212. Level int `description:"层级"`
  213. Sort int `description:"排序"`
  214. UniqueCode string `description:"唯一编码, 指标为IndexCode"`
  215. Children []*AiPredictModelClassifyListItem `description:"子分类"`
  216. }
  217. type AiPredictModelClassifyMoveReq struct {
  218. ClassifyId int `description:"分类ID"`
  219. ParentClassifyId int `description:"父级分类ID"`
  220. PrevClassifyId int `description:"上一个兄弟节点分类ID"`
  221. NextClassifyId int `description:"下一个兄弟节点分类ID"`
  222. ItemId int `description:"指标ID, 如果指标ID有值,则移动对象为指标,否则认为移动对象为分类"`
  223. PrevItemId int `description:"上一个指标ID"`
  224. NextItemId int `description:"下一个指标ID"`
  225. }
  226. func GetAiPredictModelClassifyById(classifyId int) (item *AiPredictModelClassify, err error) {
  227. o := orm.NewOrmUsingDB("data")
  228. sql := `SELECT * FROM ai_predict_model_classify WHERE ai_predict_model_classify_id = ?`
  229. err = o.Raw(sql, classifyId).QueryRow(&item)
  230. return
  231. }
  232. func GetAiPredictModelClassifyByRootIdLevel(rootId int, orderStr string) (items []*AiPredictModelClassify, err error) {
  233. o := orm.NewOrmUsingDB("data")
  234. sql := ` SELECT * FROM ai_predict_model_classify WHERE root_id = ? `
  235. if orderStr != "" {
  236. sql += orderStr
  237. } else {
  238. sql += ` order by level desc, sort asc, ai_predict_model_classify_id asc`
  239. }
  240. _, err = o.Raw(sql, rootId).QueryRows(&items)
  241. return
  242. }
  243. // UpdateAiPredictModelClassifySortByParentId 根据父类id更新排序
  244. func UpdateAiPredictModelClassifySortByParentId(parentId, classifyId, nowSort int, updateSort string) (err error) {
  245. o := orm.NewOrmUsingDB("data")
  246. sql := ` update ai_predict_model_classify set sort = ` + updateSort + ` WHERE parent_id = ? AND sort > ? `
  247. if classifyId > 0 {
  248. sql += ` or ( ai_predict_model_classify_id > ` + fmt.Sprint(classifyId) + ` and sort = ` + fmt.Sprint(nowSort) + `)`
  249. }
  250. _, err = o.Raw(sql, parentId, nowSort).Exec()
  251. return
  252. }
  253. // GetFirstAiPredictModelClassifyByParentId 获取当前父级分类下,且排序数相同 的排序第一条的数据
  254. func GetFirstAiPredictModelClassifyByParentId(parentId int) (item *AiPredictModelClassify, err error) {
  255. o := orm.NewOrmUsingDB("data")
  256. sql := ` SELECT * FROM ai_predict_model_classify WHERE parent_id = ? order by sort asc,ai_predict_model_classify_id asc limit 1`
  257. err = o.Raw(sql, parentId).QueryRow(&item)
  258. return
  259. }
  260. func UpdateAiPredictModelClassifyChildByParentClassifyId(classifyIds []int, rootId int, levelStep int) (err error) {
  261. o := orm.NewOrmUsingDB("data")
  262. var pars []interface{}
  263. pars = append(pars, rootId, levelStep)
  264. pars = append(pars, classifyIds)
  265. // 更新相关联的二级分类的parentId,和classify_name_second
  266. sql := `UPDATE ai_predict_model_classify SET root_id = ?, level = level+? where ai_predict_model_classify_id IN (` + utils.GetOrmInReplace(len(classifyIds)) + `)`
  267. _, err = o.Raw(sql, pars).Exec()
  268. if err != nil {
  269. return
  270. }
  271. return
  272. }
  273. // GetAiPredictModelIndexCountByClassifyId 获取目录下(包含子目录)的指标数量
  274. func GetAiPredictModelIndexCountByClassifyId(classifyId int) (count int, err error) {
  275. o := orm.NewOrmUsingDB("data")
  276. sql := `SELECT COUNT(1) AS count FROM ai_predict_model_index AS a
  277. WHERE a.classify_id IN(
  278. SELECT t.ai_predict_model_classify_id FROM
  279. (
  280. SELECT rd.*
  281. FROM (SELECT * FROM ai_predict_model_classify WHERE parent_id IS NOT NULL) rd,
  282. (SELECT @pid := ?) pd
  283. WHERE FIND_IN_SET(parent_id, @pid) > 0
  284. AND @pid := CONCAT(@pid, ',', ai_predict_model_classify_id)
  285. UNION SELECT * FROM ai_predict_model_classify WHERE ai_predict_model_classify_id = @pid
  286. )AS t
  287. )`
  288. err = o.Raw(sql, classifyId).QueryRow(&count)
  289. return
  290. }
  291. // GetAiPredictModelClassifyCountByClassifyId 获取目录下子目录数量
  292. func GetAiPredictModelClassifyCountByClassifyId(chartClassifyId int) (count int, err error) {
  293. o := orm.NewOrmUsingDB("data")
  294. sql := `SELECT COUNT(1) AS count FROM (
  295. SELECT rd.*
  296. FROM (SELECT * FROM ai_predict_model_classify WHERE parent_id IS NOT NULL) rd,
  297. (SELECT @pid := ?) pd
  298. WHERE FIND_IN_SET(parent_id, @pid) > 0
  299. AND @pid := CONCAT(@pid, ',', ai_predict_model_classify_id)
  300. UNION SELECT * FROM ai_predict_model_classify WHERE ai_predict_model_classify_id = @pid
  301. )AS t
  302. WHERE t.ai_predict_model_classify_id <> ?`
  303. err = o.Raw(sql, chartClassifyId, chartClassifyId).QueryRow(&count)
  304. return
  305. }
  306. // RemoveAiPredictModelClassify 删除分类及子分类
  307. func RemoveAiPredictModelClassify(classifyId int) (err error) {
  308. o := orm.NewOrmUsingDB("data")
  309. sql := `DELETE FROM ai_predict_model_classify
  310. WHERE ai_predict_model_classify_id IN(
  311. SELECT t.ai_predict_model_classify_id FROM
  312. (
  313. SELECT rd.*
  314. FROM (SELECT * FROM ai_predict_model_classify WHERE parent_id IS NOT NULL) rd,
  315. (SELECT @pid := ?) pd
  316. WHERE FIND_IN_SET(parent_id, @pid) > 0
  317. AND @pid := CONCAT(@pid, ',', ai_predict_model_classify_id)
  318. UNION SELECT * FROM ai_predict_model_classify WHERE ai_predict_model_classify_id = @pid
  319. )AS t
  320. )`
  321. _, err = o.Raw(sql, classifyId).Exec()
  322. return
  323. }