/**
 * 2023/11/06 lijinhuan 处理历史数据二维码和条形码使用 
 */
global class RentalImgRelationshipBatch implements Database.Batchable<sObject> {
    public String query;

    global RentalImgRelationshipBatch() {
        this.query = query;
    }
    private Datetime creStartDate = null;
    private Datetime creEndDate = null;
    private String objectApiName  = null;
    private String whereS  = null; 
    public RentalImgRelationshipBatch(String objectType,Datetime startTime, Datetime endTime,String myWhere) {
        objectApiName = objectType;
        creStartDate = startTime;
        creEndDate = endTime;
        whereS = myWhere;
    }
    global Database.QueryLocator start(Database.BatchableContext bc) {
        String query = 'select id,QrId__c,BRId__c ';  
        query +=' from '+objectApiName;
        query +=' where Id != null ';
        if(String.isNotBlank(whereS)){
            query += whereS;
        }
        if(creStartDate != null){
            query +=' AND CreatedDate >=:creStartDate';
        }  
        if(creEndDate != null){
            query +=' AND CreatedDate <:creEndDate';
        }
        query +=' order by createddate desc';
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, list<Sobject> scope) {
        Map<String,Rental_Apply__c> UpdateObjMap = new  Map<String,Rental_Apply__c>();
        List<Rental_Apply__c> objList = scope;
        set<Id> IdList =new Set<Id>();
        for(Rental_Apply__c asl:objList){
            IdList.add(asl.Id);
        }
        List<ContentDocumentLink> cdlink = [select LinkedEntityId,ContentDocument.LatestPublishedVersionId,ContentDocument.Title    
        from ContentDocumentLink  where LinkedEntityId in :IdList and Visibility = 'AllUsers' 
        and (ContentDocument.title like 'QRCode%' or ContentDocument.title like 'BRCode%')
        ];
        for(ContentDocumentLink cdl:cdlink){
            Rental_Apply__c obj = new Rental_Apply__c();
            String tempSS = cdl.ContentDocument.title;
            obj.Id =  cdl.LinkedEntityId;
            if(tempSS.startsWith('QRCode')){
                obj.QrId__c =  cdl.ContentDocument.LatestPublishedVersionId ;
            }
            if(tempSS.startsWith('BRCode')){
                obj.BRId__c =  cdl.ContentDocument.LatestPublishedVersionId ;
            }
            if(UpdateObjMap.containsKey(obj.Id)){
                Rental_Apply__c objNew = UpdateObjMap.get(obj.Id);
                if(tempSS.startsWith('QRCode')){
                    objNew.QrId__c =  cdl.ContentDocument.LatestPublishedVersionId ;
                }
                if(tempSS.startsWith('BRCode')){
                    objNew.BRId__c =  cdl.ContentDocument.LatestPublishedVersionId ;
                }
                UpdateObjMap.put(objNew.Id,objNew);
            }else{
                UpdateObjMap.put(obj.Id,obj);
            }
        }
        if(UpdateObjMap.size() > 0){
            update UpdateObjMap.values();
        }
    }

    global void finish(Database.BatchableContext BC) {
        Integer i = 0;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
    }
}