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

    public UpdateFileAddressBatch() {

    }

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

    global Database.QueryLocator start(Database.BatchableContext bc) {
        String querysql = 'select Id, FileName__c, ViewLink__c, DownloadLink__c from FileAddress__c where LastModifiedDate < TODAY and ParentRecordId__c  = \'FrameNumManage\' ';
        if (parentIdList != null && parentIdList.size() > 0) {
            querysql += 'and Id in :parentIdList';
        }
        querysql += ' Limit 200';
        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.FileName__c);
            }
            List<ContentVersion> contentVersionList = [SELECT Id, FirstPublishLocationId, Title, Description FROM ContentVersion where Title IN :idList AND Description = 'FrameNumManage'];
            System.debug('idList===' + JSON.serialize(idList));
            for (FileAddress__c fileAddress : fileList) {
                for (ContentVersion content : contentVersionList) {
                    if (fileAddress.FileName__c == content.Title) {
                        fileAddress.ViewLink__c = '/sfc/servlet.shepherd/version/renditionDownload?rendition=SVGZ&versionId=' + content.Id + '&operationContext=CHATTER';
                        fileAddress.DownloadLink__c = '/sfc/servlet.shepherd/version/download/' + content.Id;
                    }
                }
            }
            // Database.update(contentVersionList, false);
            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++;
    }
}