高章伟
2022-02-18 8b5f4c6c281cfa548f92de52c8021e37aa81901e
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
global class RentalTaskDispatchBatch implements Database.Batchable<sObject>  {
    public RentalTaskDispatchBatch() {
 
    }
 
    global Database.QueryLocator start(Database.BatchableContext BC) {
 
        String query = 'SELECT Id,    Rental_Apply__c,HadLostReport_Detail__c,Confirm_Lost_Date__c,Loaner_Giveup_Time__c ,Is_Body__c FROM Rental_Apply_Equipment_Set_Detail__c '  +
        ' where (HadLostReport_Detail__c = false and Confirm_Lost_Date__c != null and Loaner_Giveup_Time__c = null)'+
        ' or ( HadLostReport_Detail__c = false and Confirm_Lost_Date__c = null and Loaner_Giveup_Time__c = null '+
        ' and Check_lost_Item_F__c  = \'欠品\' and Return_DeliverySlip__r.Name = null and Lost_item_giveup__c = false )';
        //Rental_Apply_Equipment_Set_Detail__c HadLostReport_Detail__c 是否填写过遗失报告
        //    Confirm_Lost_Date__c 确认遗失时间  Loaner_Giveup_Time__c 欠品断念时间  Check_lost_Item_F__c 欠品确认结果
        // Return_DeliverySlip__r.Name 回寄运输单: 备品运输单号      Lost_item_giveup__c 放弃欠品回收
        System.debug('query---'+query);
        return Database.getQueryLocator(query);
    }
 
    global void execute(Database.BatchableContext BC, List<Rental_Apply_Equipment_Set_Detail__c> rentalApplyList) {
        //main(tasklist);
        Set<Id> loseIds = new Set<Id>();
        Set<Id> deficitIds = new Set<Id>();
        Set<Id> zhutiIds = new Set<Id>(); // 主体
        for (Rental_Apply_Equipment_Set_Detail__c raesd : rentalApplyList){
 
            if( raesd.Is_Body__c){
                zhutiIds.add(raesd.Rental_Apply__c);
            }
 
            if(raesd.Confirm_Lost_Date__c == null){
                deficitIds.add(raesd.Rental_Apply__c);
            }else{
                loseIds.add(raesd.Rental_Apply__c);
            }
            
 
        }
        if(loseIds.size() > 0){
            List<Rental_Apply__c> loseRentalApplyList = [select id,name from Rental_Apply__c where id in :loseIds];
            List<Task__c> loseTaskList=  new List<Task__c>();
            for(Rental_Apply__c rentalApply : loseRentalApplyList){
                Task__c tsk = new Task__c();
                tsk.RentalApply__c = rentalApply.id;
                tsk.recordtypeId = Schema.SObjectType.task__c.getRecordTypeInfosByDeveloperName().get('RentalLose').getRecordTypeId();
                tsk.distributionCount__c = 1;
                if(zhutiIds.contains(rentalApply.id)){
                    tsk.isMainPart__c = true;
                }
                loseTaskList.add(tsk);
            }
            insert loseTaskList;
        }
 
        if(deficitIds.size() > 0){
            List<Rental_Apply__c> deficitRentalApplyList = [select id,name from Rental_Apply__c where id in :deficitIds];
            List<Task__c> deficitTaskList=  new List<Task__c>();
            for(Rental_Apply__c rentalApply : deficitRentalApplyList){
                Task__c tsk = new Task__c();
                tsk.RentalApply__c = rentalApply.id;
                tsk.recordtypeId = Schema.SObjectType.task__c.getRecordTypeInfosByDeveloperName().get('RentalDeficit').getRecordTypeId();
                tsk.distributionCount__c = 1;
                deficitTaskList.add(tsk);
            }
            insert deficitTaskList;
        }
 
 
 
    }
 
    global void finish(Database.BatchableContext BC) {
    
    }
}