From 6a4adee8d736667ac63859f13af78fcff51c7ae5 Mon Sep 17 00:00:00 2001 From: pxh4iabou <2044878308@qq.com> Date: Sun, 30 Apr 2023 01:24:40 +0800 Subject: [PATCH] ADD file via upload --- .../com/controller/EchartsController.java | 274 ++++++++++++++++++ 1 file changed, 274 insertions(+) create mode 100644 src/main/java/com/controller/EchartsController.java diff --git a/src/main/java/com/controller/EchartsController.java b/src/main/java/com/controller/EchartsController.java new file mode 100644 index 0000000..b3a5014 --- /dev/null +++ b/src/main/java/com/controller/EchartsController.java @@ -0,0 +1,274 @@ +package com.example.controller; + +import cn.hutool.json.JSONObject; +import com.example.common.Result; +import com.example.entity.*; +import com.example.service.*; +import com.example.vo.*; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import javax.annotation.Resource; +import java.util.*; +import java.util.stream.Collectors; + +@RestController +@RequestMapping("/echarts") +public class EchartsController { + + @Resource + private AdminInfoService adminInfoService; + @Resource + private UserInfoService userInfoService; + @Resource + private ClassifyInfoService classifyInfoService; + @Resource + private SubClassifyInfoService subClassifyInfoService; + @Resource + private CollectInfoService collectInfoService; + @Resource + private PraiseInfoService praiseInfoService; + @Resource + private NewsInfoService newsInfoService; + @Resource + private MessageInfoService messageInfoService; + + + @GetMapping("/get/{modelName}") + Result> getEchartsData(@PathVariable String modelName) { + List list = new ArrayList<>(); + switch (modelName) { + case "adminInfo": + List adminInfoList = adminInfoService.findAll(); + List adminInfoMaleList = adminInfoList.stream().filter(x -> "男".equals(x.getSex())).collect(Collectors.toList()); + Map adminInfoMap = new HashMap<>(2); + adminInfoMap.put("男", adminInfoMaleList.size()); + adminInfoMap.put("女", adminInfoList.size() - adminInfoMaleList.size()); + getPieData("管理员", list, adminInfoMap); + getBarData("管理员", list, adminInfoMap); + + break; + case "userInfo": + List userInfoList = userInfoService.findAll(); + List userInfoMaleList = userInfoList.stream().filter(x -> "男".equals(x.getSex())).collect(Collectors.toList()); + Map userInfoMap = new HashMap<>(2); + userInfoMap.put("男", userInfoMaleList.size()); + userInfoMap.put("女", userInfoList.size() - userInfoMaleList.size()); + getPieData("用户", list, userInfoMap); + getBarData("用户", list, userInfoMap); + + break; + + case "classifyInfo": + List classifyInfoList = classifyInfoService.findAll(); + Map classifyInfoMap = new HashMap<>(2); + for (ClassifyInfo classifyInfo : classifyInfoList) { + Integer value = classifyInfoMap.get(classifyInfo.getName()); + if (value != null && value != 0) { + classifyInfoMap.put(classifyInfo.getName(), value + 1); + } else { + classifyInfoMap.put(classifyInfo.getName(), 1); + } + } + getPieData("菜谱大类", list, classifyInfoMap); + getBarData("菜谱大类", list, classifyInfoMap); + + break; + case "subClassifyInfo": + List subClassifyInfoList = subClassifyInfoService.findAll(); + Map subClassifyInfoMap = new HashMap<>(2); + for (SubClassifyInfo subClassifyInfo : subClassifyInfoList) { + Integer value = subClassifyInfoMap.get(subClassifyInfo.getName()); + if (value != null && value != 0) { + subClassifyInfoMap.put(subClassifyInfo.getName(), value + 1); + } else { + subClassifyInfoMap.put(subClassifyInfo.getName(), 1); + } + } + getPieData("菜谱小类", list, subClassifyInfoMap); + getBarData("菜谱小类", list, subClassifyInfoMap); + + break; + case "collectInfo": + List collectInfoList = collectInfoService.findAll(); + Map collectInfoMap = new HashMap<>(2); + for (CollectInfo collectInfo : collectInfoList) { + Integer value = collectInfoMap.get(collectInfo.getName()); + if (value != null && value != 0) { + collectInfoMap.put(collectInfo.getName(), value + 1); + } else { + collectInfoMap.put(collectInfo.getName(), 1); + } + } + getPieData("收藏", list, collectInfoMap); + getBarData("收藏", list, collectInfoMap); + + break; + case "praiseInfo": + List praiseInfoList = praiseInfoService.findAll(); + Map praiseInfoMap = new HashMap<>(2); + for (PraiseInfo praiseInfo : praiseInfoList) { + Integer value = praiseInfoMap.get(praiseInfo.getName()); + if (value != null && value != 0) { + praiseInfoMap.put(praiseInfo.getName(), value + 1); + } else { + praiseInfoMap.put(praiseInfo.getName(), 1); + } + } + getPieData("笔记点赞", list, praiseInfoMap); + getBarData("笔记点赞", list, praiseInfoMap); + + break; + case "newsInfo": + List newsInfoList = newsInfoService.findAll(); + Map newsInfoMap = new HashMap<>(2); + for (NewsInfo newsInfo : newsInfoList) { + Integer value = newsInfoMap.get(newsInfo.getName()); + if (value != null && value != 0) { + newsInfoMap.put(newsInfo.getName(), value + 1); + } else { + newsInfoMap.put(newsInfo.getName(), 1); + } + } + getPieData("饮食资讯", list, newsInfoMap); + getBarData("饮食资讯", list, newsInfoMap); + + break; + case "messageInfo": + List messageInfoList = messageInfoService.findAll(); + Map messageInfoMap = new HashMap<>(2); + for (MessageInfo messageInfo : messageInfoList) { + Integer value = messageInfoMap.get(messageInfo.getName()); + if (value != null && value != 0) { + messageInfoMap.put(messageInfo.getName(), value + 1); + } else { + messageInfoMap.put(messageInfo.getName(), 1); + } + } + getPieData("趣味答题", list, messageInfoMap); + getBarData("趣味答题", list, messageInfoMap); + + break; + + default: + break; + } + return Result.success(list); + } + + @GetMapping("/options") + Result>> getOptions() { + List> options = new ArrayList<>(); + + Map optionMap1 = new HashMap<>(); + optionMap1.put("value", "adminInfo"); + optionMap1.put("label", "管理员信息"); + options.add(optionMap1); + Map optionMap2 = new HashMap<>(); + optionMap2.put("value", "userInfo"); + optionMap2.put("label", "用户信息"); + options.add(optionMap2); + Map optionMap3 = new HashMap<>(); + optionMap3.put("value", "classifyInfo"); + optionMap3.put("label", "菜谱大类信息"); + options.add(optionMap3); + Map optionMap4 = new HashMap<>(); + optionMap4.put("value", "subClassifyInfo"); + optionMap4.put("label", "菜谱小类信息"); + options.add(optionMap4); + Map optionMap5 = new HashMap<>(); + optionMap5.put("value", "collectInfo"); + optionMap5.put("label", "收藏信息"); + options.add(optionMap5); + Map optionMap6 = new HashMap<>(); + optionMap6.put("value", "praiseInfo"); + optionMap6.put("label", "笔记点赞信息"); + options.add(optionMap6); + Map optionMap7 = new HashMap<>(); + optionMap7.put("value", "newsInfo"); + optionMap7.put("label", "饮食资讯信息"); + options.add(optionMap7); + Map optionMap8 = new HashMap<>(); + optionMap8.put("value", "messageInfo"); + optionMap8.put("label", "趣味答题信息"); + options.add(optionMap8); + + return Result.success(options); + } + + private void getPieData(String name, List pieList, Map dataMap) { + EchartsData pieData = new EchartsData(); + EchartsData.Series series = new EchartsData.Series(); + + Map titleMap = new HashMap<>(2); + titleMap.put("text", name + "信息"); + pieData.setTitle(titleMap); + + series.setName(name + "比例"); + series.setType("pie"); + series.setRadius("55%"); + + List objects = new ArrayList<>(); + List legendList = new ArrayList<>(); + for (String key : dataMap.keySet()) { + Integer value = dataMap.get(key); + objects.add(new JSONObject().putOpt("name", key).putOpt("value", value)); + legendList.add(key); + } + series.setData(objects); + + pieData.setSeries(Collections.singletonList(series)); + Map map = new HashMap<>(); + map.put("show", true); + pieData.setTooltip(map); + + Map legendMap = new HashMap<>(4); + legendMap.put("orient", "vertical"); + legendMap.put("x", "left"); + legendMap.put("y", "center"); + legendMap.put("data", legendList); + pieData.setLegend(legendMap); + + pieList.add(pieData); + } + + private void getBarData(String name, List barList, Map dataMap) { + EchartsData barData = new EchartsData(); + EchartsData.Series series = new EchartsData.Series(); + + List seriesObjs = new ArrayList<>(); + List xAxisObjs = new ArrayList<>(); + for (String key : dataMap.keySet()) { + Integer value = dataMap.get(key); + xAxisObjs.add(key); + seriesObjs.add(value); + } + + series.setType("bar"); + series.setName(name); + series.setData(seriesObjs); + barData.setSeries(Collections.singletonList(series)); + + Map xAxisMap = new HashMap<>(1); + xAxisMap.put("data", xAxisObjs); + barData.setxAxis(xAxisMap); + + barData.setyAxis(new HashMap<>()); + + Map legendMap = new HashMap<>(1); + legendMap.put("data", Collections.singletonList(name)); + barData.setLegend(legendMap); + + Map map = new HashMap<>(1); + map.put("show", true); + barData.setTooltip(map); + + Map titleMap = new HashMap<>(1); + titleMap.put("text", name + "信息"); + barData.setTitle(titleMap); + + barList.add(barData); + } +}