游畅
2022-05-17 b1dadcc62a48f7179d2d2011b17488e439d9db66
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
/*2022-05-13  you
* 更新产品咨询单
* 没有确认的,确认天数=今天-创建日----自动 batch
* 有确认的(存在最小日期) 确认天数=最小日期-创建日-----触发器
*/
global class UpdateInquiryFormConfirmationBatch implements Database.Batchable<sObject> {
    public String query;
    public String inqid;
    private BatchIF_Log__c iflog;
 
    global UpdateInquiryFormConfirmationBatch() {
        this.query = query;
        iflog = new BatchIF_Log__c();
        iflog.Type__c = 'PushNotification';
        iflog.Is_Error__c = 0;
        iflog.Log__c = 'UpdateInquiryFormConfirmationBatch start\n';
        iflog.ErrorLog__c = '';
        insert iflog;
    }
 
    global UpdateInquiryFormConfirmationBatch(String inqid) {
        this.inqid = inqid;
        this.query = query;
        iflog = new BatchIF_Log__c();
        iflog.Type__c = 'PushNotification';
        iflog.Is_Error__c = 0;
        iflog.Log__c = 'UpdateInquiryFormConfirmationBatch start\n';
        iflog.ErrorLog__c = '';
        insert iflog;
    }
 
    global Database.QueryLocator start(Database.BatchableContext BC) {
 
        query = 'select TimeoutDays__c, Id,CreateDate__c from Inquiry_form__c where CreateDate__c != null and MinimumDate__c =null';  
        if (inqid != null && inqid !='') {
           query += ' and id = :inqid ';
        }
        return Database.getQueryLocator(query);
    }
 
    global void execute(Database.BatchableContext BC, list<Inquiry_form__c> inquiryList) {
        system.debug('==='+inquiryList.size());
        String dateToday = String.valueOf(Date.today());
        for(Inquiry_form__c inq:inquiryList){
            String crdate = String.valueOf(inq.CreateDate__c);
 
            String confdate=calendarUtil.getWorkDayNum(crdate,dateToday);
            if(Integer.valueOf(confdate) > 5){
                inq.TimeoutDays__c = Integer.valueOf(confdate)-5;
 
             }else{
                inq.TimeoutDays__c = null;
             }
             inq.Confirmation_days__c =null;
            //inq.Confirmation_days__c =Integer.valueOf(confdate);
            system.debug(inq.id+'=相差几天=='+confdate +'==创建日=='+crdate+'==今天=='+dateToday);
        }
        Integer indexCon = 0;
        if(null!=inquiryList && inquiryList.size()>0){
           system.debug('inquiryList==='+inquiryList); 
           Database.SaveResult[] lsrUpdateCon = Database.update(inquiryList, false); 
            for (Database.SaveResult lsrChild : lsrUpdateCon) {
 
                if (!lsrChild.isSuccess()) {
                    iflog.Is_Error__c = 3;
                    Database.Error emsg = lsrChild.getErrors()[0];
                    iflog.ErrorLog__c += 'confdays: ' + inquiryList.get(indexCon).TimeoutDays__c + ' \n'
                                         + 'inquiryID: ' + inquiryList.get(indexCon).Id + '\n ConLog:' + emsg.getMessage() + '\n';
                    system.debug('lsrChild.isSuccess()=='+iflog.ErrorLog__c); 
                }
                indexCon ++ ;   
            } 
        }
        
    }
 
    global void finish(Database.BatchableContext BC) {
        system.debug('=====iflog:' + iflog.id+'==='+iflog.ErrorLog__c);
        iflog.Log__c += 'UpdateInquiryFormConfirmationBatch finish()\n';
        iflog.Log__c += '\nUpdateInquiryFormConfirmationBatch end';
 
        String tmp = iflog.ErrorLog__c;
        if (tmp.length() > 65000) {
            tmp = tmp.substring(0, 65000);
            tmp += ' ...have more lines...';
            iflog.ErrorLog__c = tmp;
        }
        String tmp2 = iflog.Log__c;
        if (tmp2.length() > 65000) {
            tmp2 = tmp2.substring(0, 65000);
            tmp2 += ' ...have more lines...';
            iflog.Log__c = tmp2;
        }
        if (System.Label.Log_IO_Flag == 'Keep') {
            system.debug('jinlaile');
            update iflog;
        } else if (System.Label.Log_IO_Flag == 'Auto') {
            system.debug('jinlaile1');
            if (iflog.Is_Error__c > 0) {
                update iflog;
            }
        }
 
    }
}