// 获取每天/每个月/每年新增数量 近7天 近30天 近365天
public Map<String, Object> getNewAddCount(HttpServletRequest request) {
Map<String, Object> map = new HashMap<String, Object>();
String day = request.getParameter("day");
List<String> dayList = MyUtil.getDaysBetwwen(Integer.parseInt(day)); // 获取最近N天的每一天列表
// 新增用户数量
List<Map<String, Object>> newAddUserList = userMapper.getUserNewAddCount(Integer.parseInt(day)); // 有数据的结果
// 新增店铺数量
List<Map<String, Object>> newAddShopList = shopMapper.getShopNewAddCount(Integer.parseInt(day)); // 有数据的结果
// 新增店员数量
List<Map<String, Object>> newAddEmployeeList = employeeMapper.getEmployeeNewAddCount(Integer.parseInt(day)); // 有数据的结果
for(int k = 0; k < 3; k++){
List<Map<String, Object>> listMap = new ArrayList<Map<String, Object>>();
List<Map<String, Object>> cachList = new ArrayList<Map<String, Object>>();
String cashMapName = "";
String resultMapStr = "";
if(k == 0){
cachList = newAddUserList;
cashMapName = "newAddUserList";
}else if(k == 1){
cachList = newAddShopList;
cashMapName = "newAddShopList";
}else if(k == 2){
cachList = newAddEmployeeList;
cashMapName = "newAddEmployeeList";
}
for(int i = 0 ; i < dayList.size() ; i++) {
Map<String, Object> resultMap = new HashMap<String, Object>();
String num = "";
for (Map<String, Object> li : cachList) {
String createTime = li.get("createTime").toString(); // 时间
if(createTime.equals(dayList.get(i))){
num = li.get("number").toString(); // 数量
}
}
resultMap.put(dayList.get(i).toString(), num);
listMap.add(resultMap);
};
resultMapStr = JSON.toJSONString(listMap); // 解决 Java 对象序列化 JSON时,数据出现引用结构 $ref
map.put(cashMapName, JSON.parse(resultMapStr));
}
return map;
}
核心代码
resultMapStr = JSON.toJSONString(listMap); // 解决 Java 对象序列化 JSON时,数据出现引用结构 $ref
map.put(cashMapName, JSON.parse(resultMapStr));