//DB202312680435 市场分析日PBI和SFDC不一致
//刷新询价的最后修改时间
//背景：询价的市场分析日是公式跨对象取得招标的中标日，不更新询价的最后修改时间，PBI抽数据需要看最后修改时间
global class UpdateOppLastModifiedBatch implements Database.Batchable<SObject> , Database.Stateful{
    
    private final List<String> oppIdList = null;
    
    global UpdateOppLastModifiedBatch(){
    }

    global UpdateOppLastModifiedBatch(List<String> str){
        oppIdList = str;
    }

    global Database.QueryLocator start(Database.BatchableContext BC){

        //Datetime datetime1 = Datetime.newInstance(2015,3,1,13,26,0);
        Date today = Date.today();
        Date yesterday = Date.today().addDays(-1);

        Integer year = today.year();
        Integer mon1 = today.month();
        Integer day1 = today.day();
        String mon = mon1 < 10 ? '0' + String.valueOf(mon1) : String.valueOf(mon1);
        String day = day1 < 10 ? '0' + String.valueOf(day1) : String.valueOf(day1);

        Integer yesyear = yesterday.year();
        Integer yesmon1 = yesterday.month();
        Integer yesday1 = yesterday.day();
        String yesmon = mon1 < 10 ? '0' + String.valueOf(yesmon1) : String.valueOf(yesmon1);
        String yesday = yesday1 < 10 ? '0' + String.valueOf(yesday1) : String.valueOf(yesday1);

        String totime = year + '-' + mon + '-' + day + 'T18:00:00.000+0800';
        String yestime = yesyear + '-' + yesmon + '-' + yesday + 'T18:00:00.000+0800';
        System.debug('20240109---totime:'+totime);
        
        //select Id,Field,OldValue,NewValue from Tender_information__History
        String soql = 'Select Id,Field,OldValue,NewValue,CreatedDate,ParentId '
                    + 'From Tender_information__History '
                    + 'Where Field = \'Bid_Winning_Date__c\' ';
        if(!Test.isRunningTest()){
               soql += 'And CreatedDate >= ' + yestime 
                    + ' And CreatedDate < ' + totime ;
        }
        if(oppIdList <> null){
            soql += ' And ParentId in:oppIdList';
        }
        System.debug('20240109---soql:'+soql);
        return Database.getQueryLocator(soql);
    }

    global void execute(Database.BatchableContext BC, List<Tender_information__History> tenHList){
        System.debug('20240109---tenHList---'+tenHList);

        Set<String> tenIdList = new Set<String>();

        if(tenHList.Size() > 0 ){
            for(Tender_information__History tenh : tenHList){
                tenIdList.add(tenh.ParentId);
            }
        }

        //根据招标项目Id查找询价，更新询价的最后更新时间
        if(tenIdList.Size() > 0 ){
            List<Opportunity> oppList = [Select id From Opportunity Where Bidding_Project_Name_Bid__c in :tenIdList];
            if(oppList.Size() > 0){
                StaticParameter.EscapeOppandStaTrigger = true;
                update oppList;
                StaticParameter.EscapeOppandStaTrigger = false;
            }
        }
    }

    global void finish(Database.BatchableContext BC){
        Database.executebatch(new LastRepairUpdateBatch(),50);// WYL 2024/2/23 每天6点执行
    }
    
}