1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//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点执行
    }
    
}