edb_monitor_classify.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package edbmonitor
  2. import (
  3. "eta/eta_api/utils"
  4. "fmt"
  5. "time"
  6. "github.com/beego/beego/v2/client/orm"
  7. )
  8. type EdbMonitorClassify struct {
  9. ClassifyId int `orm:"column(classify_id);pk"`
  10. ClassifyName string `description:"分类名称"`
  11. Level int `description:"分类层级"`
  12. ParentId int `description:"父分类ID"`
  13. RootId int `description:"根分类ID"`
  14. Sort int `description:"排序"`
  15. CreateTime time.Time `description:"创建时间"`
  16. }
  17. func (e *EdbMonitorClassify) Insert() (id int64, err error) {
  18. o := orm.NewOrmUsingDB("data")
  19. var edbMonitorIds []int
  20. if e.ParentId != 0 {
  21. sql := `SELECT edb_monitor_id FROM edb_monitor_info WHERE edb_monitor_classify_id = ?`
  22. _, err = o.Raw(sql, e.ParentId).QueryRows(&edbMonitorIds)
  23. if err != nil {
  24. return
  25. }
  26. }
  27. tx, err := o.Begin()
  28. if err != nil {
  29. return
  30. }
  31. defer func() {
  32. if err != nil {
  33. tx.Rollback()
  34. } else {
  35. tx.Commit()
  36. }
  37. }()
  38. id, err = tx.Insert(e)
  39. if len(edbMonitorIds) > 0 {
  40. sql := `UPDATE edb_monitor_info SET edb_monitor_classify_id = ? WHERE edb_monitor_id IN (` + utils.GetOrmInReplace(len(edbMonitorIds)) + `)`
  41. _, err = tx.Raw(sql, id, edbMonitorIds).Exec()
  42. if err != nil {
  43. return
  44. }
  45. }
  46. return
  47. }
  48. func (e *EdbMonitorClassify) Update(cols []string, classifyId []int) (err error) {
  49. o := orm.NewOrmUsingDB("data")
  50. var edbMonitorIds []int
  51. if e.ParentId != 0 {
  52. sql := `SELECT edb_monitor_id FROM edb_monitor_info WHERE edb_monitor_classify_id = ?`
  53. _, err = o.Raw(sql, e.ParentId).QueryRows(&edbMonitorIds)
  54. if err != nil {
  55. return
  56. }
  57. }
  58. tx, err := o.Begin()
  59. if err != nil {
  60. return
  61. }
  62. defer func() {
  63. if err != nil {
  64. tx.Rollback()
  65. } else {
  66. tx.Commit()
  67. }
  68. }()
  69. _, err = tx.Update(e, cols...)
  70. if err != nil {
  71. return
  72. }
  73. if len(classifyId) > 0 {
  74. sql := `UPDATE edb_monitor_classify SET root_id = ? WHERE classify_id IN (` + utils.GetOrmInReplace(len(classifyId)) + `)`
  75. _, err = tx.Raw(sql, e.RootId, classifyId).Exec()
  76. if err != nil {
  77. return
  78. }
  79. }
  80. if len(edbMonitorIds) > 0 {
  81. sql := `UPDATE edb_monitor_info SET edb_monitor_classify_id = ? WHERE edb_monitor_id IN (` + utils.GetOrmInReplace(len(edbMonitorIds)) + `)`
  82. _, err = tx.Raw(sql, e.ClassifyId, edbMonitorIds).Exec()
  83. if err != nil {
  84. return
  85. }
  86. }
  87. return
  88. }
  89. func DeleteEdbMonitorClassifyById(id int) (err error) {
  90. o := orm.NewOrmUsingDB("data")
  91. _, err = o.Raw("DELETE FROM edb_monitor_classify WHERE classify_id =?", id).Exec()
  92. return
  93. }
  94. func DeleteEdbMonitorClassifyByIdList(ids []int) (err error) {
  95. if len(ids) == 0 {
  96. return
  97. }
  98. o := orm.NewOrmUsingDB("data")
  99. sql := "DELETE FROM edb_monitor_classify WHERE classify_id IN (" + utils.GetOrmInReplace(len(ids)) + ")"
  100. _, err = o.Raw(sql, ids).Exec()
  101. return
  102. }
  103. func GetEdbMonitorClassifyList() (items []*EdbMonitorClassify, err error) {
  104. o := orm.NewOrmUsingDB("data")
  105. sql := "SELECT * FROM edb_monitor_classify ORDER BY sort ASC"
  106. _, err = o.Raw(sql).QueryRows(&items)
  107. return
  108. }
  109. func GetEdbMonitorClassifyById(id int) (item *EdbMonitorClassify, err error) {
  110. o := orm.NewOrmUsingDB("data")
  111. sql := "SELECT * FROM edb_monitor_classify WHERE classify_id =?"
  112. err = o.Raw(sql, id).QueryRow(&item)
  113. return
  114. }
  115. func GetEdbMonitorClassifyMaxSortByParentId(parentId int) (sort int, err error) {
  116. o := orm.NewOrmUsingDB("data")
  117. sql := "SELECT MAX(sort) FROM edb_monitor_classify WHERE parent_id =?"
  118. err = o.Raw(sql, parentId).QueryRow(&sort)
  119. return
  120. }
  121. func GetChildEdbMonitorClassifyIdById(id int) (classifyId []int, err error) {
  122. o := orm.NewOrmUsingDB("data")
  123. sql := "SELECT classify_id FROM edb_monitor_classify WHERE parent_id =?"
  124. _, err = o.Raw(sql, id).QueryRows(&classifyId)
  125. return
  126. }
  127. func GetChildEdbMonitorClassifyById(id int) (items []*EdbMonitorClassify, err error) {
  128. o := orm.NewOrmUsingDB("data")
  129. sql := "SELECT * FROM edb_monitor_classify WHERE parent_id =?"
  130. _, err = o.Raw(sql, id).QueryRows(&items)
  131. return
  132. }
  133. func GetChildEdbMonitorClassifyByRootId(rootId int) (items []*EdbMonitorClassify, err error) {
  134. o := orm.NewOrmUsingDB("data")
  135. sql := "SELECT * FROM edb_monitor_classify WHERE root_id =?"
  136. _, err = o.Raw(sql, rootId).QueryRows(&items)
  137. return
  138. }
  139. func GetEdbMonitorClassifyCountByIdList(ids []int) (count int, err error) {
  140. if len(ids) == 0 {
  141. return
  142. }
  143. o := orm.NewOrmUsingDB("data")
  144. sql := `SELECT COUNT(*) FROM edb_monitor_classify WHERE classify_id IN (` + utils.GetOrmInReplace(len(ids)) + `)`
  145. err = o.Raw(sql, ids).QueryRow(&count)
  146. return
  147. }
  148. // GetEdbMonitorClassifyByParentIdAndName 根据父级ID和名称获取分类
  149. func GetEdbMonitorClassifyByParentIdAndName(parentId int, classifyName string, classifyId int) (item *EdbMonitorClassify, err error) {
  150. o := orm.NewOrmUsingDB("data")
  151. sql := `SELECT * FROM edb_monitor_classify WHERE parent_id = ? AND classify_name = ? AND classify_id <> ? LIMIT 1`
  152. err = o.Raw(sql, parentId, classifyName, classifyId).QueryRow(&item)
  153. return
  154. }
  155. // UpdateEdbMonitorClassifySortByParentId 根据分类父类id更新排序
  156. func UpdateEdbMonitorClassifySortByParentId(parentId, classifyId, nowSort int, updateSort string) (err error) {
  157. o := orm.NewOrmUsingDB("data")
  158. sql := ` update edb_monitor_classify set sort = ` + updateSort + ` WHERE parent_id=? and sort > ? `
  159. if classifyId > 0 {
  160. sql += ` or ( classify_id > ` + fmt.Sprint(classifyId) + ` and sort= ` + fmt.Sprint(nowSort) + `)`
  161. }
  162. _, err = o.Raw(sql, parentId, nowSort).Exec()
  163. return
  164. }
  165. // GetFirstEdbMonitorClassifyByParentId 获取当前父级图表分类下的排序第一条的数据
  166. func GetFirstEdbMonitorClassifyByParentId(parentId int) (item *EdbMonitorClassify, err error) {
  167. o := orm.NewOrmUsingDB("data")
  168. sql := ` SELECT * FROM edb_monitor_classify WHERE parent_id=? order by sort asc,classify_id asc limit 1`
  169. err = o.Raw(sql, parentId).QueryRow(&item)
  170. return
  171. }