edb_classify.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. package data_manage
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "time"
  6. )
  7. type EdbClassify struct {
  8. ClassifyId int `orm:"column(classify_id);pk"`
  9. ClassifyType uint8 `description:"分类类型,0:普通指标分类,1:预测指标分类"`
  10. ClassifyName string `description:"分类名称"`
  11. ParentId int `description:"父级id"`
  12. HasData int `description:"是否含有指标数据"`
  13. CreateTime time.Time `description:"创建时间"`
  14. ModifyTime time.Time `description:"修改时间"`
  15. SysUserId int `description:"创建人id"`
  16. SysUserRealName string `description:"创建人姓名"`
  17. Level int `description:"层级"`
  18. UniqueCode string `description:"唯一编码"`
  19. Sort int `description:"排序字段,越小越靠前,默认值:10"`
  20. }
  21. func AddEdbClassify(item *EdbClassify) (lastId int64, err error) {
  22. o := orm.NewOrmUsingDB("data")
  23. lastId, err = o.Insert(item)
  24. return
  25. }
  26. type AddEdbClassifyReq struct {
  27. ClassifyName string `description:"分类名称"`
  28. ParentId int `description:"父级id,第一级传0"`
  29. Level int `description:"层级,第一级传0,其余传上一级的层级"`
  30. ClassifyType uint8 `description:"分类类型,0:普通指标分类,1:预测指标分类"`
  31. }
  32. func GetEdbClassifyCount(classifyName string, parentId int, classifyType uint8) (count int, err error) {
  33. o := orm.NewOrmUsingDB("data")
  34. sql := `SELECT COUNT(1) AS count FROM edb_classify WHERE parent_id=? AND classify_name=? AND classify_type = ? `
  35. err = o.Raw(sql, parentId, classifyName, classifyType).QueryRow(&count)
  36. return
  37. }
  38. type EditEdbClassifyReq struct {
  39. ClassifyName string `description:"分类名称"`
  40. ClassifyId int `description:"分类名称"`
  41. }
  42. func GetEdbClassifyById(classifyId int) (item *EdbClassify, err error) {
  43. o := orm.NewOrmUsingDB("data")
  44. sql := `SELECT * FROM edb_classify WHERE classify_id=? `
  45. err = o.Raw(sql, classifyId).QueryRow(&item)
  46. return
  47. }
  48. func GetEdbClassifyCountById(classifyId int) (count int, err error) {
  49. o := orm.NewOrmUsingDB("data")
  50. sql := `SELECT count(1) AS count FROM edb_classify WHERE classify_id=? `
  51. err = o.Raw(sql, classifyId).QueryRow(&count)
  52. return
  53. }
  54. func EditEdbClassify(classifyId int, classifyName string) (err error) {
  55. o := orm.NewOrmUsingDB("data")
  56. sql := `UPDATE edb_classify SET classify_name=?,modify_time=NOW() WHERE classify_id=? `
  57. _, err = o.Raw(sql, classifyName, classifyId).Exec()
  58. return
  59. }
  60. type DeleteEdbClassifyReq struct {
  61. ClassifyId int `description:"分类id"`
  62. EdbInfoId int `description:"指标id"`
  63. }
  64. func GetEdbInfoCountByClassifyId(classifyId int) (count int, err error) {
  65. o := orm.NewOrmUsingDB("data")
  66. sql := ` SELECT COUNT(1) AS count FROM edb_info AS a
  67. WHERE a.classify_id IN(
  68. SELECT t.classify_id FROM
  69. (
  70. SELECT rd.*
  71. FROM (SELECT * FROM edb_classify WHERE parent_id IS NOT NULL) rd,
  72. (SELECT @pid := ?) pd
  73. WHERE FIND_IN_SET(parent_id, @pid) > 0
  74. AND @pid := CONCAT(@pid, ',', classify_id)
  75. UNION SELECT * FROM edb_classify WHERE classify_id = @pid
  76. )AS t
  77. ) `
  78. err = o.Raw(sql, classifyId).QueryRow(&count)
  79. return
  80. }
  81. func DeleteEdbClassify(classifyId int) (err error) {
  82. o := orm.NewOrmUsingDB("data")
  83. sql := ` DELETE FROM edb_classify
  84. WHERE classify_id IN(
  85. SELECT t.classify_id FROM
  86. (
  87. SELECT rd.*
  88. FROM (SELECT * FROM edb_classify WHERE parent_id IS NOT NULL) rd,
  89. (SELECT @pid := ?) pd
  90. WHERE FIND_IN_SET(parent_id, @pid) > 0
  91. AND @pid := CONCAT(@pid, ',', classify_id)
  92. UNION SELECT * FROM edb_classify WHERE classify_id = @pid
  93. )AS t
  94. ) `
  95. _, err = o.Raw(sql, classifyId).Exec()
  96. return
  97. }
  98. func GetEdbClassifyByParentId(parentId int, classifyType int8) (items []*EdbClassifyItems, err error) {
  99. o := orm.NewOrmUsingDB("data")
  100. sql := ` SELECT * FROM edb_classify WHERE parent_id=? AND classify_type = ? order by sort asc,classify_id asc`
  101. _, err = o.Raw(sql, parentId, classifyType).QueryRows(&items)
  102. return
  103. }
  104. // GetEdbClassifyAll 获取普通指标的分类列表
  105. func GetEdbClassifyAll() (items []*EdbClassifyItems, err error) {
  106. o := orm.NewOrmUsingDB("data")
  107. sql := ` SELECT * FROM edb_classify WHERE parent_id<>0 AND classify_type = 0 order by sort asc,classify_id asc`
  108. _, err = o.Raw(sql).QueryRows(&items)
  109. return
  110. }
  111. // GetPredictEdbClassifyAll 获取预测指标的分类列表
  112. func GetPredictEdbClassifyAll() (items []*EdbClassifyItems, err error) {
  113. o := orm.NewOrmUsingDB("data")
  114. sql := ` SELECT * FROM edb_classify WHERE classify_type = 1 order by sort asc,classify_id asc`
  115. _, err = o.Raw(sql).QueryRows(&items)
  116. return
  117. }
  118. type EdbClassifyItems struct {
  119. ClassifyId int `description:"分类id"`
  120. EdbInfoId int `description:"指标id"`
  121. ClassifyName string
  122. ParentId int
  123. Level int `description:"层级"`
  124. Sort int `description:"排序字段,越小越靠前,默认值:10"`
  125. UniqueCode string `description:"唯一编码"`
  126. Source int `description:"来源id"`
  127. SourceName string `description:"来源名称"`
  128. SysUserId int `description:"创建人id"`
  129. SysUserRealName string `description:"创建人姓名"`
  130. StartDate string
  131. EdbCode string
  132. EdbType int `description:"指标类型:1:基础指标,2:计算指标"`
  133. Children []*EdbClassifyItems
  134. Button EdbClassifyItemsButton `description:"操作权限"`
  135. }
  136. // EdbClassifyItemsButton 操作按钮
  137. type EdbClassifyItemsButton struct {
  138. AddButton bool `description:"是否可添加"`
  139. OpButton bool `description:"是否可编辑"`
  140. DeleteButton bool `description:"是否可删除"`
  141. MoveButton bool `description:"是否可移动"`
  142. ShowEdbRelation bool `description:"是否展示关联指标"`
  143. ShowChartRelation bool `description:"是否展示关联图表"`
  144. InsertNewDataButton bool `description:"是否可插入最新值"`
  145. }
  146. type EdbClassifyListResp struct {
  147. AllNodes []*EdbClassifyItems
  148. CanOpClassify bool `description:"是否允许操作分类"`
  149. }
  150. type ClassifyDeleteCheckResp struct {
  151. DeleteStatus int `description:"检测状态:0:默认值,如果为0,继续走其他校验,1:若目录关联指标不可删除,2:确认删除当前目录及包含的子目录吗,3:当前指标已用作画图,不可删除"`
  152. TipsMsg string `description:"提示信息"`
  153. }
  154. type ClassifyDeleteCheckReq struct {
  155. ClassifyId int `description:"分类id"`
  156. EdbInfoId int `description:"指标id"`
  157. }
  158. func GetClassifyCountByClassifyId(classifyId int) (count int, err error) {
  159. o := orm.NewOrmUsingDB("data")
  160. sql := ` SELECT COUNT(1) AS count FROM (
  161. SELECT rd.*
  162. FROM (SELECT * FROM edb_classify WHERE parent_id IS NOT NULL) rd,
  163. (SELECT @pid := ?) pd
  164. WHERE FIND_IN_SET(parent_id, @pid) > 0
  165. AND @pid := CONCAT(@pid, ',', classify_id)
  166. UNION SELECT * FROM edb_classify WHERE classify_id = @pid
  167. )AS t
  168. WHERE t.classify_id<>? `
  169. err = o.Raw(sql, classifyId, classifyId).QueryRow(&count)
  170. return
  171. }
  172. type EdbClassifySimplify struct {
  173. ClassifyId int `description:"分类id"`
  174. ClassifyName string `description:"分类名称"`
  175. ParentId int
  176. }
  177. // 递归获取所有目录
  178. func GetClassifyAllByClassifyId(classifyId int) (resultList []*EdbClassifySimplify, err error) {
  179. fmt.Println("185")
  180. o := orm.NewOrmUsingDB("data")
  181. for {
  182. sql := ` SELECT * FROM edb_classify AS a
  183. WHERE a.classify_id=? order by sort asc,classify_id asc`
  184. item := new(EdbClassifySimplify)
  185. err = o.Raw(sql, classifyId).QueryRow(&item)
  186. if err != nil {
  187. break
  188. }
  189. if item.ClassifyId > 0 {
  190. resultList = append(resultList, item)
  191. classifyId = item.ParentId
  192. } else {
  193. break
  194. }
  195. }
  196. return resultList, err
  197. }
  198. // MoveEdbClassifyReq 移动分类请求参数
  199. type MoveEdbClassifyReq struct {
  200. ClassifyId int `description:"分类id"`
  201. ParentClassifyId int `description:"父级分类id"`
  202. PrevClassifyId int `description:"上一个兄弟节点分类id"`
  203. NextClassifyId int `description:"下一个兄弟节点分类id"`
  204. }
  205. // GetFirstEdbClassifyByParentId 获取当前父级分类下,且排序数相同 的排序第一条的数据
  206. func GetFirstEdbClassifyByParentId(parentId int) (item *EdbClassify, err error) {
  207. o := orm.NewOrmUsingDB("data")
  208. sql := ` SELECT * FROM edb_classify WHERE parent_id=? order by sort asc,classify_id asc limit 1`
  209. err = o.Raw(sql, parentId).QueryRow(&item)
  210. return
  211. }
  212. // UpdateEdbClassifySortByParentId 根据父类id更新排序
  213. func UpdateEdbClassifySortByParentId(parentId, classifyId, nowSort int, updateSort string, classifyType uint8) (err error) {
  214. o := orm.NewOrmUsingDB("data")
  215. sql := ` update edb_classify set sort = ` + updateSort + ` WHERE parent_id=? AND sort > ? AND classify_type = ? `
  216. if classifyId > 0 {
  217. sql += ` or ( classify_id > ` + fmt.Sprint(classifyId) + ` and sort = ` + fmt.Sprint(nowSort) + `)`
  218. }
  219. _, err = o.Raw(sql, parentId, nowSort, classifyType).Exec()
  220. return
  221. }
  222. // Update 更新分类基础信息
  223. func (edbClassify *EdbClassify) Update(cols []string) (err error) {
  224. o := orm.NewOrmUsingDB("data")
  225. _, err = o.Update(edbClassify, cols...)
  226. return
  227. }
  228. // GetEdbClassifyMaxSort 获取分类下最大的排序数
  229. func GetEdbClassifyMaxSort(parentId int, classifyType uint8) (sort int, err error) {
  230. o := orm.NewOrmUsingDB("data")
  231. sql := `SELECT Max(sort) AS sort FROM edb_classify WHERE parent_id=? AND classify_type=? `
  232. err = o.Raw(sql, parentId, classifyType).QueryRow(&sort)
  233. return
  234. }
  235. type ClassifyEdbInfoListResp struct {
  236. EdbInfoList []*EdbClassifyItems
  237. }