增加全局搜索功能的后端逻辑
This commit is contained in:
@@ -0,0 +1,70 @@
|
|||||||
|
package com.mzaxd.noodles.controller;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.mzaxd.noodles.domain.ResponseResult;
|
||||||
|
import com.mzaxd.noodles.domain.search.SearchCategory;
|
||||||
|
import com.mzaxd.noodles.domain.search.SearchItem;
|
||||||
|
import com.mzaxd.noodles.util.SearchUtil;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author ThinkBook
|
||||||
|
*/
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
public class OtherController {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 全局搜索功能的接口
|
||||||
|
*
|
||||||
|
* @param str
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@GetMapping("/app-bar/search")
|
||||||
|
public ResponseResult searchbar(@RequestParam("q") String str) {
|
||||||
|
List<SearchCategory> searchCategories = SearchUtil.getSearchCategory();
|
||||||
|
|
||||||
|
List<Object> result = new ArrayList<>();
|
||||||
|
|
||||||
|
for (SearchCategory category : searchCategories) {
|
||||||
|
result.add(category);
|
||||||
|
List<SearchItem> searchItems = null;
|
||||||
|
switch (category) {
|
||||||
|
case INSTANCE:
|
||||||
|
searchItems = SearchUtil.getInstanceSearchItem();
|
||||||
|
break;
|
||||||
|
case STATISTICS:
|
||||||
|
searchItems = SearchUtil.getStatisticsSearchItem();
|
||||||
|
break;
|
||||||
|
case SETTING:
|
||||||
|
searchItems = SearchUtil.getSettingSearchItem();
|
||||||
|
break;
|
||||||
|
case OTHER:
|
||||||
|
searchItems = SearchUtil.getOtherSearchItem();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (searchItems != null) {
|
||||||
|
List<SearchItem> filteredItems = searchItems.stream()
|
||||||
|
.filter(item -> item.getTitle().contains(str))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
if (!filteredItems.isEmpty()) {
|
||||||
|
result.addAll(filteredItems);
|
||||||
|
} else {
|
||||||
|
result.remove(result.size() - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ResponseResult.okResult(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
package com.mzaxd.noodles.domain.search;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.annotation.JSONType;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.ToString;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author ThinkBook
|
||||||
|
*/
|
||||||
|
@Getter
|
||||||
|
@ToString
|
||||||
|
@JSONType(serializeEnumAsJavaBean = true)
|
||||||
|
public enum SearchCategory {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 实例统计
|
||||||
|
*/
|
||||||
|
INSTANCE("实例统计", "实例统计"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 统计
|
||||||
|
*/
|
||||||
|
STATISTICS("统计", "统计"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置
|
||||||
|
*/
|
||||||
|
SETTING("设置", "设置"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 其他
|
||||||
|
*/
|
||||||
|
OTHER("其他", "其他");
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 搜索标题
|
||||||
|
*/
|
||||||
|
final String header;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 搜搜名字
|
||||||
|
*/
|
||||||
|
final String title;
|
||||||
|
|
||||||
|
SearchCategory(String header, String title) {
|
||||||
|
this.header = header;
|
||||||
|
this.title = title;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
package com.mzaxd.noodles.domain.search;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.annotation.JSONType;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonValue;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.ToString;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author ThinkBook
|
||||||
|
*/
|
||||||
|
@Getter
|
||||||
|
@ToString
|
||||||
|
@JSONType(serializeEnumAsJavaBean = true)
|
||||||
|
public enum SearchItem {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* HOST
|
||||||
|
*/
|
||||||
|
HOST(SearchCategory.INSTANCE.title, "tabler-server", 1, "物理机", Url.HOST),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* VM
|
||||||
|
*/
|
||||||
|
VM(SearchCategory.INSTANCE.title, "tabler-chart-bubble", 1, "虚拟机", Url.VM),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* CONTAINER
|
||||||
|
*/
|
||||||
|
CONTAINER(SearchCategory.INSTANCE.title, "tabler-brand-docker", 1, "容器", Url.CONTAINER),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* DASHBOARD
|
||||||
|
*/
|
||||||
|
DASHBOARD(SearchCategory.STATISTICS.title, "tabler-dashboard", 1, "仪表盘", Url.DASHBOARD),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AUDIT_LOG
|
||||||
|
*/
|
||||||
|
AUDIT_LOG(SearchCategory.STATISTICS.title, "tabler-book", 1, "操作记录", Url.AUDIT_LOG),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* PANEL_MANAGEMENT
|
||||||
|
*/
|
||||||
|
PANEL_MANAGEMENT(SearchCategory.SETTING.title, "tabler-settings", 1, "面板管理", Url.PANEL_MANAGEMENT),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ACCOUNT_SETTING
|
||||||
|
*/
|
||||||
|
ACCOUNT_SETTING(SearchCategory.SETTING.title, "tabler-user", 1, "账号设置", Url.ACCOUNT_MANAGEMENT),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* NOTIFICATION
|
||||||
|
*/
|
||||||
|
NOTIFICATION(SearchCategory.OTHER.title, "tabler-bell", 1, "通知提醒", Url.NOTIFICATION),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* HELP
|
||||||
|
*/
|
||||||
|
HELP(SearchCategory.OTHER.title, "tabler-help", 1, "帮助文档", Url.HELP);
|
||||||
|
|
||||||
|
final String category;
|
||||||
|
|
||||||
|
final String icon;
|
||||||
|
|
||||||
|
final Integer id;
|
||||||
|
|
||||||
|
final String title;
|
||||||
|
|
||||||
|
final Url url;
|
||||||
|
|
||||||
|
SearchItem(String category, String icon, Integer id, String title, Url url) {
|
||||||
|
this.category = category;
|
||||||
|
this.icon = icon;
|
||||||
|
this.id = id;
|
||||||
|
this.title = title;
|
||||||
|
this.url = url;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
package com.mzaxd.noodles.util;
|
||||||
|
|
||||||
|
import com.mzaxd.noodles.domain.search.SearchCategory;
|
||||||
|
import com.mzaxd.noodles.domain.search.SearchItem;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author ThinkBook
|
||||||
|
*/
|
||||||
|
public class SearchUtil {
|
||||||
|
|
||||||
|
public static ArrayList<SearchItem> getInstanceSearchItem() {
|
||||||
|
ArrayList<SearchItem> searchItems = new ArrayList<>();
|
||||||
|
searchItems.add(SearchItem.HOST);
|
||||||
|
searchItems.add(SearchItem.VM);
|
||||||
|
searchItems.add(SearchItem.CONTAINER);
|
||||||
|
return searchItems;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ArrayList<SearchItem> getStatisticsSearchItem() {
|
||||||
|
ArrayList<SearchItem> searchItems = new ArrayList<>();
|
||||||
|
searchItems.add(SearchItem.DASHBOARD);
|
||||||
|
searchItems.add(SearchItem.AUDIT_LOG);
|
||||||
|
return searchItems;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ArrayList<SearchItem> getSettingSearchItem() {
|
||||||
|
ArrayList<SearchItem> searchItems = new ArrayList<>();
|
||||||
|
searchItems.add(SearchItem.PANEL_MANAGEMENT);
|
||||||
|
searchItems.add(SearchItem.ACCOUNT_SETTING);
|
||||||
|
return searchItems;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ArrayList<SearchItem> getOtherSearchItem() {
|
||||||
|
ArrayList<SearchItem> searchItems = new ArrayList<>();
|
||||||
|
searchItems.add(SearchItem.NOTIFICATION);
|
||||||
|
searchItems.add(SearchItem.HELP);
|
||||||
|
return searchItems;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ArrayList<SearchCategory> getSearchCategory() {
|
||||||
|
return new ArrayList<>(Arrays.asList(
|
||||||
|
SearchCategory.INSTANCE,
|
||||||
|
SearchCategory.STATISTICS,
|
||||||
|
SearchCategory.SETTING,
|
||||||
|
SearchCategory.OTHER));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user