/**
 * wangweipeng   20210723
 * 每天赋值给 备品借出申请表的 询价状态辅助（申请时）和 询价状态辅助（现在） 值
 *
 */
global class EnquiryBoostTypeBatch implements Database.Batchable<sObject> {
    public String query;
    private String errStr;
    private BatchIF_Log__c iflog;
    private final List<Id> TEST_ID = null;

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

        TEST_ID = testId;
    }

    global Database.QueryLocator start(Database.BatchableContext bc) {
        query = 'select id,name,Follow_pcl_status2_Text__c,Follow_pcl_status2__c,Follow_UP_Opp__r.StageName,Follow_UP_Opp__r.Status_Formula__c,Inquiry_Status_History__c,Inquiry_Status_Now__c from Rental_Apply__c  '
            + 'where  Bollow_Date__c != null and demo_purpose2__c = \'试用（有询价）\'  ';
        if (TEST_ID <> null) {
            query += ' and Id IN: TEST_ID';
        }
        iflog.Log__c += query+'\n';
        update iflog;
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, list<Rental_Apply__c> scope) {
        if(scope != null && scope.size() > 0){
            List<Rental_Apply__c> addListRentalApply = new List<Rental_Apply__c>();
            // 20221129 ljh DB202211366462 询价状态2 start
            String InquiryStatusMapping = System.Label.Inquiry_Status_Mapping;
            Map<String, Object> mapping = (Map<String, Object>) JSON.deserializeUntyped(InquiryStatusMapping);
            // 20221129 ljh DB202211366462 询价状态2 end
            for(Rental_Apply__c ra : scope){
                String newFollowPclStatus2Text = ra.Follow_pcl_status2_Text__c;
                String newFollowPclStatus2 = ra.Follow_pcl_status2__c;
                Rental_Apply__c raData = new Rental_Apply__c();
                raData.id = ra.id;
                
                if(newFollowPclStatus2Text != null){
                    // 20221129 ljh DB202211366462 询价状态2 start
                    //跟进中
                    // if(newFollowPclStatus2Text == '引合 : -' || newFollowPclStatus2Text == '引合 : 00 还没更新' || newFollowPclStatus2Text == '引合 : 01 还没拜访' || newFollowPclStatus2Text == '引合 : 02 跟进中'){
                    //     raData.Inquiry_Status_History__c = 1;
                    // }else if(newFollowPclStatus2Text == '引合 : 03 已授权'){//已授权
                    //     raData.Inquiry_Status_History__c = 2;
                    // }else if(newFollowPclStatus2Text == '引合 : 04 已中标' || newFollowPclStatus2Text == '引合 : 05 价格申请中' || newFollowPclStatus2Text == '引合 : 06 用户未签约' || newFollowPclStatus2Text == '引合 : 07 用户已签约'
                    //      || newFollowPclStatus2Text == '引合 : 08 已签约'){//准备签约
                    //     raData.Inquiry_Status_History__c = 3;
                    // }else if(newFollowPclStatus2Text == '出荷 : -' || newFollowPclStatus2Text == '完了 : -'  || newFollowPclStatus2Text == '注残 : -' || newFollowPclStatus2Text == '完了 : 08 已签约'){//已订货
                    //     raData.Inquiry_Status_History__c = 4;
                    // }else if(newFollowPclStatus2Text == '削除 : 17 取消' || newFollowPclStatus2Text == '敗戦 : 18 失单'){//0.取消  0.失单 
                    //     raData.Inquiry_Status_History__c = 0;
                    // }
                    raData.Inquiry_Status_History__c = mapping.get(newFollowPclStatus2Text) != null ? Integer.valueOf(mapping.get(newFollowPclStatus2Text)) : null;
                    // System.debug('zheli00:'+mapping.get(newFollowPclStatus2Text));
                    // 20221129 ljh DB202211366462 询价状态2 end
                }
                if(newFollowPclStatus2 != null){
                    // 20221129 ljh DB202211366462 询价状态2 start
                    //跟进中
                    // if(newFollowPclStatus2 == '引合 : -' || newFollowPclStatus2 == '引合 : 00 还没更新' || newFollowPclStatus2 == '引合 : 01 还没拜访' || newFollowPclStatus2 == '引合 : 02 跟进中'){
                    //     raData.Inquiry_Status_Now__c = 1;
                    // }else if(newFollowPclStatus2 == '引合 : 03 已授权'){//已授权
                    //     raData.Inquiry_Status_Now__c = 2;
                    // }else if(newFollowPclStatus2 == '引合 : 04 已中标' || newFollowPclStatus2 == '引合 : 05 价格申请中' || newFollowPclStatus2 == '引合 : 06 用户未签约' || newFollowPclStatus2 == '引合 : 07 用户已签约'
                    //      || newFollowPclStatus2 == '引合 : 08 已签约'){//准备签约
                    //     raData.Inquiry_Status_Now__c = 3;
                    // }else if(newFollowPclStatus2 == '出荷 : -' || newFollowPclStatus2 == '完了 : -'  || newFollowPclStatus2 == '注残 : -' || newFollowPclStatus2 == '完了 : 08 已签约'){//已订货
                    //     raData.Inquiry_Status_Now__c = 4;
                    // }else if(newFollowPclStatus2 == '削除 : 17 取消' || newFollowPclStatus2 == '敗戦 : 18 失单'){//0.取消  0.失单
                    //     raData.Inquiry_Status_Now__c = 0;
                    // }
                     
                    
                    raData.Inquiry_Status_Now__c = mapping.get(newFollowPclStatus2) != null ? Integer.valueOf(mapping.get(newFollowPclStatus2)) : null;
                    // System.debug('zheli01:'+mapping.get(newFollowPclStatus2));
                    // 20221129 ljh DB202211366462 询价状态2 end
                }
                Boolean aa = raData.Inquiry_Status_Now__c != ra.Inquiry_Status_Now__c;
                // System.debug('zheli03:'+aa+raData.Inquiry_Status_Now__c +'~~'+ ra.Inquiry_Status_Now__c);
                //为了减少update，如果值没有发生变化，那么不需要更新
                if((raData.Inquiry_Status_Now__c != ra.Inquiry_Status_Now__c) || (raData.Inquiry_Status_History__c != ra.Inquiry_Status_History__c)){
                    addListRentalApply.add(raData);
                }
            }
            Savepoint sp = Database.setSavepoint();
            try{
                // System.debug('zheli05:');
                update addListRentalApply;
            }catch(exception e){
                // System.debug('zheli04:'+e.getMessage());
                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 += 'OPDStrategyPersonShareBatch finish()\n';
        iflog.Log__c += '\nOPDStrategyPersonShareBatch 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;
          }
        }

        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 ++;
    }
}