smm_api.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. package data_manage
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/models"
  5. "eta/eta_api/models/data_manage"
  6. "eta/eta_api/services/elastic"
  7. "eta/eta_api/utils"
  8. "fmt"
  9. "github.com/rdlucklib/rdluck_tools/paging"
  10. "strings"
  11. )
  12. // SmmIndexList
  13. // @Title 有色api数据指标列表
  14. // @Description 有色api数据指标列表
  15. // @Success 200 {object} data_manage.SmmClassify
  16. // @router /smm/api/list [post]
  17. func (this *EdbInfoController) SmmApiList() {
  18. br := new(models.BaseResponse).Init()
  19. defer func() {
  20. this.Data["json"] = br
  21. this.ServeJSON()
  22. }()
  23. sysUser := this.SysUser
  24. if sysUser == nil {
  25. br.Msg = "请登录"
  26. br.ErrMsg = "请登录,SysUser Is Empty"
  27. br.Ret = 408
  28. return
  29. }
  30. var req data_manage.SmmIndexListReq
  31. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  32. if err != nil {
  33. br.Msg = "参数解析异常!"
  34. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  35. return
  36. }
  37. types := req.Types
  38. frequency := req.Frequency
  39. dataState := req.DataState
  40. keyword := req.Keyword
  41. indexCodes := req.IndexCodes
  42. sortType := req.SortType
  43. sortParam := req.SortParam
  44. pageSize := req.PageSize
  45. currentIndex := req.CurrentIndex
  46. if sortType == "" {
  47. sortType = "desc"
  48. }
  49. var startSize int
  50. if pageSize <= 0 {
  51. pageSize = utils.PageSize20
  52. }
  53. if currentIndex <= 0 {
  54. currentIndex = 1
  55. }
  56. startSize = utils.StartIndex(currentIndex, pageSize)
  57. var condition string
  58. var pars []interface{}
  59. if len(types) > 0 {
  60. condition += " AND ( "
  61. for _, v := range types {
  62. typeArr := strings.Split(v, ",")
  63. for i, v := range typeArr {
  64. if i == 0 {
  65. condition += " ( "
  66. }
  67. typeStr := "type_"
  68. typeStr += fmt.Sprintf("%d", i+1)
  69. condition += typeStr + " =? "
  70. pars = append(pars, v)
  71. if i == len(typeArr)-1 {
  72. condition += " ) "
  73. } else {
  74. condition += " AND "
  75. }
  76. }
  77. condition += " OR "
  78. }
  79. condition = strings.Trim(condition, "OR ")
  80. condition += " ) "
  81. }
  82. if frequency != "" {
  83. frequencyArr := strings.Split(frequency, ",")
  84. condition += ` AND frequency IN (` + utils.GetOrmInReplace(len(frequencyArr)) + `) `
  85. pars = append(pars, frequencyArr)
  86. }
  87. if dataState != "" {
  88. stateArr := strings.Split(dataState, ",")
  89. if strings.Contains(dataState, "normal") {
  90. stateArr = append(stateArr, "")
  91. condition += ` AND data_state IN (` + utils.GetOrmInReplace(len(stateArr)) + `) `
  92. pars = append(pars, stateArr)
  93. } else {
  94. condition += ` AND data_state IN (` + utils.GetOrmInReplace(len(stateArr)) + `) `
  95. pars = append(pars, stateArr)
  96. }
  97. }
  98. sortStr := ``
  99. keyword = strings.TrimSpace(keyword)
  100. if keyword != "" {
  101. //keyWordArr := strings.Split(keyword, " ")
  102. //if len(keyWordArr) > 0 {
  103. // condition += " AND ( "
  104. // keywordStr := strings.Replace(keyword, " ", "", -1)
  105. // condition += ` CONCAT(index_name,index_code) LIKE '%` + keywordStr + `%' OR `
  106. // sortStr += ` CASE WHEN CONCAT(index_name,index_code) LIKE '%` + keywordStr + `%' THEN 1 `
  107. // for i, v := range keyWordArr {
  108. // condition += ` CONCAT(index_name,index_code) LIKE '%` + v + `%' OR`
  109. // sortStr += ` WHEN CONCAT(index_name,index_code) LIKE '%` + v + `%' THEN ` + strconv.Itoa(i+2) + ` `
  110. // }
  111. // sortStr += ` END, `
  112. // condition = strings.TrimRight(condition, "OR")
  113. // condition += " ) "
  114. //}
  115. // ES搜
  116. _, list, e := elastic.SearchDataSourceIndex(utils.EsDataSourceIndexName, keyword, utils.DATA_SOURCE_YS, 0, []int{}, []int{}, []string{}, startSize, req.PageSize)
  117. if e != nil {
  118. br.Msg = "获取失败"
  119. br.ErrMsg = fmt.Sprintf("ES-搜索SMM指标失败, %v", e)
  120. return
  121. }
  122. if len(list) == 0 {
  123. br.Data = data_manage.BaseFromSmmIndexListResp{
  124. Paging: paging.GetPaging(req.CurrentIndex, req.PageSize, 0),
  125. List: make([]*data_manage.BaseFromSmmIndexItem, 0),
  126. }
  127. br.Ret = 200
  128. br.Success = true
  129. br.Msg = "获取成功"
  130. return
  131. }
  132. var indexIds []int
  133. for _, v := range list {
  134. indexIds = append(indexIds, v.PrimaryId)
  135. }
  136. condition += fmt.Sprintf(" AND base_from_smm_index_id IN (%s)", utils.GetOrmInReplace(len(indexIds)))
  137. pars = append(pars, indexIds)
  138. }
  139. if indexCodes != "" {
  140. indexCodeArr := strings.Split(indexCodes, ",")
  141. indexCodeStr := ""
  142. for _, v := range indexCodeArr {
  143. indexCodeStr += "'" + v + "',"
  144. }
  145. indexCodeStr = strings.TrimRight(indexCodeStr, ",")
  146. condition += " AND index_code IN (" + indexCodeStr + ") "
  147. }
  148. if sortParam != `` {
  149. sortStr += fmt.Sprintf("%s %s,modify_time desc ", utils.PascalToSnake(sortParam), sortType)
  150. } else {
  151. sortStr += " modify_time desc "
  152. }
  153. total, err := data_manage.GetSmmIndexDataListCount(condition, pars)
  154. if err != nil {
  155. br.Msg = "获取指标总数失败"
  156. br.ErrMsg = "获取指标总数失败,Err:" + err.Error()
  157. return
  158. }
  159. indexList, err := data_manage.GetSmmIndexDataList(condition, sortStr, pars, startSize, pageSize)
  160. if err != nil {
  161. br.Msg = "获取指标列表失败"
  162. br.ErrMsg = "获取指标列表失败,Err:" + err.Error()
  163. return
  164. }
  165. for _, v := range indexList {
  166. v.TypeAll = v.Type1 + "/" + v.Type2 + "/" + v.Type3
  167. }
  168. page := paging.GetPaging(currentIndex, pageSize, total)
  169. var ret data_manage.BaseFromSmmIndexListResp
  170. ret.List = indexList
  171. ret.Paging = page
  172. br.Ret = 200
  173. br.Success = true
  174. br.Msg = "获取成功"
  175. br.Data = ret
  176. }
  177. // SmmApiTypeList
  178. // @Title 有色api数据分类列表
  179. // @Description 有色api数据分类列表
  180. // @Success 200 {object} data_manage.SmmClassify
  181. // @router /smm/api/type/list [get]
  182. func (this *EdbInfoController) SmmApiTypeList() {
  183. br := new(models.BaseResponse).Init()
  184. defer func() {
  185. this.Data["json"] = br
  186. this.ServeJSON()
  187. }()
  188. sysUser := this.SysUser
  189. if sysUser == nil {
  190. br.Msg = "请登录"
  191. br.ErrMsg = "请登录,SysUser Is Empty"
  192. br.Ret = 408
  193. return
  194. }
  195. typeList, err := data_manage.GetBaseFromSmmIndexTypeList()
  196. if err != nil {
  197. br.Msg = "获取指标列表失败"
  198. br.ErrMsg = "获取指标列表失败,Err:" + err.Error()
  199. return
  200. }
  201. resp := make([]data_manage.TypeListRespItem, 0)
  202. typeMap := make(map[string]map[string][]string)
  203. //type2Map := make(map[string][]string)
  204. //type2Map := make(map[string]data_manage.TypeListRespItem)
  205. // 初始化
  206. for _, v := range typeList {
  207. if v.Type1 != "" {
  208. if _, ok := typeMap[v.Type1]; !ok {
  209. typeMap[v.Type1] = make(map[string][]string)
  210. } else {
  211. if _, ok := typeMap[v.Type1][v.Type2]; !ok {
  212. typeMap[v.Type1][v.Type2] = make([]string, 0)
  213. }
  214. }
  215. }
  216. }
  217. for _, v := range typeList {
  218. if v.Type1 != "" {
  219. typeMap[v.Type1][v.Type2] = append(typeMap[v.Type1][v.Type2], v.Type3)
  220. }
  221. }
  222. for type1, type2Map := range typeMap {
  223. var item data_manage.TypeListRespItem
  224. item.Type = type1
  225. for type2, type3List := range type2Map {
  226. var child data_manage.TypeListRespItem
  227. child.Type = type2
  228. for _, type3 := range type3List {
  229. child.Child = append(child.Child, data_manage.TypeListRespItem{type3, nil})
  230. }
  231. item.Child = append(item.Child, child)
  232. }
  233. resp = append(resp, item)
  234. }
  235. br.Ret = 200
  236. br.Success = true
  237. br.Msg = "获取成功"
  238. br.Data = resp
  239. }