category.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package jiayue
  2. import (
  3. "database/sql"
  4. "eta/eta_bridge/global"
  5. "fmt"
  6. )
  7. var (
  8. IndexMenuTableName = "DICT_CATEGORY" // 指标目录表
  9. IndexMenuRelateTableName = "INDEX_CATEGORY" // 指标目录关联表
  10. )
  11. type DictCategory struct {
  12. Id int `description:"主键"`
  13. ParentId int `description:"父级目录ID"`
  14. ParentName string `description:"父级目录名称"`
  15. Type string `description:"目录类型"`
  16. Code string `description:"目录编码"`
  17. Name string `description:"目录名称"`
  18. Icon string `description:"目录图标"`
  19. Sorting int `description:"排序"`
  20. Description string `description:"描述"`
  21. UserId int `description:"创建用户"`
  22. Path string `description:"目录全路径"`
  23. }
  24. type DictCategorySql struct {
  25. Id int `description:"主键" json:"ID"`
  26. ParentId sql.NullInt32 `description:"父级目录ID" json:"PARENT_ID"`
  27. ParentName sql.NullString `description:"父级目录名称" json:"PARENT_NAME"`
  28. Type sql.NullString `description:"目录类型" json:"TYPE"`
  29. Code sql.NullString `description:"目录编码" json:"CODE"`
  30. Name sql.NullString `description:"目录名称" json:"NAME"`
  31. Icon sql.NullString `description:"目录图标" json:"ICON"`
  32. Sorting sql.NullInt32 `description:"排序" json:"SORTING"`
  33. Description sql.NullString `description:"描述" json:"DESCRIPTION"`
  34. UserId sql.NullInt32 `description:"创建用户" json:"USER_ID"`
  35. Path sql.NullString `description:"目录全路径" json:"PATH"`
  36. }
  37. // GetDictCategory 获取指标目录
  38. func GetDictCategory(condition string, pars []interface{}, orderRule string) (categories []DictCategory, err error) {
  39. defer func() {
  40. if err != nil {
  41. global.LOG.Info("GetDictCategory Err:" + err.Error())
  42. }
  43. }()
  44. // 查询字段固定, 避免嘉悦加了字段这边报错
  45. fields := "C.ID, C.PARENT_ID, C.PARENT_NAME, C.TYPE, C.CODE, C.NAME, C.ICON, C.SORTING, C.DESCRIPTION, C.USER_ID, C.PATH"
  46. querySql := fmt.Sprintf(`SELECT %s FROM %s AS C JOIN %s AS R ON C.ID = R.INDEX_ID WHERE %s %s`, fields, IndexMenuTableName, IndexMenuRelateTableName, condition, orderRule)
  47. order := ``
  48. if orderRule != "" {
  49. order += fmt.Sprintf(` ORDER BY %s`, orderRule)
  50. }
  51. list, e := execDictCategory(querySql, pars)
  52. if e != nil {
  53. err = fmt.Errorf("execDictCategory err: %s", e.Error())
  54. return
  55. }
  56. categories = list
  57. return
  58. }
  59. // execDictCategory 执行目录查询SQL
  60. func execDictCategory(querySql string, pars []interface{}) (categories []DictCategory, err error) {
  61. stmt, e := global.OracleJy.Prepare(querySql)
  62. if e != nil {
  63. err = fmt.Errorf("execDictCategory Prepare err: %s", e.Error())
  64. return
  65. }
  66. rows, e := stmt.Query(pars...) //输入sql中对应参数的值
  67. if e != nil {
  68. err = fmt.Errorf("execDictCategory Query err: %s", e.Error())
  69. return
  70. }
  71. defer func() {
  72. _ = rows.Close()
  73. }()
  74. for rows.Next() {
  75. var t DictCategorySql
  76. e = rows.Scan(&t.Id, &t.ParentId, &t.ParentName, &t.Type, &t.Code, &t.Name, &t.Icon, &t.Sorting, &t.Description, &t.UserId, &t.Path)
  77. if e != nil {
  78. err = fmt.Errorf("execDictCategory Scan err:" + e.Error())
  79. return
  80. }
  81. v := DictCategory{
  82. Id: t.Id,
  83. ParentId: int(t.ParentId.Int32),
  84. ParentName: t.ParentName.String,
  85. Type: t.Type.String,
  86. Code: t.Code.String,
  87. Name: t.Name.String,
  88. Icon: t.Icon.String,
  89. Sorting: int(t.Sorting.Int32),
  90. Description: t.Description.String,
  91. UserId: int(t.UserId.Int32),
  92. Path: t.Path.String,
  93. }
  94. categories = append(categories, v)
  95. }
  96. if e = rows.Err(); e != nil {
  97. err = fmt.Errorf("execDictCategory rows err: %s", e.Error())
  98. return
  99. }
  100. defer func() {
  101. _ = stmt.Close()
  102. }()
  103. return
  104. }