我正在尝试使用地图对我的事件数据进行分组。我试图完成的是将我的活动分组在周>预订者>每周预订的appt的汇总计数。到目前为止我的逻辑是:
map<string,list<Event>> Date2Event = new map<string,list<Event>>();
for(Event e: eventLst){
if(!Date2Event.containsKey(dateMapKey(e.CreatedDate))){
Date2Event.put(dateMapKey(e.CreatedDate), new list<Event>());
}
Date2Event.get(dateMapKey(e.CreatedDate)).add(e);
}
public string dateMapKey(datetime dt){
datetime myDt = dt;
date myDate = myDt.date();
date StartOfWeekDate = myDate.toStartOfWeek();
string mapKey = StartOfWeekDate.format() + '-' + StartOfWeekDate.addDays(6).format();
return mapKey;
}
所以我有一个函数,它基本上是用一个从开始日期到结束日期的字符串来设置地图的键。到目前为止,我所完成的是按周对事件进行分组。现在我需要更多地嵌套分组,并按周和按预订者分组,然后为该周的每个预订appt汇总计数。任何想法都将不胜感激。谢谢。
发布于 2018-12-21 06:16:03
过了一段时间,我终于决定了嵌套映射的最佳设计模式。我将在这里为其他人张贴我的答案,因为我得到的回应有时比所需的过于泛化。
map<string,map<string,map<string,decimal>>> mapData = new map<string,map<string,map<string,decimal>>>();
//get all events
for(Event e: eventLst){
string dateKey = dateMapKey(e.CreatedDate);
map<string,map<string,decimal>> bmap = new map<string,map<string,decimal>>();
map<string,decimal> fieldMap = new map<string,decimal>();
if(mapData.get(dateKey) != null){
bmap = mapData.get(datekey);
if(bmap.get(e.Booker__c) != null){
fieldMap = bmap.get(e.Booker__c);
}
}
decimal countShow=0,countNoShow=0,showPerc=0;
if(fieldMap.get('countShow') != null){
countShow = fieldMap.get('countShow');
}
if(fieldMap.get('countNoShow') != null){
countNoShow = fieldMap.get('countNoShow');
}
if(fieldMap.get('countNoShow') != null && fieldMap.get('countShow') != null){
showPerc = fieldMap.get('showPerc');
}
if(e.Booking_Status__c == 'Show'){
countShow++;
}
if(e.Booking_Status__c == 'No Show'){
countNoShow++;
}
showPerc = countNoShow != 0 ? (countShow / (countShow + countNoShow)* 100).setScale(1) : 0.0;
map<string,decimal> fm = new map<string,decimal>();
fm.put('countShow',countShow);
fm.put('countNoShow',countNoShow);
fm.put('showPerc',showPerc);
map<string,map<string,decimal>> bm = new map<string,map<string,decimal>>();
bm.put(e.Booker__c,fm);
mapData.put(dateKey,bm);
}
https://stackoverflow.com/questions/53863078
复制相似问题