public with sharing class AssetMaintainAbandonSummaryController {
    public Id amhId{get;set;}
    public Map<String, Integer> bjMap{get;set;}
    public Map<String, Integer> shMap{get;set;}
    public Map<String, Integer> gzMap{get;set;}
    public Map<String, Integer> totalMap{get;set;}
    public final static List<String> COLUMN_NAMES{get;set;}
    static{
        COLUMN_NAMES = new List<String> {
                '待废弃数量(固定资产)'
              , '待废弃数量(非固定资产)'
              , '废弃数量(固定资产)'
              , '废弃数量(非固定资产)'
        };
    }

    public String getInit(){

        bjMap = initMap();
        shMap = initMap();
        gzMap = initMap();
        totalMap = initMap();
        AggregateResult[] agrList = [
            SELECT Asset__r.Internal_asset_location__c
              , Asset__r.Internal_Asset_number__c
              , sum(MaintainCount__c) daifeiqi
              , sum(AbandonCount__c) feiqi
           FROM AssetMaintainDetail__c
           WHERE AssetMaintainHeader__c =: amhId
           GROUP BY Asset__r.Internal_asset_location__c,Asset__r.Internal_Asset_number__c
        ];
        for(AggregateResult agr: agrList){
            String location = String.valueOf(agr.get('Internal_asset_location__c'));
            Boolean isFixed = String.isNotBlank(String.valueOf(agr.get('Internal_Asset_number__c')));
            Map<String, Integer> tempMap = null;
            switch on location {
                when '北京 备品中心'{
                    tempMap = bjMap;
                }
                when '上海 备品中心'{
                    tempMap = shMap;
                }
                when '广州 备品中心'{
                    tempMap = gzMap;
                }
                when else{
                    continue;
                }
            }
            String col_daifeiqi = isFixed ? COLUMN_NAMES[0]:COLUMN_NAMES[1];
            String col_feiqi = isFixed ? COLUMN_NAMES[2]:COLUMN_NAMES[3];
            Integer val_daifeiqi =intValueOf(agr.get('daifeiqi'));
            Integer val_feiqi = intValueOf(agr.get('feiqi'));

            tempMap.put(col_daifeiqi, tempMap.get(col_daifeiqi) + val_daifeiqi);
            tempMap.put(col_feiqi, tempMap.get(col_feiqi) + val_feiqi);
            totalMap.put(col_daifeiqi, totalMap.get(col_daifeiqi) + val_daifeiqi);
            totalMap.put(col_feiqi, totalMap.get(col_feiqi) + val_feiqi);
        }
        return '';
    }
    private Integer intValueOf(Object obj){
        if(obj == null){
            return 0;
        }
        return Integer.valueOf((Decimal) obj);
    }
    private Map<String, Integer> initMap(){
        Map<String, Integer> newMap = new Map<String, Integer>();
        for(String colName:COLUMN_NAMES){
            newMap.put(colName, 0);
        }
        return newMap;
    }

}