global with sharing class BatchFileUploadController {
    public String newUrl {get; set;}
    public String staticResource {get; set;}

    public BatchFileUploadController(){
        AWS_Integration_Info__mdt awsConfiguration = [SELECT App_Id__c,Token_URL__c,App_Secret__c,Host_URL__c FROM AWS_Integration_Info__mdt  WHERE DeveloperName = 'AWS_Default_Configuration'];
        if (awsConfiguration == null) {
            System.debug('AWS_Integration_Info__mdt没配置');
        }else {
            newUrl = awsConfiguration.Host_URL__c + '/api/file/batchupload';
            staticResource = JSON.serialize(PIHelper.getPIIntegrationInfo('Document'));
        }
    }

    global class Response{
        public String recordId{set;get;}
        public String message{set;get;}
        public String status{set;get;}
    }

    global class ResponseDeleteFile{
        public List<String> keyList{set;get;}
        public String message{set;get;}
        public String status{set;get;}
    }

    @RemoteAction
    global static Response saveFile(String fileName,String key,String transId,String parentId,String bool){
        FileAddress__c file = new FileAddress__c();
        PIHelper.PIIntegration pI=PIHelper.getPIIntegrationInfo('Document');
        // 去除filename里得“&” zhj 2022-11-17
        fileName = fileName.remove('&');
        file.DownloadLink__c =pI.undeleteUrl+key+'&fileName='+fileName;
        file.FileName__c =fileName;
        file.ViewLink__c =pI.queryUrl+key;
        file.ParentRecordId__c =parentId;
        file.AWS_File_Key__c = key;
        Response response =new Response();
        Savepoint sp = Database.setSavepoint();
        try {
            insert file;
            //插入日志 
            //update 2022-11-17 加入新的日志方式
            PIHelper.saveTransLog('Document',key,transId,file.Id,JSON.serialize(file),'success','');
            response.recordId=file.Id;
            response.status='success';
            if(bool == 'true'){
                System.debug('成功调用batch');
                Database.executeBatch(new SetFrameNumManageBatch(), 100);     //上传成功后需要手动跑batch做数据和附件的关联 2022-12-27
            }
            return response;
        } catch (Exception e) {
            System.debug('into catch'+e.getMessage());
            PIHelper.saveTransLog('Document',key,transId,file.Id,JSON.serialize(file),'fail',e.getMessage());
            Database.rollback(sp);
            response.message=e.getMessage();
            response.status='fail';
            return response;
        }
    }

    @RemoteAction
    global static ResponseDeleteFile deleteFile(String fileIds){
        String[] ids = fileIds.split(',');
        System.debug('ids = ' + ids);
        List<String> idList = new List<String>();
        for(String str : ids){
            idList.add(str);
        }
        System.debug('idList = ' + idList);
        List<FileAddress__c> fList = [select id,AWS_File_Key__c from FileAddress__c where id in:idList];

        List<String> keyList = new List<String>();
        for(FileAddress__c fa : fList){
            keyList.add(fa.AWS_File_Key__c);
        }
        System.debug('keyList = ' + keyList);
        ResponseDeleteFile response =new ResponseDeleteFile();
        Savepoint sp = Database.setSavepoint();
        try {
            delete fList;
            response.keyList = keyList;
            response.message = '';
            response.status = 'success';
            return response;
        } catch (Exception e) {
            Database.rollback(sp);
            response.keyList = null;
            response.message = e.getMessage();
            response.status = 'success';
            return response;
        }
    }
}