boolean allColumn = pivotList.stream().allMatch(e -> "column".equals(e.getColumnType()));
List<TableFieldInfo> fieldList = TableInfoHelper.getTableInfo(this.getEntityClass()).getFieldList();
Map<String, String> fieldMap = fieldList.stream().collect(Collectors.toMap(TableFieldInfo::getProperty, TableFieldInfo::getColumn, (v1 ,v2) -> v2));
Set<String> columnSet = pivotList.stream().map(e -> "case when "+fieldMap.get(e.getColumnCode())+" is null then '' else "+fieldMap.get(e.getColumnCode())+" end "+ e.getColumnCode()).collect(Collectors.toSet());
columnSet.add("count(*) count");
columnSet.add("sum(teu) teu");
columnSet.add("sum(total_weight) totalWeight");
columnSet.add("sum(net_weight) netWeight");
String columns = String.join(",", columnSet);
Set<String> groupSet = pivotList.stream().map(e -> fieldMap.get(e.getColumnCode())).collect(Collectors.toSet());
String groups = String.join(",", groupSet);
List<Map<String, Object>> list = this.getBaseMapper().deviceCntrStatistics(wrapper, deviceContainerDTO, columns, groups);
if(CollectionUtils.isNotEmpty(list) && allColumn){
Map<String, Object> map = Maps.newHashMap();
map.put("count", list.stream().filter(e -> Objects.nonNull(e.get("count"))).map(e -> Long.parseLong(e.get("count").toString())).reduce(0L, Long::sum));
map.put("teu", list.stream().filter(e -> Objects.nonNull(e.get("teu"))).map(e -> Double.parseDouble(e.get("teu").toString())).reduce(0.0, Double::sum));
map.put("totalWeight", list.stream().filter(e -> Objects.nonNull(e.get("totalWeight"))).map(e -> new BigDecimal(e.get("totalWeight").toString())).reduce(new BigDecimal("0"), BigDecimal::add));
map.put("netWeight", list.stream().filter(e -> Objects.nonNull(e.get("netWeight"))).map(e -> new BigDecimal(e.get("netWeight").toString())).reduce(new BigDecimal("0"), BigDecimal::add));
list.add(map);
}
<select id="deviceCntrStatistics" resultType="java.util.HashMap">
SELECT
${columns}
FROM
cntr_container a
<where>
<if test="dc.damageRecordList != null and dc.damageRecordList != ''">
<if test="dc.damageRecordList == 1">
AND EXISTS
</if>
<if test="dc.damageRecordList == 0">
AND NOT EXISTS
</if>
(SELECT 1 FROM doc_cntr_damage_record damage WHERE damage.cntr_id = a.id)
</if>
<if test="dc.dangerCntrList != null and dc.dangerCntrList != ''">
<if test="dc.dangerCntrList == 1">
AND EXISTS
</if>
<if test="dc.dangerCntrList == 0">
AND NOT EXISTS
</if>
(SELECT 1 FROM doc_danger_cntr danger WHERE danger.cntr_id = a.id)
</if>
<if test="dc.billNo != null and dc.billNo != ''">
AND c.bill_no = #{dc.billNo}
</if>
<if test="ew!=null and ew.isEmptyOfWhere == false">
AND ${ew.sqlSegment}
</if>
</where>
group by ${groups}
</select>
List<Map<String, Object>> deviceCntrStatistics(@Param("ew") QueryWrapper<DeviceContainer> wrapper, @Param("dc") DeviceContainerDTO deviceContainerDTO,
@Param("columns") String columns, @Param("groups") String groups);