base_from_sci_hq_classify.go 8.9 KB

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