liuyn
2024-03-11 a87f1c3df03078814ee97ad0c8ac200a232419e9
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
global class ConsumableTargetManageBatch6 implements Database.Batchable<sObject> {
public Date currentMonth;
    public Date endDate;
    public Date startDate;
    public String queryStr;
    public String year;
    public Boolean isIncrementOnly; //是否只跑增量
    public String OCSMYear;
    global ConsumableTargetManageBatch6(Date startDate,Date endDate) {
        this.startDate=startDate;
        this.endDate=endDate;
        Integer year = startDate.year();
        Integer FYear = year;
        if(startDate>=Date.newInstance(year, 4, 1)){
            FYear = FYear+1;
        }
        String  OCSMYear= 'FY'+FYear;
        this.OCSMYear=OCSMYear;
        queryStr = 'SELECT  nowDosageNumTotal__c,nowDosageAmountTotal__c,lastDosageNumTotal__c,lastDosageAmountTotal__c,Hospital__c,OCM_Year__c,Category3__c,Category4__c FROM ConsumableTargetManage__c WHERE OCM_Year__c = :OCSMYear';
    }
 
    global Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator(queryStr);
    }
 
    global void execute(Database.BatchableContext BC, list<ConsumableTargetManage__c> scope) {
        Integer year = startDate.year();
        Integer FYear = year;
        if(startDate>=Date.newInstance(year, 4, 1)){
            FYear = FYear+1;
        }
 
        String  OCSMYear= 'FY'+FYear;        
        Integer lastFYear = FYear-1;
        String  lastOCSMYear= 'FY'+lastFYear;
 
        Set<String> ctmKeySet=new Set<String>();
        for(ConsumableTargetManage__c ctm:scope){
            ctmKeySet.add(ctm.Hospital__c+'-'+ctm.Category3__c+'-'+ctm.Category4__c+'-'+lastOCSMYear);
        }
 
 
        List<ConsumableTargetManage__c> lastCtmList= [SELECT  nowDosageNumTotal__c,nowDosageAmountTotal__c,lastDosageNumTotal__c,lastDosageAmountTotal__c,Hospital__c,OCM_Year__c,Category3__c,Category4__c FROM ConsumableTargetManage__c WHERE  ConsumableTargetManageKey__c in :ctmKeySet];
        Map<String,ConsumableTargetManage__c> lastCtmMap=new Map<String,ConsumableTargetManage__c>();
        
        for(ConsumableTargetManage__c ctm:lastCtmList){
            String key=ctm.Category3__c+'-'+ctm.Category4__c+'-'+ ctm.Hospital__c;
            lastCtmMap.put(key, ctm);
        }
        for(ConsumableTargetManage__c ctm:scope){
            String key=ctm.Category3__c+'-'+ctm.Category4__c+'-'+ ctm.Hospital__c;
            if(lastCtmMap.containsKey(key)){
                if(lastCtmMap.get(key).nowDosageNumTotal__c!=null){
                    ctm.lastDosageNumTotal__c=lastCtmMap.get(key).nowDosageNumTotal__c;
                }
                
                if(lastCtmMap.get(key).nowDosageAmountTotal__c!=null){
                    ctm.lastDosageAmountTotal__c=lastCtmMap.get(key).nowDosageAmountTotal__c;
                }
                
            }else{
                ctm.lastDosageNumTotal__c=0;
                ctm.lastDosageAmountTotal__c=0;
            }
        }
 
        update scope;
        
    }
 
    global void finish(Database.BatchableContext BC) {
 
    }
}