base_from_sci_hq_classify.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. package data_manage
  2. import (
  3. "eta/eta_api/utils"
  4. "time"
  5. "github.com/beego/beego/v2/client/orm"
  6. )
  7. // BaseFromSciHqClassify 卓创红期原始数据分类表
  8. type BaseFromSciHqClassify struct {
  9. ClassifyId int `orm:"column(classify_id);pk"`
  10. ClassifyName string `description:"分类名称"`
  11. ParentId int `description:"父级id"`
  12. Level int `description:"层级"`
  13. Sort int `description:"排序字段"`
  14. ModifyTime time.Time `description:"修改时间"`
  15. CreateTime time.Time `description:"创建时间"`
  16. ClassifyNameEn string `description:"英文分类名称"`
  17. }
  18. type BaseFromSciHqClassifyItem struct {
  19. ClassifyId int `orm:"column(classify_id);pk"`
  20. ClassifyName string `description:"分类名称"`
  21. ParentId int `description:"父级id"`
  22. Level int `description:"层级"`
  23. Sort int `description:"排序字段"`
  24. UniqueCode string `description:"唯一code"`
  25. ModifyTime time.Time `description:"修改时间"`
  26. CreateTime time.Time `description:"创建时间"`
  27. ClassifyNameEn string `description:"英文分类名称"`
  28. Children []*BaseFromSciHqClassifyItem `description:"子分类"`
  29. }
  30. type BaseFromSciHqClassifyMaxSort struct {
  31. ClassifyId int `description:"分类id"`
  32. MaxSort int `description:"最大排序"`
  33. }
  34. func (t *BaseFromSciHqClassify) Add() (insertId int64, err error) {
  35. o := orm.NewOrmUsingDB("data")
  36. insertId, err = o.Insert(t)
  37. return
  38. }
  39. func (t *BaseFromSciHqClassify) Update(cols []string) (err error) {
  40. o := orm.NewOrmUsingDB("data")
  41. _, err = o.Update(t, cols...)
  42. return
  43. }
  44. func BatchAddSciHqClassify(items []*BaseFromSciHqClassify) (err error) {
  45. o := orm.NewOrmUsingDB("data")
  46. _, err = o.InsertMulti(len(items), items)
  47. return
  48. }
  49. // 获取所有分类
  50. func GetSciHqClassifyAll() (items []*BaseFromSciHqClassifyItem, err error) {
  51. o := orm.NewOrmUsingDB("data")
  52. sql := ` SELECT * FROM base_from_sci_hq_classify ORDER BY sort ASC, classify_id ASC`
  53. _, err = o.Raw(sql).QueryRows(&items)
  54. return
  55. }
  56. // GetChildSciHqClassifyListById 获取子分类列表
  57. func GetChildSciHqClassifyListById(classifyId int) (items []*BaseFromSciHqClassifyItem, err error) {
  58. o := orm.NewOrmUsingDB("data")
  59. sql := ` SELECT * FROM base_from_sci_hq_classify WHERE parent_id=? `
  60. _, err = o.Raw(sql, classifyId).QueryRows(&items)
  61. return
  62. }
  63. // GetChildSciHqClassifyIdsById 获取子分类的id集合
  64. func GetChildSciHqClassifyIdsById(classifyId int) (items []int, err error) {
  65. o := orm.NewOrmUsingDB("data")
  66. sql := ` SELECT classify_id FROM base_from_sci_hq_classify WHERE parent_id=? `
  67. _, err = o.Raw(sql, classifyId).QueryRows(&items)
  68. return
  69. }
  70. // GetChildSciHqClassifyMaxSortById 获取子分类最大排序
  71. func GetChildSciHqClassifyMaxSortById(classifyId int) (sort int, err error) {
  72. o := orm.NewOrmUsingDB("data")
  73. sql := ` SELECT MAX(sort) AS sort FROM base_from_sci_hq_classify WHERE parent_id=? `
  74. err = o.Raw(sql, classifyId).QueryRow(&sort)
  75. return
  76. }
  77. // GetSciHqClassifyCountById 获取分类数量
  78. func GetSciHqClassifyCountById(classifyId int) (count int, err error) {
  79. o := orm.NewOrmUsingDB("data")
  80. sql := ` SELECT COUNT(*) AS count FROM base_from_sci_hq_classify WHERE classify_id=? `
  81. err = o.Raw(sql, classifyId).QueryRow(&count)
  82. return
  83. }
  84. // GetSciHqClassifyById 通过分类id获取分类
  85. func GetSciHqClassifyById(classifyId int) (item *BaseFromSciHqClassify, err error) {
  86. o := orm.NewOrmUsingDB("data")
  87. sql := ` SELECT * FROM base_from_sci_hq_classify WHERE classify_id=? `
  88. err = o.Raw(sql, classifyId).QueryRow(&item)
  89. return
  90. }
  91. // GetSciHqClassifyListByIds 通过分类id获取分类列表
  92. func GetSciHqClassifyListByIds(classifyIds []int) (items []*BaseFromSciHqClassify, err error) {
  93. if len(classifyIds) == 0 {
  94. return
  95. }
  96. o := orm.NewOrmUsingDB("data")
  97. sql := ` SELECT * FROM base_from_sci_hq_classify WHERE classify_id IN (` + utils.GetOrmInReplace(len(classifyIds)) + `)`
  98. _, err = o.Raw(sql, classifyIds).QueryRows(&items)
  99. return
  100. }
  101. // GetSciHqClassifyCountByName 通过分类名称获取分类
  102. func GetSciHqClassifyCountByName(classifyName string, lang string) (count int, err error) {
  103. o := orm.NewOrmUsingDB("data")
  104. sql := ` SELECT COUNT(*) AS count FROM base_from_sci_hq_classify WHERE 1=1`
  105. if lang == utils.EnLangVersion {
  106. sql += ` AND classify_name_en=? `
  107. } else {
  108. sql += ` AND classify_name=? `
  109. }
  110. err = o.Raw(sql, classifyName).QueryRow(&count)
  111. return
  112. }
  113. func GetBaseFromSciHqClassifyEnCount(classifyNameEn string, parentId int) (count int, err error) {
  114. o := orm.NewOrmUsingDB("data")
  115. sql := `SELECT COUNT(1) AS count FROM base_from_sci_classify WHERE classify_name_en=? AND parent_id=? `
  116. err = o.Raw(sql, classifyNameEn, parentId).QueryRow(&count)
  117. return
  118. }
  119. func GetBaseFromSciHqClassifyCount(classifyName string, parentId int) (count int, err error) {
  120. o := orm.NewOrmUsingDB("data")
  121. sql := `SELECT COUNT(1) AS count FROM base_from_sci_classify WHERE classify_name=? AND parent_id=? `
  122. err = o.Raw(sql, classifyName, parentId).QueryRow(&count)
  123. return
  124. }
  125. func DeleteSciHqClassifyById(classifyId int) (err error) {
  126. o := orm.NewOrmUsingDB("data")
  127. sql := ` DELETE FROM base_from_sci_hq_classify WHERE classify_id=? `
  128. _, err = o.Raw(sql, classifyId).Exec()
  129. return
  130. }
  131. // BatchDeleteSciHqClassifyById 批量删除分类
  132. func BatchDeleteSciHqClassifyById(classifyId []int) (err error) {
  133. if len(classifyId) == 0 {
  134. return
  135. }
  136. o := orm.NewOrmUsingDB("data")
  137. to, err := o.Begin()
  138. if err != nil {
  139. return
  140. }
  141. defer func() {
  142. if err != nil {
  143. _ = to.Rollback()
  144. } else {
  145. _ = to.Commit()
  146. }
  147. }()
  148. sql := ` DELETE FROM base_from_sci_hq_classify WHERE classify_id IN (` + utils.GetOrmInReplace(len(classifyId)) + `) `
  149. _, err = o.Raw(sql, classifyId).Exec()
  150. return
  151. }
  152. // DeleteSciHqClassifyByClassifyId 根据分类id删除对应的指标分类
  153. func DeleteSciHqClassifyByClassifyId(classifyIdList []int) (err error) {
  154. o := orm.NewOrmUsingDB("data")
  155. num := len(classifyIdList)
  156. if num <= 0 {
  157. return
  158. }
  159. //删除分类
  160. sql := `DELETE FROM base_from_sci_hq_classify WHERE classify_id IN (` + utils.GetOrmInReplace(num) + `) `
  161. _, err = o.Raw(sql, classifyIdList).Exec()
  162. return
  163. }
  164. // GetSciHqIndexClassifyMinSort 获取最小不等于0的排序
  165. func GetSciHqIndexClassifyMinSort(parentId int) (sort int, err error) {
  166. o := orm.NewOrmUsingDB("data")
  167. sql := `SELECT MIN(sort) FROM base_from_sci_hq_classify WHERE parent_id=? AND sort <> 0 `
  168. err = o.Raw(sql, parentId).QueryRow(&sort)
  169. return
  170. }
  171. // MoveUpSciHqIndexClassifyBySort 往上移动
  172. func MoveUpSciHqIndexClassifyBySort(parentId, nextSort, currentSort int) (err error) {
  173. o := orm.NewOrmUsingDB("data")
  174. sql := `update base_from_sci_hq_classify set sort = sort + 1 where parent_id=? and sort >= ? and sort< ?`
  175. _, err = o.Raw(sql, parentId, nextSort, currentSort).Exec()
  176. return
  177. }
  178. // MoveDownSciHqIndexClassifyBySort 往下移动
  179. func MoveDownSciHqIndexClassifyBySort(parentId, prevSort, currentSort int) (err error) {
  180. o := orm.NewOrmUsingDB("data")
  181. sql := `update base_from_sci_hq_classify set sort = sort - 1 where parent_id=? and sort <= ? and sort> ? `
  182. _, err = o.Raw(sql, parentId, prevSort, currentSort).Exec()
  183. return
  184. }