public with sharing class MCAEHandler extends Oly_TriggerHandler{

    private Map<Id, Maintenance_Contract_Asset_Estimate__c> newMap;
    private Map<Id, Maintenance_Contract_Asset_Estimate__c> oldMap;
    private List<Maintenance_Contract_Asset_Estimate__c> newList;
    private List<Maintenance_Contract_Asset_Estimate__c> oldList;


    public MCAEHandler () {
        this.newMap = (Map<Id, Maintenance_Contract_Asset_Estimate__c>) Trigger.newMap;
        this.oldMap = (Map<Id, Maintenance_Contract_Asset_Estimate__c>) Trigger.oldMap;
        this.newList = (List<Maintenance_Contract_Asset_Estimate__c>) Trigger.new;
        this.oldList = (List<Maintenance_Contract_Asset_Estimate__c>) Trigger.old;
    }

    protected override void afterUpdate() {
        // updateMainContract();
    }

    public void updateMainContract(){
        System.debug(LoggingLevel.INFO, '*** in update: ');
        Map<String,List<Maintenance_Contract_Asset_Estimate__c>> mcaMap = new Map<String,List<Maintenance_Contract_Asset_Estimate__c>>();
        Map<String,Maintenance_Contract_Asset_Estimate__c> assetIds = new Map<String,Maintenance_Contract_Asset_Estimate__c>();
        for (Maintenance_Contract_Asset_Estimate__c  mca: newList) {
            System.debug(LoggingLevel.INFO, '***test mca.Asset__c: '+ mca.Asset__c);
            //保有设备
            if(String.isNotBlank(mca.Asset__c)){
                assetIds.put(mca.Asset__c,mca);
            System.debug(LoggingLevel.INFO, '***test mca.Asset__c: '+ mca.Asset__c);

            }
            //报价
            if(mca.Repair_Price__c!=0 && mca.Repair_Price__c!=null){
            // if(mca.Maintenance_Contract_Estimate__c!='' && mca.Maintenance_Contract_Estimate__c!=null){
                List<Maintenance_Contract_Asset_Estimate__c> mcaList = new List<Maintenance_Contract_Asset_Estimate__c>();
                mcaList.add(mca);
                if(mcaMap.containsKey(mca.Maintenance_Contract_Estimate__c)){
                    List <Maintenance_Contract_Asset_Estimate__c> mlist = mcaMap.get(mca.Maintenance_Contract_Estimate__c);
                    mlist.add(mca);
                    mcaMap.put(mca.Maintenance_Contract_Estimate__c,mlist);
                }else{
                    mcaMap.put(mca.Maintenance_Contract_Estimate__c,mcaList);
                }
            }
            
        }
        //合同设备 
        List<Maintenance_Contract_Asset__c> upMcAssetList = new List<Maintenance_Contract_Asset__c>();
        for (Maintenance_Contract_Asset__c mcasset : [SELECT Id,Maintenance_Contract_Asset_Estimate__c FROM Maintenance_Contract_Asset__c WHERE Asset__c IN:assetIds.keySet()]) {
            Maintenance_Contract_Asset_Estimate__c mca= assetIds.get(mcasset.Maintenance_Contract_Asset_Estimate__c); 
            mcasset.Third_Party_Return__c = mca.Third_Party_Return__c;
            mcasset.Surcharge_Defective_Contract__c = mca.Surcharge_Defective_Contract__c;
            upMcAssetList.add(mcasset);
        }


        //报价
        List<Maintenance_Contract_Estimate__c> upMEstimateList = new List<Maintenance_Contract_Estimate__c>();
        //维修合同
        List<Maintenance_Contract__c> upMContractList = new List<Maintenance_Contract__c>();
        for (Maintenance_Contract_Estimate__c mc : [SELECT Id, Maintenance_Contract__c,IsSyncing__c FROM Maintenance_Contract_Estimate__c WHERE Id IN:mcaMap.keySet()]) {
            Boolean hasThirdPartyReturnc = false;
            Boolean hasSurchargeDefective = false;

            for (Maintenance_Contract_Asset_Estimate__c mca: mcaMap.get(mc.Id)) {
                    if(mca.Third_Party_Return__c == true){
                        hasThirdPartyReturnc = true;
                    }
                    if(mca.Surcharge_Defective_Contract__c == true){
                        hasSurchargeDefective = true;
                    }
                System.debug(LoggingLevel.INFO, '*** boolean: ' + hasThirdPartyReturnc);
                System.debug(LoggingLevel.INFO, '*** boolean: ' + hasSurchargeDefective);
            }
            if(hasThirdPartyReturnc == true){
                mc.Third_Party_Return__c = true;
                mc.Surcharge_Defective_Contract__c = false;
            }else if(hasThirdPartyReturnc == false && hasSurchargeDefective == true){
                mc.Surcharge_Defective_Contract__c = true;
                mc.Third_Party_Return__c = false;
            }else if(hasThirdPartyReturnc == false && hasSurchargeDefective == false){
                mc.Surcharge_Defective_Contract__c = false;
                mc.Third_Party_Return__c = false;
            }
 
            if(mc.IsSyncing__c == true){           
                if(mc.Maintenance_Contract__c != null){
                    Maintenance_Contract__c mContract = new Maintenance_Contract__c();
                    mContract.Id = mc.Maintenance_Contract__c;
                    mContract.Third_Party_Return_Contract__c = mc.Third_Party_Return__c;
                    mContract.Surcharge_Defective_Contract__c = mc.Surcharge_Defective_Contract__c;
                    // if(hasThirdPartyReturnc == true){
                    //     mContract.Third_Party_Return_Contract__c = true;
                    //     mContract.Surcharge_Defective_Contract__c = false;
                    // }else{
                    //     mContract.Surcharge_Defective_Contract__c = true;
                    //     mContract.Third_Party_Return_Contract__c = false;
                    // }

                    System.debug(LoggingLevel.INFO, '*** upMContractList: ' + upMContractList);
                    upMContractList.add(mContract);
                }
            }
            upMEstimateList.add(mc);

        }
        if(upMEstimateList.size()>0){
            update upMEstimateList;
        }
        System.debug(LoggingLevel.INFO, '*** 报价: ' + upMEstimateList);
        System.debug(LoggingLevel.INFO, '*** 维修合同: ' + upMContractList);
        System.debug(LoggingLevel.INFO, '*** 合同设备: ' + upMcAssetList);

        if(upMContractList.size()>0){
            update upMContractList;
        }

        if(upMcAssetList.size()>0){
            update upMcAssetList;
        }

    }

    protected override void beforeUpdate() {
        
    }
}