liuyn
2024-03-11 a87f1c3df03078814ee97ad0c8ac200a232419e9
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
global class UpdateTenderInformationBatch2 implements Database.Batchable<sObject>, Database.Stateful{
    //历史数据处理时设置成false
    Boolean IsNeedExecute = true;
    //邮件信息
    List<String> emailMessages = new List<String>();
 
    //招投标: 报错的招投标Id
    String TenderlogStr = '招标项目 : ';
 
    //招投标: 报错信息
    String TendererrorStr = '';
 
    //招投标: 总件数
    Integer TendertotalCount = 0;
 
    //招投标: 失败件数
    Integer TenderfailedCount = 0; 
 
    //传过来的招标项目id集合
    List<String> TenderIdList2 = new List<String>();
 
    //从反应询价状态进来的时候设为true
    Boolean IsNeedfalg = false;
 
    public UpdateTenderInformationBatch2() {
    }
    public UpdateTenderInformationBatch2(Boolean falg) {
        this.IsNeedExecute=falg;
    }
    public UpdateTenderInformationBatch2(List<String> TenderIdList) {
        this.TenderIdList2=TenderIdList;
        this.IsNeedExecute=false;
        this.IsNeedfalg=true;
    }
    global Database.QueryLocator start(Database.BatchableContext bc) {
        Date today = Date.today();
        Date yesterday = Date.today().addDays(-1);
        Datetime firstDatetime=Datetime.newInstance(yesterday.year(),yesterday.month(),yesterday.day(),0,0,0);
        Datetime lasttDatetime=Datetime.newInstance(today.year(),today.month(),today.day(),23,59,59);
        String query = 'select Id,Tender_information__c,Opportunity__c ';
               query += 'FROM Tender_Opportunity_Link__c ';
        if(this.IsNeedExecute){
            query += 'WHERE Opportunity__r.LastModifiedDate >=:firstDatetime and Opportunity__r.LastModifiedDate <=:lasttDatetime';
        }
        if(this.IsNeedfalg){
            query += 'WHERE Tender_information__c =:TenderIdList2';
        }
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext BC, list<Tender_Opportunity_Link__c> TenderList) {
        Set<Id> enderOpportunityId = new Set<Id>();
        if(TenderList.size()>0){
            for (Tender_Opportunity_Link__c TenderOpportunity : TenderList) {
                enderOpportunityId.add(TenderOpportunity.Tender_information__c);
            }
        }
        system.debug('TenderList+++'+TenderList);
        //20240223 lt DB202402167647 【重要优先】定时刷新任务:询价/招标页面主机统计字段 add
        //lt add sql ,Opportunity__r.ConfirmationofAward__c,Opportunity__r.RivalHostsNumber__c,Opportunity__r.OlyNumberHosts__c,Opportunity__r.InquireNumberHosts__c
        List<Tender_Opportunity_Link__c> TenderOpportunityLinkList2 = [select Id,Tender_information__c,Opportunity__r.NumberOfBids__c,Opportunity__r.BidWinningNumber__c,Opportunity__r.Tender_Number_Flag__c,Opportunity__r.ConfirmationofAward__c,Opportunity__r.RivalHostsNumber__c,Opportunity__r.OlyNumberHosts__c,Opportunity__r.InquireNumberHosts__c from Tender_Opportunity_Link__c where Tender_information__c in:enderOpportunityId];
        Map<String,Tender_information__c> TenderinformationMap = new Map<String,Tender_information__c>();
        system.debug('TenderOpportunityLinkList2+++'+TenderOpportunityLinkList2);
        if(TenderOpportunityLinkList2.size()>0){
            for(Tender_Opportunity_Link__c TenderOpportunity2 :TenderOpportunityLinkList2){
                    if(TenderinformationMap.containsKey(TenderOpportunity2.Tender_information__c)){
                        Tender_information__c Tender_informationvalue = new Tender_information__c();
                        Tender_informationvalue = TenderinformationMap.get(TenderOpportunity2.Tender_information__c);
                        if(TenderOpportunity2.Opportunity__r.NumberOfBids__c==1&&Tender_informationvalue.NumberOfBids__c==0){
                            Tender_informationvalue.NumberOfBids__c=1;
                        }
                        if(Tender_informationvalue.BidWinningNumber__c==1&&TenderOpportunity2.Opportunity__r.BidWinningNumber__c==0){
                            Tender_informationvalue.BidWinningNumber__c=1;
                        }
                        if(TenderOpportunity2.Opportunity__r.Tender_Number_Flag__c==1&&Tender_informationvalue.Tender_Number__c==0){
                            Tender_informationvalue.Tender_Number__c=1;
                        }
                        //20240223 lt DB202402167647 【重要优先】定时刷新任务:询价/招标页面主机统计字段 start
                        // if(!String.isBlank(String.ValueOf(TenderOpportunity2.Opportunity__r.InquireNumberHosts__c))){
                        //     Tender_informationvalue.TotalNumberHosts__c += TenderOpportunity2.Opportunity__r.InquireNumberHosts__c;  //询价主机台数
                        // }
                        // if(!String.isBlank(String.ValueOf(TenderOpportunity2.Opportunity__r.OlyNumberHosts__c))){
                        //     Tender_informationvalue.OlyNumberHosts__c += TenderOpportunity2.Opportunity__r.OlyNumberHosts__c;  //询价主机台数
                        // }
                        // if(!String.isBlank(String.ValueOf(TenderOpportunity2.Opportunity__r.RivalHostsNumber__c))){
                        //     Tender_informationvalue.RivalHostsNumber__c += TenderOpportunity2.Opportunity__r.RivalHostsNumber__c;  //询价主机台数
                        // }
                        //20240223 lt DB202402167647 【重要优先】定时刷新任务:询价/招标页面主机统计字段 end
                        TenderinformationMap.put(TenderOpportunity2.Tender_information__c, Tender_informationvalue);
                    }else{
                        Tender_information__c Tender_informationvalue = new Tender_information__c();
                        Tender_informationvalue.Id=TenderOpportunity2.Tender_information__c;
                        Tender_informationvalue.NumberOfBids__c=TenderOpportunity2.Opportunity__r.NumberOfBids__c;
                        Tender_informationvalue.BidWinningNumber__c=TenderOpportunity2.Opportunity__r.BidWinningNumber__c;
                        Tender_informationvalue.Tender_Number__c=TenderOpportunity2.Opportunity__r.Tender_Number_Flag__c;
                        //20240223 lt DB202402167647 【重要优先】定时刷新任务:询价/招标页面主机统计字段 start
                        // Tender_informationvalue.TotalNumberHosts__c = String.isBlank(String.ValueOf(TenderOpportunity2.Opportunity__r.InquireNumberHosts__c)) ? 0 : TenderOpportunity2.Opportunity__r.InquireNumberHosts__c;  //询价主机台数
                        // Tender_informationvalue.OlyNumberHosts__c = String.isBlank(String.ValueOf(TenderOpportunity2.Opportunity__r.OlyNumberHosts__c)) ? 0 : TenderOpportunity2.Opportunity__r.OlyNumberHosts__c;        //oly主机台数
                        // Tender_informationvalue.RivalHostsNumber__c = String.isBlank(String.ValueOf(TenderOpportunity2.Opportunity__r.RivalHostsNumber__c)) ? 0 : TenderOpportunity2.Opportunity__r.RivalHostsNumber__c;    //对手主机台数
                        //20240223 lt DB202402167647 【重要优先】定时刷新任务:询价/招标页面主机统计字段 end
                        TenderinformationMap.put(TenderOpportunity2.Tender_information__c, Tender_informationvalue);
                    }
            }
        }
        List<Tender_information__c> Tender_informationList = new List<Tender_information__c>();
        for (Tender_information__c value : TenderinformationMap.values()) {
            Tender_informationList.add(value);
        }
        system.debug('Tender_informationList+++'+Tender_informationList);
        if(Tender_informationList.size()>0){
            //一个招投标项目更新失败
            List<String> failedTenderList = new List<String>();
            Database.SaveResult[] saveTenderResults = Database.update(Tender_informationList,false);
            //招投标项目的总数
            TendertotalCount += saveTenderResults.size();
 
            for(Integer i = 0;i<saveTenderResults.size();i++) {
                if(!saveTenderResults.get(i).isSuccess() ){                   
                    TenderlogStr += Tender_informationList.get(i).id +' ,';
                    TendererrorStr += '失败招标项目 :'+Tender_informationList.get(i).id+'  失败原因:'+ String.ValueOf(saveTenderResults.get(i).getErrors()[0]).split(';')[2].split('=')[1] 
                    +' : '+String.ValueOf(saveTenderResults.get(i).getErrors()[0]).split(';')[1].split('=')[1] + '\r\n';
                    TenderfailedCount++ ;
                }
            }
        }
 
    }
    global void finish(Database.BatchableContext BC) {
        BatchIF_Log__c TenderIfLog = new BatchIF_Log__c();
        TenderIfLog.Type__c = 'UpdateTenderInformationBatch2ByTenderErrorLog';
 
        if (TenderlogStr.length() > 60000) {
            TenderlogStr = TenderlogStr.substring(0, 60000);
        }
        TenderIfLog.Log__c = TenderlogStr;
        TenderIfLog.Log__c += '\n end';
        if (TendererrorStr.length() > 60000) {
            TenderIfLog.ErrorLog__c = TendererrorStr.substring(0, 60000);
        } else {
            TenderIfLog.ErrorLog__c = TendererrorStr.substring(0, TendererrorStr.length());
        }
 
        insert TenderIfLog;
 
        emailMessages.add('失败日志ID为:' + TenderIfLog.Id + '\r\n失败信息:\r\n'+TendererrorStr);
 
        //发送邮件
        sendFieldEmail();
    }
    // 发送提醒邮件
    private void sendFieldEmail() {
        PretechBatchEmailUtil be = new PretechBatchEmailUtil();
        String[] toList = new String[] {UserInfo.getUserEmail()};
        String title = '招标项目招标数,中标数或者应标数更新失败';
        String[] ccList = new String[] {'fuyu@prec-tech.com'};
        if (System.Test.isRunningTest()) {
            be.successMail('', 1);
        }
        if (emailMessages.size() > 0 && TenderfailedCount > 0) {
            be.failedMail(toList, ccList, title, this.emailMessages.get(0)+'\n',
                            TendertotalCount, TendertotalCount - TenderfailedCount, TenderfailedCount,'',true);
            if(!Test.isRunningTest()){
                be.send();
            }
        }       
    }
}