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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
//拜访次数
//获取日报的拜访次数
 
global class ConsumableTargetManageBatch3 implements Database.Batchable<sObject> {
    public Date currentMonth;
    public Date endDate;
    public Date startDate;
    public String queryStr;
    public String year;
    public Boolean isIncrementOnly; //是否只跑增量
    global ConsumableTargetManageBatch3(Date startDate,Date endDate,Boolean isIncrementOnly) {
        //当年
        this.startDate=startDate;
        this.endDate=endDate;
        this.isIncrementOnly=isIncrementOnly;
        queryStr = 'SELECT id,Hospital_ID__c,etapp_third_category__c,etapp_third_category2__c,etapp_third_category3__c,etapp_forth_category__c,etapp_forth_category2__c,etapp_forth_category3__c,ActivityDate__c'
                  +' FROM Event__c ' 
                  +' WHERE (Status__c=\'申請中\' OR Status__c=\'承認\') AND Hospital_ID__c!=null '
                  +' AND ((etapp_third_category__c !=null AND etapp_forth_category__c!=null) '
                  +' OR (etapp_third_category2__c !=null AND etapp_forth_category2__c!=null)  '
                  +' OR (etapp_third_category3__c !=null AND etapp_forth_category3__c!=null) ) '
                  +' AND ActivityDate__c>=:startDate AND ActivityDate__c<=:endDate';
    }
 
    global Database.QueryLocator start(Database.BatchableContext bc) {
        //如果跑全量数据,需要重置
        //数据超过一万条会报错
        if(!isIncrementOnly){
            // Integer year = startDate.year();
            // Integer FYear = year;
            // if(startDate>=Date.newInstance(year, 4, 1)){
            //     FYear = FYear+1;
            // }
            // String  OCSMYear= 'FY'+FYear;
            // List<ConsumableTargetManage__c> ctmList= [SELECT  id FROM ConsumableTargetManage__c WHERE OCM_Year__c = :OCSMYear ];
            // for(ConsumableTargetManage__c ctm:ctmList){
            //     ctm.SpecialistVisitsNum1__c=0;
            //     ctm.SpecialistVisitsNum2__c=0;
            //     ctm.SpecialistVisitsNum3__c=0;
            //     ctm.SpecialistVisitsNum4__c=0;
            //     ctm.SpecialistVisitsNum5__c=0;
            //     ctm.SpecialistVisitsNum6__c=0;
            //     ctm.SpecialistVisitsNum7__c=0;
            //     ctm.SpecialistVisitsNum8__c=0;
            //     ctm.SpecialistVisitsNum9__c=0;
            //     ctm.SpecialistVisitsNum10__c=0;
            //     ctm.SpecialistVisitsNum11__c=0;
            //     ctm.SpecialistVisitsNum12__c=0;
            // }
            // update ctmList;
        }
        return Database.getQueryLocator(queryStr);
    }
 
    global void execute(Database.BatchableContext BC, list<Event__c> scope) {
        System.debug('获取到报告一览:');
        System.debug(scope);
        Integer year = startDate.year();
        Integer FYear = year;
        if(startDate>=Date.newInstance(year, 4, 1)){
            FYear = FYear+1;
        }
 
        String  OCSMYear= 'FY'+FYear;        
        Set<String> ctmKeySet=new Set<String>();
        Map<String,List<Event__c>> eventMap = new Map<String,List<Event__c>>();
        Map<String,String> hpIdMap=new Map<String,String>();
        List<String> hpIdList=new List<String>();
        for(Event__c event:scope){
            hpIdList.add(event.Hospital_ID__c);
        }
        List<Account> accList=[SELECT id from Account where id IN :hpIdList];
        for(Account acc:accList){
            String id=acc.Id;
            hpIdMap.put(id.substring(0,15),id);
        }
        for(Event__c event:scope){
            String key1=event.etapp_third_category__c+'-'+event.etapp_forth_category__c+'-'+event.Hospital_ID__c;
            String key2=event.etapp_third_category2__c+'-'+event.etapp_forth_category2__c+'-'+event.Hospital_ID__c;
            String key3=event.etapp_third_category3__c+'-'+event.etapp_forth_category3__c+'-'+event.Hospital_ID__c;
            String eventHpId=hpIdMap.get(event.Hospital_ID__c);
            ctmKeySet.add(eventHpId+'-'+event.etapp_third_category__c+'-'+event.etapp_forth_category__c+'-'+OCSMYear);
            ctmKeySet.add(eventHpId+'-'+event.etapp_third_category2__c+'-'+event.etapp_forth_category2__c+'-'+OCSMYear);
            ctmKeySet.add(eventHpId+'-'+event.etapp_third_category3__c+'-'+event.etapp_forth_category3__c+'-'+OCSMYear);
            
            if(eventMap.containsKey(key1)){
                eventMap.get(key1).add(event);
            }else{
                List<Event__c> eventList = new List<Event__c>();
                eventList.add(event);
                eventMap.put(key1,eventList);
            }
 
            if(eventMap.containsKey(key2)){
                eventMap.get(key2).add(event);
            }else{
                List<Event__c> eventList = new List<Event__c>();
                eventList.add(event);
                eventMap.put(key2,eventList);
            }
 
            if(eventMap.containsKey(key3)){
                eventMap.get(key3).add(event);
            }else{
                List<Event__c> eventList = new List<Event__c>();
                eventList.add(event);
                eventMap.put(key3,eventList);
            }
        }
 
 
        List<ConsumableTargetManage__c> ctmList= [SELECT  SpecialistVisitsNum1__c,SpecialistVisitsNum2__c,SpecialistVisitsNum3__c,
             SpecialistVisitsNum4__c,SpecialistVisitsNum5__c,SpecialistVisitsNum6__c,SpecialistVisitsNum7__c,SpecialistVisitsNum8__c,
             SpecialistVisitsNum9__c,SpecialistVisitsNum10__c,SpecialistVisitsNum11__c,SpecialistVisitsNum12__c,
             Hospital__c,OCM_Year__c,Category3__c,Category4__c FROM ConsumableTargetManage__c WHERE OCM_Year__c = :OCSMYear  AND ConsumableTargetManageKey__c in :ctmKeySet];
 
        for(ConsumableTargetManage__c ctm:ctmList){
            String hpId='';
            if(ctm.Hospital__c!=null){
                hpId=ctm.Hospital__c;
                hpId=hpId.substring(0,15);
            }
            String key=ctm.Category3__c+'-'+ctm.Category4__c+'-'+ hpId;
            if(ctm.SpecialistVisitsNum1__c==null){
                ctm.SpecialistVisitsNum1__c=0;
            }
            if(ctm.SpecialistVisitsNum2__c==null){
                ctm.SpecialistVisitsNum2__c=0;
            }
            if(ctm.SpecialistVisitsNum3__c==null){
                ctm.SpecialistVisitsNum3__c=0;
            }
            if(ctm.SpecialistVisitsNum4__c==null){
                ctm.SpecialistVisitsNum4__c=0;
            }
            if(ctm.SpecialistVisitsNum5__c==null){
                ctm.SpecialistVisitsNum5__c=0;
            }
            if(ctm.SpecialistVisitsNum6__c==null){
                ctm.SpecialistVisitsNum6__c=0;
            }
            if(ctm.SpecialistVisitsNum7__c==null){
                ctm.SpecialistVisitsNum7__c=0;
            }
            if(ctm.SpecialistVisitsNum8__c==null){
                ctm.SpecialistVisitsNum8__c=0;
            }
            if(ctm.SpecialistVisitsNum9__c==null){
                ctm.SpecialistVisitsNum9__c=0;
            }
            if(ctm.SpecialistVisitsNum10__c==null){
                ctm.SpecialistVisitsNum10__c=0;
            }
            if(ctm.SpecialistVisitsNum11__c==null){
                ctm.SpecialistVisitsNum11__c=0;
            }
            if(ctm.SpecialistVisitsNum12__c==null){
                ctm.SpecialistVisitsNum12__c=0;
            }
            if(eventMap.containsKey(key)){
                List<Event__c> eventList=eventMap.get(key);
 
                System.debug('修改前:'+ctm);
                System.debug('event:'+eventList);
                for(Event__c event:eventList){
                    if(event.ActivityDate__c >= Date.newInstance(FYear, 1, 1) && event.ActivityDate__c < Date.newInstance(FYear, 2, 1)){
                        ctm.SpecialistVisitsNum1__c++;
                    }else if(event.ActivityDate__c >= Date.newInstance(FYear, 2, 1) && event.ActivityDate__c < Date.newInstance(FYear, 3, 1)){
                        ctm.SpecialistVisitsNum2__c++;
                    }else if(event.ActivityDate__c >= Date.newInstance(FYear, 3, 1) && event.ActivityDate__c < Date.newInstance(FYear, 4, 1)){
                        ctm.SpecialistVisitsNum3__c++;
                    }else if(event.ActivityDate__c >= Date.newInstance(FYear-1, 4, 1) && event.ActivityDate__c < Date.newInstance(FYear-1, 5, 1)){
                        ctm.SpecialistVisitsNum4__c++;
                    }else if(event.ActivityDate__c >= Date.newInstance(FYear-1, 5, 1) && event.ActivityDate__c < Date.newInstance(FYear-1, 6, 1)){
                        ctm.SpecialistVisitsNum5__c++;
                    }else if(event.ActivityDate__c >= Date.newInstance(FYear-1, 6, 1) && event.ActivityDate__c < Date.newInstance(FYear-1, 7, 1)){
                        ctm.SpecialistVisitsNum6__c++;
                    }else if(event.ActivityDate__c >= Date.newInstance(FYear-1, 7, 1) && event.ActivityDate__c < Date.newInstance(FYear-1, 8, 1)){
                        ctm.SpecialistVisitsNum7__c++;
                    }else if(event.ActivityDate__c >= Date.newInstance(FYear-1, 8, 1) && event.ActivityDate__c < Date.newInstance(FYear-1, 9, 1)){
                        ctm.SpecialistVisitsNum8__c++;
                    }else if(event.ActivityDate__c >= Date.newInstance(FYear-1, 9, 1) && event.ActivityDate__c < Date.newInstance(FYear-1, 10, 1)){
                        ctm.SpecialistVisitsNum9__c++;
                    }else if(event.ActivityDate__c >= Date.newInstance(FYear-1, 10, 1) && event.ActivityDate__c < Date.newInstance(FYear-1, 11, 1)){
                        ctm.SpecialistVisitsNum10__c++;
                    }else if(event.ActivityDate__c >= Date.newInstance(FYear-1, 11, 1) && event.ActivityDate__c < Date.newInstance(FYear-1, 12, 1)){
                        ctm.SpecialistVisitsNum11__c++;
                    }else if(event.ActivityDate__c >= Date.newInstance(FYear-1, 12, 1) && event.ActivityDate__c < Date.newInstance(FYear, 1, 1)){
                        ctm.SpecialistVisitsNum12__c++;
                    }
                }
                System.debug('修改后:'+ctm);
            }
        }
 
        if(ctmList.size()>0){
            update ctmList;
        }
        
    }
 
    global void finish(Database.BatchableContext BC) {
        Id batjobId = Database.executeBatch(new ConsumableTargetManageBatch4(startDate,endDate), 200);
 
    }
}