// Package data // @Author gmy 2024/8/12 16:09:00 package data import ( "eta/eta_api/models/data_manage" "sort" ) // RzdClassifyList 获取分类列表 包含 分类下的指标名称 func RzdClassifyList() ([]*data_manage.BaseFromRzdClassifyResponse, error) { // step_1 获取分类列表 rzdClassify, err := data_manage.GetAllRzdClassify() if err != nil { return nil, err } // step_2 分类id列表 classifyIds := make([]int, 0) for _, v := range rzdClassify { classifyIds = append(classifyIds, v.BaseFromRzdClassifyId) } // step_3 获取分类下的指标 rzdIndices, err := data_manage.GetRzdIndexByClassifyIds(classifyIds) if err != nil { return nil, err } // step_4 封装返回 classifyMap := make(map[int]*data_manage.BaseFromRzdClassifyResponse) indexMap := make(map[int][]*data_manage.BaseFromRzdIndex) // 创建分类响应映射 for _, classify := range rzdClassify { classifyMap[classify.BaseFromRzdClassifyId] = &data_manage.BaseFromRzdClassifyResponse{ BaseFromRzdClassifyId: classify.BaseFromRzdClassifyId, CreateTime: classify.CreateTime, ModifyTime: classify.ModifyTime, ClassifyName: classify.ClassifyName, ParentId: classify.ParentId, Sort: classify.Sort, ClassifyNameEn: classify.ClassifyNameEn, } } // 将指标归类到各自的分类 for _, index := range rzdIndices { indexMap[index.BaseFromRzdClassifyId] = append(indexMap[index.BaseFromRzdClassifyId], index) } // step_5 组装最终结果 rzdClassifyAndIndexInfos := make([]*data_manage.BaseFromRzdClassifyResponse, 0) for _, classifyResponse := range classifyMap { if classifyResponse.ParentId == 0 { // 根分类 // 添加指标信息 if indices, exists := indexMap[classifyResponse.BaseFromRzdClassifyId]; exists { classifyResponse.IndexInfo = indices } rzdClassifyAndIndexInfos = append(rzdClassifyAndIndexInfos, classifyResponse) } else { // 查找父分类并添加到其子分类中 if parent, exists := classifyMap[classifyResponse.ParentId]; exists { // 添加指标信息 if indices, exists := indexMap[classifyResponse.BaseFromRzdClassifyId]; exists { classifyResponse.IndexInfo = indices } parent.Child = append(parent.Child, classifyResponse) } } } sort.Slice(rzdClassifyAndIndexInfos, func(i, j int) bool { return rzdClassifyAndIndexInfos[i].Sort < rzdClassifyAndIndexInfos[j].Sort }) for _, info := range rzdClassifyAndIndexInfos { sort.Slice(info.Child, func(i, j int) bool { return info.Child[i].Sort < info.Child[j].Sort }) } return rzdClassifyAndIndexInfos, nil } // GetRzdClassifyItemByParentId 根据分类id获取分类信息 func GetRzdClassifyItemByParentId(classifyId int) ([]*data_manage.BaseFromRzdClassify, error) { rzdClassify, err := data_manage.GetRzdClassifyItemByParentId(classifyId) if err != nil { return nil, err } return rzdClassify, nil }