edb_classify.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package models
  2. import (
  3. "errors"
  4. "eta/eta_index_lib/utils"
  5. "github.com/beego/beego/v2/client/orm"
  6. "strconv"
  7. "time"
  8. )
  9. type EdbClassify struct {
  10. ClassifyId int `orm:"column(classify_id);pk"`
  11. ClassifyType uint8 `description:"分类类型,0:普通指标分类,1:预测指标分类"`
  12. ClassifyName string `description:"分类名称"`
  13. ParentId int `description:"父级id"`
  14. RootId int `description:"顶级id"`
  15. HasData int `description:"是否含有指标数据"`
  16. CreateTime time.Time `description:"创建时间"`
  17. ModifyTime time.Time `description:"修改时间"`
  18. SysUserId int `description:"创建人id"`
  19. SysUserRealName string `description:"创建人姓名"`
  20. Level int `description:"层级"`
  21. UniqueCode string `description:"唯一编码"`
  22. Sort int `description:"排序字段,越小越靠前,默认值:10"`
  23. }
  24. func AddEdbClassify(item *EdbClassify) (lastId int64, err error) {
  25. o := orm.NewOrm()
  26. lastId, err = o.Insert(item)
  27. return
  28. }
  29. // Update 更新分类基础信息
  30. func (edbClassify *EdbClassify) Update(cols []string) (err error) {
  31. o := orm.NewOrm()
  32. _, err = o.Update(edbClassify, cols...)
  33. return
  34. }
  35. func GetEdbClassifyById(classifyId int) (item *EdbClassify, err error) {
  36. o := orm.NewOrm()
  37. sql := `SELECT * FROM edb_classify WHERE classify_id=? `
  38. err = o.Raw(sql, classifyId).QueryRow(&item)
  39. return
  40. }
  41. type EdbClassifySimplify struct {
  42. ClassifyId int `description:"分类id"`
  43. ClassifyName string `description:"分类名称"`
  44. ParentId int
  45. }
  46. // 递归获取所有目录
  47. func GetClassifyAllByClassifyId(classifyId int) (resultList []*EdbClassifySimplify, err error) {
  48. o := orm.NewOrm()
  49. for {
  50. sql := ` SELECT * FROM edb_classify AS a WHERE a.classify_id=? order by sort asc,classify_id asc`
  51. item := new(EdbClassifySimplify)
  52. err = o.Raw(sql, classifyId).QueryRow(&item)
  53. if err != nil {
  54. break
  55. }
  56. if item.ClassifyId > 0 {
  57. resultList = append(resultList, item)
  58. classifyId = item.ParentId
  59. } else {
  60. break
  61. }
  62. }
  63. return resultList, err
  64. }
  65. // 判断分类名称是否存在
  66. func GetEdbClassifyCount(classifyName string, parentId int, classifyType uint8) (item *EdbClassify, err error) {
  67. o := orm.NewOrm()
  68. sql := `SELECT * FROM edb_classify WHERE parent_id=? AND classify_name=? AND classify_type = ? `
  69. err = o.Raw(sql, parentId, classifyName, classifyType).QueryRow(&item)
  70. return
  71. }
  72. // GetEdbClassifyMaxSort 获取分类下最大的排序数
  73. func GetEdbClassifyMaxSort(parentId int, classifyType uint8) (sort int, err error) {
  74. o := orm.NewOrm()
  75. sql := `SELECT Max(sort) AS sort FROM edb_classify WHERE parent_id=? AND classify_type=? `
  76. err = o.Raw(sql, parentId, classifyType).QueryRow(&sort)
  77. return
  78. }
  79. type AddEdbClassifyReq struct {
  80. ClassifyName string `description:"分类名称"`
  81. ParentId int `description:"父级id,第一级传0"`
  82. Level int `description:"层级,第一级传0,其余传上一级的层级"`
  83. ClassifyType uint8 `description:"分类类型,0:普通指标分类,1:预测指标分类"`
  84. }
  85. // AddEdbClassify 添加指标分类
  86. func SaveEdbClassify(classifyName string, parentId, level int, classifyType uint8, sysUserId, sysUserName string) (classifyInfo *EdbClassify, err error, errMsg string) {
  87. edbClassify, err := GetEdbClassifyCount(classifyName, parentId, classifyType)
  88. var isAdd bool
  89. if err != nil {
  90. if err.Error() == utils.ErrNoRow() {
  91. isAdd = true
  92. } else {
  93. errMsg = `判断名称是否已存在失败`
  94. return
  95. }
  96. }
  97. if edbClassify != nil && edbClassify.ClassifyId > 0 {
  98. classifyInfo = edbClassify
  99. return edbClassify, nil, ""
  100. } else {
  101. isAdd = true
  102. }
  103. if isAdd {
  104. sysUserIdInt, _ := strconv.Atoi(sysUserId)
  105. //获取该层级下最大的排序数
  106. maxSort, e := GetEdbAndClassifyMaxSort(parentId, classifyType)
  107. if e != nil {
  108. errMsg = "查询分类信息出错" + e.Error()
  109. err = errors.New(errMsg)
  110. return
  111. }
  112. rootId := 0
  113. if parentId > 0 {
  114. parentClassify, tErr := GetEdbClassifyById(parentId)
  115. if tErr != nil {
  116. if tErr.Error() == utils.ErrNoRow() {
  117. errMsg = "父级分类不存在"
  118. err = errors.New(errMsg)
  119. return
  120. }
  121. errMsg = "获取失败"
  122. err = errors.New("获取分类信息失败,Err:" + tErr.Error())
  123. return
  124. }
  125. rootId = parentClassify.RootId
  126. }
  127. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  128. classifyInfo = &EdbClassify{
  129. //ClassifyId: 0,
  130. ClassifyType: classifyType,
  131. ClassifyName: classifyName,
  132. ParentId: parentId,
  133. RootId: rootId,
  134. HasData: 0,
  135. CreateTime: time.Now(),
  136. ModifyTime: time.Now(),
  137. SysUserId: sysUserIdInt,
  138. SysUserRealName: sysUserName,
  139. Level: level + 1,
  140. UniqueCode: utils.MD5(utils.DATA_PREFIX + "_" + timestamp),
  141. Sort: maxSort + 1,
  142. }
  143. var classifyId int64
  144. classifyId, err = AddEdbClassify(classifyInfo)
  145. if err != nil {
  146. errMsg = "保存分类失败"
  147. return
  148. }
  149. classifyInfo.ClassifyId = int(classifyId)
  150. if parentId == 0 { //一级目录的rootId等于自己本身
  151. classifyInfo.RootId = int(classifyId)
  152. err = classifyInfo.Update([]string{"RootId"})
  153. if err != nil {
  154. errMsg = "更新分类失败"
  155. return
  156. }
  157. }
  158. }
  159. return
  160. }