举例1.统计一周或一月的订单交易量,你会怎么写
@ApiOperationSupport(author = "@王",responses = @DynamicResponseParameters(properties = {
@DynamicParameter(name = "date",value = "日期"),
@DynamicParameter(name = "quantity",value = "数量")
}))
@ApiOperation(value = "订单预览")
@PostMapping(value = "/orderOverview")
public ValueHolder<List<JSONObject>> orderOverview(@ApiParam(value = "类型(近一周:1,近一月:2)") @RequestParam(defaultValue = "1") Integer type) {
Date nowDate = new Date();
Date earlyDate;
Map<String,Integer> map;
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
if(type==1){
earlyDate = DateUtil.getEarlyRecentWeek();
map = new HashMap<>();
}else {
earlyDate = DateUtil.getEarlyRecentMonth();
map = new HashMap<>(40);
}
List<YqlOrder> orderList = orderService.list(new LambdaQueryWrapper<YqlOrder>().ge(YqlOrder::getVerifyTime, earlyDate).eq(YqlOrder::getStatus, OrderStatusEnum.END.getVal()));
orderList.stream().map(YqlOrder::getVerifyTime).forEach(date -> map.merge(sdf.format(date),1,Integer::sum));
List<JSONObject> list = new ArrayList<>();
Calendar calendar = Calendar.getInstance();
calendar.setTime(earlyDate);
while (calendar.getTime().getTime()<nowDate.getTime()){
String format = sdf.format(calendar.getTime());
JSONObject jsonObject = new JSONObject();
jsonObject.put("date",format);
jsonObject.put("quantity",map.containsKey(format)?map.get(format):0);
list.add(jsonObject);
calendar.add(Calendar.DATE,1);
}
return ValueHolder.success(list);
}
2.统计网点销量排行数据,你会怎么写
@ApiOperationSupport(author = "@王",responses = @DynamicResponseParameters(properties = {
@DynamicParameter(name = "name",value = "网点名称"),
@DynamicParameter(name = "quantity",value = "销量")
}))
@ApiOperation(value = "销量排行")
@PostMapping(value = "/salesRanking")
public ValueHolder<List<JSONObject>> salesRanking(@ApiParam(value = "类型(今日:1,近一周:2,近一月:3)") @RequestParam(defaultValue = "1") Integer type) {
Date earlyDate;
Map<Integer,Integer> map = new HashMap<>();
if(type==1){
earlyDate = DateUtil.getEarlyThisMorning();
}else if(type==2){
earlyDate = DateUtil.getEarlyRecentWeek();
}else {
earlyDate = DateUtil.getEarlyRecentMonth();
}
List<YqlOrder> orderList = orderService.list(new LambdaQueryWrapper<YqlOrder>().ge(YqlOrder::getVerifyTime, earlyDate).eq(YqlOrder::getStatus, OrderStatusEnum.END.getVal()));
orderList.stream().map(YqlOrder::getShopId).forEach(shopId -> map.merge(shopId,1,Integer::sum));
List<JSONObject> list = new ArrayList<>();
map.forEach((key,value)->{
Shop shop = shopService.getById(key);
JSONObject jsonObject = new JSONObject();
jsonObject.put("name",shop.getName());
jsonObject.put("quantity",value);
list.add(jsonObject);
});
//自定义比较器
Comparator<JSONObject> valCmp = new Comparator<JSONObject>() {
@Override
public int compare(JSONObject o1, JSONObject o2) {
return o2.getInteger("quantity")-o1.getInteger("quantity");
}
};
Collections.sort(list,valCmp);
return ValueHolder.success(list);
}