Posted on 2015-04-16 15:22
朱杰兵 阅读(3074)
评论(0) 编辑 收藏
List<UserOutPlanItem> itemList = info.getItemList();//需要去重汇总的list
List<UserOutAuditDTO> summary = new ArrayList<UserOutAuditDTO>();;
//汇总到的map value为总数量和总重量
Map<String, String> map = new HashMap<String, String>();
if (ObjectUtils.notEmpty(itemList))
{
for (UserOutPlanItem item : itemList)
{
//这六个字段相同则汇成一条,并且把重量和数量累加
String key = item.getBreedName()+"_"+item.getSpecName()+"_"+
item.getMaterialName()+"_"+item.getFactoryName()+"_"+
item.getNumUtil()+"_"+item.getWeightUtil();
String num_weight = map.get(key);
//当键不存在的时候汇一条
if (StringUtils.isNull(num_weight))
{
map.put(key, item.getNum()+"_"+item.getWeight());
}
//当key已经存在了,则累加
else
{
String[] arr = num_weight.split("_");
String thisNum = MoneyCalculateUtils.add(arr[0], item.getNum(), 0);
String thisWeight = MoneyCalculateUtils.add(arr[1], item.getWeight(), 4);
map.put(key, thisNum + "_" + thisWeight);
}
}
}
//重新解析map,把数据封装到summary
if (ObjectUtils.notEmpty(map))
{
Set<String> ketSet = map.keySet();
for (String str : ketSet)
{
String arr[] = str.split("_");
UserOutAuditDTO dto = new UserOutAuditDTO();
dto.setBreedName(arr[0]);
dto.setSpec(arr[1]);
dto.setMaterial(arr[2]);
dto.setFactory(arr[3]);
dto.setNumUtil(arr[4]);
dto.setWeightUtil(arr[5]);
String num_weight = map.get(str);
String arr2[] = num_weight.split("_");
dto.setSumNum(arr2[0]);
dto.setSumWeight(arr2[1]);
summary.add(dto);
}
}