public with sharing class NFM202OppHandler extends Oly_TriggerHandler
{
    private Map<Id, Opportunity> newMap;
    private Map<Id, Opportunity> oldMap;
    private List<Opportunity> newList;
    private List<Opportunity> oldList;

    public NFM202OppHandler(){
        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;
    }

    protected override void beforeUpdate() {
        // CHAN-B9R472 <变更用户标识>变更时，记录变更前询价状态 20190305 LHJ Start
        //accChange();
        // CHAN-B9R472 <变更用户标识>变更时，记录变更前询价状态 20190305 LHJ End

        if (NFM202Controller.isRunning) {
            return;
        }
        // 调用询价接口ToSPO
        oppToSPO();
    }
    protected override void beforeInsert() {
        separateOpportunity();
    }
    //CHAN-B9L64W 询价拆单时，将授权信息携带到新询价中  XHL Start
    private void separateOpportunity(){
        
        List<String> originalOpportunityIds = new List<String>();
        for (Opportunity nObj : newList) {
            // CHAN-BK96DL 2019-12-28 by vivek start
            // if(Trigger.isInsert && String.isNotBlank(nObj.old_Oppo_No__c) && nObj.SeparateBillMark__c == '1'){
            //     originalOpportunityIds.add(nObj.old_Oppo_No__c);
            // }
            System.debug('======Opportunity_No__c'+nObj.Opportunity_No__c);
            if(Trigger.isInsert && String.isNotBlank(nObj.Old_Opportunity_ID__c) && nObj.SeparateBillMark__c == '1'){
                originalOpportunityIds.add(nObj.Old_Opportunity_ID__c);
            }
            // CHAN-BK96DL 2019-12-28 by vivek end
        }
        // CHAN-BK96DL 2019-12-28 by vivek start
        // List<Opportunity>  opportunitys = [select Id,Opportunity_No__c,Bidding_Project_Name__c,Bidding_No__c,Bidding_Content__c,Autholization_Activated_Date__c,Authorized_Finish_Sales__c,Authorized_DB_No__c,Authorized_Date__c,Bid_Date__c from Opportunity where Opportunity_No__c in :originalOpportunityIds];
        List<Opportunity>  opportunitys = [select Id,Opportunity_No__c,Bidding_Project_Name__c,Bidding_No__c,Bidding_Content__c,Autholization_Activated_Date__c,Authorized_Finish_Sales__c,Authorized_DB_No__c,Authorized_Date__c,Bid_Date__c,RecordType.DeveloperName from Opportunity where id = :originalOpportunityIds];
        // CHAN-BK96DL 2019-12-28 by vivek end
        Map<String,Opportunity> OpportMap = new Map<String,Opportunity>();
        for (Opportunity opp : opportunitys) {
            // CHAN-BK96DL 2019-12-28 by vivek start
            // OpportMap.put(opp.Opportunity_No__c, opp);
            OpportMap.put(opp.Id, opp);
            // CHAN-BK96DL 2019-12-28 by vivek end
        }
        for (Opportunity nObj : newList) { 
             // CHAN-BK96DL 2019-12-28 by vivek start
            // if (OpportMap.containsKey(nObj.old_Oppo_No__c)) {
            //     nObj.Authorized_DB_No__c = OpportMap.get(nObj.old_Oppo_No__c).Authorized_DB_No__c;
            //     nObj.Authorized_Date__c = OpportMap.get(nObj.old_Oppo_No__c).Authorized_Date__c;
            //     nObj.Autholization_Activated_Date__c  = OpportMap.get(nObj.old_Oppo_No__c).Autholization_Activated_Date__c;
            //     nObj.Authorized_Finish_Sales__c  = OpportMap.get(nObj.old_Oppo_No__c).Authorized_Finish_Sales__c;

            //     nObj.Bidding_Project_Name__c  = OpportMap.get(nObj.old_Oppo_No__c).Bidding_Project_Name__c;
            //     nObj.Bidding_No__c  = OpportMap.get(nObj.old_Oppo_No__c).Bidding_No__c;
            //     nObj.Bidding_Content__c  = OpportMap.get(nObj.old_Oppo_No__c).Bidding_Content__c;
            if (OpportMap.containsKey(nObj.Old_Opportunity_ID__c)) {
                nObj.Authorized_DB_No__c = OpportMap.get(nObj.Old_Opportunity_ID__c).Authorized_DB_No__c;
                nObj.Authorized_Date__c = OpportMap.get(nObj.Old_Opportunity_ID__c).Authorized_Date__c;
                nObj.Autholization_Activated_Date__c  = OpportMap.get(nObj.Old_Opportunity_ID__c).Autholization_Activated_Date__c;
                nObj.Authorized_Finish_Sales__c  = OpportMap.get(nObj.Old_Opportunity_ID__c).Authorized_Finish_Sales__c;

                nObj.Bidding_Project_Name__c  = OpportMap.get(nObj.Old_Opportunity_ID__c).Bidding_Project_Name__c;
                nObj.Bidding_No__c  = OpportMap.get(nObj.Old_Opportunity_ID__c).Bidding_No__c;
                nObj.Bidding_Content__c  = OpportMap.get(nObj.Old_Opportunity_ID__c).Bidding_Content__c;
                // CHAN-BVYAFW XHL
                if ('Opportunity'.equals(OpportMap.get(nObj.Old_Opportunity_ID__c).RecordType.DeveloperName)) {
                    // 普通询价 点击 政府招标拆单 按钮 进行拆单时的标记
                    // 本字段用在业务机会工作流 清空 原询价编码
                    nObj.GeneralInquirySeparateBill__c = true;
                }
                nObj.SeparateBill__c = true;
                nObj.old_Oppo_No__c = OpportMap.get(nObj.Old_Opportunity_ID__c).Opportunity_No__c;
                // CHAN-BVYAFW XHL

                System.debug('old_Oppo_No__c======'+nObj.old_Oppo_No__c);
                
                 // CHAN-BK96DL 2019-12-28 by vivek end
                //nObj.Bid_Date__c  = OpportMap.get(nObj.old_Oppo_No__c).Bid_Date__c; 
                // nObj.SeparateBillMark__c = '';
                

            }
            nObj.SeparateBillMark__c = '';
        }
    }
    //CHAN-B9L64W 询价拆单时，将授权信息携带到新询价中  XHL End
    

    //// CHAN-B9R472 <变更用户标识>变更时，记录变更前询价状态 20190305 LHJ Start
    //protected override void beforeInsert() {
    //    accChange();
    //}

    //private void accChange() {
    //    for (Opportunity nObj : newList) {
            
    //        // 如果新建时<变更用户标识>勾选,默认修改前是"引合"
    //        if (Trigger.isInsert && (nObj.If_Account_Change__c == True)) {
    //            nObj.AccChange_Stage_Before__c = '引合'; //oldMap.get(nObj.id).StageName;
            
    //        // 如果更新时<变更用户标识>变成勾选,保存修改前的状态
    //        } else if (Trigger.isUpdate && (nObj.If_Account_Change__c != oldMap.get(nObj.id).If_Account_Change__c)
    //                && (nObj.If_Account_Change__c == True)) {
    //            nObj.AccChange_Stage_Before__c = oldMap.get(nObj.id).StageName;
    //        }

    //        // 如果更新时<变更用户标识>变成不勾选，恢复状态
    //        if (Trigger.isUpdate && (nObj.If_Account_Change__c != oldMap.get(nObj.id).If_Account_Change__c)
    //                && (nObj.If_Account_Change__c == False)) {
    //            nObj.StageName = nObj.AccChange_Stage_Before__c;
    //        }
    //    }
    //}
    //// CHAN-B9R472 <变更用户标识>变更时，记录变更前询价状态 20190305 LHJ End


    private void oppToSPO() {
          
        // 更新対象的询价Id をセット
        List<String> oppIds = new List<String>();
        Map<String, String> transTypeMap = new Map<String, String>(); 

        for (Opportunity n : newList) {
            
            Opportunity o = oldMap.get(n.id);

            // 经销商授权(是否授权中由否变为是)
            if(n.If_Authorizing__c == true && o.If_Authorizing__c == false) {
                NFM202Controller.isRunning = true;
                oppIds.add(n.id);
                transTypeMap.put(n.id, '1'); 
            }
            // 合同授权
            if (n.Contract_Authorize__c == true && o.Contract_Authorize__c == false ) {
                NFM202Controller.isRunning = true;
                oppIds.add(n.id);
                transTypeMap.put(n.id, '2');
                
            }
        }

        if (oppIds.size() > 0) {
            // MessageGroupNumber の採番
            BatchIF_Log__c iflog = new BatchIF_Log__c();
            iflog.Type__c = 'NFM202';
            iflog.Log__c  = 'callout start\n';
            insert iflog;
            iflog = [Select Id, Name from BatchIF_Log__c where Id = :iflog.Id];
            System.debug(Logginglevel.DEBUG, 'NFM202_Opp_' + iflog.Name + ' start');                  // callout の中 end のlogを出します

            NFM202Controller.callout(iflog.Id, oppIds, transTypeMap);
        }

        for (Opportunity n : newList) {
            Opportunity o = oldMap.get(n.id);
            if (n.If_Authorizing__c == true && o.If_Authorizing__c == false ) {
                n.If_Authorizing__c = false;
            }
            if (n.Contract_Authorize__c == true && o.Contract_Authorize__c == false ) {
                n.Contract_Authorize__c = false;
            }
        }
    }
}