/**
 * wangweipeng  2021/08/24   
 * UpdatekeyPointProductBatch
 * 主要更新 备品借出申请 是否重点产品历史数据更新
 */
global class UpdatekeyPointProductBatch implements Database.Batchable<sObject> {
    public String query;
    private String errStr;
    private BatchIF_Log__c iflog;
    private final List<Id> TEST_ID = null;

    global UpdatekeyPointProductBatch() {
        iflog = new BatchIF_Log__c();
        iflog.Type__c = 'PushNotification';
        iflog.Is_Error__c = 0;
        iflog.Log__c = 'UpdatekeyPointProductBatch start1\n';
        iflog.ErrorLog__c = '';
        insert iflog;
    }
    global UpdatekeyPointProductBatch(List<Id> testId) {
        iflog = new BatchIF_Log__c();
        iflog.Type__c = 'PushNotification';
        iflog.Is_Error__c = 0;
        iflog.Log__c = 'UpdatekeyPointProductBatch start1\n';
        iflog.ErrorLog__c = '';
        insert iflog;

        TEST_ID = testId;
    }


    global Database.QueryLocator start(Database.BatchableContext bc) {
        query = 'select id,name,CreatedDate  from Rental_Apply__c where CreatedDate >= 2021-04-01T00:00:01.000+0000 and DeliverySlip__c != null and RA_Status__c != \'取消\' ';

        if (TEST_ID <> null) {
            query += ' and Id IN: TEST_ID';
        }

        query += ' order by CreatedDate';
        iflog.Log__c += query+'\n';
        update iflog;

        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, list<Rental_Apply__c> scope) {
        List<String> racIdList = new List<String>();
        if(scope != null && scope.size() > 0){
            for(Rental_Apply__c rac : scope){
                racIdList.add(rac.Id);
            }
        }
        if(racIdList != null && racIdList.size() > 0){
            String SQlStr3 = 'select id,name,Key_product__c,Rental_Apply_Equipment_Set__c,Rental_Apply__c from Rental_Apply_Equipment_Set_Detail__c '
                    +' where RAESD_Status__c != \'取消\' and EquipmentSet_Detail_Status_Status__c != \'取消\' and Rental_Apply__c in :racIdList';
            List<Rental_Apply_Equipment_Set_Detail__c> raesdList = Database.query(SQlStr3);
            List<Rental_Apply__c> rr = new List<Rental_Apply__c>();
            if(scope != null && scope.size() > 0){
                for(Rental_Apply__c rac : scope){
                    Rental_Apply__c rrr = new Rental_Apply__c();
                    boolean flag = false;
                    if(raesdList != null && raesdList.size() > 0){
                        for(Rental_Apply_Equipment_Set_Detail__c raesd : raesdList){
                            String racStrId = rac.id;
                            racStrId = racStrId.substring(0,15);
                            String raesdStrId = raesd.Rental_Apply__c;
                            raesdStrId = raesdStrId.substring(0,15);
                            if(racStrId == raesdStrId){
                                if(raesd.Key_product__c != null && raesd.Key_product__c != ''){
                                    flag = true;
                                    break;
                                }
                            }
                        }
                    }
                    if(flag){
                        rrr.id = rac.id;
                        rrr.Is_keyPoint_Product__c = flag;
                        rr.add(rrr);
                    }
                }
            }
            Savepoint sp = Database.setSavepoint();
            try{
                if(rr.size() > 0){
                    update rr;
                }
            }catch(exception e){
                errStr = e.getMessage()+'\n';
                errStr += e.getLineNumber()+'\n';
                Database.rollback(sp);
            }
        }
    }

    global void finish(Database.BatchableContext BC) {
        String tmp = '';
        if (String.isNotBlank(errStr)) {
            tmp = errStr;
        }
        iflog.Log__c += 'UpdatekeyPointProductBatch finish()\n';
        iflog.Log__c += '\nUpdatekeyPointProductBatch end';

        tmp += iflog.ErrorLog__c;
        if (tmp.length() > 60000) {
          tmp = tmp.substring(0, 60000);
          tmp += ' ...have more lines...';
          iflog.ErrorLog__c = tmp;
        }
        String tmp2 = iflog.Log__c;
        if (tmp2.length() > 60000) {
          tmp2 = tmp2.substring(0, 60000);
          tmp2 += ' ...have more lines...';
          iflog.Log__c = tmp2;
        }
        if (System.Label.Log_IO_Flag == 'Keep') {
          update iflog;
        } else if (System.Label.Log_IO_Flag == 'Auto') {
          if (iflog.Is_Error__c > 0) {
            update iflog;
          }
        }
    }
}