edb_classify.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. ClassifyNameEn string
  123. ParentId int
  124. Level int `description:"层级"`
  125. Sort int `description:"排序字段,越小越靠前,默认值:10"`
  126. UniqueCode string `description:"唯一编码"`
  127. Source int `description:"来源id"`
  128. SourceName string `description:"来源名称"`
  129. SysUserId int `description:"创建人id"`
  130. SysUserRealName string `description:"创建人姓名"`
  131. StartDate string
  132. EdbCode string
  133. EdbType int `description:"指标类型:1:基础指标,2:计算指标"`
  134. Children []*EdbClassifyItems
  135. Button EdbClassifyItemsButton `description:"操作权限"`
  136. }
  137. // EdbClassifyItemsButton 操作按钮
  138. type EdbClassifyItemsButton struct {
  139. AddButton bool `description:"是否可添加"`
  140. OpButton bool `description:"是否可编辑"`
  141. DeleteButton bool `description:"是否可删除"`
  142. MoveButton bool `description:"是否可移动"`
  143. ShowEdbRelation bool `description:"是否展示关联指标"`
  144. ShowChartRelation bool `description:"是否展示关联图表"`
  145. InsertNewDataButton bool `description:"是否可插入最新值"`
  146. }
  147. type EdbClassifyListResp struct {
  148. AllNodes []*EdbClassifyItems
  149. CanOpClassify bool `description:"是否允许操作分类"`
  150. Language string `description:"指标的展示语言,CN:中文,EN:英文"`
  151. }
  152. type ClassifyDeleteCheckResp struct {
  153. DeleteStatus int `description:"检测状态:0:默认值,如果为0,继续走其他校验,1:若目录关联指标不可删除,2:确认删除当前目录及包含的子目录吗,3:当前指标已用作画图,不可删除"`
  154. TipsMsg string `description:"提示信息"`
  155. }
  156. type ClassifyDeleteCheckReq struct {
  157. ClassifyId int `description:"分类id"`
  158. EdbInfoId int `description:"指标id"`
  159. }
  160. func GetClassifyCountByClassifyId(classifyId int) (count int, err error) {
  161. o := orm.NewOrmUsingDB("data")
  162. sql := ` SELECT COUNT(1) AS count FROM (
  163. SELECT rd.*
  164. FROM (SELECT * FROM edb_classify WHERE parent_id IS NOT NULL) rd,
  165. (SELECT @pid := ?) pd
  166. WHERE FIND_IN_SET(parent_id, @pid) > 0
  167. AND @pid := CONCAT(@pid, ',', classify_id)
  168. UNION SELECT * FROM edb_classify WHERE classify_id = @pid
  169. )AS t
  170. WHERE t.classify_id<>? `
  171. err = o.Raw(sql, classifyId, classifyId).QueryRow(&count)
  172. return
  173. }
  174. type EdbClassifySimplify struct {
  175. ClassifyId int `description:"分类id"`
  176. ClassifyName string `description:"分类名称"`
  177. ParentId int
  178. }
  179. // 递归获取所有目录
  180. func GetClassifyAllByClassifyId(classifyId int) (resultList []*EdbClassifySimplify, err error) {
  181. fmt.Println("185")
  182. o := orm.NewOrmUsingDB("data")
  183. for {
  184. sql := ` SELECT * FROM edb_classify AS a
  185. WHERE a.classify_id=? order by sort asc,classify_id asc`
  186. item := new(EdbClassifySimplify)
  187. err = o.Raw(sql, classifyId).QueryRow(&item)
  188. if err != nil {
  189. break
  190. }
  191. if item.ClassifyId > 0 {
  192. resultList = append(resultList, item)
  193. classifyId = item.ParentId
  194. } else {
  195. break
  196. }
  197. }
  198. return resultList, err
  199. }
  200. // MoveEdbClassifyReq 移动分类请求参数
  201. type MoveEdbClassifyReq struct {
  202. ClassifyId int `description:"分类id"`
  203. ParentClassifyId int `description:"父级分类id"`
  204. PrevClassifyId int `description:"上一个兄弟节点分类id"`
  205. NextClassifyId int `description:"下一个兄弟节点分类id"`
  206. }
  207. // GetFirstEdbClassifyByParentId 获取当前父级分类下,且排序数相同 的排序第一条的数据
  208. func GetFirstEdbClassifyByParentId(parentId int) (item *EdbClassify, err error) {
  209. o := orm.NewOrmUsingDB("data")
  210. sql := ` SELECT * FROM edb_classify WHERE parent_id=? order by sort asc,classify_id asc limit 1`
  211. err = o.Raw(sql, parentId).QueryRow(&item)
  212. return
  213. }
  214. // UpdateEdbClassifySortByParentId 根据父类id更新排序
  215. func UpdateEdbClassifySortByParentId(parentId, classifyId, nowSort int, updateSort string, classifyType uint8) (err error) {
  216. o := orm.NewOrmUsingDB("data")
  217. sql := ` update edb_classify set sort = ` + updateSort + ` WHERE parent_id=? AND sort > ? AND classify_type = ? `
  218. if classifyId > 0 {
  219. sql += ` or ( classify_id > ` + fmt.Sprint(classifyId) + ` and sort = ` + fmt.Sprint(nowSort) + `)`
  220. }
  221. _, err = o.Raw(sql, parentId, nowSort, classifyType).Exec()
  222. return
  223. }
  224. // Update 更新分类基础信息
  225. func (edbClassify *EdbClassify) Update(cols []string) (err error) {
  226. o := orm.NewOrmUsingDB("data")
  227. _, err = o.Update(edbClassify, cols...)
  228. return
  229. }
  230. // GetEdbClassifyMaxSort 获取分类下最大的排序数
  231. func GetEdbClassifyMaxSort(parentId int, classifyType uint8) (sort int, err error) {
  232. o := orm.NewOrmUsingDB("data")
  233. sql := `SELECT Max(sort) AS sort FROM edb_classify WHERE parent_id=? AND classify_type=? `
  234. err = o.Raw(sql, parentId, classifyType).QueryRow(&sort)
  235. return
  236. }
  237. type ClassifyEdbInfoListResp struct {
  238. EdbInfoList []*EdbClassifyItems
  239. }