edb_classify.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package models
  2. import (
  3. "errors"
  4. "eta_gn/eta_index_lib/global"
  5. "eta_gn/eta_index_lib/utils"
  6. "strconv"
  7. "time"
  8. )
  9. type EdbClassify struct {
  10. ClassifyId int `gorm:"primaryKey;autoIncrement;column:classify_id"`
  11. ClassifyType uint8 `gorm:"column:classify_type" description:"分类类型,0:普通指标分类,1:预测指标分类"`
  12. ClassifyName string `gorm:"column:classify_name" description:"分类名称"`
  13. ParentId int `gorm:"column:parent_id" description:"父级id"`
  14. RootId int `gorm:"column:root_id" description:"顶级id"`
  15. HasData int `gorm:"column:has_data" description:"是否含有指标数据"`
  16. CreateTime time.Time `gorm:"column:create_time" description:"创建时间"`
  17. ModifyTime time.Time `gorm:"column:modify_time" description:"修改时间"`
  18. SysUserId int `gorm:"column:sys_user_id" description:"创建人id"`
  19. SysUserRealName string `gorm:"column:sys_user_real_name" description:"创建人姓名"`
  20. Level int `gorm:"column:level" description:"层级"`
  21. UniqueCode string `gorm:"column:unique_code" description:"唯一编码"`
  22. Sort int `gorm:"column:sort" description:"排序字段,越小越靠前,默认值:10"`
  23. }
  24. func AddEdbClassify(item *EdbClassify) (lastId int64, err error) {
  25. err = global.DEFAULT_DmSQL.Create(item).Error
  26. lastId = int64(item.ClassifyId)
  27. return
  28. }
  29. // Update 更新分类基础信息
  30. func (edbClassify *EdbClassify) Update(cols []string) (err error) {
  31. err = global.DEFAULT_DmSQL.Model(edbClassify).Select(cols).Updates(edbClassify).Error
  32. return
  33. }
  34. func GetEdbClassifyById(classifyId int) (item *EdbClassify, err error) {
  35. sql := `SELECT * FROM edb_classify WHERE classify_id=? `
  36. err = global.DEFAULT_DmSQL.Raw(sql, classifyId).First(&item).Error
  37. return
  38. }
  39. type EdbClassifySimplify struct {
  40. ClassifyId int `gorm:"primaryKey;autoIncrement;column:classify_id" description:"分类id"`
  41. ClassifyName string `description:"分类名称"`
  42. ParentId int
  43. }
  44. // 判断分类名称是否存在
  45. func GetEdbClassifyCount(classifyName string, parentId int, classifyType uint8) (item *EdbClassify, err error) {
  46. sql := `SELECT * FROM edb_classify WHERE parent_id=? AND classify_name=? AND classify_type = ? `
  47. err = global.DEFAULT_DmSQL.Raw(sql, parentId, classifyName, classifyType).First(&item).Error
  48. return
  49. }
  50. // GetEdbClassifyMaxSort 获取分类下最大的排序数
  51. func GetEdbClassifyMaxSort(parentId int, classifyType uint8) (sort int, err error) {
  52. sql := `SELECT COALESCE(MAX(sort),0) AS sort FROM edb_classify WHERE parent_id=? AND classify_type=? `
  53. err = global.DEFAULT_DmSQL.Raw(sql, parentId, classifyType).First(&sort).Error
  54. return
  55. }
  56. type AddEdbClassifyReq struct {
  57. ClassifyName string `description:"分类名称"`
  58. ParentId int `description:"父级id,第一级传0"`
  59. Level int `description:"层级,第一级传0,其余传上一级的层级"`
  60. ClassifyType uint8 `description:"分类类型,0:普通指标分类,1:预测指标分类"`
  61. }
  62. // AddEdbClassify 添加指标分类
  63. func SaveEdbClassify(classifyName string, parentId, level int, classifyType uint8, sysUserId, sysUserName string) (classifyInfo *EdbClassify, err error, errMsg string) {
  64. edbClassify, err := GetEdbClassifyCount(classifyName, parentId, classifyType)
  65. var isAdd bool
  66. if err != nil {
  67. if err.Error() == utils.ErrNoRow() {
  68. isAdd = true
  69. } else {
  70. errMsg = `判断名称是否已存在失败`
  71. return
  72. }
  73. }
  74. if edbClassify != nil && edbClassify.ClassifyId > 0 {
  75. classifyInfo = edbClassify
  76. return edbClassify, nil, ""
  77. } else {
  78. isAdd = true
  79. }
  80. if isAdd {
  81. sysUserIdInt, _ := strconv.Atoi(sysUserId)
  82. rootId := 0
  83. if parentId > 0 {
  84. parentClassify, tErr := GetEdbClassifyById(parentId)
  85. if tErr != nil {
  86. if tErr.Error() == utils.ErrNoRow() {
  87. errMsg = "父级分类不存在"
  88. err = errors.New(errMsg)
  89. return
  90. }
  91. errMsg = "获取失败"
  92. err = errors.New("获取分类信息失败,Err:" + tErr.Error())
  93. return
  94. }
  95. rootId = parentClassify.RootId
  96. }
  97. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  98. classifyInfo = &EdbClassify{
  99. //ClassifyId: 0,
  100. ClassifyType: classifyType,
  101. ClassifyName: classifyName,
  102. ParentId: parentId,
  103. RootId: rootId,
  104. HasData: 0,
  105. CreateTime: time.Now(),
  106. ModifyTime: time.Now(),
  107. SysUserId: sysUserIdInt,
  108. SysUserRealName: sysUserName,
  109. Level: level + 1,
  110. UniqueCode: utils.MD5(utils.DATA_PREFIX + "_" + timestamp),
  111. Sort: GetAddEdbMaxSortByClassifyId(parentId, classifyType),
  112. }
  113. var classifyId int64
  114. classifyId, err = AddEdbClassify(classifyInfo)
  115. if err != nil {
  116. errMsg = "保存分类失败"
  117. return
  118. }
  119. classifyInfo.ClassifyId = int(classifyId)
  120. if parentId == 0 { //一级目录的rootId等于自己本身
  121. classifyInfo.RootId = int(classifyId)
  122. err = classifyInfo.Update([]string{"RootId"})
  123. if err != nil {
  124. errMsg = "更新分类失败"
  125. return
  126. }
  127. }
  128. }
  129. return
  130. }