优化统计分析
This commit is contained in:
@@ -60,34 +60,30 @@ public class CountDemo {
|
||||
//统计时间
|
||||
List<String> days = listDate(startDate, endDate);
|
||||
result.put("xAxis", days);
|
||||
//统计数据
|
||||
JSONArray aa = new JSONArray();
|
||||
JSONArray bb = new JSONArray();
|
||||
if (list == null) {
|
||||
list = Collections.EMPTY_LIST;
|
||||
}
|
||||
Map<String, List<JSONObject>> collect = list.stream().collect(Collectors.groupingBy(obj -> obj.getString("day")));
|
||||
days.forEach(day -> {
|
||||
List<JSONObject> sample = collect.get(day);
|
||||
if (sample != null) {
|
||||
int sumA = sample.stream().filter(obj -> "aa".equals(obj.getString("item"))).mapToInt(obj -> obj.getInteger("value")).sum();
|
||||
int sumB = sample.stream().filter(obj -> "bb".equals(obj.getString("item"))).mapToInt(obj -> obj.getInteger("value")).sum();
|
||||
aa.add(sumA);
|
||||
bb.add(sumB);
|
||||
} else {
|
||||
aa.add(0);
|
||||
bb.add(0);
|
||||
}
|
||||
});
|
||||
JSONObject obj;
|
||||
JSONArray array;
|
||||
JSONArray series = new JSONArray();
|
||||
JSONObject obj = new JSONObject();
|
||||
obj.put("name", "aa");
|
||||
obj.put("data", aa);
|
||||
series.add(obj);
|
||||
obj = new JSONObject();
|
||||
obj.put("name", "bb");
|
||||
obj.put("data", bb);
|
||||
series.add(obj);
|
||||
//统计数据
|
||||
for (String item : ITEM.split(",")) {
|
||||
obj = new JSONObject();
|
||||
obj.put("name", item);
|
||||
array = new JSONArray();
|
||||
for (String day : days) {
|
||||
List<JSONObject> sample = collect.get(day);
|
||||
if (sample != null) {
|
||||
int sum = sample.stream().filter(it -> item.equals(it.getString("item"))).mapToInt(it -> it.getInteger("value")).sum();
|
||||
array.add(sum);
|
||||
} else {
|
||||
array.add(0);
|
||||
}
|
||||
}
|
||||
obj.put("data", array);
|
||||
series.add(obj);
|
||||
}
|
||||
result.put("series", series);
|
||||
return result;
|
||||
}
|
||||
@@ -106,31 +102,18 @@ public class CountDemo {
|
||||
if (list == null) {
|
||||
list = Collections.EMPTY_LIST;
|
||||
}
|
||||
Map<String, List<JSONObject>> collect = list.stream().collect(Collectors.groupingBy(obj -> obj.getString("item")));
|
||||
Map<String, List<JSONObject>> collect = list.stream().collect(Collectors.groupingBy(obj -> obj.getString("day")));
|
||||
JSONObject obj;
|
||||
for (String name : ITEM.split(",")) {
|
||||
for (String item : ITEM.split(",")) {
|
||||
obj = new JSONObject();
|
||||
obj.put("name", name);
|
||||
List<JSONObject> sample = collect.get(name);
|
||||
if (sample != null) {
|
||||
Iterator<JSONObject> it;
|
||||
for (String day : days) {
|
||||
int sum = 0;
|
||||
it = sample.iterator();
|
||||
JSONObject next;
|
||||
while (it.hasNext()) {
|
||||
next = it.next();
|
||||
String date = next.getString("day");
|
||||
int value = next.getInteger("value");
|
||||
if (day.equals(date)) {
|
||||
sum += value;
|
||||
it.remove();
|
||||
}
|
||||
}
|
||||
obj.put("name", item);
|
||||
for (String day : days) {
|
||||
List<JSONObject> sample = collect.get(day);
|
||||
if (sample != null) {
|
||||
int sum = sample.stream().filter(it -> item.equals(it.getString("item"))).mapToInt(it -> it.getInteger("value")).sum();
|
||||
obj.put(day, sum);
|
||||
}
|
||||
} else {
|
||||
for (String day : days) {
|
||||
|
||||
} else {
|
||||
obj.put(day, 0);
|
||||
}
|
||||
}
|
||||
@@ -173,7 +156,7 @@ public class CountDemo {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数据列表
|
||||
* 模拟数据列表
|
||||
*
|
||||
* @param startDate
|
||||
* @param endDate
|
||||
@@ -188,7 +171,8 @@ public class CountDemo {
|
||||
obj = new JSONObject();
|
||||
obj.put("day", object);
|
||||
obj.put("value", ++i);
|
||||
obj.put("item", ITEM.split(",")[new Random().nextInt(2)]);
|
||||
String[] array = ITEM.split(",");
|
||||
obj.put("item", array[new Random().nextInt(array.length)]);
|
||||
result.add(obj);
|
||||
}
|
||||
return result;
|
||||
@@ -213,12 +197,14 @@ public class CountDemo {
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
List<JSONObject> list = listByDate("2018-01-01", "2018-01-02");
|
||||
String startDate = "2018-01-01";
|
||||
String endDate = "2018-01-03";
|
||||
List<JSONObject> list = listByDate(startDate, endDate);
|
||||
// List<JSONObject> list = new ArrayList<>();
|
||||
// List<JSONObject> list = null;
|
||||
log.info(JSON.toJSONString(pie(list), SerializerFeature.PrettyFormat));
|
||||
log.info(JSON.toJSONString(eChartsBarOrLine("2018-01-01", "2018-01-02", list), SerializerFeature.PrettyFormat));
|
||||
log.info(JSON.toJSONString(g2Bar("2018-01-01", "2018-01-02", list), SerializerFeature.PrettyFormat));
|
||||
log.info(JSON.toJSONString(g2Line("2018-01-01", "2018-01-02", list), SerializerFeature.PrettyFormat));
|
||||
log.info("pie:{}", JSON.toJSONString(pie(list), SerializerFeature.PrettyFormat));
|
||||
log.info("eChartsBarOrLine{}", JSON.toJSONString(eChartsBarOrLine(startDate, endDate, list), SerializerFeature.PrettyFormat));
|
||||
log.info("g2Bar{}" + JSON.toJSONString(g2Bar(startDate, endDate, list), SerializerFeature.PrettyFormat));
|
||||
log.info("g2Line{}", JSON.toJSONString(g2Line(startDate, endDate, list), SerializerFeature.PrettyFormat));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,10 +15,7 @@ import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
@@ -29,6 +26,7 @@ import java.util.stream.Collectors;
|
||||
*/
|
||||
@Service
|
||||
public class SysUserLoginInOutService {
|
||||
public static final int[] ITEM = {1, 2};
|
||||
@Resource
|
||||
private SysUserLoginInOutMapper loginInOutMapper;
|
||||
|
||||
@@ -87,6 +85,8 @@ public class SysUserLoginInOutService {
|
||||
* @return
|
||||
*/
|
||||
public JSONArray pie(String startDate, String endDate) {
|
||||
JSONArray result = new JSONArray();
|
||||
//获取数据
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
if (StringUtils.isNotEmpty(startDate)) {
|
||||
map.put("startDate", startDate + " 00:00:00");
|
||||
@@ -95,23 +95,22 @@ public class SysUserLoginInOutService {
|
||||
map.put("endDate", endDate + " 23:59:59");
|
||||
}
|
||||
List<SysUserLoginInOut> list = loginInOutMapper.countByType(map);
|
||||
int in = 0;
|
||||
int out = 0;
|
||||
for (SysUserLoginInOut inOut : list) {
|
||||
if (inOut.getInoutType() == 1) {
|
||||
in = inOut.getCount();
|
||||
//统计数据
|
||||
Map<Integer, List<SysUserLoginInOut>> collect = list.stream().collect(Collectors.groupingBy(SysUserLoginInOut::getInoutType));
|
||||
JSONObject obj;
|
||||
for (int item : ITEM) {
|
||||
obj = new JSONObject();
|
||||
String name = Arrays.stream(CommonEnum.LoginInOutEnum.values()).filter(login -> login.getValue() == item).findFirst().get().getName();
|
||||
obj.put("name", name);
|
||||
List<SysUserLoginInOut> sample = collect.get(item);
|
||||
if (sample != null) {
|
||||
int sum = sample.get(0).getCount();
|
||||
obj.put("value", sum);
|
||||
} else {
|
||||
out = inOut.getCount();
|
||||
obj.put("value", 0);
|
||||
}
|
||||
result.add(obj);
|
||||
}
|
||||
JSONArray result = new JSONArray();
|
||||
JSONObject obj = new JSONObject();
|
||||
obj.put("name", "in");
|
||||
obj.put("value", in);
|
||||
result.add(obj);
|
||||
obj = new JSONObject();
|
||||
obj.put("name", out);
|
||||
obj.put("value", out);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -124,10 +123,10 @@ public class SysUserLoginInOutService {
|
||||
*/
|
||||
public Map<String, Object> bar(String startDate, String endDate) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
//统计时间
|
||||
//统计日期
|
||||
List<String> days = DateUtil2.listDate(startDate, endDate);
|
||||
result.put("xAxis", days);
|
||||
//统计数据
|
||||
//获取数据
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
if (StringUtils.isNotEmpty(startDate)) {
|
||||
map.put("startDate", startDate + " 00:00:00");
|
||||
@@ -136,40 +135,29 @@ public class SysUserLoginInOutService {
|
||||
map.put("endDate", endDate + " 23:59:59");
|
||||
}
|
||||
List<SysUserLoginInOut> list = loginInOutMapper.findByInoutType(map);
|
||||
JSONArray in = new JSONArray();
|
||||
JSONArray out = new JSONArray();
|
||||
if (CollectionUtils.isNotEmpty(list)) {
|
||||
Map<String, List<SysUserLoginInOut>> collect = list.stream().peek(obj -> obj.setRecordTime(obj.getRecordTime().substring(0, 10))).collect(Collectors.groupingBy(SysUserLoginInOut::getRecordTime));
|
||||
days.forEach(day -> {
|
||||
List<SysUserLoginInOut> sample = collect.get(day);
|
||||
if (CollectionUtils.isNotEmpty(sample)) {
|
||||
int sumIn = sample.stream().filter(obj -> obj.getInoutType() == 1).mapToInt(SysUserLoginInOut::getCount).sum();
|
||||
int sumOut = sample.stream().filter(obj -> obj.getInoutType() == 2).mapToInt(SysUserLoginInOut::getCount).sum();
|
||||
in.add(sumIn);
|
||||
out.add(sumOut);
|
||||
} else {
|
||||
in.add(0);
|
||||
out.add(0);
|
||||
}
|
||||
});
|
||||
|
||||
} else {
|
||||
days.forEach(day -> {
|
||||
in.add(0);
|
||||
out.add(0);
|
||||
});
|
||||
}
|
||||
//统计数据
|
||||
Map<String, List<SysUserLoginInOut>> collect = list.stream().peek(loginInOut -> loginInOut.setRecordTime(loginInOut.getRecordTime().substring(0, 10))).collect(Collectors.groupingBy(SysUserLoginInOut::getRecordTime));
|
||||
JSONObject obj;
|
||||
JSONArray array;
|
||||
JSONArray series = new JSONArray();
|
||||
JSONObject obj = new JSONObject();
|
||||
obj.put("name", "in");
|
||||
obj.put("data", in);
|
||||
series.add(obj);
|
||||
obj = new JSONObject();
|
||||
obj.put("name", "out");
|
||||
obj.put("data", out);
|
||||
series.add(obj);
|
||||
for (int item : ITEM) {
|
||||
obj = new JSONObject();
|
||||
String name = Arrays.stream(CommonEnum.LoginInOutEnum.values()).filter(login -> login.getValue() == item).findFirst().get().getName();
|
||||
obj.put("name", name);
|
||||
array = new JSONArray();
|
||||
for (String day : days) {
|
||||
List<SysUserLoginInOut> sample = collect.get(day);
|
||||
if (sample != null) {
|
||||
int sum = (int) sample.stream().filter(it -> item == it.getInoutType()).count();
|
||||
array.add(sum);
|
||||
} else {
|
||||
array.add(0);
|
||||
}
|
||||
}
|
||||
obj.put("data", array);
|
||||
series.add(obj);
|
||||
}
|
||||
result.put("series", series);
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -71,10 +71,10 @@
|
||||
<include refid="table"/>
|
||||
<where>
|
||||
<if test="startDate!=null">
|
||||
and a.create_time>=#{startDate}
|
||||
and create_time>=#{startDate}
|
||||
</if>
|
||||
<if test="endDate!=null">
|
||||
and a.create_time <![CDATA[ <= ]]> #{endDate}
|
||||
and create_time <![CDATA[ <= ]]> #{endDate}
|
||||
</if>
|
||||
</where>
|
||||
group by inout_type
|
||||
|
||||
@@ -46,6 +46,7 @@ public class SysUserLoginInOutControl {
|
||||
}
|
||||
|
||||
@LogInfo("获取饼图数据")
|
||||
@ApiOperation(value = "获取饼图数据")
|
||||
@GetMapping("pie")
|
||||
public JSONObject pie(@RequestParam(required = false) String startDate, @RequestParam(required = false) String endDate) {
|
||||
JSONArray pie = inOutService.pie(startDate, endDate);
|
||||
@@ -53,6 +54,7 @@ public class SysUserLoginInOutControl {
|
||||
}
|
||||
|
||||
@LogInfo("获取柱状图数据")
|
||||
@ApiOperation(value = "获取柱状图数据")
|
||||
@GetMapping("bar")
|
||||
public JSONObject bar(@RequestParam(required = false) String startDate, @RequestParam(required = false) String endDate) {
|
||||
Map<String, Object> bar = inOutService.bar(startDate, endDate);
|
||||
|
||||
Reference in New Issue
Block a user