首页 > 代码库 > groupBy
groupBy
public List groupBy(List list,String flag,String... sortName) throws Exception{
Map<String,List<Object>> tMap = new HashMap<String,List<Object>>();
for(Object t : list){
String filedKey ="";
for(String filedName : sortName){
Field field = t.getClass().getDeclaredField(filedName);
filedKey = field.get(t)+","+filedKey;
System.out.println("filedName======"+filedKey);
}
if(tMap.containsKey(filedKey)){
tMap.get(filedKey).add(t);
}else{
List tList1 = new ArrayList();
tList1.add(t);
tMap.put(filedKey,tList1);
}
}
Field fields[]=list.get(0).getClass().getDeclaredFields();//获得对象所有属性
Field field=null;
for (int i = 0; i < fields.length; i++) {
field=fields[i];
field.setAccessible(true);
}
List<CoastClaimResult> claimList = new ArrayList<CoastClaimResult>();
for(Map.Entry<String,List<Object>> entry : tMap.entrySet()){
BigDecimal billedAmt=new BigDecimal(0);
BigDecimal paidAmt = new BigDecimal(0);
List<String> distinctCountList = new ArrayList<String>();
CoastClaimResult result = new CoastClaimResult();
System.out.println("Key = " + entry.getKey());
for(Object t : entry.getValue()){
System.out.println(t.toString());
String bill = (String) t.getClass().getDeclaredField("amount"+flag).get(t);
billedAmt.add(new BigDecimal(bill));
String claimNo = (String) t.getClass().getDeclaredField("claimNo").get(t);
if(!distinctCountList.contains(claimNo)){
distinctCountList.add(claimNo);
}
/*String paid = (String) t.getClass().getDeclaredField("amountpaid").get(t);
paidAmt.add(new BigDecimal(paid));*/
}
for(String filedName : sortName){
Field f = CoastClaimResult.class.getClass().getDeclaredField("filedName");
f.setAccessible(true);
f.set(CoastClaimResult.class, (String) list.get(0).getClass().getDeclaredField("filedName").get(list.get(0)));
}
result.setExt1(new BigDecimal(distinctCountList.size()));
if(flag.equals("billed")){
result.setBilledAmt(billedAmt);
result.setTitleCategory("BILLED");
}else{
result.setPaidAmt(billedAmt);
result.setTitleCategory("PAID");
}
// result.setPaidAmt(paidAmt);
claimList.add(result);
// System.out.println(polNo.toString());
}
return claimList;
}
DECLARE CATEGORY_CURSOR CURSOR FOR
SELECT DISTINCT
DEPCODE,
DEPNAME
FROM ( SELECT DEPNAME, MAX(DEPCODE) DEPCODE FROM #CLAIMDATA (nolock) GROUP BY DEPNAME) M
ORDER BY DEPCODE,
DEPNAME
OPEN CATEGORY_CURSOR
FETCH NEXT FROM CATEGORY_CURSOR INTO @LS_CATEGORY,@LS_CATEGORY
WHILE @@FETCH_STATUS=0
BEGIN
SELECT @LS_HEADERTITLE=@LS_HEADERTITLE+@LS_CATEGORY+‘,‘
FETCH NEXT FROM CATEGORY_CURSOR INTO @LS_CATEGORY,@LS_CATEGORY
END
groupBy