global with sharing class UpdateContentDocumentBatch implements Database.Batchable<sObject>, Database.Stateful{
    public List<String> parentIdList;

    public UpdateContentDocumentBatch() {

    }

    public UpdateContentDocumentBatch(List<String> parentIdList) {
        this.parentIdList = parentIdList;
    }

    global Database.QueryLocator start(Database.BatchableContext bc) {
        String querysql = 'select Id, FileName__c, InfoId__c, InfoType__c, subInfoType__c, UpdateDate__c, ParentRecordId__c, LastModifiedBy.Name from FileAddress__c where UpdateDate__c != null and LastModifiedDate < TODAY and ParentRecordId__c like \'a4L%\' ';
        if (parentIdList != null && parentIdList.size() > 0) {
            querysql += 'and ParentRecordId__c in :parentIdList';
        }
        System.debug('sql======>' + querysql);
        return Database.getQueryLocator(querysql);
    }

    global void execute(Database.BatchableContext BC, List<FileAddress__c> fileList) {
        try {
            List<String> idList = new List<String>();
            for (FileAddress__c fileAddress : fileList) {
                idList.add(fileAddress.ParentRecordId__c);
            }
            Map<String, Tender_information__c> tenderList = new Map<String, Tender_information__c>([SELECT Id, Original_ID__c FROM Tender_information__c WHERE Original_ID__c IN :idList]);
            Set<String> temderIdLIst = tenderList.keySet();
            List<ContentVersion> contentVersionList = [SELECT Id, FirstPublishLocationId, Title, Description FROM ContentVersion where FirstPublishLocationId IN :temderIdLIst];
            Map<String, Map<String, Object>> tenderMap = new Map<String, Map<String, Object>>();
            for (Tender_information__c tender : tenderList.values()) {
                Map<String, Object> mso = new Map<String, Object>();
                for (FileAddress__c fileAddress : fileList) {
                    if (fileAddress.ParentRecordId__c == tender.Original_ID__c) {
                        String descrip = fileAddress.InfoId__c + ';' + fileAddress.InfoType__c + ';' + fileAddress.subInfoType__c + ';' + fileAddress.UpdateDate__c;
                        mso.put(fileAddress.FileName__c, descrip);
                    }
                }
                tenderMap.put(tender.Id, mso);
            }
            for (ContentVersion content : contentVersionList) {
                if (tenderMap.containsKey(content.FirstPublishLocationId)) {
                    Map<String, Object> contentMap = tenderMap.get(content.FirstPublishLocationId);
                    if (contentMap.containsKey(content.Title)) {
                        System.debug('description=======>' + contentMap.get(content.Title) + ';---' + content.FirstPublishLocationId);
                        content.Description = String.valueOf(contentMap.get(content.Title));
                    }
                }
            }
            // Database.update(contentVersionList, false);
            update contentVersionList;
            update fileList;
        } catch (Exception e) {
            System.debug('error========?' + e.getLineNumber());
            System.debug('error========>' + e.getMessage());
        }

    }

    global void finish(Database.BatchableContext BC) {}

    public static void test1(){
        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++;
        i++;
        i++;
    }
}