global class NFM609FiledDownloadBatch implements Database.Batchable < sObject > , Database.AllowsCallouts {
    public String query;
    public string objectType;
    global NFM609FiledDownloadBatch() {
        this.query = query;
    }

    global NFM609FiledDownloadBatch(String objectType) {
        this.objectType = objectType;
    }

    global Database.QueryLocator start(Database.BatchableContext bc) {
        String sql = 'SELECT Id, AttachmentDownload__c, AttachmentLink__c, AttachmentName__c FROM '+objectType+' where AttachmentDownload__c = true AND AttachmentLink__c != \'\' AND AttachmentName__c != \'\'';
        System.debug('sql--->'+sql);
        return Database.getQueryLocator(sql);
    }

    global void execute(Database.BatchableContext BC, List < sObject > sObjectList) {
        String endpoint = '';
        String parentId = '';
        String attachmentName = '';
        List<Case> updateCaseList = new List<Case>();
        List<RepairSubOrder__c> updateSubOrderList = new List<RepairSubOrder__c>();
        for (sObject obj: sObjectList) {
            if ('Case'.equals(objectType)) {
                Case cas = (Case)obj;
                cas.AttachmentDownload__c = false;
                endpoint = cas.AttachmentLink__c;
                parentId = cas.Id;
                attachmentName = cas.AttachmentName__c;
                updateCaseList.add(cas);    
            }

            if ('RepairSubOrder__c'.equals(objectType)) {
                RepairSubOrder__c subOrder = (RepairSubOrder__c)obj;
                subOrder.AttachmentDownload__c = false;
                endpoint = subOrder.AttachmentLink__c;
                parentId = subOrder.Id;
                attachmentName = subOrder.AttachmentName__c;
                updateSubOrderList.add(subOrder);    
            }
            
        }

        try{
            List < Attachment > newAttList = AttachmentFiledDownload(endpoint, parentId, attachmentName);

            if (newAttList.size() > 0) {
                List < Attachment > delAttaList = [select id from Attachment where ParentId =: parentId];
                if (delAttaList.size() > 0) {
                    delete delAttaList;
                }
                insert newAttList;
            } 
            if (updateCaseList.size() > 0) {
                update updateCaseList;
            }

            if (updateSubOrderList.size() > 0) {
                update updateSubOrderList;
            }
            // update sObjectList;
        } catch (Exception ex) {

        }
        // }
    }

    global static List < Attachment > AttachmentFiledDownload(String endpoint, String parentId, String attachmentName) {

        List < Attachment > result = new List < Attachment > ();
        Http http = new Http();
        HttpRequest req = new HttpRequest();
        req.setTimeout(120000);
        req.setEndpoint(endpoint);
        req.setMethod('GET');

        HTTPResponse response = http.send(req);

        String status = response.getStatus();
        system.debug('status---->' + status);

        String statusCode = String.valueOf(response.getStatusCode());
        system.debug('statusCode---->' + statusCode);
        Attachment atta = new Attachment();
        atta.ParentId = parentId;
        atta.Name = attachmentName;
        if ('200'.equals(statusCode)) {
            // 附件大小
            // 12,582,912
            Integer ContentLength = Integer.valueOf(response.getHeader('Content-Length'));
            System.debug('ContentLength----->' + ContentLength);
            if (ContentLength < 12582912) {
                Blob bodyAsBlob = response.getBodyAsBlob();
                atta.Body = bodyAsBlob;
                result.add(atta);
            } else {
                atta.Name = '文件大小超过12M,请在新服务系统查看';
                atta.Body = Blob.valueOf('文件大小超过12M');
                result.add(atta);
            }

        }



        // system.debug('responseBody503--->'+bodyAsBlob);
        return result;
    }

    global void finish(Database.BatchableContext BC) {

    }
}