高章伟
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*  分2种情况
    *1.需要更新历史数据,1)历史财年数据 2)财年初4月1号
    *2.不需要更新历史数据
*/
global class ETAPPActivityManageBatch implements Database.Batchable<sObject> {
    public String query;
    public String nyear;
    public String yearLastPeriod;
    public List<String> yearList = new list<String>();
 
    global ETAPPActivityManageBatch() {
        this.query = query;
        Date dateNow = Date.today();
        Integer month = dateNow.month();
        Integer day = dateNow.day();
 
        // if (String.isBlank(nyear)) {
        //     nyear = getYear();
        //     yearLastPeriod = getLastPeriodYear();
        //     yearList.add(nyear);
        //     yearList.add(yearLastPeriod);
        // }
        // 如果4月1号,取上财年数据更新
        if (month == 4 && day == 1) {
            nyear = getLastPeriodYear();
            yearLastPeriod = getLastPeriodYear(nyear);
            yearList.add(nyear);
            yearList.add(yearLastPeriod);
        }else{
            nyear = getYear();
            yearLastPeriod = getLastPeriodYear();
            yearList.add(nyear);
            yearList.add(yearLastPeriod);
        }
 
 
    }
    global ETAPPActivityManageBatch(String nyear) {
        this.query = query;
        this.nyear = nyear;
        yearLastPeriod = getLastPeriodYear(nyear);
        yearList.add(nyear);
        yearList.add(yearLastPeriod);
    }
 
    global Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator([select id, Category3__c, Category4__c, Hospital__c,
                                         ETAPP_key__c, Result__c
                                         from Product_Score_Table_History__c
                                         where OCSM_Period__c = :nyear
                                                 and RecordType.DeveloperName = 'TargetPDCA'
                                                         and Hospital__c != null
                                        ]);
    }
 
    global void execute(Database.BatchableContext BC, list<Product_Score_Table_History__c> scope) {
        // 获取需要查找的key
        Date dateNow = Date.today();
        Integer month = dateNow.month();
        Integer day = dateNow.day();
        List<String> etappkey = new List<String>();
        for (Product_Score_Table_History__c pst : scope) {
            etappkey.add(pst.ETAPP_key__c);
        }
        Map<String, Product_Score_Table_History__c> lastPeriodMap = new Map<String, Product_Score_Table_History__c>();
        Map<String, Product_Score_Table_History__c> agencyMap = new Map<String, Product_Score_Table_History__c>();
        List<Product_Score_Table_History__c> agencyList = [select id, OCSM_Period__c, Category3__c, Category4__c, Hospital__c, ETAPP_key__c
                from Product_Score_Table_History__c
                where OCSM_Period__c in :yearList
                and RecordType.DeveloperName in ('Agency','Company')
                                               and Hospital__c != null
                                               and ETAPP_key__c in :etappkey
                                               and Province_From_Consume__c = false
                                               and Qty__c > 0];
        for (Product_Score_Table_History__c pst : agencyList) {
            // String key = pst.Hospital__c + pst.Category3__c + pst.Category4__c;
            if (nyear.equals(pst.OCSM_Period__c) && !agencyMap.containsKey(pst.ETAPP_key__c)) {
                agencyMap.put(pst.ETAPP_key__c, pst);
            }
            if (yearLastPeriod.equals(pst.OCSM_Period__c) && !lastPeriodMap.containsKey(pst.ETAPP_key__c)) {
 
                lastPeriodMap.put(pst.ETAPP_key__c, pst);
            }
        }
 
        
        //Map<ID,Product_Score_Table_History__c> updateMap = new Map<ID,Product_Score_Table_History__c>();
        List<Product_Score_Table_History__c> updateList = new List<Product_Score_Table_History__c>();
        // 区分正常更新和 历史数据(包括4月1号)
        for (Product_Score_Table_History__c argetPDCA : scope) {
            // String key = argetPDCA.Hospital__c + argetPDCA.Category3__c + argetPDCA.Category4__c;     
            if (agencyMap.containsKey(argetPDCA.ETAPP_key__c)  && !lastPeriodMap.containsKey(argetPDCA.ETAPP_key__c) ) {
                if (!'使用'.equals(argetPDCA.Result__c)) {
                    argetPDCA.Result__c = '使用';
                    argetPDCA.Agency_Linkage__c = true;
                    updateList.add(argetPDCA);
                    // updateMap.put(argetPDCA.id,argetPDCA);
                }
 
            } else {
                if (nyear == getYear() && month != 4 && day != 1) {
                    argetPDCA.Result__c = null;
                    argetPDCA.Agency_Linkage__c = false;
                    updateList.add(argetPDCA);
                }else{
                    if (!'不使用'.equals(argetPDCA.Result__c)) {
                        argetPDCA.Result__c = '不使用';
                        argetPDCA.Agency_Linkage__c = true;
                        updateList.add(argetPDCA);
 
                    }
                }
                // if (!'不使用'.equals(argetPDCA.Result__c)) {
                    // argetPDCA.Result__c = null;
                    // argetPDCA.Agency_Linkage__c = false;
                    // updateList.add(argetPDCA);
                    // updateMap.put(argetPDCA.id,argetPDCA);
 
                // }
            }
        }
 
        if (updateList.size() > 0) {
            update updateList;
        }
        // if (updateMap.size() > 0) {
        //     update updateMap.values();
        // }
 
 
    }
 
    global void finish(Database.BatchableContext BC) {
 
    }
 
    public static string getYear() {
        Date dateNow = Date.today();
        Integer year = dateNow.year();
        Integer month = dateNow.month();
        if (month < 4) {
            year -= 1;
        }
        Integer tempiYear = year + 1;
        string currentPeriod = String.valueOf('FY' + tempiYear);
        return currentPeriod;
    }
    public static String getLastPeriodYear() {
        Date dateNow = Date.today();
        Integer year = dateNow.year();
        Integer month = dateNow.month();
        if (month < 4) {
            year -= 1;
        }
        Integer lastPeriodYear = year;
        string currentlastPeriodYear = String.valueOf('FY' + lastPeriodYear);
        return currentlastPeriodYear;
    }
 
    public static String getLastPeriodYear(String thisyear) {
        string lastPeriod;
        if (String.isNotBlank(thisyear) && thisyear.length() > 4) {
            Integer lastPeriodYear = Integer.valueOf(thisyear.substring(thisyear.length()-4,thisyear.length())) - 1;
            lastPeriod = String.valueOf('FY' + lastPeriodYear);
        }else{
            lastPeriod = getLastPeriodYear();
        }
        return lastPeriod;
    }
}