base_from_sci_hq_classify.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. // GetSciHqChildClassifyById 通过分类id获取子分类
  92. func GetSciHqChildClassifyIdsById(classifyId int) (items []int, err error) {
  93. o := orm.NewOrmUsingDB("data")
  94. sql := ` SELECT classify_id FROM base_from_sci_hq_classify WHERE parent_id=? `
  95. _, err = o.Raw(sql, classifyId).QueryRows(&items)
  96. return
  97. }
  98. // GetSciHqClassifyListByIds 通过分类id获取分类列表
  99. func GetSciHqClassifyListByIds(classifyIds []int) (items []*BaseFromSciHqClassify, err error) {
  100. if len(classifyIds) == 0 {
  101. return
  102. }
  103. o := orm.NewOrmUsingDB("data")
  104. sql := ` SELECT * FROM base_from_sci_hq_classify WHERE classify_id IN (` + utils.GetOrmInReplace(len(classifyIds)) + `)`
  105. _, err = o.Raw(sql, classifyIds).QueryRows(&items)
  106. return
  107. }
  108. // GetSciHqClassifyCountByName 通过分类名称获取分类
  109. func GetSciHqClassifyCountByName(classifyName string, lang string) (count int, err error) {
  110. o := orm.NewOrmUsingDB("data")
  111. sql := ` SELECT COUNT(*) AS count FROM base_from_sci_hq_classify WHERE 1=1`
  112. if lang == utils.EnLangVersion {
  113. sql += ` AND classify_name_en=? `
  114. } else {
  115. sql += ` AND classify_name=? `
  116. }
  117. err = o.Raw(sql, classifyName).QueryRow(&count)
  118. return
  119. }
  120. func GetBaseFromSciHqClassifyEnCount(classifyNameEn string, parentId int) (count int, err error) {
  121. o := orm.NewOrmUsingDB("data")
  122. sql := `SELECT COUNT(1) AS count FROM base_from_sci_hq_classify WHERE classify_name_en=? AND parent_id=? `
  123. err = o.Raw(sql, classifyNameEn, parentId).QueryRow(&count)
  124. return
  125. }
  126. func GetBaseFromSciHqClassifyCount(classifyName string, parentId int) (count int, err error) {
  127. o := orm.NewOrmUsingDB("data")
  128. sql := `SELECT COUNT(1) AS count FROM base_from_sci_hq_classify WHERE classify_name=? AND parent_id=? `
  129. err = o.Raw(sql, classifyName, parentId).QueryRow(&count)
  130. return
  131. }
  132. func DeleteSciHqClassifyById(classifyId int) (err error) {
  133. o := orm.NewOrmUsingDB("data")
  134. sql := ` DELETE FROM base_from_sci_hq_classify WHERE classify_id=? `
  135. _, err = o.Raw(sql, classifyId).Exec()
  136. return
  137. }
  138. // BatchDeleteSciHqClassifyById 批量删除分类
  139. func BatchDeleteSciHqClassifyById(classifyId []int) (err error) {
  140. if len(classifyId) == 0 {
  141. return
  142. }
  143. o := orm.NewOrmUsingDB("data")
  144. to, err := o.Begin()
  145. if err != nil {
  146. return
  147. }
  148. defer func() {
  149. if err != nil {
  150. _ = to.Rollback()
  151. } else {
  152. _ = to.Commit()
  153. }
  154. }()
  155. sql := ` DELETE FROM base_from_sci_hq_classify WHERE classify_id IN (` + utils.GetOrmInReplace(len(classifyId)) + `) `
  156. _, err = o.Raw(sql, classifyId).Exec()
  157. return
  158. }
  159. // DeleteSciHqClassifyByClassifyId 根据分类id删除对应的指标分类
  160. func DeleteSciHqClassifyByClassifyId(classifyIdList []int) (err error) {
  161. o := orm.NewOrmUsingDB("data")
  162. num := len(classifyIdList)
  163. if num <= 0 {
  164. return
  165. }
  166. //删除分类
  167. sql := `DELETE FROM base_from_sci_hq_classify WHERE classify_id IN (` + utils.GetOrmInReplace(num) + `) `
  168. _, err = o.Raw(sql, classifyIdList).Exec()
  169. return
  170. }
  171. // GetSciHqIndexClassifyMinSort 获取最小不等于0的排序
  172. func GetSciHqIndexClassifyMinSort(parentId int) (sort int, err error) {
  173. o := orm.NewOrmUsingDB("data")
  174. sql := `SELECT MIN(sort) FROM base_from_sci_hq_classify WHERE parent_id=? AND sort <> 0 `
  175. err = o.Raw(sql, parentId).QueryRow(&sort)
  176. return
  177. }
  178. // MoveUpSciHqIndexClassifyBySort 往上移动
  179. func MoveUpSciHqIndexClassifyBySort(parentId, nextSort, 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, nextSort, currentSort).Exec()
  183. return
  184. }
  185. // MoveDownSciHqIndexClassifyBySort 往下移动
  186. func MoveDownSciHqIndexClassifyBySort(parentId, prevSort, currentSort int) (err error) {
  187. o := orm.NewOrmUsingDB("data")
  188. sql := `update base_from_sci_hq_classify set sort = sort - 1 where parent_id=? and sort <= ? and sort> ? `
  189. _, err = o.Raw(sql, parentId, prevSort, currentSort).Exec()
  190. return
  191. }