数据结构如下:
[{
"fileId": 436,
"fileName": "新建文件夹",
"fileSize": 0,
"fileUrl": "",
"fileCreateTime": 1562058941010,
"fileMd5": "",
"pid": 0,
"is_file": 1
}, {
"fileId": 623909,
"fileName": "hhhh.mp4",
"fileSize": 3359560,
"fileUrl": "http://files-storage.oss-cn-beijing.aliyuncs.com/4ca59c211185adc009378f50bc0bb029.mp4",
"fileCreateTime": 1561087643623,
"fileMd5": "4ca59c211185adc009378f50bc0bb029",
"pid": 0,
"is_file": 0
}, {
"fileId": 624079,
"fileName": "IMG_20190704_143444.jpg",
"fileSize": 1779713,
"fileUrl": "http://files-storage.oss-cn-beijing.aliyuncs.com/3ddfa908f70c4a1ede3339f67a54fdb8.jpg",
"fileCreateTime": 1562230327145,
"fileMd5": "3ddfa908f70c4a1ede3339f67a54fdb8",
"pid": 436,
"is_file": 0
}]
关于数据结构说明:
pid为0,表示处于顶级;is_file为1说明为文件夹,0为文件;
如果数据A的pid等于数据B的fileId,则说明A是B儿子;
如果数据A的pid等于数据B的pid,则说明A和B处于同一层级;
解析出来的样式:
如果下载文件夹,则需要把文件夹下所有的子文件找出来,具体实现如下:
/**
* 传入选中的文件、文件夹列表,
*
* @param allList 所有数据
* @param selectList 需要下载的数据
* @return 返回需要下载所有的文件列表
*/
public static List<CoursewareListItem> getAllFileList(List<CoursewareListItem> allList, List<CoursewareListItem> selectList) {
List<CoursewareListItem> resultList = new ArrayList<>();
Map<Long, CoursewareListItem> allMap = new LinkedHashMap<>();
List<Long> selectFileIdList = new ArrayList<>();
for (CoursewareListItem item : allList) {
allMap.put(item.getFileId(), item);
}
for (CoursewareListItem item : selectList) {
selectFileIdList.add(item.getFileId());
}
for (CoursewareListItem item : allMap.values()) {
CoursewareListItem tempItem = item;
if (tempItem.getIs_file() == 0) {
if (selectFileIdList.contains(item.getFileId())) {
resultList.add(item);
} else {
while (tempItem.getPid() != 0) {
CoursewareListItem listItem = allMap.get(tempItem.getPid());
if (listItem == null) {
break;
} else {
if (selectFileIdList.contains(listItem.getFileId())) {
resultList.add(item);
break;
} else {
tempItem = listItem;
}
}
}
}
}
}
return resultList;
}