实例
public Map<String, Object> lifeCycle(QueryOrderProductLifeCircle queryOrderProductLifeCircle) {
Map<String, Object> map = new LinkedHashMap<>();
//查询所有不是Z4的
List<GrandvisionOrderLifeCircleDO> orders = grandVisionOrderDetailMapper.getAllOrderByTime(queryOrderProductLifeCircle.withOutZ4(true));
//查询z4且productAttribute3是Color
// List<GrandvisionOrderLifeCircleDO> color = grandVisionOrderDetailMapper.getAllOrderByTime(queryOrderProductLifeCircle.withOutZ4(false).productAttribute3("Color"));
//美瞳日抛
List<GrandvisionOrderLifeCircleDO> colorDaily = grandVisionOrderDetailMapper.getAllOrderByTime(queryOrderProductLifeCircle.withOutZ4(false).productAttribute3("Color").productAttribute1("daily"));
//美瞳两周抛
List<GrandvisionOrderLifeCircleDO> colorBiWeekly = grandVisionOrderDetailMapper.getAllOrderByTime(queryOrderProductLifeCircle.withOutZ4(false).productAttribute3("Color").productAttribute1("bi-weekly"));
//美瞳月抛
List<GrandvisionOrderLifeCircleDO> colorMonthy = grandVisionOrderDetailMapper.getAllOrderByTime(queryOrderProductLifeCircle.withOutZ4(false).productAttribute3("Color").productAttribute1("monthly"));
//美瞳半年抛
List<GrandvisionOrderLifeCircleDO> colorSemiAnnual = grandVisionOrderDetailMapper.getAllOrderByTime(queryOrderProductLifeCircle.withOutZ4(false).productAttribute3("Color").productAttribute1("semi-annual"));
//美瞳年抛
List<GrandvisionOrderLifeCircleDO> colorAnnual = grandVisionOrderDetailMapper.getAllOrderByTime(queryOrderProductLifeCircle.withOutZ4(false).productAttribute3("Color").productAttribute1("annual"));
//查询z4且productAttribute3是Plain
// List<GrandvisionOrderLifeCircleDO> plain = grandVisionOrderDetailMapper.getAllOrderByTime(queryOrderProductLifeCircle.withOutZ4(false).productAttribute3("Plain"));
//普片日抛
List<GrandvisionOrderLifeCircleDO> plainDaily = grandVisionOrderDetailMapper.getAllOrderByTime(queryOrderProductLifeCircle.withOutZ4(false).productAttribute3("Plain").productAttribute1("daily"));
//普片两周抛
List<GrandvisionOrderLifeCircleDO> plainBiWeekly = grandVisionOrderDetailMapper.getAllOrderByTime(queryOrderProductLifeCircle.withOutZ4(false).productAttribute3("Plain").productAttribute1("bi-weekly"));
//普片月抛
List<GrandvisionOrderLifeCircleDO> plainMonthy = grandVisionOrderDetailMapper.getAllOrderByTime(queryOrderProductLifeCircle.withOutZ4(false).productAttribute3("Plain").productAttribute1("monthly"));
//普片半年抛
List<GrandvisionOrderLifeCircleDO> plainSemiAnnual = grandVisionOrderDetailMapper.getAllOrderByTime(queryOrderProductLifeCircle.withOutZ4(false).productAttribute3("Plain").productAttribute1("semi-annual"));
//普片年抛
List<GrandvisionOrderLifeCircleDO> plainAnnual = grandVisionOrderDetailMapper.getAllOrderByTime(queryOrderProductLifeCircle.withOutZ4(false).productAttribute3("Plain").productAttribute1("annual"));
//key商品类型名称进行分组放进集合
Map<String, List<GrandvisionOrderLifeCircleDO>> groupByCatetory = orders.stream().collect(Collectors.groupingBy(n -> n.getCategoryName()));
groupByCatetory.put("Z4Colordaily", colorDaily);
groupByCatetory.put("Z4Colorbi-weekly", colorBiWeekly);
groupByCatetory.put("Z4Colormonthly", colorMonthy);
groupByCatetory.put("Z4Colorsemi-annual", colorSemiAnnual);
groupByCatetory.put("Z4Colorannual", colorAnnual);
groupByCatetory.put("Z4Plaindaily", plainDaily);
groupByCatetory.put("Z4Plainbi-weekly", plainBiWeekly);
groupByCatetory.put("Z4Plainmonthly", plainMonthy);
groupByCatetory.put("Z4Plainsemi-annual", plainSemiAnnual);
groupByCatetory.put("Z4Plainannual", plainAnnual);
//循环每个集合进行操作
groupByCatetory.forEach((categoryName, orderList) -> {
//key unionid 根据用户唯一标识进行订单分组
Map<String, List<GrandvisionOrderLifeCircleDO>> groupByUnionid = orderList.stream().collect(Collectors.groupingBy(n ->
n.getUnionid()
));
GrandvisionTimestampBO b = new GrandvisionTimestampBO(0L);
groupByUnionid.forEach((unionid, unionOrders) -> {
//如果单个人订单数大于1 就算乡里的两个的时间差
if (unionOrders.size() > 1) {
// miao
b.setTime(b.getTime() + getTime(unionOrders, 0, 0L));
}
});
if (groupByUnionid.size() > 0) {
map.put(categoryName, b.getTime() / groupByUnionid.size() / 60 / 24);
} else {
log.info("商品分类名称:" + categoryName + " 时间差:" + b.getTime() + "会员集合大小:" + groupByUnionid.size());
map.put(categoryName, 0);
}
});
return returnMap(map);
}
算相邻两个时间差总和的方法
private Long getTime(List<GrandvisionOrderLifeCircleDO> orderList, int index, Long count) {
Long currentCount = count + (orderList.get(index).getCreateTime().getTime() - orderList.get(index + 1).getCreateTime().getTime()) / 1000;
if (orderList.size() - 2 == index) {
return currentCount;
} else {
return getTime(orderList, ++index, currentCount);
}
}
基础类
package me.ocheng.module.grandvision.entity.domain;
import lombok.Data;
import java.util.Date;
/**
* @author: jerry
* @date: 2019/1/17 7:16 PM
*/
@Data
public class GrandvisionOrderLifeCircleDO {
private String categoryName;
private String productAttribute;
private String unionid;
private Long memberId;
private Date createTime;
private Integer productNum;
private Integer discountPrice;
}