Commit f3926aab by liang

事件的分析结果数据输出成Excel

70e153396066ac8610007408
40d0e2e2e7ca4c7d10007560
parent 861c3990
package com.liang.mongoevent.controller; package com.liang.mongoevent.controller;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.liang.mongoevent.pojo.Media; import com.liang.mongoevent.pojo.Media;
import com.liang.mongoevent.pojo.Weibo; import com.liang.mongoevent.service.SingleService;
import com.liang.mongoevent.service.MediaService;
import com.liang.mongoevent.service.WeiboService;
import com.liang.mongoevent.utils.RespBean; import com.liang.mongoevent.utils.RespBean;
import com.liang.mongoevent.vo.Trend2; import com.liang.mongoevent.vo.*;
import com.liang.mongoevent.vo.Trend3;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.time.format.DateTimeFormatter; import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
@RestController @RestController
@RequestMapping("/single") @RequestMapping("/single")
public class SingleController { public class SingleController {
@Autowired @Autowired
private MongoTemplate mongoTemplate; private SingleService singleService;
@Autowired @Autowired
private MediaService mediaService; private MongoTemplate mongoTemplate;
@Autowired
private WeiboService weiboService; @ApiOperation("事件的分析结果数据输出成Excel")
@GetMapping(value = "/exportExcel",produces = "application/octet-stream")
public RespBean exportExcel(String eventId, HttpServletResponse response){
return singleService.exportExcel(eventId,response);
}
/** /**
* *
...@@ -45,49 +50,8 @@ public class SingleController { ...@@ -45,49 +50,8 @@ public class SingleController {
*/ */
@ApiOperation("分析单个事件") @ApiOperation("分析单个事件")
@GetMapping("/singleEvent") @GetMapping("/singleEvent")
public RespBean singleEvent(String eventId){ public Map<String, Object> singleEvent(String eventId){
Map<String, Object> map=new LinkedHashMap<>();
Map<String, Integer> map1=new LinkedHashMap<>();
List<Map> list=new ArrayList<>();
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH");
Aggregation agg = Aggregation.newAggregation(
Aggregation.match(Criteria.where("eventId").is(eventId)),
Aggregation.group("time").first("time").as("time"),
Aggregation.sort(Sort.Direction.DESC,"time")
);
AggregationResults<Trend3> aggregate = mongoTemplate.aggregate(agg, Media.class, Trend3.class);
for (Trend3 trend3 : aggregate) {
String time = df.format(trend3.getTime().minusHours(8));
if (map1.containsKey(time)) {
int i = map1.get(time) + 1;
map1.put(time, i);
} else {
map1.put(time, 1);
}
}
AggregationResults<Trend2> aggregate1 = mongoTemplate.aggregate(agg, Weibo.class, Trend2.class);
for (Trend2 trend2 : aggregate1) {
String time = trend2.getTime().substring(0,13);
if (map1.containsKey(time)) {
int i = map1.get(time) + 1;
map1.put(time, i);
} else {
map1.put(time, 1);
}
}
map.put("eventId",eventId);
map.put("性别统计",weiboService.singleCountSex(eventId));
map.put("地域统计",weiboService.singleCountLocation(eventId));
map.put("用户发博数",weiboService.singleCountWeibo(eventId));
map.put("用户粉丝数前十",weiboService.singleTopTenFensiNum(eventId));
map.put("各级媒体占比",mediaService.singleProportion(eventId));
map.put("trend",map1); return singleService.singleEvent(eventId);
list.add(map);
return RespBean.success("成功",list);
} }
} }
package com.liang.mongoevent.service;
import com.liang.mongoevent.utils.RespBean;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
public interface SingleService {
Map<String, Object> singleEvent(String eventId);
RespBean exportExcel(String eventId, HttpServletResponse response);
}
package com.liang.mongoevent.service;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.liang.mongoevent.pojo.Media;
import com.liang.mongoevent.pojo.Weibo;
import com.liang.mongoevent.utils.RespBean;
import com.liang.mongoevent.vo.*;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.stereotype.Service;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.time.format.DateTimeFormatter;
import java.util.*;
@Service
public class SingleServiceImpl implements SingleService {
@Autowired
private MongoTemplate mongoTemplate;
@Autowired
private MediaService mediaService;
@Autowired
private WeiboService weiboService;
@Autowired
private SingleService singleService;
@Override
public Map<String, Object> singleEvent(String eventId) {
Map<String, Object> map=new LinkedHashMap<>();
Map<String, Integer> map1=new LinkedHashMap<>();
List<Map<String,Object>> list=new ArrayList<>();
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH");
Aggregation agg = Aggregation.newAggregation(
Aggregation.match(Criteria.where("eventId").is(eventId)),
Aggregation.group("time").first("time").as("time"),
Aggregation.sort(Sort.Direction.DESC,"time")
);
AggregationResults<Trend3> aggregate = mongoTemplate.aggregate(agg, Media.class, Trend3.class);
for (Trend3 trend3 : aggregate) {
String time = df.format(trend3.getTime().minusHours(8));
if (map1.containsKey(time)) {
int i = map1.get(time) + 1;
map1.put(time, i);
} else {
map1.put(time, 1);
}
}
AggregationResults<Trend2> aggregate1 = mongoTemplate.aggregate(agg, Weibo.class, Trend2.class);
for (Trend2 trend2 : aggregate1) {
String time = trend2.getTime().substring(0,13);
if (map1.containsKey(time)) {
int i = map1.get(time) + 1;
map1.put(time, i);
} else {
map1.put(time, 1);
}
}
map.put("eventId",eventId);
map.put("性别统计",weiboService.singleCountSex(eventId));
map.put("地域统计",weiboService.singleCountLocation(eventId));
map.put("用户发博数",weiboService.singleCountWeibo(eventId));
map.put("用户粉丝数前十",weiboService.singleTopTenFensiNum(eventId));
map.put("各级媒体占比",mediaService.singleProportion(eventId));
map.put("时间趋势",map1);
//list.add(map);
return map;
}
@Override
public RespBean exportExcel(String eventId, HttpServletResponse response) {
Map<String, Object> map = singleService.singleEvent(eventId);
// eventId
List<ExportVo> data=new ArrayList<>();
ExportVo exportVo = new ExportVo();
exportVo.setEventId((String) map.get("eventId"));
// 性别
List<SexCount> sexCounts=new ArrayList<>();
Map<String,Long> sexcount = (Map) map.get("性别统计");
sexCounts.add(new SexCount("男",sexcount.get("男")));
sexCounts.add(new SexCount("女",sexcount.get("女")));
exportVo.setSexCount(sexCounts);
// 地域
List<Location> locations=new ArrayList<>();
Map<String,Long> locationMap = (Map) map.get("地域统计");
Set<Map.Entry<String, Long>> entries = locationMap.entrySet();
for (Map.Entry<String, Long> entry : entries) {
locations.add(new Location(entry.getKey(),entry.getValue()));
}
exportVo.setLocation(locations);
// 用户发博数
List<VlogCount> vlogCounts=new ArrayList<>();
Map<String,Long> vlogMap=(Map)map.get("用户发博数");
Set<Map.Entry<String, Long>> entries1 = vlogMap.entrySet();
for (Map.Entry<String, Long> stringLongEntry : entries1) {
vlogCounts.add(new VlogCount(stringLongEntry.getKey(),stringLongEntry.getValue()));
}
exportVo.setVlogCount(vlogCounts);
// 参与用户粉丝数前十
List<FenSiCount> fenSiCounts=new ArrayList<>();
Map<String,Long> fenSiMap=(Map)map.get("用户粉丝数前十");
Set<Map.Entry<String, Long>> entries2 = fenSiMap.entrySet();
for (Map.Entry<String, Long> stringLongEntry : entries2) {
fenSiCounts.add(new FenSiCount(stringLongEntry.getKey(), stringLongEntry.getValue()));
}
exportVo.setFenSiCount(fenSiCounts);
// 各级媒体占比
List<MediaCount> mediaCounts=new ArrayList<>();
Map<String,Long> mediasMap=(Map)map.get("各级媒体占比");
Set<Map.Entry<String, Long>> entries3 = mediasMap.entrySet();
for (Map.Entry<String, Long> stringLongEntry : entries3) {
mediaCounts.add(new MediaCount(stringLongEntry.getKey(), stringLongEntry.getValue()));
}
exportVo.setMediaCount(mediaCounts);
//
List<TimeCount> timeCounts=new ArrayList<>();
Map<String,Integer> timeMap=(Map)map.get("时间趋势");
Set<Map.Entry<String, Integer>> entries4 = timeMap.entrySet();
for (Map.Entry<String, Integer> stringLongEntry : entries4) {
timeCounts.add(new TimeCount(stringLongEntry.getKey(), stringLongEntry.getValue()));
}
exportVo.setTimeCount(timeCounts);
data.add(exportVo);
ExportParams params = new ExportParams(eventId,eventId, ExcelType.HSSF);
Workbook workbook = ExcelExportUtil.exportExcel(params, ExportVo.class, data);
ServletOutputStream outputStream=null;
try {
response.setHeader("content-haha","en");
response.setHeader("content-type","application/octet-stream");
response.setHeader("Content-disposition","attachment;fileName="+ URLEncoder.encode(eventId+".xls","UTF-8"));
outputStream = response.getOutputStream();
workbook.write(outputStream);
} catch (IOException e) {
e.printStackTrace();
}finally {
if(null!=outputStream){
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(null!=workbook){
try {
workbook.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return RespBean.success("ok");
}
}
package com.liang.mongoevent.vo;
import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelCollection;
import cn.afterturn.easypoi.excel.annotation.ExcelIgnore;
import cn.afterturn.easypoi.excel.annotation.ExcelTarget;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
@Data
@AllArgsConstructor
@NoArgsConstructor
@ExcelTarget("exportVo")
public class ExportVo {
@Excel(name = "eventId",width = 40.0,orderNum = "0")
private String eventId;
@ExcelCollection(name = "性别统计",orderNum = "1")
private List<SexCount> sexCount;
@ExcelCollection(name = "地域统计",orderNum = "2")
private List<Location> location;
@ExcelCollection(name = "用户发博数",orderNum = "3")
private List<VlogCount> vlogCount;
@ExcelCollection(name = "参与用户粉丝数前十",orderNum = "4")
private List<FenSiCount> fenSiCount;
@ExcelCollection(name = "各级媒体占比",orderNum = "5")
private List<MediaCount> mediaCount;
@ExcelCollection(name = "时间趋势",orderNum = "6")
private List<TimeCount> timeCount;
}
package com.liang.mongoevent.vo;
import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelTarget;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@ExcelTarget("fenSiCount")
public class FenSiCount {
@Excel(name = "用户名",orderNum = "9",width = 20.0)
private String username;
@Excel(name = "粉丝数",orderNum = "10",width = 20.0)
private Long count;
}
package com.liang.mongoevent.vo; package com.liang.mongoevent.vo;
import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelTarget;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
...@@ -7,8 +9,10 @@ import lombok.NoArgsConstructor; ...@@ -7,8 +9,10 @@ import lombok.NoArgsConstructor;
@Data @Data
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor @NoArgsConstructor
@ExcelTarget("location")
public class Location { public class Location {
private String id; @Excel(name = "地域",orderNum = "5",width = 20.0)
private String location; private String location;
@Excel(name = "数量",orderNum = "6",width = 20.0)
private Long count; private Long count;
} }
package com.liang.mongoevent.vo;
import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelTarget;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@ExcelTarget("mediaCount")
public class MediaCount {
@Excel(name = "媒体名",orderNum = "11",width = 20.0)
private String username;
@Excel(name = "数量",orderNum = "12",width = 20.0)
private Long count;
}
package com.liang.mongoevent.vo;
import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelTarget;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@ExcelTarget("sexCount")
public class SexCount {
@Excel(name = "性别",orderNum = "3",width = 20.0)
private String gender;
@Excel(name = "数量",orderNum = "4",width = 20.0)
private Long count;
}
package com.liang.mongoevent.vo; package com.liang.mongoevent.vo;
import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelTarget;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
...@@ -7,8 +9,10 @@ import lombok.NoArgsConstructor; ...@@ -7,8 +9,10 @@ import lombok.NoArgsConstructor;
@Data @Data
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor @NoArgsConstructor
@ExcelTarget("timeCount")
public class TimeCount { public class TimeCount {
private String id; @Excel(name = "时间",orderNum = "13",width = 20.0)
private String time; private String time;
@Excel(name = "数量",orderNum = "14",width = 20.0)
private long count; private long count;
} }
package com.liang.mongoevent.vo;
import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelTarget;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@ExcelTarget("vlogCount")
public class VlogCount {
@Excel(name = "用户名",orderNum = "7",width = 20.0)
private String username;
@Excel(name = "数量",orderNum = "8",width = 20.0)
private Long count;
}
package com.liang.mongoevent;
import com.liang.mongoevent.service.SingleService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
@SpringBootTest
public class TestPoI {
@Autowired
private SingleService singleService;
@Test
public void testData() throws IllegalAccessException {
String eventId="62b63a8069b6835310006494";
Map<String, Object> map = singleService.singleEvent(eventId);
System.out.println(map.get("eventId"));
Map sexcount = (Map) map.get("性别统计");
System.out.println(sexcount.get("女"));
System.out.println(sexcount.get("男"));
Map<String,Long> locationMap = (Map) map.get("地域统计");
Set<Map.Entry<String, Long>> entries = locationMap.entrySet();
for (Map.Entry<String, Long> entry : entries) {
System.out.println(entry.getKey());
}
}
public static Map<String, Object> getObjectToMap(Object obj) throws IllegalAccessException {
Map<String, Object> map = new HashMap<String, Object>();
Class<?> cla = obj.getClass();
Field[] fields = cla.getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
String keyName = field.getName();
Object value = field.get(obj);
if (value == null)
value = "";
map.put(keyName, value);
}
return map;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment