高章伟
2022-03-10 1312ba82d4c880bdb5357d28e0d4af5b285f610f
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
global class EquipmentTargetCoverageBatch implements Database.Batchable<sObject> {
    public String query;
    // public List<Id> prdIds = new List<Id>();
    public List<Id> hospIds = new List<Id>();
    global EquipmentTargetCoverageBatch() {
        this.query = query;
    }
 
    global Database.QueryLocator start(Database.BatchableContext bc) {
        // List<String> classify = new List<String>();
        // classify.add('软性镜');
        // classify.add('硬性镜');
        // classify.add('周边');
        // List<Product2> prdList = [select Id,ServiceCategory__c from Product2 where ServiceCategory__c in :classify and Can_Repair__c != '不'];
        // system.debug('产品'+prdList);
        // if (prdList!=null) {
        //     for (Product2 p : prdList) {
        //     prdIds.add(p.Id);
        //     }
        // }
       List<AggregateResult> Asset1 = [select count(Id),Hospital__c from asset where Product2.ServiceCategory__c ='软性镜'  Group by Hospital__c having count(Id)>=5];
        system.debug('第一重'+Asset1);
       if(Asset1!=null){
           for (AggregateResult a1 : Asset1) {
               hospIds.add(String.valueOf(a1.get('Hospital__c')));
           }
       }
       
       query = 'select Id,IF_Coverage_Target_Asset__c from asset where hospital__c in :hospIds' 
                            +' and Status != \'廃棄\' and Status != \'待报废\''
                            +' and SerMarGuranteeType__c != \'服务多年保修\' and SerMarGuranteeType__c != \'市场多年保修\''
                            +' and Product2.ServiceCategory__c != null';
        return Database.getQueryLocator(query);
    }
 
    global void execute(Database.BatchableContext BC, list<Asset> Assets) {
        List<Asset> AssetsNew = new List<Asset>();
        system.debug('第二重'+Assets);
        for (Asset a : Assets) {
            // a.IF_Coverage_Target_Asset__c = true;
            AssetsNew.add(a);
        }
        update AssetsNew;
        system.debug('执行完成');
    }
 
    global void finish(Database.BatchableContext BC) {
 
    }
}