/*
** 20220512 ljh create 
** XLIU-CCY5ZW 【委托】【重要】询价中是否战略产品的判断确认
** 战略产品更新后执行 
** 历史数据执行 需要设定元数据  
*/
global class OppStrategyProductBatch implements Database.Batchable<sObject> {
    public String query;
    public Boolean isHistory = false;
    global OppStrategyProductBatch() {
        this.query = query;
    }
    // 历史数据
    global OppStrategyProductBatch(Boolean isHistory) {
        this.query = query;
        this.isHistory = isHistory;
    }

    global Database.QueryLocator start(Database.BatchableContext bc) {
        list<HistoryData__mdt> hDataList = new  list<HistoryData__mdt>();
        HistoryData__mdt hData = new HistoryData__mdt();
        Datetime startT;
        Datetime endT;
        Date startDate;
        Date endDate;
        // if(isHistory){
        hDataList = [SELECT DatetimeStart__c,DatetimeEnd__c  FROM  HistoryData__mdt  where DatetimeStart__c != null  limit 1];        
        if(hDataList.size() > 0){
            hData = hDataList[0];
            startT = hData.DatetimeStart__c;
            endT = hData.DatetimeEnd__c;
            startDate = startT.date();
            endDate = endT.date();
        }
        // }
        query = 'select Id from OpportunityLineItem ';
        query += ' where ((strategyProductText__c = 1 and strategyProduct__c = 0) or (strategyProductText__c = 0 and strategyProduct__c = 1)) ';
        if(isHistory){
            if(startT != null && endT != null){
                query += ' and ((Opportunity.SAP_Send_OK__c  = true';
                query += ' and Opportunity.SAP_Send_OK_Date__c >= :startT';
                query += ' and Opportunity.SAP_Send_OK_Date__c <= :endT';
                query += ') OR ';
                query += ' (Opportunity.Final_Contract_Proceeded_Date__c >= :startDate';
                query += ' and Opportunity.Final_Contract_Proceeded_Date__c <= :endDate';
                query += '))';
            }             
        }else{
            // startT 必须设置，不设置不满足需求。
            if(startT != null){
                query += ' and ((Opportunity.SAP_Send_OK__c = false  and Opportunity.StageName__c = \'询价\' ) ';
                query += ' OR (Opportunity.SAP_Send_OK__c  = true and Opportunity.SAP_Send_OK_Date__c >= :startT';
                query += '))';

                query += ' and ((Opportunity.SAP_Send_OK__c = false  and Opportunity.StageName__c = \'询价\' ) ';
                query += ' OR (Opportunity.SAP_Send_OK__c  = true and Opportunity.SAP_Send_OK_Date__c >= :startT';
                query += ')';
                query += ' OR Opportunity.Final_Contract_Proceeded_Date__c >= :startDate';
                query += ')';
            }
        }
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, list<OpportunityLineItem> oppLIList) {
        
        if(oppLIList.size() > 0){
            // 跳过这个对象的触发器 OpportunityLineItem
            StaticParameter.EscapeOppandStaTrigger = true;
            update oppLIList;
            StaticParameter.EscapeOppandStaTrigger = false;
        }  
    }

    global void finish(Database.BatchableContext BC) {

    }
}