public with sharing class OpportunityFileTriggerHandler  extends Oly_TriggerHandler {
    //CHAN-BCNCRB XHL 

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

    protected override void beforeInsert(){
        beforeInsertValue();
    }

    protected override void beforeUpdate(){
        beforeSetValue();
    }

    protected override void beforeDelete(){
        beforeDeleteSetValue();
    }
    //CHAN-BER3N8; 一个询价只能有一个类型为"G.T1清单"的询价文件
    private void beforeInsertValue(){
        List<String> oppIds = new List<String>();
        List<Opportunity_File__c> opportunityFiles = new List<Opportunity_File__c>();

        for( Opportunity_File__c ofc :newList ){

            if ( ofc.Oppor_File_Stage__c == 'G' ) {

                oppIds.add(ofc.Opportunity__c);
                opportunityFiles.add(ofc);
            }
        }

        List<Opportunity_File__c> opportunity_FileS = [select Id,Oppor_File_Stage__c,Opportunity__c,
                                                        Opportunity__r.Opportunity_No__c 
                                                        from Opportunity_File__c
                                                        where Opportunity__c In :oppIds
                                                        And Oppor_File_Stage__c = 'G'];
        if ( opportunity_FileS.size() > 0) {
            for (Opportunity_File__c ofc : opportunityFiles) {
                ofc.addError('该询价已含有类型为G.T1清单的询价文件，不可重复创建 ');
                continue;
            }
        }

    }
    //CHAN-BER3N8; 询价做win之后，不可删除类型为"G.T1清单"的询价文件
    private void beforeDeleteSetValue () {
        List<String> oppIds = new List<String>();
        List<Opportunity_File__c>  opportunity_FileS = new List<Opportunity_File__c>();
        List<String> idNowList  = new List<String>();
        List<Opportunity> opportunitys  = new List<Opportunity>();
    
        for( Opportunity_File__c ofc :oldList ){

            if ( ofc.Oppor_File_Stage__c == 'G' && ofc.If_Opportunity_win__c == true){
                
                ofc.addError('询价已SAP上传（WIN）,不能删除该询价文件');
                continue;
            } else  if (ofc.Oppor_File_Stage__c == 'G'){
                oppIds.add(ofc.Opportunity__c);
                idNowList.add(ofc.Id);
            }
        }

        if (oppIds.size() > 0) {
            opportunity_FileS = [select Id,Oppor_File_Stage__c,Opportunity__c 
                                    from Opportunity_File__c
                                    where Opportunity__c In :oppIds
                                          And Id not In :idNowList
                                          And Oppor_File_Stage__c = 'G'];
            if (opportunity_FileS.size() <= 0) {
                for (String oppId : oppIds ) {
                    Opportunity opp = new Opportunity();
                    opp.Id = oppId;
                    opp.If_UploadT1Detailed__c = false;
                    opportunitys.add(opp);
                }
            } 

            if (opportunitys.size() > 0) {
                update opportunitys;
            }                             
        }
    }
    //询价win之后，不可修改类型"G.T1清单"的询价文件
    private void beforeSetValue() { 
        List<String> oppIds = new List<String>();
        List<Opportunity> opportunitys  = new List<Opportunity>();
        for( Opportunity_File__c ofc :newList ){
            //CHAN-BER3N8;Start
            if (ofc.Oppor_File_Stage__c == 'G' && ofc.If_Opportunity_win__c == true) {
                ofc.addError('询价已SAP上传（WIN）,不能修改该询价文件');
                continue;
            }
            //CHAN-BER3N8;End
            if (ofc.Oppor_File_Stage__c == 'G' && ofc.Last_upload_time__c != null) {
                oppIds.add(ofc.Opportunity__c);
            }
        }

        if (oppIds.size() > 0) {

            for (String oppId : oppIds ) {
                Opportunity opp = new Opportunity();
                opp.Id = oppId;
                opp.If_UploadT1Detailed__c = true;
                opportunitys.add(opp);
            }
        }

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