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
global class NFM605Batch implements Database.Batchable < sObject > , Database.AllowsCallouts {
    public String query;
    private List < Id > idList;
    public String qDate;
    public Date queryDate;
    public Boolean reissueBatch = false;
    global NFM605Batch(List < Id > idList) {
        this.idList = idList;
    }
    // eg:19000101
    global NFM605Batch(String querydate) {
        this.qDate = querydate;
    }
    // 指定数据
    global NFM605Batch(Boolean reissueBatch) {
        this.reissueBatch = reissueBatch;
    }
    global NFM605Batch() {}
 
    global Database.QueryLocator start(Database.BatchableContext bc) {
        if (this.idList != null && this.idList.size() != 0) {
            System.debug('+++++++++++指定数据Id+++++++++++' );
 
            return Database.getQueryLocator([
                SELECT id,NewMaintenance_Contract__c,reissueBatch__c 
                FROM NewMaintenanceReport_Task__c 
                WHERE id in : idList]);
        } else if (String.isNotBlank(this.qDate)) {
 
            queryDate = NFMUtil.parseStr2Date(qDate);
 
            return Database.getQueryLocator([
                SELECT Id,Name,NewMaintenance_Contract__c,reissueBatch__c 
                FROM NewMaintenanceReport_Task__c 
                WHERE CorrespondingPeriod__c =:queryDate]);
        } else if(this.reissueBatch){
            System.debug('+++++++++++数据量大时将数据的reissueBatch__c设置为true+++++++++++' );
 
            return Database.getQueryLocator([
                SELECT id,NewMaintenance_Contract__c,reissueBatch__c 
                FROM NewMaintenanceReport_Task__c 
                WHERE reissueBatch__c = true]);
        } else {
            System.debug('+++++++++++指定默认昨天+++++++++++' );
 
            queryDate = Date.today().addDays(-1); 
            return Database.getQueryLocator([
                SELECT Id,Name,NewMaintenance_Contract__c,reissueBatch__c 
                FROM NewMaintenanceReport_Task__c 
                WHERE CorrespondingPeriod__c =:queryDate ]);
        }
    }
    global void execute(Database.BatchableContext BC, list < NewMaintenanceReport_Task__c > scope) {
        List < Id > reportIdList = new  List < Id >();
        List <Id> ncIdList = new List <Id>();
 
        List<NewMaintenanceReport_Task__c> updateList = new List<NewMaintenanceReport_Task__c>();
        
        Map < Id, Set < Id >> tempIdMap = new Map < Id, Set < Id >> ();
 
        for (NewMaintenanceReport_Task__c task: scope) {
            if (task.reissueBatch__c) {
                task.reissueBatch__c = false; 
                updateList.add(task);   
            }
            Id contractId = task.NewMaintenance_Contract__c;
            Set < Id > idSet = new Set < Id > ();
            if (tempIdMap.containsKey(contractId)) {
                idSet = tempIdMap.get(contractId);
            }
            idSet.add(task.Id);
            tempIdMap.put(contractId, idSet);
 
        }
        // 查找符合的数据
        List < Repair__c > repairList = [
            SELECT Id, VM_Maintenance_Contract__c, Maintenance_Contract__c, MaintenanceContractType__c
            FROM Repair__c
            WHERE Maintenance_Contract__c IN: tempIdMap.keySet() 
            AND MaintenanceContractType__c = '服务合同'
            AND RepairSubOrder__c != ''
        ];
 
        for (Repair__c repair :repairList) {
            
            for (Id taskId: tempIdMap.get(repair.Maintenance_Contract__c)) {
                reportIdList.add(taskId);    
            }
        }
 
 
 
 
        if (reportIdList.size() >0 ) {
            NFM605Controller.executeNotFuture('', reportIdList);
        }
        
        if (updateList.size() > 0) {
            update updateList;
        }
    }
    global void finish(Database.BatchableContext BC) {
        System.debug('finish----->NFM605Controller');
    }
}