public without sharing class AgencyOppUpdHandler extends Oly_TriggerHandler {

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

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

    private static Map<Id, Agency_Opportunity__c> updAgencyOpportunityMap = new Map<Id, Agency_Opportunity__c>();

    protected override void beforeInsert() {
        //OLY_OCM-334
        //以下のロジックはなくしてください。（金額の入力は不要）
        //OCSM询价.病院契約金額_元(Dealer_Final_Price__c) = 经销商询价. 医院合同金额(含税)
        //OCSM询价.OCM预测成交金额(含税)_元(Wholesale_Price__c) = 经销商询价.订货金额(含税)
        //insertOppFromAgencyOpportunity();
    }

    protected override void afterInsert() {
        set_AgencyOpportunity_ChangeToOpportunity();
        set_AgencyOpportunity_OCSMOwner();

        if (updAgencyOpportunityMap.size() > 0) {
            update updAgencyOpportunityMap.values();
        }
    }

    protected override void afterUpdate() {
        set_AgencyOpportunity_Amount();
        set_AgencyOpportunity_OCSMOwner();

        if (updAgencyOpportunityMap.size() > 0) {
            update updAgencyOpportunityMap.values();
        }
    }

    //private void insertOppFromAgencyOpportunity() {
    //    for(Opportunity nObj : newList) {
    //        if(nObj.Agency_Opportunity__c != null) {
    //            nObj.Dealer_Final_Price__c = nObj.AgencyOpp_Amount__c;
    //            nObj.Wholesale_Price__c = nObj.AgencyOpp_OCMSale_Price__c;
    //        }
    //    }
    //}

    // .询价 Copy 到 询价、.询价.Change_To_Opportunity__c = oppIds.get(ao.Id)
    private void set_AgencyOpportunity_ChangeToOpportunity() {
        Map<Id, Id> oppIds = new Map<Id, Id>();
        for(Opportunity nObj : newList) {
            if(nObj.Agency_Opportunity__c != null) {
                oppIds.put(nObj.Agency_Opportunity__c, nObj.Id);
            }
        }
        if (oppIds.size() > 0) {
            List<Agency_Opportunity__c> aoList = [select Id, Name, Change_To_Opportunity__c from Agency_Opportunity__c where Id in :oppIds.keySet()];
            for(Agency_Opportunity__c ao : aoList) {
                if (false == updAgencyOpportunityMap.containsKey(ao.Id)) {
                    updAgencyOpportunityMap.put(ao.Id, ao);
                }
                ao = updAgencyOpportunityMap.get(ao.Id);
                ao.Change_To_Opportunity__c = oppIds.get(ao.Id);
            }
        }
    }

    // 询价 Win 的时候 更新 .询价 的 Amount__c = oppMap.get(ao.Id)
    private void set_AgencyOpportunity_Amount() {
        Map<Id, Decimal> oppMap = new Map<Id, Decimal>();
        for(Opportunity nObj : newList) {
            Opportunity oObj = oldMap.get(nObj.Id);
            if(nObj.Agency_Opportunity__c != null && nObj.AgencyOpp_FromOCM__c == true && oObj.SAP_Send_OK__c == false && nObj.SAP_Send_OK__c == true) {
                oppMap.put(nObj.Agency_Opportunity__c, nObj.Dealer_Final_Price__c);
            }
        }
        if (oppMap.size() > 0) {
            List<Agency_Opportunity__c> aoList = [select Id, Name, Amount__c from Agency_Opportunity__c where Id in :oppMap.keySet()];
            for(Agency_Opportunity__c ao : aoList) {
                if (false == updAgencyOpportunityMap.containsKey(ao.Id)) {
                    updAgencyOpportunityMap.put(ao.Id, ao);
                }
                ao = updAgencyOpportunityMap.get(ao.Id);
                ao.Amount__c = oppMap.get(ao.Id);
            }
        }
    }

    // 询价新規 or Owner変更 的时候 更新 .询价 的 OCSM_Owner__c = nObj.OwnerId;
    private void set_AgencyOpportunity_OCSMOwner() {
        for(Opportunity nObj : newList) {
            Opportunity oObj;
            if (oldMap != null) {
                oObj = oldMap.get(nObj.Id);
            }
            if(nObj.Agency_Opportunity__c != null
                && (oObj == null || nObj.OwnerId != oObj.OwnerId)
            ) {
                Agency_Opportunity__c ao = new Agency_Opportunity__c(Id = nObj.Agency_Opportunity__c);
                if (false == updAgencyOpportunityMap.containsKey(ao.Id)) {
                    updAgencyOpportunityMap.put(ao.Id, ao);
                }
                ao = updAgencyOpportunityMap.get(ao.Id);
                ao.OCSM_Owner__c = nObj.OwnerId;
            }
        }

    }
}