public without sharing class OpportunitySpecialApplyHandler extends Oly_TriggerHandler
{
    // 询价预测特殊对应
    private Map<Id, OpportunitySpecialApply__c> newMap;
    private Map<Id, OpportunitySpecialApply__c> oldMap;
    private List<OpportunitySpecialApply__c> newList;
    private List<OpportunitySpecialApply__c> oldList;

    Id recordTypeOpp =
    Schema.SObjectType.OpportunitySpecialApply__c.getRecordTypeInfosByDeveloperName().get('OpportunityRecordType').getRecordTypeId();

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

    protected override void beforeInsert() {
        // 批准后,将内容和理由赋值到询价
        updateWindow();
    }
    // taoqz 2020/07/02 start
    protected override void afterInsert() {
        updateOpp();
    }
    // taoqz 2020/07/02 end
    protected override void beforeUpdate() {
        // 批准后,将内容和理由赋值到询价
        updateWindow();
    }

    protected override void afterUpdate() {
        // 批准后,将内容和理由赋值到询价
        updateOpp();
    }

    private void updateWindow() {

        List<OCM_Management_Province__c> mpList = 
                    [select id, Name, Window1__c from OCM_Management_Province__c];

        // 取得OCSM管理省的营业窗口
        Map<String, OCM_Management_Province__c> mpMap = new Map<String, OCM_Management_Province__c>();
        for (OCM_Management_Province__c mp : mpList) {
            mpMap.put(mp.Name, mp);
        }

        // 设置审批人
        for (OpportunitySpecialApply__c n : newList) {
            OCM_Management_Province__c omp = mpMap.get(n.OCM_man_province_no_dealer__c);
            if (omp != null) {
                n.OCM_Window__c = omp.Window1__c;
            }
        }

    }

    // taoqz 2020/07/02 start
    // 更新注残预测
    
    // taoqz 2020/07/02 end

    private void updateOpp() {
        // 有重复更新询价 最新特殊对应 的情况，list 变map  Gzw 20201030 update
        // List<Opportunity> oppList = new List<Opportunity>();
        Map<String,Opportunity> oppMap = new Map<String,Opportunity>();
        List<Statu_Achievements__c> achList = new List<Statu_Achievements__c>();
        for (OpportunitySpecialApply__c n : newList) {

            Opportunity opp = new Opportunity();
            Statu_Achievements__c ach = new Statu_Achievements__c();
            //if (oldMap.get(n.Id).get('Status__c') != n.Status__c && n.Status__c == '已批准') {
            if ((Trigger.isInsert && n.Status__c == '已批准') 
                    || (Trigger.isUpdate && oldMap.get(n.Id).get('Status__c') != n.Status__c 
                        && n.Status__c == '已批准')) {    
                String strMemo;
                strMemo = '特殊对应:' + n.Apply_Content__c + ' ' + n.NMApply_Content_c__c;
                strMemo += '; 理由:' + n.Apply_Reason__c + ((n.Apply_Memo__c == null) ? '' : n.Apply_Memo__c);
                strMemo += '; 批准日期:' + n.Approval_Date__c.format();

                //if (n.recordType.DeveloperName == 'OpportunityRecordType') {
                if (n.RecordTypeId == recordTypeOpp) {    
                    // 询价特殊申请记录类型
                    opp.Id = n.Opportunity__c;
                    opp.SpecialApply__c = n.Id;
                    opp.SpecialApplyMemo__c = strMemo;
                    opp.Special_Apply_Content__c = n.Apply_Content__c;
                    opp.Special_Approval_Date__c = n.Approval_Date__c;
                    opp.Special_Apply_Reason__c = n.Apply_Reason__c + ((n.Apply_Memo__c == null) ? '' : n.Apply_Memo__c);


                    if (n.Apply_Content__c == '删除本月订货预测' || n.Apply_Content__c == '删除本月订发货预测') {
                        opp.Forecast_this_month__c = '';

                    } else if (n.Apply_Content__c == '新增本月订货预测') {
                        opp.Forecast_this_month__c = '本月订货预测';

                    } else if (n.Apply_Content__c == '新增本月订发货预测') {
                        opp.Forecast_this_month__c = '本月订发货预测';
                    }
                    // oppList.add(opp);
                    oppMap.put(opp.Id, opp);

                } else {
                    // 注残特殊申请记录类型
                    ach.Id = n.Achievements__c;
                    ach.SpecialApply__c = n.Id;
                    ach.Special_Apply_Content__c = n.Apply_Content__c;
                    ach.Special_NMApply_Content__c = n.NMApply_Content_c__c;
                    ach.Special_Approval_Date__c = n.Approval_Date__c;
                    ach.Special_Apply_Reason__c = n.Apply_Reason__c + ((n.Apply_Memo__c == null) ? '' : n.Apply_Memo__c);
                    ach.SpecialApplyMemo__c = strMemo;
                    // 2020/07/06 taoqz start 与月末判断有关,表明注残为手动审批,月末判断时跳过
                    ach.isManualApprove__c = true;
                    // 2020/07/06 taoqz end
                    if (n.Apply_Content__c == '新增本月发货预测') {
                        ach.Monthly_forecast_shipping__c = '本月发货预测';

                    } else if (n.Apply_Content__c == '删除本月发货预测') {
                        ach.Monthly_forecast_shipping__c = '';
                    }
                    //
                    if (n.NMApply_Content_c__c == '新增次月发货预测') {
                        ach.NM_forecast_shipping__c = '次月发货预测';

                    } else if (n.NMApply_Content_c__c == '删除次月发货预测') {
                        ach.NM_forecast_shipping__c = '';
                    }
                    //
                    achList.add(ach);

                    opp.Id = n.Opportunity__c;
                    opp.SpecialApply__c = n.Id;
                    // oppList.add(opp);
                    oppMap.put(opp.Id, opp);
                }
                
            }
        }

        // if (oppList.size() > 0) {
        //     update oppList;
        // }
        if (oppMap.size() > 0) {
            update oppMap.values();
        }
        if (achList.size() > 0) {
            update achList;
        }
    } 
}