base_from_smm_classify.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package models
  2. import (
  3. "eta_gn/eta_index_lib/utils"
  4. "github.com/beego/beego/v2/client/orm"
  5. "strconv"
  6. "time"
  7. )
  8. type BaseFromSmmClassify struct {
  9. ClassifyId int `orm:"column(classify_id);pk"`
  10. ClassifyName string `description:"分类名称"`
  11. ParentId int `description:"父级id"`
  12. SysUserId int `description:"创建人id"`
  13. SysUserRealName string `description:"创建人姓名"`
  14. Level int `description:"层级"`
  15. Sort int `description:"排序字段,越小越靠前,默认值:10"`
  16. ModifyTime time.Time `description:"修改时间"`
  17. CreateTime time.Time `description:"创建时间"`
  18. }
  19. func AddSmmClassify(item *BaseFromSmmClassify) (lastId int64, err error) {
  20. o := orm.NewOrm()
  21. lastId, err = o.Insert(item)
  22. return
  23. }
  24. type AddSmmClassifyReq struct {
  25. ClassifyName string `description:"分类名称"`
  26. ParentId int `description:"父级id,第一级传0"`
  27. Level int `description:"层级,第一级传0,其余传上一级的层级"`
  28. SysUserId int `description:"操作人id"`
  29. SysUserRealName string `description:"操作人真实名称"`
  30. }
  31. // 判断分类名称是否存在
  32. func GetSmmClassify(classifyName string, parentId int) (item *BaseFromSmmClassify, err error) {
  33. o := orm.NewOrm()
  34. sql := `SELECT * FROM base_from_smm_classify WHERE parent_id=? AND classify_name=? `
  35. err = o.Raw(sql, parentId, classifyName).QueryRow(&item)
  36. return
  37. }
  38. // 添加指标分类
  39. func SaveSmmClassify(classifyName string, parentId, level int, sysUserId, sysUserName string) (classifyInfo *BaseFromSmmClassify, err error, errMsg string) {
  40. edbClassify, err := GetSmmClassify(classifyName, parentId)
  41. var isAdd bool
  42. if err != nil {
  43. if err.Error() == utils.ErrNoRow() {
  44. isAdd = true
  45. } else {
  46. errMsg = `判断名称是否已存在失败`
  47. return
  48. }
  49. }
  50. if edbClassify != nil && edbClassify.ClassifyId > 0 {
  51. classifyInfo = edbClassify
  52. return edbClassify, nil, ""
  53. } else {
  54. isAdd = true
  55. }
  56. if isAdd {
  57. sysUserIdInt, _ := strconv.Atoi(sysUserId)
  58. //获取该层级下最大的排序数
  59. maxSort, err := GetMysteelClassifyMaxSort(parentId)
  60. classifyInfo = &BaseFromSmmClassify{
  61. ClassifyName: classifyName,
  62. ParentId: parentId,
  63. CreateTime: time.Now(),
  64. ModifyTime: time.Now(),
  65. SysUserId: sysUserIdInt,
  66. SysUserRealName: sysUserName,
  67. Level: level + 1,
  68. Sort: maxSort,
  69. }
  70. classifyId, err := AddSmmClassify(classifyInfo)
  71. if err != nil {
  72. errMsg = "保存分类失败"
  73. }
  74. classifyInfo.ClassifyId = int(classifyId)
  75. }
  76. return
  77. }