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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
global class RenewTargetToAsotBatch implements Database.Batchable<sObject> {
    public String query;
    private BatchIF_Log__c iflog;
    public Date toDayTime = Date.today();
    public String OCSM_Period_half;
    public String OCSM_Period;
    public List < String > accList;
    global RenewTargetToAsotBatch() {
        this.query = query;
        OCSM_Period = 'FY'+(toDayTime.year()+1);
    }
 
    global RenewTargetToAsotBatch(List < String > accList) {
        this.query = query;
        OCSM_Period = 'FY'+(toDayTime.year()+1);
        this.accList = accList;
    }
 
    global Database.QueryLocator start(Database.BatchableContext bc) {
        if (toDayTime.month() >= 4 && toDayTime.month() <= 9) {
            OCSM_Period_half = '1H';
        }else{
            OCSM_Period_half = '2H';
        }
        if (accList != null && accList.size() > 0) {
            query ='select id,name,IF_Renewalrate_Target_Asset__c,Hospital__c,Product2.ServiceCategory__c '
                    + 'from asset  where IF_Renewalrate_Target_Asset__c= \'1\' and id In :accList';
        }else{
            query = 'select id,name,IF_Renewalrate_Target_Asset__c,Hospital__c,Product2.ServiceCategory__c '
                    +' from asset  where IF_Renewalrate_Target_Asset__c= \'1\'';
        }
        return Database.getQueryLocator(query);
    }
 
    global void execute(Database.BatchableContext BC, list<Asset> assList) {
        system.debug('执行execute');
        Set<String> accIds = new Set<String>();
        for(Asset ass1:assList){
            accIds.add(ass1.Hospital__c);
        } 
        Set<String> accIdSet = new Set<String>();
        List<Account_Service_Of_Target__c> targetObjs = new List<Account_Service_Of_Target__c>(); 
        Map<String,Account_Service_Of_Target__c> targetMap = new Map<String,Account_Service_Of_Target__c>();
        for(Account_Service_Of_Target__c target:[SELECT Id ,Target_Rigid_Mirror_2__c,Target_Soft_Mirror_2__c,Target_Correlation_Lightsource__c,Account_HP__c,OCSM_Period_half__c 
                                                FROM Account_Service_Of_Target__c 
                                                WHERE OCSM_Period_half__c = :OCSM_Period_half 
                                                AND OCSM_Period__c = :OCSM_Period
                                                AND Account_HP__c IN:accIds]){
            targetMap.put(target.Account_HP__c,target);
        }
        for(Asset ass:assList){
            // 避免重复
            if(!accIdSet.contains(ass.Hospital__c)){
                // 如果查询到Account_Service_Of_Target__c直接取出来用
                if(targetMap.containskey(ass.Hospital__c)){
                    Account_Service_Of_Target__c asot = targetMap.get(ass.Hospital__c);
                    asot.Renew_Target_Soft_Mirror_2__c = 0;
                    asot.Renew_Target_Rigid_Mirror_2__c = 0;
                    asot.Renew_Target_Correlation_Lightsource__c = 0;
                }else{
                    // 如果没有查询到Account_Service_Of_Target__c,新建
                    Account_Service_Of_Target__c asot = new Account_Service_Of_Target__c();
                    asot.Account_HP__c = ass.Hospital__c;
                    asot.Coverage_Target_Account__c = true;
                    asot.Renew_Target_Soft_Mirror_2__c = 0;
                    asot.Renew_Target_Rigid_Mirror_2__c = 0;
                    asot.Renew_Target_Correlation_Lightsource__c = 0;
                    asot.OCSM_Period__c = OCSM_Period; 
                    asot.OCSM_Period_half__c = OCSM_Period_half;
                    targetMap.put(ass.Hospital__c,asot);
                }
                accIdSet.add(ass.Hospital__c);
            }
 
            if (ass.Product2.ServiceCategory__c == '硬性镜') {
                targetMap.get(ass.Hospital__c).Renew_Target_Soft_Mirror_2__c ++;
            }
            if(ass.Product2.ServiceCategory__c =='软性镜'){
                targetMap.get(ass.Hospital__c).Renew_Target_Rigid_Mirror_2__c ++;
            }
            if (ass.Product2.ServiceCategory__c == '周边') {
                targetMap.get(ass.Hospital__c).Renew_Target_Correlation_Lightsource__c ++;
            }
 
        }
        UpSert targetMap.values();
    }
 
    global void finish(Database.BatchableContext BC) {
        
    }
}