sand_box_classify.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package sandbox
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "time"
  6. )
  7. type SandboxClassify struct {
  8. SandboxClassifyId int `orm:"column(sandbox_classify_id);pk"`
  9. SandboxClassifyName string `description:"分类名称"`
  10. ParentId int `description:"父级id"`
  11. HasData int `description:"是否含有指标数据"`
  12. CreateTime time.Time `description:"创建时间"`
  13. ModifyTime time.Time `description:"修改时间"`
  14. SysUserId int `description:"创建人id"`
  15. SysUserRealName string `description:"创建人姓名"`
  16. Level int `description:"层级"`
  17. Sort int `description:"排序字段,越小越靠前,默认值:10"`
  18. }
  19. func AddSandboxClassify(item *SandboxClassify) (lastId int64, err error) {
  20. o := orm.NewOrmUsingDB("data")
  21. lastId, err = o.Insert(item)
  22. return
  23. }
  24. // GetSandboxClassifyByParentId
  25. func GetSandboxClassifyByParentId(parentId int) (items []*SandboxClassifyItems, err error) {
  26. o := orm.NewOrmUsingDB("data")
  27. sql := ` SELECT * FROM sandbox_classify WHERE parent_id=? order by sort asc,sandbox_classify_id asc`
  28. _, err = o.Raw(sql, parentId).QueryRows(&items)
  29. return
  30. }
  31. // GetSandboxClassifyAll
  32. func GetSandboxClassifyAll() (items []*SandboxClassifyItems, err error) {
  33. o := orm.NewOrmUsingDB("data")
  34. sql := ` SELECT * FROM sandbox_classify WHERE parent_id<>0 order by sort asc,sandbox_classify_id asc`
  35. _, err = o.Raw(sql).QueryRows(&items)
  36. return
  37. }
  38. type SandboxClassifyItems struct {
  39. SandboxClassifyId int `orm:"column(sandbox_classify_id);pk"`
  40. SandboxClassifyName string `description:"分类名称"`
  41. ParentId int `description:"父级id"`
  42. HasData int `description:"是否含有指标数据"`
  43. CreateTime time.Time `description:"创建时间"`
  44. ModifyTime time.Time `description:"修改时间"`
  45. SysUserId int `description:"创建人id"`
  46. SysUserRealName string `description:"创建人姓名"`
  47. Level int `description:"层级"`
  48. Sort int `description:"排序字段,越小越靠前,默认值:10"`
  49. SandboxId int `description:"沙盘id"`
  50. Children []*SandboxClassifyItems
  51. }
  52. type SandboxClassifyListResp struct {
  53. AllNodes []*SandboxClassifyItems
  54. }
  55. type AddSandboxClassifyReq struct {
  56. SandboxClassifyName string `description:"分类名称"`
  57. ParentId int `description:"父级id,第一级传0"`
  58. Level int `description:"层级,第一级传0,其余传上一级的层级"`
  59. }
  60. func GetSandboxClassifyCount(sandboxClassifyName string, parentId int) (count int, err error) {
  61. o := orm.NewOrmUsingDB("data")
  62. sql := `SELECT COUNT(1) AS count FROM sandbox_classify WHERE parent_id=? AND sandbox_classify_name=? `
  63. err = o.Raw(sql, parentId, sandboxClassifyName).QueryRow(&count)
  64. return
  65. }
  66. // GetSandboxClassifyMaxSort 获取沙盘分类下最大的排序数
  67. func GetSandboxClassifyMaxSort(parentId int) (sort int, err error) {
  68. o := orm.NewOrmUsingDB("data")
  69. sql := `SELECT Max(sort) AS sort FROM sandbox_classify WHERE parent_id=? `
  70. err = o.Raw(sql, parentId).QueryRow(&sort)
  71. return
  72. }
  73. type EditSandboxClassifyReq struct {
  74. SandboxClassifyName string `description:"分类名称"`
  75. SandboxClassifyId int `description:"分类id"`
  76. }
  77. func GetSandboxClassifyById(classifyId int) (item *SandboxClassify, err error) {
  78. o := orm.NewOrmUsingDB("data")
  79. sql := `SELECT * FROM sandbox_classify WHERE sandbox_classify_id=? `
  80. err = o.Raw(sql, classifyId).QueryRow(&item)
  81. return
  82. }
  83. func EditSandboxClassify(classifyId int, sandboxClassifyName string) (err error) {
  84. o := orm.NewOrmUsingDB("data")
  85. sql := `UPDATE sandbox_classify SET sandbox_classify_name=?,modify_time=NOW() WHERE sandbox_classify_id=? `
  86. _, err = o.Raw(sql, sandboxClassifyName, classifyId).Exec()
  87. return
  88. }
  89. type SandboxClassifyDeleteCheckReq struct {
  90. SandboxClassifyId int `description:"分类id"`
  91. }
  92. func GetSandboxInfoCountByClassifyId(classifyId int) (count int, err error) {
  93. o := orm.NewOrmUsingDB("data")
  94. sql := ` SELECT COUNT(1) AS count FROM sandbox AS a
  95. WHERE a.sandbox_classify_id IN(
  96. SELECT t.sandbox_classify_id FROM
  97. (
  98. SELECT rd.*
  99. FROM (SELECT * FROM sandbox_classify WHERE parent_id IS NOT NULL) rd,
  100. (SELECT @pid := ?) pd
  101. WHERE FIND_IN_SET(parent_id, @pid) > 0
  102. AND @pid := CONCAT(@pid, ',', sandbox_classify_id)
  103. UNION SELECT * FROM sandbox_classify WHERE sandbox_classify_id = @pid
  104. )AS t
  105. ) `
  106. err = o.Raw(sql, classifyId).QueryRow(&count)
  107. return
  108. }
  109. type SandboxClassifyDeleteCheckResp struct {
  110. DeleteStatus int `description:"检测状态:0:默认值,如果为0,继续走其他校验,1:该分类下关联图表不可删除,2:确认删除当前目录及包含的子目录吗"`
  111. TipsMsg string `description:"提示信息"`
  112. }
  113. type DeleteSandboxClassifyReq struct {
  114. SandboxClassifyId int `description:"分类id"`
  115. SandboxId int `description:"指标id"`
  116. }
  117. func DeleteSandboxClassify(classifyId int) (err error) {
  118. o := orm.NewOrmUsingDB("data")
  119. sql := ` DELETE FROM sandbox_classify
  120. WHERE sandbox_classify_id IN(
  121. SELECT t.sandbox_classify_id FROM
  122. (
  123. SELECT rd.*
  124. FROM (SELECT * FROM sandbox_classify WHERE parent_id IS NOT NULL) rd,
  125. (SELECT @pid := ?) pd
  126. WHERE FIND_IN_SET(parent_id, @pid) > 0
  127. AND @pid := CONCAT(@pid, ',', sandbox_classify_id)
  128. UNION SELECT * FROM sandbox_classify WHERE sandbox_classify_id = @pid
  129. )AS t
  130. ) `
  131. _, err = o.Raw(sql, classifyId).Exec()
  132. return
  133. }
  134. // MoveSandboxClassifyReq 移动沙盘分类请求参数
  135. type MoveSandboxClassifyReq struct {
  136. ClassifyId int `description:"分类id"`
  137. ParentClassifyId int `description:"父级分类id"`
  138. PrevClassifyId int `description:"上一个兄弟节点分类id"`
  139. NextClassifyId int `description:"下一个兄弟节点分类id"`
  140. }
  141. // UpdateSandboxClassifySortByParentId 根据沙盘父类id更新排序
  142. func UpdateSandboxClassifySortByParentId(parentId, classifyId, nowSort int, updateSort string) (err error) {
  143. o := orm.NewOrmUsingDB("data")
  144. sql := ` update sandbox_classify set sort = ` + updateSort + ` WHERE parent_id=? and sort > ? `
  145. if classifyId > 0 {
  146. sql += ` or ( sandbox_classify_id > ` + fmt.Sprint(classifyId) + ` and sort= ` + fmt.Sprint(nowSort) + `)`
  147. }
  148. _, err = o.Raw(sql, parentId, nowSort).Exec()
  149. return
  150. }
  151. // GetFirstSandboxClassifyByParentId 获取当前父级沙盘分类下的排序第一条的数据
  152. func GetFirstSandboxClassifyByParentId(parentId int) (item *SandboxClassify, err error) {
  153. o := orm.NewOrmUsingDB("data")
  154. sql := ` SELECT * FROM sandbox_classify WHERE parent_id=? order by sort asc,sandbox_classify_id asc limit 1`
  155. err = o.Raw(sql, parentId).QueryRow(&item)
  156. return
  157. }
  158. // Update 更新沙盘分类基础信息
  159. func (sandboxClassify *SandboxClassify) Update(cols []string) (err error) {
  160. o := orm.NewOrmUsingDB("data")
  161. _, err = o.Update(sandboxClassify, cols...)
  162. return
  163. }