base_from_mysteel_chemical_classify.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package models
  2. import (
  3. "eta/eta_index_lib/utils"
  4. "github.com/beego/beego/v2/client/orm"
  5. "strconv"
  6. "time"
  7. )
  8. type BaseFromMysteelChemicalClassify struct {
  9. BaseFromMysteelChemicalClassifyId int `orm:"column(base_from_mysteel_chemical_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. // 判断分类名称是否存在
  20. func GetMysteelClassify(classifyName string, parentId int) (item *BaseFromMysteelChemicalClassify, err error) {
  21. o := orm.NewOrm()
  22. sql := `SELECT * FROM base_from_mysteel_chemical_classify WHERE parent_id=? AND classify_name=? `
  23. err = o.Raw(sql, parentId, classifyName).QueryRow(&item)
  24. return
  25. }
  26. // GetEdbClassifyMaxSort 获取分类下最大的排序数
  27. func GetMysteelClassifyMaxSort(parentId int) (sort int, err error) {
  28. o := orm.NewOrm()
  29. sql := `SELECT Max(sort) AS sort FROM base_from_mysteel_chemical_classify WHERE parent_id=? `
  30. err = o.Raw(sql, parentId).QueryRow(&sort)
  31. return
  32. }
  33. func AddMysteelClassify(item *BaseFromMysteelChemicalClassify) (lastId int64, err error) {
  34. o := orm.NewOrm()
  35. lastId, err = o.Insert(item)
  36. return
  37. }
  38. type AddMysteelClassifyReq struct {
  39. ClassifyName string `description:"分类名称"`
  40. ParentId int `description:"父级id,第一级传0"`
  41. Level int `description:"层级,第一级传0,其余传上一级的层级"`
  42. SysUserId int `description:"操作人id"`
  43. SysUserRealName string `description:"操作人真实名称"`
  44. }
  45. // AddEdbClassify 添加指标分类
  46. func SaveMysteelClassify(classifyName string, parentId, level int, sysUserId, sysUserName string) (classifyInfo *BaseFromMysteelChemicalClassify, err error, errMsg string) {
  47. edbClassify, err := GetMysteelClassify(classifyName, parentId)
  48. var isAdd bool
  49. if err != nil {
  50. if err.Error() == utils.ErrNoRow() {
  51. isAdd = true
  52. } else {
  53. errMsg = `判断名称是否已存在失败`
  54. return
  55. }
  56. }
  57. if edbClassify != nil && edbClassify.BaseFromMysteelChemicalClassifyId > 0 {
  58. classifyInfo = edbClassify
  59. return edbClassify, nil, ""
  60. } else {
  61. isAdd = true
  62. }
  63. if isAdd {
  64. sysUserIdInt, _ := strconv.Atoi(sysUserId)
  65. //获取该层级下最大的排序数
  66. maxSort, err := GetMysteelClassifyMaxSort(parentId)
  67. classifyInfo = &BaseFromMysteelChemicalClassify{
  68. ClassifyName: classifyName,
  69. ParentId: parentId,
  70. CreateTime: time.Now(),
  71. ModifyTime: time.Now(),
  72. SysUserId: sysUserIdInt,
  73. SysUserRealName: sysUserName,
  74. Level: level + 1,
  75. Sort: maxSort,
  76. }
  77. classifyId, err := AddMysteelClassify(classifyInfo)
  78. if err != nil {
  79. errMsg = "保存分类失败"
  80. }
  81. classifyInfo.BaseFromMysteelChemicalClassifyId = int(classifyId)
  82. }
  83. return
  84. }