Java Spring Boot根据多个日期组合获取嵌套字段组的总和
我有下面的实体结构。产品与内向外列表之间存在一对一关系,而内向外列表与内向库存之间存在多对多关系。InwardInventory有字段日期
public class Product extends ReusableFields
{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
Long productId;
@NonNull
@Column(name = "product_name")
String productName;
//more fields
}
public class InwardOutwardList extends ReusableFields
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Long entryid;
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "productId", nullable = false)
@JsonIgnoreProperties(
{ "hibernateLazyInitializer", "handler" })
Product product;
Double quantity;
//more fields
public class InwardInventory extends ReusableFields implements Cloneable
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "inwardid")
Long inwardid;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy")
@Column(nullable = false)
@NonNull
Date date;
@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinTable(name = "inwardinventory_entry", joinColumns =
{ @JoinColumn(name = "inwardid", referencedColumnName = "inwardid") }, inverseJoinColumns =
{ @JoinColumn(name = "entryId", referencedColumnName = "entryId") })
Set<InwardOutwardList> inwardOutwardList = new HashSet<>();
//more fields
}
要求-我想在仪表板上显示10种产品,包括今天、本周和本月收到的向内和(向内和向外列表的和。数量)
我有两份清单
List<Product> productsForDashboard = getDashboardProducts(); //List of products to be displayed on dashboard
List<InwardInventory> inwardsInOneMonth =iiRepo.getCurrentMonthData(); //list of inward this months
现在,我想根据3种可能性(今天、本周、本月)了解groupby Inward Sinonemonth的最佳方法。我应该使用3种不同的流,还是可以在单个流或任何其他建议的方法中使用,以获得更好的性能
期待
{
"Product":"Iron",
"Inward":
{
"Daily":20,
"Weekly":30,
"Monthly":70
}
}
我使用嵌套循环编写了一个工作逻辑,但这似乎很沉重。下面是我写的逻辑
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
HashMap<Product,InventoryHistoricalStats> map = new HashMap<>();
initializeMap(productsForDashboard,map); //add all products to map and set sum as zero
for(Product p:productsForDashboard) {
for(InwardInventory ii:inwardsInOneMonth){
for(InwardOutwardList iol:ii.getInwardOutwardList()){
if(iol.getProduct().equals(p)){
InventoryHistoricalStats ihs = map.get(p);
cal1.setTime(ii.getDate());
cal2.setTime(new Date());
if(cal1.get(Calendar.DAY_OF_YEAR)==cal1.get(Calendar.DAY_OF_YEAR))
ihs.getInward().setDaily(ihs.getInward().getDaily()+iol.getQuantity());
if(cal1.get(Calendar.WEEK_OF_YEAR)==cal1.get(Calendar.WEEK_OF_YEAR))
ihs.getInward().setWeekly(ihs.getInward().getWeekly()+iol.getQuantity());
if(cal1.get(Calendar.MONTH)==cal1.get(Calendar.MONTH))
ihs.getInward().setMonthly(ihs.getInward().getMonthly()+iol.getQuantity());
map.replace(p,ihs);
}
}
}
}
}
}
return transformDataForDashboard(map);
}
private List<InventoryHistoricalStats> transformDataForDashboard(HashMap<Product, InventoryHistoricalStats> map) {
List<InventoryHistoricalStats> returnData = new ArrayList<>();
for(Map.Entry<Product,InventoryHistoricalStats> set:map.entrySet()){
InventoryHistoricalStats ihs = set.getValue();
ihs.setProductName(set.getKey().getProductName());
ihs.setMeasurementUnit(set.getKey().getMeasurementUnit());
returnData.add(ihs);
}
return returnData;
}
共 (0) 个答案