manual_classify.go 6.0 KB

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