public without sharing class BatchUrlHardCodeProcessing implements Database.Batchable<sObject>, Database.Stateful {
    public String query;
    public Map<List<String>,String> errorMap;

    public BatchUrlHardCodeProcessing(String q){
        this.query = q;
        this.errorMap = new Map<List<String>,String>();
    }

    public Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator(query);
    }

    public void execute(Database.BatchableContext BC, List<Sobject> scope) {
        try {
            //组装ID
            List<String> olds = new List<String>();
            //正则匹配https
            Pattern MyPattern = Pattern.compile('(https?:\\/\\/[^\\s]+)');
            
            if(this.query.contains('QuoteIrai__c')) {
                for(QuoteIrai__c oldItem : (List<QuoteIrai__c>)scope) {
                    List<String> ss = oldItem.Note__c.split('/');
                    String oldID = '';
                    if(ss.size() > 3) {
                        if(ss[3].length() >= 15) {
                            oldID = ss[3].substring(0, 15);
                            String oldidE = '';
                            Integer flags;
            
                            for (Integer i = 0; i < 3; i++) {
                                flags = 0;
                                for (Integer j = 0; j < 5; j++) {
                                    string c = oldID.substring(i * 5 + j,i * 5 + j + 1);
                                    if (c.toUpperCase().equals(c) && c >= 'A' && c <= 'Z') {
                                        flags = flags + (1 << j);
                                    }
                                }
                                if (flags <= 25) {
                                    oldidE += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.substring(flags,flags+1);
                                }else{
                                    oldidE += '012345'.substring(flags - 26, flags-25);
                                }
                            }
                            oldidE = oldID+oldidE;
                            olds.add(oldidE);
                        }
                    }
                }
            }

            if(this.query.contains('Rental_Apply__c')) {
                for(Rental_Apply__c oldItem : (List<Rental_Apply__c>)scope) {
                    if(String.isNotBlank(oldItem.Response__c)) {
                        if(oldItem.Response__c.contains('https')) {
                            Matcher MyMatcher = MyPattern.matcher(oldItem.Response__c);

                            Integer count = 0;
                            while (MyMatcher.find()) {
                                count++;
                                System.debug(MyMatcher.group(1));
                                List<String> ss = MyMatcher.group(1).split('/');
                                String oldID = '';
                                if(ss.size() > 3) {
                                    if(ss[3].length() >= 15) {
                                        oldID = ss[3].substring(0, 15);
                                        String oldidE = '';
                                        Integer flags;
                        
                                        for (Integer i = 0; i < 3; i++) {
                                            flags = 0;
                                            for (Integer j = 0; j < 5; j++) {
                                                string c = oldID.substring(i * 5 + j,i * 5 + j + 1);
                                                if (c.toUpperCase().equals(c) && c >= 'A' && c <= 'Z') {
                                                    flags = flags + (1 << j);
                                                }
                                            }
                                            if (flags <= 25) {
                                                oldidE += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.substring(flags,flags+1);
                                            }else{
                                                oldidE += '012345'.substring(flags - 26, flags-25);
                                            }
                                        }
                                        oldidE = oldID+oldidE;
                                        olds.add(oldidE);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            
            if(this.query.contains('Task')) {
                for(Task oldItem : (List<Task>)scope) {
                    if(String.isNotBlank(oldItem.Description)) {
                        if(oldItem.Description.contains('https')) {
                            Matcher MyMatcher = MyPattern.matcher(oldItem.Description);

                            Integer count = 0;
                            while (MyMatcher.find()) {
                                count++;
                                System.debug(MyMatcher.group(1));
                                List<String> ss = MyMatcher.group(1).split('/');
                                String oldID = '';
                                if(ss.size() > 3) {
                                    if(ss[3].length() >= 15) {
                                        oldID = ss[3].substring(0, 15);
                                        String oldidE = '';
                                        Integer flags;
                        
                                        for (Integer i = 0; i < 3; i++) {
                                            flags = 0;
                                            for (Integer j = 0; j < 5; j++) {
                                                string c = oldID.substring(i * 5 + j,i * 5 + j + 1);
                                                if (c.toUpperCase().equals(c) && c >= 'A' && c <= 'Z') {
                                                    flags = flags + (1 << j);
                                                }
                                            }
                                            if (flags <= 25) {
                                                oldidE += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.substring(flags,flags+1);
                                            }else{
                                                oldidE += '012345'.substring(flags - 26, flags-25);
                                            }
                                        }
                                        oldidE = oldID+oldidE;
                                        olds.add(oldidE);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            
            if(this.query.contains('Consum_Apply__c')) {
                for(Consum_Apply__c oldItem : (List<Consum_Apply__c>)scope) {
                    if(String.isNotBlank(oldItem.Response__c)) {
                        if(oldItem.Response__c.contains('https')) {
                            Matcher MyMatcher = MyPattern.matcher(oldItem.Response__c);

                            Integer count = 0;
                            while (MyMatcher.find()) {
                                count++;
                                System.debug(MyMatcher.group(1));
                                List<String> ss = MyMatcher.group(1).split('/');
                                String oldID = '';
                                if(ss.size() > 3) {
                                    if(ss[3].length() >= 15) {
                                        oldID = ss[3].substring(0, 15);
                                        String oldidE = '';
                                        Integer flags;
                        
                                        for (Integer i = 0; i < 3; i++) {
                                            flags = 0;
                                            for (Integer j = 0; j < 5; j++) {
                                                string c = oldID.substring(i * 5 + j,i * 5 + j + 1);
                                                if (c.toUpperCase().equals(c) && c >= 'A' && c <= 'Z') {
                                                    flags = flags + (1 << j);
                                                }
                                            }
                                            if (flags <= 25) {
                                                oldidE += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.substring(flags,flags+1);
                                            }else{
                                                oldidE += '012345'.substring(flags - 26, flags-25);
                                            }
                                        }
                                        oldidE = oldID+oldidE;
                                        olds.add(oldidE);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            
            System.debug(olds);

            List<Tender_information__c> tenderInfoList = [select Id,Original_ID__c from Tender_information__c where Original_ID__c in :olds];
            List<Opportunity> oppList = [select Id,Original_ID__c from Opportunity where Original_ID__c in :olds];
            List<Lead> leadList = [select Id,Original_ID__c from Lead where Original_ID__c in :olds];
            List<Agency_Opportunity__c> agencyOppList = [select Id,Original_ID__c from Agency_Opportunity__c where Original_ID__c in :olds];

            List<Sobject> newList = new List<Sobject>();
            if(this.query.contains('QuoteIrai__c')) {
                for(QuoteIrai__c oldItem : (List<QuoteIrai__c>)scope) {
                    List<String> ss = oldItem.Note__c.split('/');
                    System.debug(ss);
                    String oldID = '';
                    String oldidE = '';
                    if(ss.size() > 3) {
                        if(ss[3].length() >= 15) {
                            oldID = ss[3].substring(0, 15);
                            Integer flags;
                            for (Integer i = 0; i < 3; i++) {
                                flags = 0;
                                for (Integer j = 0; j < 5; j++) {
                                    string c = oldID.substring(i * 5 + j,i * 5 + j + 1);
                                    if (c.toUpperCase().equals(c) && c >= 'A' && c <= 'Z') {
                                        flags = flags + (1 << j);
                                    }
                                }
                                if (flags <= 25) {
                                    oldidE += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.substring(flags,flags+1);
                                }else{
                                    oldidE += '012345'.substring(flags - 26, flags-25);
                                }
                            }
                            oldidE = oldID + oldidE;
                        }
                    }

                    String newStr ='';
                    for(Tender_information__c qic : tenderInfoList){
                        if(oldidE == qic.Original_ID__c){
                            if(ss[3].length()>15){
                                newStr = ss[0]+'//olympus.my.sfcrmproducts.cn/'+qic.Id+'\n'+ss[3].substring(16);
                            }else{
                                newStr = ss[0]+'//olympus.my.sfcrmproducts.cn/'+qic.Id;
                            }
                            oldItem.Note__c = newStr;
                        }
                    }
                    for(Opportunity qic : oppList){
                        if(oldidE == qic.Original_ID__c){
                            if(ss[3].length()>15){
                                newStr = ss[0]+'//olympus.my.sfcrmproducts.cn/'+qic.Id+'\n'+ss[3].substring(16);
                            }else{
                                newStr = ss[0]+'//olympus.my.sfcrmproducts.cn/'+qic.Id;
                            }
                            oldItem.Note__c = newStr;
                        }
                    }
                    for(Lead qic : leadList){
                        if(oldidE == qic.Original_ID__c){
                            if(ss[3].length()>15){
                                newStr = ss[0]+'//olympus.my.sfcrmproducts.cn/'+qic.Id+'\n'+ss[3].substring(16);
                            }else{
                                newStr = ss[0]+'//olympus.my.sfcrmproducts.cn/'+qic.Id;
                            }
                            oldItem.Note__c = newStr;
                        }
                    }
                    for(Agency_Opportunity__c qic : agencyOppList){
                        if(oldidE == qic.Original_ID__c){
                            if(ss[3].length()>15){
                                newStr = ss[0]+'//olympus.my.sfcrmproducts.cn/'+qic.Id+'\n'+ss[3].substring(16);
                            }else{
                                newStr = ss[0]+'//olympus.my.sfcrmproducts.cn/'+qic.Id;
                            }
                            oldItem.Note__c = newStr;
                        }
                    }
                    newList.add(oldItem);
                }
            }

            if(this.query.contains('Rental_Apply__c')) {
                for(Rental_Apply__c oldItem : (List<Rental_Apply__c>)scope) {
                    if(String.isNotBlank(oldItem.Response__c)) {
                        if(oldItem.Response__c.contains('https')) {
                            Matcher MyMatcher = MyPattern.matcher(oldItem.Response__c);

                            Integer count = 0;
                            while (MyMatcher.find()) {
                                count++;
                                System.debug(MyMatcher.group(1));
                                List<String> ss = MyMatcher.group(1).split('/');
                                System.debug(ss);
                                String oldID = '';
                                String oldidE = '';
                                if(ss.size() > 3) {
                                    if(ss[3].length() >= 15) {
                                        oldID = ss[3].substring(0, 15);
                                        Integer flags;
                                        for (Integer i = 0; i < 3; i++) {
                                            flags = 0;
                                            for (Integer j = 0; j < 5; j++) {
                                                string c = oldID.substring(i * 5 + j,i * 5 + j + 1);
                                                if (c.toUpperCase().equals(c) && c >= 'A' && c <= 'Z') {
                                                    flags = flags + (1 << j);
                                                }
                                            }
                                            if (flags <= 25) {
                                                oldidE += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.substring(flags,flags+1);
                                            }else{
                                                oldidE += '012345'.substring(flags - 26, flags-25);
                                            }
                                        }
                                        oldidE = oldID + oldidE;
                                    }
                                }

                                String newStr ='';
                                for(Tender_information__c qic : tenderInfoList){
                                    if(oldidE == qic.Original_ID__c){
                                        if(ss[3].length()>15){
                                            newStr = ss[0]+'//olympus.my.sfcrmproducts.cn/'+qic.Id+'\n'+ss[3].substring(16);
                                        }else{
                                            newStr = ss[0]+'//olympus.my.sfcrmproducts.cn/'+qic.Id;
                                        }
                                        oldItem.Response__c = MyMatcher.replaceAll(newStr);
                                    }
                                }
                                for(Opportunity qic : oppList){
                                    if(oldidE == qic.Original_ID__c){
                                        if(ss[3].length()>15){
                                            newStr = ss[0]+'//olympus.my.sfcrmproducts.cn/'+qic.Id+'\n'+ss[3].substring(16);
                                        }else{
                                            newStr = ss[0]+'//olympus.my.sfcrmproducts.cn/'+qic.Id;
                                        }
                                        oldItem.Response__c = MyMatcher.replaceAll(newStr);
                                    }
                                }
                                for(Lead qic : leadList){
                                    if(oldidE == qic.Original_ID__c){
                                        if(ss[3].length()>15){
                                            newStr = ss[0]+'//olympus.my.sfcrmproducts.cn/'+qic.Id+'\n'+ss[3].substring(16);
                                        }else{
                                            newStr = ss[0]+'//olympus.my.sfcrmproducts.cn/'+qic.Id;
                                        }
                                        oldItem.Response__c = MyMatcher.replaceAll(newStr);
                                    }
                                }
                                for(Agency_Opportunity__c qic : agencyOppList){
                                    if(oldidE == qic.Original_ID__c){
                                        if(ss[3].length()>15){
                                            newStr = ss[0]+'//olympus.my.sfcrmproducts.cn/'+qic.Id+'\n'+ss[3].substring(16);
                                        }else{
                                            newStr = ss[0]+'//olympus.my.sfcrmproducts.cn/'+qic.Id;
                                        }
                                        oldItem.Response__c = MyMatcher.replaceAll(newStr);
                                    }
                                }
                                newList.add(oldItem);
                            }
                        }
                    }
                }
            }
            
            if(this.query.contains('Task')) {
                for(Task oldItem : (List<Task>)scope) {
                    if(String.isNotBlank(oldItem.Description)) {
                        if(oldItem.Description.contains('https')) {
                            Matcher MyMatcher = MyPattern.matcher(oldItem.Description);

                            Integer count = 0;
                            while (MyMatcher.find()) {
                                count++;
                                System.debug(MyMatcher.group(1));
                                List<String> ss = MyMatcher.group(1).split('/');
                                System.debug(ss);
                                String oldID = '';
                                String oldidE = '';
                                if(ss.size() > 3) {
                                    if(ss[3].length() >= 15) {
                                        oldID = ss[3].substring(0, 15);
                                        Integer flags;
                                        for (Integer i = 0; i < 3; i++) {
                                            flags = 0;
                                            for (Integer j = 0; j < 5; j++) {
                                                string c = oldID.substring(i * 5 + j,i * 5 + j + 1);
                                                if (c.toUpperCase().equals(c) && c >= 'A' && c <= 'Z') {
                                                    flags = flags + (1 << j);
                                                }
                                            }
                                            if (flags <= 25) {
                                                oldidE += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.substring(flags,flags+1);
                                            }else{
                                                oldidE += '012345'.substring(flags - 26, flags-25);
                                            }
                                        }
                                        oldidE = oldID + oldidE;
                                    }
                                }

                                String newStr ='';
                                for(Tender_information__c qic : tenderInfoList){
                                    if(oldidE == qic.Original_ID__c){
                                        if(ss[3].length()>15){
                                            newStr = ss[0]+'//olympus.my.sfcrmproducts.cn/'+qic.Id+'\n'+ss[3].substring(16);
                                        }else{
                                            newStr = ss[0]+'//olympus.my.sfcrmproducts.cn/'+qic.Id;
                                        }
                                        oldItem.Description = MyMatcher.replaceAll(newStr);
                                    }
                                }
                                for(Opportunity qic : oppList){
                                    if(oldidE == qic.Original_ID__c){
                                        if(ss[3].length()>15){
                                            newStr = ss[0]+'//olympus.my.sfcrmproducts.cn/'+qic.Id+'\n'+ss[3].substring(16);
                                        }else{
                                            newStr = ss[0]+'//olympus.my.sfcrmproducts.cn/'+qic.Id;
                                        }
                                        oldItem.Description = MyMatcher.replaceAll(newStr);
                                    }
                                }
                                for(Lead qic : leadList){
                                    if(oldidE == qic.Original_ID__c){
                                        if(ss[3].length()>15){
                                            newStr = ss[0]+'//olympus.my.sfcrmproducts.cn/'+qic.Id+'\n'+ss[3].substring(16);
                                        }else{
                                            newStr = ss[0]+'//olympus.my.sfcrmproducts.cn/'+qic.Id;
                                        }
                                        oldItem.Description = MyMatcher.replaceAll(newStr);
                                    }
                                }
                                for(Agency_Opportunity__c qic : agencyOppList){
                                    if(oldidE == qic.Original_ID__c){
                                        if(ss[3].length()>15){
                                            newStr = ss[0]+'//olympus.my.sfcrmproducts.cn/'+qic.Id+'\n'+ss[3].substring(16);
                                        }else{
                                            newStr = ss[0]+'//olympus.my.sfcrmproducts.cn/'+qic.Id;
                                        }
                                        oldItem.Description = MyMatcher.replaceAll(newStr);
                                    }
                                }
                                newList.add(oldItem);
                            }
                        }
                    }
                }
            }

            if(this.query.contains('Consum_Apply__c')) {
                for(Consum_Apply__c oldItem : (List<Consum_Apply__c>)scope) {
                    if(String.isNotBlank(oldItem.Response__c)) {
                        if(oldItem.Response__c.contains('https')) {
                            Matcher MyMatcher = MyPattern.matcher(oldItem.Response__c);

                            Integer count = 0;
                            while (MyMatcher.find()) {
                                count++;
                                System.debug(MyMatcher.group(1));
                                List<String> ss = MyMatcher.group(1).split('/');
                                System.debug(ss);
                                String oldID = '';
                                String oldidE = '';
                                if(ss.size() > 3) {
                                    if(ss[3].length() >= 15) {
                                        oldID = ss[3].substring(0, 15);
                                        Integer flags;
                                        for (Integer i = 0; i < 3; i++) {
                                            flags = 0;
                                            for (Integer j = 0; j < 5; j++) {
                                                string c = oldID.substring(i * 5 + j,i * 5 + j + 1);
                                                if (c.toUpperCase().equals(c) && c >= 'A' && c <= 'Z') {
                                                    flags = flags + (1 << j);
                                                }
                                            }
                                            if (flags <= 25) {
                                                oldidE += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.substring(flags,flags+1);
                                            }else{
                                                oldidE += '012345'.substring(flags - 26, flags-25);
                                            }
                                        }
                                        oldidE = oldID + oldidE;
                                    }
                                }

                                String newStr ='';
                                for(Tender_information__c qic : tenderInfoList){
                                    if(oldidE == qic.Original_ID__c){
                                        if(ss[3].length()>15){
                                            newStr = ss[0]+'//olympus.my.sfcrmproducts.cn/'+qic.Id+'\n'+ss[3].substring(16);
                                        }else{
                                            newStr = ss[0]+'//olympus.my.sfcrmproducts.cn/'+qic.Id;
                                        }
                                        oldItem.Response__c = MyMatcher.replaceAll(newStr);
                                    }
                                }
                                for(Opportunity qic : oppList){
                                    if(oldidE == qic.Original_ID__c){
                                        if(ss[3].length()>15){
                                            newStr = ss[0]+'//olympus.my.sfcrmproducts.cn/'+qic.Id+'\n'+ss[3].substring(16);
                                        }else{
                                            newStr = ss[0]+'//olympus.my.sfcrmproducts.cn/'+qic.Id;
                                        }
                                        oldItem.Response__c = MyMatcher.replaceAll(newStr);
                                    }
                                }
                                for(Lead qic : leadList){
                                    if(oldidE == qic.Original_ID__c){
                                        if(ss[3].length()>15){
                                            newStr = ss[0]+'//olympus.my.sfcrmproducts.cn/'+qic.Id+'\n'+ss[3].substring(16);
                                        }else{
                                            newStr = ss[0]+'//olympus.my.sfcrmproducts.cn/'+qic.Id;
                                        }
                                        oldItem.Response__c = MyMatcher.replaceAll(newStr);
                                    }
                                }
                                for(Agency_Opportunity__c qic : agencyOppList){
                                    if(oldidE == qic.Original_ID__c){
                                        if(ss[3].length()>15){
                                            newStr = ss[0]+'//olympus.my.sfcrmproducts.cn/'+qic.Id+'\n'+ss[3].substring(16);
                                        }else{
                                            newStr = ss[0]+'//olympus.my.sfcrmproducts.cn/'+qic.Id;
                                        }
                                        oldItem.Response__c = MyMatcher.replaceAll(newStr);
                                    }
                                }
                                newList.add(oldItem);
                            }
                        }
                    }
                }
            }

            system.debug(newList);
            system.debug(newList.size());
            update newList;
        } catch (Exception e) {
            List<String> errorIdList = new List<String>();
            for(sObject obj : scope){
                errorIdList.add(obj.Id);
            }
            String error = 'e.getMessage():' + e.getMessage() + '-----e.getStackTraceString():' + e.getStackTraceString();
            System.debug('error Happens:' + error);
            errorMap.put(errorIdList, error);
        }
    }

    public void finish(Database.BatchableContext BC) {
        if(!errorMap.isEmpty()){
            System.debug('BatchUrlHardCodeProcessing 运行报错' +  + Json.serialize(errorMap));
        }
    }

    
}