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

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

    protected override void afterUpdate() {
        List<Maintenance_Contract__c> mcUpdateLists= new List<Maintenance_Contract__c>();
        List<String> mcpIds = new List<string>();
        List<MaintanceContractPack__c> mcpList = new List<MaintanceContractPack__c>();
        for(MaintanceContractPack__c mcp: newList){
            mcpIds.add(mcp.Id);
        }
        List<Maintenance_Contract__c> mcAllList =  [Select Id,MaintanceContractPack__c from Maintenance_Contract__c where MaintanceContractPack__c in :mcpIds Order By MaintanceContractPack__c];
        for(MaintanceContractPack__c mcp: newList){
            MaintanceContractPack__c mcpUpdate = new MaintanceContractPack__c();
            //打包合同合同复核 同步更新小合同合同复核相关字段的值
            MaintanceContractPack__c oldMcp = oldMap.get(mcp.Id);
            //提交
            if (mcp.MC_approval_status__c == 'Submit' && oldMcp.MC_approval_status__c != 'Submit') {
                for(Maintenance_Contract__c mc: mcAllList) {
                    if(mc.MaintanceContractPack__c == mcp.Id) {
                        mc.MC_approval_status__c = 'Submit';
                        mc.MC_approval_submit_time__c = Datetime.now();
                        //合同复核每个阶段解锁或锁定对应的小合同
                        Approval.lock(mc.Id);
                        mcUpdateLists.add(mc);
                    }
                }
            }
            //撤回
            if (mcp.MC_approval_status__c == 'Draft' && oldMcp.MC_approval_status__c != 'Draft') {
                for(Maintenance_Contract__c mc: mcAllList) {
                    if(mc.MaintanceContractPack__c == mcp.Id) {
                        mc.MC_approval_status__c = 'Draft';
                        mc.MC_approval_submit_time__c = null;
                        Approval.unlock(mc.Id);
                        mcUpdateLists.add(mc);
                    }
                }
            }
            //拒绝
            if (mcp.MC_approval_status__c == 'Reject' && oldMcp.MC_approval_status__c != 'Reject') {
                for(Maintenance_Contract__c mc: mcAllList) {
                    if(mc.MaintanceContractPack__c == mcp.Id) {
                        mc.MC_approval_status__c = 'Reject';
                        Approval.unlock(mc.Id);
                        mcUpdateLists.add(mc);
                    }
                }
            }
            //批准
            if (mcp.MC_approval_status__c == 'Pass' && oldMcp.MC_approval_status__c != 'Pass') {
                for(Maintenance_Contract__c mc: mcAllList) {
                    if(mc.MaintanceContractPack__c == mcp.Id) {
                        mc.MC_approval_status__c = 'Pass';
                        mc.MC_approval_pass_time__c  = Datetime.now();
                        Approval.lock(mc.Id);
                        mcUpdateLists.add(mc);
                         
                    }
                }
            }
            //更新合同相关日期
            if (mcp.Contract_Conclusion_Date__c !=  oldMcp.Contract_Conclusion_Date__c
                ||mcp.OCM_Print_request__c!=  oldMcp.OCM_Print_request__c 
                ||mcp.Contract_print_completed__c !=  oldMcp.Contract_print_completed__c 
                ||mcp.one_off_payment_agreed_period__c !=  oldMcp.one_off_payment_agreed_period__c 
                ||mcp.HP_Dealer_print__c !=  oldMcp.HP_Dealer_print__c 
                ||mcp.Submit_contract_to_Financial_dept__c     !=  oldMcp.Submit_contract_to_Financial_dept__c ) {
                for(Maintenance_Contract__c mc: mcAllList) {
                    if(mc.MaintanceContractPack__c == mcp.Id) {
                        //更新合同日
                        if(mcp.Contract_Conclusion_Date__c !=  oldMcp.Contract_Conclusion_Date__c) {
                            mc.Contract_Conclusion_Date__c = mcp.Contract_Conclusion_Date__c;
                        }
                        //更新OCM盖章申請 
                        if (mcp.OCM_Print_request__c!=  oldMcp.OCM_Print_request__c ) {
                            mc.OCM_Print_request__c  = mcp.OCM_Print_request__c;
                            
                        }
                        //更新合同盖章完毕--通过流取OCM 和医院（经销商）印的最晚值
                        // 同时更新第一次付款计划日 Contract_print_completed__c + VALUE(TEXT(one_off_payment_agreed_period__c))
                        if (mcp.Contract_print_completed__c!=  oldMcp.Contract_print_completed__c 
                            || mcp.one_off_payment_agreed_period__c !=  oldMcp.one_off_payment_agreed_period__c  ) {
                            if(mcp.Contract_print_completed__c!=null && String.isNotBlank(mcp.one_off_payment_agreed_period__c)) {
                                mc.Payment_Plan_Date_First__c = mcp.OCM_Print_request__c.addDays(Integer.valueof(mcp.one_off_payment_agreed_period__c));
                                mcpUpdate.Id = mcp.Id;
                                mcpUpdate.Payment_Plan_Date_First__c = mcp.OCM_Print_request__c.addDays(Integer.valueof(mcp.one_off_payment_agreed_period__c));
                            }
                            // mc.Contract_print_completed__c  = mcp.Contract_print_completed__c;
                        }
                        //更新医院（经销商）印
                        if (mcp.HP_Dealer_print__c!=  oldMcp.HP_Dealer_print__c ) {
                            mc.HP_Dealer_print__c  = mcp.HP_Dealer_print__c;
                        }
                        //更新财务提交日
                        if (mcp.Submit_contract_to_Financial_dept__c!=  oldMcp.Submit_contract_to_Financial_dept__c ) {
                            mc.Submit_contract_to_Financial_dept__c  = mcp.Submit_contract_to_Financial_dept__c;
                        }

                        mcUpdateLists.add(mc);
                    }
                    
                }
            }
            if(String.isNotBlank(mcpUpdate.Id)) {
                mcpList.add(mcpUpdate);
            }
        }
        if (!mcUpdateLists.isEmpty()) {
            update mcUpdateLists;
            update mcpList;
        }     

    }
}