高章伟
2022-02-24 2aa8da8af66aa8ae00f25831aed6bb0364176e7b
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
global class UpdateStatuAchievementsBatch implements Database.Batchable<sObject> {
    
    /**
     * コンスタント
     */
    global UpdateStatuAchievementsBatch() {
    }
    
    /**
     * startには、queryを実行、备品Set明细を検索
     */
    global Database.QueryLocator start(Database.BatchableContext BC) {
        return Database.getQueryLocator(
            [select Id, Current_status__c, Last_week__c, X2weeks_ago__c, X3Weeks_ago__c, X4weeks_ago__c, X5weeks_ago__c, Last_Batch_Day__c
               from Statu_Achievements__c
              order by Id]
        );
    }
    
    global void execute(Database.BatchableContext BC, List<SObject> saList) {
        List<Statu_Achievements__c> updateList = new List<Statu_Achievements__c>();
        
        for (SObject obj: saList) {
            Statu_Achievements__c sa = (Statu_Achievements__c) obj;
            Date lastweek = sa.Last_Batch_Day__c == null ? Date.today().addDays(-7).toStartOfWeek() : sa.Last_Batch_Day__c.toStartOfWeek();
            Date todayweek = Date.today().toStartOfWeek();
            Integer cntweek = lastweek.daysBetween(todayweek) / 7;
 
            if (cntweek == 0) {
                continue;
            } else if (cntweek == 1) {
                sa.X5weeks_ago__c = sa.X4weeks_ago__c;
                sa.X4weeks_ago__c = sa.X3Weeks_ago__c;
                sa.X3Weeks_ago__c = sa.X2weeks_ago__c;
                sa.X2weeks_ago__c = sa.Last_week__c;
                sa.Last_week__c = sa.Current_status__c;
                sa.Last_Batch_Day__c = Date.today();
                updateList.add(sa);
            } else if (cntweek == 2) {
                sa.X5weeks_ago__c = sa.X3weeks_ago__c;
                sa.X4weeks_ago__c = sa.X2Weeks_ago__c;
                sa.X3Weeks_ago__c = sa.Last_week__c;
                sa.X2weeks_ago__c = null;
                sa.Last_week__c = sa.Current_status__c;
                sa.Last_Batch_Day__c = Date.today();
                updateList.add(sa);
            } else if (cntweek == 3) {
                sa.X5weeks_ago__c = sa.X2Weeks_ago__c;
                sa.X4weeks_ago__c = sa.Last_week__c;
                sa.X3Weeks_ago__c = null;
                sa.X2weeks_ago__c = null;
                sa.Last_week__c = sa.Current_status__c;
                sa.Last_Batch_Day__c = Date.today();
                updateList.add(sa);
            } else if (cntweek == 4) {
                sa.X5weeks_ago__c = sa.Last_week__c;
                sa.X4weeks_ago__c = null;
                sa.X3Weeks_ago__c = null;
                sa.X2weeks_ago__c = null;
                sa.Last_week__c = sa.Current_status__c;
                sa.Last_Batch_Day__c = Date.today();
                updateList.add(sa);
            } else if (cntweek >= 5) {
                sa.X5weeks_ago__c = null;
                sa.X4weeks_ago__c = null;
                sa.X3Weeks_ago__c = null;
                sa.X2weeks_ago__c = null;
                sa.Last_week__c = sa.Current_status__c;
                sa.Last_Batch_Day__c = Date.today();
                updateList.add(sa);
            }
        }
        
        if (updateList.size() > 0) update updateList;
    }
    
    global void finish(Database.BatchableContext BC) {
        // 今回はやることないです
    }
    
}