public with sharing class OpportunityFileOrderTriggerHandler  extends Oly_TriggerHandler {

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

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

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

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


    private void beforeInsertValue(){

        List<String> statuIds = new List<String>();
        List<OpportunityFileOrder__c> statuOFiles = new List<OpportunityFileOrder__c>();
        for( OpportunityFileOrder__c ofc :newList ){
            
            if ( ofc.OpporFileStage__c == 'G' ) {

                statuIds.add(ofc.StatuAchievements__c);
                statuOFiles.add(ofc);
            }
        }

        if (statuIds.size() > 0) {
            List<OpportunityFileOrder__c> opportunity_FileS = [select Id,OpporFileStage__c,StatuAchievements__c
                                                            from OpportunityFileOrder__c
                                                            where StatuAchievements__c In :statuIds
                                                            And OpporFileStage__c = 'G'];
            if (opportunity_FileS.size() > 0) {

                for (OpportunityFileOrder__c ofc : statuOFiles) {
                    ofc.addError('该注残已含有类型为G.T1清单的询价文件，不可重复创建 ');
                    continue;
                }    
            }
        }

    }


    private void beforeSetValue() { 
        List<String> statuIds = new List<String>();
        Map<String,Boolean> statuIdMap = new Map<String,Boolean>();
        List<Statu_Achievements__c> statuAList = new List<Statu_Achievements__c>();

        for( OpportunityFileOrder__c ofo :newList ){

            if (ofo.OpporFileStage__c == 'G' && ofo.LatestUploadTime__c != null) {

                statuIds.add(ofo.StatuAchievements__c);
                statuIdMap.put(ofo.StatuAchievements__c,true);
            } else if (ofo.OpporFileStage__c == 'G' && ofo.LatestUploadTime__c == null) {
            	statuIdMap.put(ofo.StatuAchievements__c,false);
            }
        }

        if (statuIdMap.size() > 0) {

            for (String statuId : statuIdMap.keySet() ) {
                Statu_Achievements__c statuA = new Statu_Achievements__c();
                statuA.Id = statuId;
                //statuA.If_UploadT1Detailed__c = true;
                statuA.If_UploadT1Detailed__c = statuIdMap.get(statuId);

                statuAList.add(statuA);
            }
        }

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