高章伟
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
public without sharing class LostReportDetailHandler  extends Oly_TriggerHandler  {
    private Map<Id, LostReport_Detail__c> newMap;
    private Map<Id, LostReport_Detail__c> oldMap;
    private List<LostReport_Detail__c> newList;
    private List<LostReport_Detail__c> oldList;
 
    public LostReportDetailHandler() {
        this.newMap = (Map<Id, LostReport_Detail__c>) Trigger.newMap;
        this.oldMap = (Map<Id, LostReport_Detail__c>) Trigger.oldMap;
        this.newList = (List<LostReport_Detail__c>) Trigger.new;
        this.oldList = (List<LostReport_Detail__c>) Trigger.old;
    }
 
    protected override void beforeInsert() {
        beforeSetValue();
    }
 
    protected override void afterUpdate() {
        checkDelete();
    }
 
    protected override void afterDelete() {
        setDeleteField();
    }
 
    private void beforeSetValue() {
        for (LostReport_Detail__c nObj : newList) {
            if (Trigger.isInsert && nObj.Asset_day__c == null) {
                nObj.Asset_day__c = nObj.Asset_day_F__c;
            }
        }
    }
 
    private void setDeleteField() {
        List<Inventory_Detail__c> ldList = new List<Inventory_Detail__c>();
        List<Rental_Apply_Equipment_Set_Detail__c> raesdList = new List<Rental_Apply_Equipment_Set_Detail__c>();
        for (LostReport_Detail__c oObj : oldList) {
            if (String.isNotBlank(oObj.Rental_Apply_Equipment_Set_Detail__c) && oObj.LostReport_Status_F__c != '草案中') {
                raesdList.add(new Rental_Apply_Equipment_Set_Detail__c(Id = oObj.Rental_Apply_Equipment_Set_Detail__c
                        , DeleteLostReport_Detail_Reason__c = oObj.DeleteLostReport_Detail_Reason__c
                        , HadLostReport_Detail__c = oObj.HadLostReport_Detail__c
                        ));
            }
            if (String.isNotBlank(oObj.Inventory_Detail__c) && oObj.LostReport_Status_F__c != '草案中') {
                ldList.add(new Inventory_Detail__c(Id = oObj.Inventory_Detail__c
                        , DeleteLostReport_Detail_Reason__c = oObj.DeleteLostReport_Detail_Reason__c
                        , HadLostReport_Detail__c = oObj.HadLostReport_Detail__c
                        ));
            }
        }
        if (ldList.size() > 0) {
            update ldList;
        }
        if (raesdList.size() > 0) {
            update raesdList;
        }
    }
    private void checkDelete() {
        List<LostReport_Detail__c> ldList = new List<LostReport_Detail__c>();
        for (LostReport_Detail__c nObj : newList) {
            LostReport_Detail__c oObj = oldMap.get(nObj.Id);
            if (oObj.CancelLostReport__c != nObj.CancelLostReport__c
                    && nObj.CancelLostReport__c) {
                ldList.add(new LostReport_Detail__c(Id = nObj.Id));
            }
        }
        if (ldList.size() > 0) {
            delete ldList;
        }
    }
}