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) {

    }
}