manual_classify.go 5.6 KB

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