public without sharing class MaintanceContractPackEstimateHandler extends Oly_TriggerHandler{
    private Map<Id, MaintanceContractPackEstimate__c> newMap;
    private Map<Id, MaintanceContractPackEstimate__c> oldMap;
    private List<MaintanceContractPackEstimate__c> newList;
    private List<MaintanceContractPackEstimate__c> oldList;

    public MaintanceContractPackEstimateHandler(){
        this.newMap =  (Map<Id, MaintanceContractPackEstimate__c>) Trigger.newMap;
        this.oldMap =  (Map<Id, MaintanceContractPackEstimate__c>) Trigger.oldMap;
        this.newList = (List<MaintanceContractPackEstimate__c>) Trigger.new;
        this.oldList = (List<MaintanceContractPackEstimate__c>) Trigger.old;
        
    }
    protected override void afterUpdate() {
        List<Maintenance_Contract_Estimate__c> mceUpdateLists= new List<Maintenance_Contract_Estimate__c>();
        List<String> mcpIds = new List<string>();
        for(MaintanceContractPackEstimate__c mcpe: newList){
            mcpIds.add(mcpe.MaintanceContractPack__c);
        }
        List<Maintenance_Contract_Estimate__c> mceList = [Select Id,Name,Process_Status__c,
                    Maintenance_Contract__r.Id 
                    from Maintenance_Contract_Estimate__c 
                    where Maintenance_Contract__c in (Select Id from Maintenance_Contract__c  where MaintanceContractPack__c In :mcpIds) order by Maintenance_Contract__r.Id];


        for(MaintanceContractPackEstimate__c mcpe: newList){
            //打包合同报价提交审批，同步更新合同报价相关字段的值
            MaintanceContractPackEstimate__c oldMcpe = oldMap.get(mcpe.Id);
            // List<MaintanceContractPackEstimate__c> mcpeSelect = [Select Maintenance_Contract_Estimate__c from MaintanceContractPackEstimate__c where Id = :mcpe.Id ];
            // if(mcpeSelect.size()>0) {
            //     List<String> mceIds = mcpeSelect[0].Maintenance_Contract_Estimate__c.split(',');
            //     mceList = [Select Id,Name,Process_Status__c from Maintenance_Contract_Estimate__c where Name in :mceIds];
            // }

            //提交
            if (mcpe.Process_Status__c == '申请中' && oldMcpe.Process_Status__c != '申请中') {
                for(Maintenance_Contract_Estimate__c mce : mceList) {
                    //已经批准的合同报价不能再进行操作
                    if(mce.Process_Status__c != '批准' && mcpe.Maintenance_Contract_Estimate__c.contains(mce.Name)) {
                        mce.Process_Status__c = '申请中';
                        mce.Submit_quotation_day__c = System.today();
                        Approval.lock(mce.Id);
                        mceUpdateLists.add(mce);
                    }
                }
            }
            //撤回
            if (mcpe.Process_Status__c == '草案中' && oldMcpe.Process_Status__c != '草案中') {
                for(Maintenance_Contract_Estimate__c mce : mceList) {
                    if(mce.Process_Status__c != '批准' && mcpe.Maintenance_Contract_Estimate__c.contains(mce.Name)) {
                        mce.Process_Status__c = '草案中';
                        mce.Submit_quotation_day__c = null;
                        mceUpdateLists.add(mce);
                    }
                    
                }
                                                    
            }
            //拒绝
            if (mcpe.Process_Status__c == '不批准' && oldMcpe.Process_Status__c != '不批准') {
                for(Maintenance_Contract_Estimate__c mce : mceList) {
                    if(mce.Process_Status__c != '批准' && mcpe.Maintenance_Contract_Estimate__c.contains(mce.Name)) {
                        mce.Process_Status__c = '不批准';
                        mceUpdateLists.add(mce);
                    }
                    
                }
                                                    
            }
            //批准
            if (mcpe.Process_Status__c == '批准' && oldMcpe.Process_Status__c != '批准') {
                for(Maintenance_Contract_Estimate__c mce : mceList) {
                    System.debug('------MaintanceContractPackEstimateHandler contains:' +mcpe.Maintenance_Contract_Estimate__c +','+mce.Name+','+mcpe.Maintenance_Contract_Estimate__c.contains(mce.Name));

                    if(mce.Process_Status__c != '批准' && mcpe.Maintenance_Contract_Estimate__c.contains(mce.Name)) {
                        mce.Process_Status__c = '批准';
                        mce.Approved_quotation_day__c = System.today();
                        Approval.lock(mce.Id);
                        mceUpdateLists.add(mce);
                    }
                    
                }
                                                    
            }
            if (!mceUpdateLists.isEmpty()) {
                update mceUpdateLists;
            }

        }          

    }
}