高章伟
2022-02-24 2aa8da8af66aa8ae00f25831aed6bb0364176e7b
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
public with sharing class SyncRepairFromMCAETrigger {
    public static void syncRepair(List<Repair__c> newList, Map<Id, Repair__c> newMap, List<Repair__c> oldList, Map<Id, Repair__c> oldMap) {
        if (Trigger.isBefore) {
            List<String> mcId = new List<String> ();
            if (Trigger.isInsert || Trigger.isUpdate) {
                for (Repair__c r : newList) {
                    // if (String.isBlank(r.Maintenance_Contract__c)  == false) {
                    if (String.isBlank(r.Maintenance_Contract__c)  == false && 
                            (Trigger.isInsert || Trigger.isUpdate && r.Maintenance_Contract__c != oldMap.get(r.Id).Maintenance_Contract__c)) {
                        mcId.add(r.Maintenance_Contract__c);
                    }
                }
            }
            List<String> mceId = new List<String> ();
            List<Maintenance_Contract__c> mcList = new List<Maintenance_Contract__c> ();
            List<Maintenance_Contract_Asset_Estimate__c> mcaeList = new List<Maintenance_Contract_Asset_Estimate__c> ();
            if (mcId.size() > 0) {
                mcList = [SELECT Id, Estimation_Id__c FROM Maintenance_Contract__c WHERE Id IN:mcId];
            }
            //if (mcList.size() > 0) {
            //    for (Maintenance_Contract__c mc : mcList) {
            //        if (String.isBlank(mc.Estimation_Id__c) == false) {
            //            mceId.add(mc.Estimation_Id__c);
            //        }
            //    }
            //}
            //if (mceId.size() > 0) {
            //    mcaeList = [SELECT Id, Asset__c, Maintenance_Contract_Estimate__c, Maintenance_Contract_Estimate__r.Maintenance_Contract__c FROM Maintenance_Contract_Asset_Estimate__c WHERE Maintenance_Contract_Estimate__c IN : mceId];
            //}
            if (mcId.size() > 0) {
                mcaeList = [SELECT Id, Asset__c, Maintenance_Contract_Estimate__c, Maintenance_Contract_Estimate__r.Maintenance_Contract__c 
                              FROM Maintenance_Contract_Asset_Estimate__c 
                            WHERE Maintenance_Contract_Estimate__r.Maintenance_Contract__c IN : mcId
                              AND Maintenance_Contract_Estimate__r.IsSyncing__c = True];
            }
            if (mcaeList.size() > 0) {
                for (Repair__c rpr : newList) {
                    if (String.isBlank(rpr.Maintenance_Contract__c)  == false) {
                        for (Maintenance_Contract_Asset_Estimate__c mcae : mcaeList) {
                            if (rpr.Maintenance_Contract__c == mcae.Maintenance_Contract_Estimate__r.Maintenance_Contract__c
                                && rpr.Delivered_Product__c == mcae.Asset__c) {
                                rpr.Maintenance_Contract_Asset_Estimate__c = mcae.Id;
                                break;
                            }
                        }
                    }
                }
            }
        }
    }
}