manual_classify.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package data_manage
  2. import (
  3. "eta/eta_api/global"
  4. "eta/eta_api/utils"
  5. "time"
  6. )
  7. type EdbdataClassify struct {
  8. ClassifyId int `orm:"column(classify_id);pk" gorm:"primaryKey" description:"分类id"`
  9. ClassifyName string `description:"分类名称"`
  10. ParentId int `description:"父级id"`
  11. CreateTime time.Time `description:"创建时间"`
  12. Sort int `description:"排序"`
  13. IsShow int `description:"是否显示"`
  14. }
  15. type EdbdataClassifyList struct {
  16. ClassifyId int `description:"分类id"`
  17. ClassifyName string `description:"分类名称"`
  18. ParentId int `description:"父级id"`
  19. CreateTime time.Time `description:"创建时间"`
  20. Sort int `description:"排序"`
  21. IsShow int `description:"是否显示"`
  22. Total int `description:"指标总数"`
  23. }
  24. func AddEdbdataClassify(item *EdbdataClassify) (lastId int64, err error) {
  25. o := global.DbMap[utils.DbNameManualIndex]
  26. err = o.Create(item).Error
  27. if err != nil {
  28. return
  29. }
  30. lastId = int64(item.ClassifyId)
  31. return
  32. }
  33. type ManualClassifyList struct {
  34. ClassifyId int
  35. ClassifyName string
  36. ParentId int
  37. CheckList []int `gorm:"-"`
  38. Child []*ManualClassifyList `gorm:"-"`
  39. }
  40. func GetManualClassifyAll(adminId int) (items []*ManualClassifyList, err error) {
  41. var newItems []*ManualClassifyList
  42. o := global.DbMap[utils.DbNameManualIndex]
  43. sql := ` SELECT classify_id,classify_name,parent_id FROM edbdata_classify WHERE parent_id=0 AND is_show=1 ORDER BY sort ASC ,classify_id ASC `
  44. err = o.Raw(sql).Find(&newItems).Error
  45. if err != nil {
  46. return
  47. }
  48. classifyLen := len(newItems)
  49. for i := 0; i < classifyLen; i++ {
  50. var childItems []*ManualClassifyList
  51. parentId := newItems[i].ClassifyId
  52. childSql := ``
  53. childSql = "SELECT classify_id,classify_name,parent_id FROM edbdata_classify WHERE parent_id=? AND is_show=1 ORDER BY sort ASC ,classify_id ASC "
  54. err = o.Raw(childSql, parentId).Find(&childItems).Error
  55. if err != nil {
  56. return
  57. }
  58. checkList := make([]int, 0)
  59. for _, v := range childItems {
  60. if v.ParentId > 0 {
  61. count, err := GetManualUserClassifyCount(adminId, v.ClassifyId)
  62. if err != nil {
  63. return items, err
  64. }
  65. if count > 0 {
  66. checkList = append(checkList, v.ClassifyId)
  67. }
  68. }
  69. }
  70. newItems[i].CheckList = checkList
  71. newItems[i].Child = childItems
  72. }
  73. for _, v := range newItems {
  74. if v.ParentId > 0 {
  75. count, err := GetManualUserClassifyCount(adminId, v.ClassifyId)
  76. if err != nil {
  77. return items, err
  78. }
  79. if count > 0 {
  80. v.CheckList = append(v.CheckList, v.ClassifyId)
  81. }
  82. }
  83. childLen := len(v.Child)
  84. if childLen > 0 {
  85. items = append(items, v)
  86. }
  87. }
  88. return
  89. }
  90. func GetManualClassifyAllV2() (items []*ManualClassifyList, err error) {
  91. var newItems []*ManualClassifyList
  92. o := global.DbMap[utils.DbNameManualIndex]
  93. sql := ` SELECT classify_id,classify_name,parent_id FROM edbdata_classify WHERE parent_id=0 AND is_show=1 ORDER BY sort ASC ,classify_id ASC `
  94. err = o.Raw(sql).Find(&newItems).Error
  95. if err != nil {
  96. return
  97. }
  98. classifyLen := len(newItems)
  99. for i := 0; i < classifyLen; i++ {
  100. var childItems []*ManualClassifyList
  101. parentId := newItems[i].ClassifyId
  102. childSql := ``
  103. childSql = "SELECT classify_id,classify_name,parent_id FROM edbdata_classify WHERE parent_id=? AND is_show=1 ORDER BY sort ASC ,classify_id ASC "
  104. err = o.Raw(childSql, parentId).Find(&childItems).Error
  105. if err != nil {
  106. return
  107. }
  108. if childItems != nil && len(childItems) > 0 {
  109. newItems[i].Child = childItems
  110. }
  111. }
  112. for _, v := range newItems {
  113. items = append(items, v)
  114. }
  115. return
  116. }
  117. type AddManualClassifyReq struct {
  118. ClassifyName string `description:"分类名称"`
  119. ParentId int `description:"父级id,第一级传0"`
  120. }
  121. func GetManualClassifyCount(classifyName string, parentId int) (count int, err error) {
  122. o := global.DbMap[utils.DbNameManualIndex]
  123. sql := `SELECT COUNT(1) AS count FROM edbdata_classify WHERE parent_id=? AND classify_name=? `
  124. err = o.Raw(sql, parentId, classifyName).Scan(&count).Error
  125. return
  126. }
  127. func GetManualClassifyByClassifyName(classifyName string) (item *EdbdataClassify, err error) {
  128. o := global.DbMap[utils.DbNameManualIndex]
  129. sql := `SELECT * FROM edbdata_classify WHERE classify_name=? `
  130. err = o.Raw(sql, classifyName).Find(&item).Error
  131. return
  132. }
  133. func ModifyManualClassifyIsShow(isShow, classifyId, parentId int) (err error) {
  134. o := global.DbMap[utils.DbNameManualIndex]
  135. sql := `UPDATE edbdata_classify SET is_show=?,parent_id=? WHERE classify_id=? `
  136. err = o.Exec(sql, isShow, parentId, classifyId).Error
  137. return
  138. }
  139. type EditManualClassifyReq struct {
  140. ClassifyName string `description:"分类名称"`
  141. ClassifyId int `orm:"column(classify_id);pk" gorm:"primaryKey" description:"分类id"`
  142. ParentId int `description:"上级id"`
  143. }
  144. // DelManualClassifyReq 删除手工指标分类请求
  145. type DelManualClassifyReq struct {
  146. ClassifyId int `description:"分类id"`
  147. }
  148. func GetManualClassifyById(classifyId int) (item *EdbdataClassify, err error) {
  149. o := global.DbMap[utils.DbNameManualIndex]
  150. sql := ` SELECT * FROM edbdata_classify WHERE classify_id=? `
  151. err = o.Raw(sql, classifyId).Find(&item).Error
  152. return
  153. }
  154. func ModifyManualClassifyName(classifyName string, classifyId, parentId int) (err error) {
  155. o := global.DbMap[utils.DbNameManualIndex]
  156. updateStr := ` classify_name=? `
  157. pars := []interface{}{classifyName}
  158. if parentId > 0 {
  159. updateStr += ` , parent_id=? `
  160. pars = append(pars, parentId)
  161. }
  162. sql := `UPDATE edbdata_classify SET ` + updateStr + ` WHERE classify_id=? `
  163. pars = append(pars, classifyId)
  164. err = o.Exec(sql, pars...).Error
  165. return
  166. }
  167. func GetManualClassifyAllByCondition(condition string) (items []*EdbdataClassifyList, err error) {
  168. o := global.DbMap[utils.DbNameManualIndex]
  169. sql := ` SELECT classify_id,classify_name,parent_id FROM edbdata_classify WHERE is_show=1 `
  170. if condition != "" {
  171. sql += condition
  172. }
  173. sql += ` ORDER BY sort ASC `
  174. err = o.Raw(sql).Find(&items).Error
  175. return
  176. }
  177. // GetChildManualClassifyCount 根据分类id获取下面存在的子分类数量(展示的)
  178. func GetChildManualClassifyCount(parentId int) (count int, err error) {
  179. o := global.DbMap[utils.DbNameManualIndex]
  180. sql := `SELECT COUNT(1) AS count FROM edbdata_classify WHERE parent_id=? AND is_show = 1`
  181. err = o.Raw(sql, parentId).Scan(&count).Error
  182. return
  183. }