高章伟
2022-02-18 8b5f4c6c281cfa548f92de52c8021e37aa81901e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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;
    }
 
}