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();
    // }
}