base_from_rzd_classify_service.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Package data
  2. // @Author gmy 2024/8/12 16:09:00
  3. package data
  4. import (
  5. "eta/eta_api/models/data_manage"
  6. "sort"
  7. )
  8. // RzdClassifyList 获取分类列表 包含 分类下的指标名称
  9. func RzdClassifyList() ([]*data_manage.BaseFromRzdClassifyResponse, error) {
  10. // step_1 获取分类列表
  11. rzdClassify, err := data_manage.GetAllRzdClassify()
  12. if err != nil {
  13. return nil, err
  14. }
  15. // step_2 分类id列表
  16. classifyIds := make([]int, 0)
  17. for _, v := range rzdClassify {
  18. classifyIds = append(classifyIds, v.BaseFromRzdClassifyId)
  19. }
  20. // step_3 获取分类下的指标
  21. rzdIndices, err := data_manage.GetRzdIndexByClassifyIds(classifyIds)
  22. if err != nil {
  23. return nil, err
  24. }
  25. // step_4 封装返回
  26. classifyMap := make(map[int]*data_manage.BaseFromRzdClassifyResponse)
  27. indexMap := make(map[int][]*data_manage.BaseFromRzdIndex)
  28. // 创建分类响应映射
  29. for _, classify := range rzdClassify {
  30. classifyMap[classify.BaseFromRzdClassifyId] = &data_manage.BaseFromRzdClassifyResponse{
  31. BaseFromRzdClassifyId: classify.BaseFromRzdClassifyId,
  32. CreateTime: classify.CreateTime,
  33. ModifyTime: classify.ModifyTime,
  34. ClassifyName: classify.ClassifyName,
  35. ParentId: classify.ParentId,
  36. Sort: classify.Sort,
  37. ClassifyNameEn: classify.ClassifyNameEn,
  38. }
  39. }
  40. // 将指标归类到各自的分类
  41. for _, index := range rzdIndices {
  42. indexMap[index.BaseFromRzdClassifyId] = append(indexMap[index.BaseFromRzdClassifyId], index)
  43. }
  44. // step_5 组装最终结果
  45. rzdClassifyAndIndexInfos := make([]*data_manage.BaseFromRzdClassifyResponse, 0)
  46. for _, classifyResponse := range classifyMap {
  47. if classifyResponse.ParentId == 0 { // 根分类
  48. // 添加指标信息
  49. if indices, exists := indexMap[classifyResponse.BaseFromRzdClassifyId]; exists {
  50. classifyResponse.IndexInfo = indices
  51. }
  52. rzdClassifyAndIndexInfos = append(rzdClassifyAndIndexInfos, classifyResponse)
  53. } else {
  54. // 查找父分类并添加到其子分类中
  55. if parent, exists := classifyMap[classifyResponse.ParentId]; exists {
  56. // 添加指标信息
  57. if indices, exists := indexMap[classifyResponse.BaseFromRzdClassifyId]; exists {
  58. classifyResponse.IndexInfo = indices
  59. }
  60. parent.Child = append(parent.Child, classifyResponse)
  61. }
  62. }
  63. }
  64. sort.Slice(rzdClassifyAndIndexInfos, func(i, j int) bool {
  65. return rzdClassifyAndIndexInfos[i].Sort < rzdClassifyAndIndexInfos[j].Sort
  66. })
  67. for _, info := range rzdClassifyAndIndexInfos {
  68. sort.Slice(info.Child, func(i, j int) bool {
  69. return info.Child[i].Sort < info.Child[j].Sort
  70. })
  71. }
  72. return rzdClassifyAndIndexInfos, nil
  73. }
  74. // GetRzdClassifyItemByParentId 根据分类id获取分类信息
  75. func GetRzdClassifyItemByParentId(classifyId int) ([]*data_manage.BaseFromRzdClassify, error) {
  76. rzdClassify, err := data_manage.GetRzdClassifyItemByParentId(classifyId)
  77. if err != nil {
  78. return nil, err
  79. }
  80. return rzdClassify, nil
  81. }