高章伟
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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
public without sharing class RepairReceiptHandler{
    // XHL 20200820 update
    public static  Map<Id,Repair__c> updateMaps(Map<Id,Repair__c> updateMap , Repair_receipt__c rr , Repair__c rep) {
        rep.id = rr.Repair__c;
        Boolean updateFlag = false;
 
        //开票名称
        if (String.isNotBlank(rr.BilltoPartyName__c)) {
            rep.BilltoPartyNoAccount__c = getValue(rr.BilltoPartyName__c,rr.Repair_NameOfInvoice__c);
            updateFlag = true;
        }
        
        // 经销商分类参考(发票)
        if (String.isNotBlank(rr.DealerType__c)) {
            rep.DealerClassification__c = getValue(rr.DealerType__c,rr.Repair_DealerClassificationReference__c);
            updateFlag = true;
        }
        
        // 经销商分类(发票)
        if (String.isNotBlank(rr.DealerTypeOrg__c)) {
            rep.DealerType__c = getValue(rr.DealerTypeOrg__c,rr.Repair_DealerClassification__c);
            updateFlag = true;
        }
        
        if (updateFlag) {
            updateMap.put(rep.id,rep);
        }
 
        return updateMap;
    }
    // XHL 20200820 update
    public static void UpdateMaintenanceInvoiceCollection(List<Repair_receipt__c> newList, Map<Id, Repair_receipt__c> newMap, List<Repair_receipt__c> oldList, Map<Id, Repair_receipt__c> oldMap){    
        Map<Id,Repair__c> updateMap = new Map<Id,Repair__c>();
 
        for (Repair_receipt__c rr : newList) {
 
            Repair__c rep = new Repair__c();
 
            if(updateMap != null && updateMap.size() > 0) {
                rep = updateMap.get(rr.Repair__c);
                if(rep != null) {
                    Boolean updateFlag = false;
 
                    //开票名称
                    if (String.isNotBlank(rr.BilltoPartyName__c)) {
                        rep.BilltoPartyNoAccount__c = getValue(rr.BilltoPartyName__c,rep.BilltoPartyNoAccount__c);
                        updateFlag = true;
                    }
                    
                    // 经销商分类参考(发票)
                    if (String.isNotBlank(rr.DealerType__c)) {
                        rep.DealerClassification__c = getValue(rr.DealerType__c,rep.DealerClassification__c);
                        updateFlag = true;
                    }
                    // 经销商分类(发票)
                    if (String.isNotBlank(rr.DealerTypeOrg__c)) {
                        rep.DealerType__c = getValue(rr.DealerTypeOrg__c,rep.DealerType__c);
                        updateFlag = true;
                    }
                    if (updateFlag) {
                        updateMap.put(rep.id,rep);
                    }
                }else {
                    rep = new Repair__c();
                    updateMap = updateMaps(updateMap,rr,rep);
                }
            }else {
 
                updateMap = updateMaps(updateMap,rr,rep);
            }
        }
        if(updateMap != null && updateMap.size() > 0){
            update updateMap.values();
        }
    }
    /** XHL 20200820 add
     * [getValue 数据拼接]
     * @param  repairReceiptValue [维修发票/回款 的 值]
     * @param  repairValue        [修理 的值]
     * @return                    [description]
     */
    public static  String getValue(String repairReceiptValue,String repairValue) {
        String value = '';
        if (String.isBlank(repairValue)) {
            value = repairReceiptValue;
        }else if ( String.isNotBlank(repairValue)) {
            if (repairValue.indexOf(repairReceiptValue) == -1) {
                String valueStr = repairReceiptValue + '/' + repairValue;
                if(valueStr != '' && valueStr.length() >= 255) {
                    value = valueStr.subString(0,255);
                } else {
                    value = valueStr;
                }                
            } else {
                value = repairValue;
            }
        }
        return value;
    }
    
 
    // public static void DeleteMaintenanceInvoiceCollection(List<Repair_receipt__c> newList, Map<Id, Repair_receipt__c> newMap, List<Repair_receipt__c> oldList, Map<Id, Repair_receipt__c> oldMap){
    //     List<Repair_receipt__c> rcs = new List<Repair_receipt__c>();
 
    //     // 需要删除的维修,在拼接名称的时候跳过
    //     Map<String,Repair_receipt__c> delRRMap = new Map<String,Repair_receipt__c>();
 
    //     // 修理Id对应下的维修
    //     Map<String,List<Repair_receipt__c>> rcMaps = new Map<String,List<Repair_receipt__c>>();
 
 
    //     rcs = oldList;
    //     for(Repair_receipt__c rr : rcs){
    //         delRRMap.put(rr.Id,rr);
    //     }
 
    //     // 拿到所有的修理id
    //     Set<String> ids = new Set<String>();
    //     for (Repair_receipt__c re : rcs) {
    //         ids.add(re.Repair__c);
    //     }
 
        
    //     // 拿到所有的修理
    //     Map<Id,Repair__c> resMap =  new Map<Id,Repair__c>([SELECT id,name,BilltoPartyNoAccount__c,DealerClassification__c FROM Repair__c WHERE id IN : ids]);
 
    //     // 拿到修理对应的所有维修发票/回款
    //     rcs = [SELECT id,BilltoPartyName__c,Repair_DealerClassificationReference__c,Repair_DealerClassification__c,Repair_NameOfInvoice__c,Repair__c,BilltoPartyNoAccount__c,AccNameF__c,DealerType__c,DealerTypeOrg__c FROM Repair_receipt__c WHERE Repair__c IN :ids];
 
    //     for (Repair_receipt__c rc : rcs) {
    //         List<Repair_receipt__c> res =  rcMaps.get(rc.Repair__c);
    //         if (res == null) {
    //             res = new List<Repair_receipt__c>();
    //         }
    //         res.add(rc);
    //         rcMaps.put(rc.Repair__c,res);
    //     }
        
 
    //     // 迭代集合,并对修理的新增字段开票名称赋值
    //     for (String repairId : rcMaps.keySet()) {
    //         Repair__c re = resMap.get(repairId);
    //         if (re != null) {
 
    //             //开票名称
    //             Set<String> names = new Set<String>();
 
    //             //经销商分类参考(发票)
    //             Set<String> Dealers = new Set<String>();
 
    //             //经销商分类(发票)
    //             Set<String> DealerTypes = new Set<String>();
 
    //             List<Repair_receipt__c> rrs = rcMaps.get(repairId);
 
    //             if(rrs != null){
 
    //                 for (Repair_receipt__c rr : rrs) {
    //                     if (delRRMap.get(rr.Id) != null) {
    //                         continue;
    //                     }
    //                     if (!String.isBlank(rr.BilltoPartyName__c) && String.isBlank(rr.Repair_NameOfInvoice__c)) {
    //                         names.add(rr.AccNameF__c);
    //                     }else if (!String.isBlank(rr.BilltoPartyName__c) && !String.isBlank(rr.Repair_NameOfInvoice__c)) {
    //                         if (rr.Repair_NameOfInvoice__c.indexOf(rr.BilltoPartyName__c) == -1) {
    //                             names.add(rr.AccNameF__c);
    //                         }
    //                     }
    
    
    //                     if (!String.isBlank(rr.DealerType__c) && String.isBlank(rr.Repair_DealerClassificationReference__c)){
    //                         Dealers.add(rr.DealerType__c);
    //                     }else if (!String.isBlank(rr.DealerType__c) && !String.isBlank(rr.Repair_DealerClassificationReference__c)) {
    //                         if (rr.Repair_DealerClassificationReference__c.indexOf(rr.DealerType__c) == -1) {
    //                             Dealers.add(rr.DealerType__c);
    //                         }
    //                     }
    
    //                     if(!String.isBlank(rr.DealerTypeOrg__c) && String.isBlank(rr.Repair_DealerClassification__c)){
    //                         DealerTypes.add(rr.DealerTypeOrg__c);  
    //                     }else if (!String.isBlank(rr.DealerTypeOrg__c) && !String.isBlank(rr.Repair_DealerClassification__c)) {
    //                         if (rr.Repair_DealerClassification__c.indexOf(rr.DealerTypeOrg__c) == -1) {
    //                             DealerTypes.add(rr.DealerTypeOrg__c);
    //                         }
    //                     }
    //                 }
    
    //                 if(names != null && names.size() > 0) {
    //                     //开票名称
    //                     String nameStr = String.join(new List<String>(names), '/');
    //                     if(nameStr != '' && nameStr.length() >= 255) {
    //                         re.BilltoPartyNoAccount__c = nameStr.subString(0,255);
    //                     } else {
    //                         re.BilltoPartyNoAccount__c = nameStr;
    //                     }
    //                 }
    
    //                 if (Dealers != null && Dealers.size() > 0) {
    //                     String dealersStr = String.join(new List<String>(Dealers), '/');
    //                     // 经销商分类参考(发票)
    //                     re.DealerClassification__c = dealersStr;
    //                 }
    
    //                 if (DealerTypes != null && DealerTypes.size() > 0) {
    //                     String DealerTypesStr = String.join(new List<String>(DealerTypes), '/');
    //                     // 经销商分类(发票)
    //                     re.DealerType__c = DealerTypesStr;
    //                 }
    //             }
    //         }
    //     }
    //     update resMap.values();
    // }
}