category.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. fields := "ID, PARENT_ID, PARENT_NAME, TYPE, CODE, NAME, ICON, SORTING, DESCRIPTION, USER_ID, PATH"
  45. querySql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, IndexMenuTableName, condition, orderRule)
  46. order := ``
  47. if orderRule != "" {
  48. order += fmt.Sprintf(` ORDER BY %s`, orderRule)
  49. }
  50. global.LOG.Info("querySql SQL: ", querySql)
  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. // GetIndexCategory 获取指标关联目录
  60. func GetIndexCategory(condition string, pars []interface{}, orderRule string) (categories []DictCategory, err error) {
  61. defer func() {
  62. if err != nil {
  63. global.LOG.Info("GetIndexCategory Err:" + err.Error())
  64. }
  65. }()
  66. 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"
  67. querySql := fmt.Sprintf(`SELECT %s FROM %s C JOIN %s R ON C.ID = R.CATEGORY_ID WHERE %s %s`, fields, IndexMenuTableName, IndexMenuRelateTableName, condition, orderRule)
  68. order := ``
  69. if orderRule != "" {
  70. order += fmt.Sprintf(` ORDER BY %s`, orderRule)
  71. }
  72. global.LOG.Info("querySql SQL: ", querySql)
  73. list, e := execDictCategory(querySql, pars)
  74. if e != nil {
  75. err = fmt.Errorf("execDictCategory err: %s", e.Error())
  76. return
  77. }
  78. categories = list
  79. return
  80. }
  81. // execDictCategory 执行目录查询SQL
  82. func execDictCategory(querySql string, pars []interface{}) (categories []DictCategory, err error) {
  83. stmt, e := global.OracleJy.Prepare(querySql)
  84. if e != nil {
  85. err = fmt.Errorf("execDictCategory Prepare err: %s", e.Error())
  86. return
  87. }
  88. rows, e := stmt.Query(pars...) //输入sql中对应参数的值
  89. if e != nil {
  90. err = fmt.Errorf("execDictCategory Query err: %s", e.Error())
  91. return
  92. }
  93. defer func() {
  94. _ = rows.Close()
  95. }()
  96. for rows.Next() {
  97. var t DictCategorySql
  98. 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)
  99. if e != nil {
  100. err = fmt.Errorf("execDictCategory Scan err:" + e.Error())
  101. return
  102. }
  103. v := DictCategory{
  104. Id: t.Id,
  105. ParentId: int(t.ParentId.Int32),
  106. ParentName: t.ParentName.String,
  107. Type: t.Type.String,
  108. Code: t.Code.String,
  109. Name: t.Name.String,
  110. Icon: t.Icon.String,
  111. Sorting: int(t.Sorting.Int32),
  112. Description: t.Description.String,
  113. UserId: int(t.UserId.Int32),
  114. Path: t.Path.String,
  115. }
  116. categories = append(categories, v)
  117. }
  118. if e = rows.Err(); e != nil {
  119. err = fmt.Errorf("execDictCategory rows err: %s", e.Error())
  120. return
  121. }
  122. defer func() {
  123. _ = stmt.Close()
  124. }()
  125. return
  126. }