public without sharing class MaintenanceNewUpdateHandler extends Oly_TriggerHandler {
    private Map<Id, Maintenance_New_Update__c> newMap;
    private Map<Id, Maintenance_New_Update__c> oldMap;
    private List<Maintenance_New_Update__c> newList;
    private List<Maintenance_New_Update__c> oldList;
    public MaintenanceNewUpdateHandler() {
        this.newMap = (Map<Id, Maintenance_New_Update__c>) Trigger.newMap;
        this.oldMap = (Map<Id, Maintenance_New_Update__c>) Trigger.oldMap;
        this.newList = (List<Maintenance_New_Update__c>) Trigger.new;
        this.oldList = (List<Maintenance_New_Update__c>) Trigger.old;
    }
    protected override void afterInsert() {
        //insertMaintenance();
    }
    protected override void afterUpdate() {
        updateMaintenance();
    }
    protected override void beforeInsert() {
        insertMaintenance();
    }
    public void insertMaintenance(){
         system.debug('zheli:kaishi');
        // List<Maintenance_New_Update__c> upObj = new List<Maintenance_New_Update__c>();
        // List<Maintenance_New_Update__c> inObj = new List<Maintenance_New_Update__c>();
        Map<String,Date> startMap = new Map<String,Date>(); // 用于存放合同开始日
        Map<String,Date> endMap = new Map<String,Date>(); // 用于存放合同结束日
        Set<String> mcId = new Set<String>(); // 用于存放维修合同Id

        // 存放维修合同Id
        for (Maintenance_New_Update__c nObj : newList) {
            mcId.add(nObj.Maintenance_Contract__c);
        }

        // 查询当前维修合同
        List<Maintenance_Contract__c> mcList = [SELECT Id,Contract_End_Date__c,Contract_Start_Date__c From Maintenance_Contract__c WHERE Id IN:mcId];
        system.debug('zheli:'+ mcList.size());

        // 给原合同日期赋值
        if (mcList.size() > 0) {
        	// 存放维修合同当前日期
            for (Maintenance_Contract__c mcnObj : mcList) {
                startMap.put(mcnObj.Id, mcnObj.Contract_Start_Date__c);
                endMap.put(mcnObj.Id, mcnObj.Contract_End_Date__c);
            }
            // 赋值
            for (Maintenance_New_Update__c nObj : newList) {
                nObj.Old_Contract_Start_Date__c = startMap.get(nObj.Maintenance_Contract__c);
                nObj.Old_Contract_End_Date__c = endMap.get(nObj.Maintenance_Contract__c);
            }
        }
    }
    public void updateMaintenance(){
        Set<String> mcId = new Set<String>();
        Map<String,Date> startMap = new Map<String,Date>();
        Map<String,Date> endMap = new Map<String,Date>();
		System.debug('jinlai====================================updateMaintenance');
    	// Maintenance_New_Update__c nObj = new Maintenance_New_Update__c();

    	for (Maintenance_New_Update__c nObj : newList) {
			System.debug('jinlaifor====================================for');
    		Maintenance_New_Update__c oObj = oldMap.get(nObj.Id);
    		// 合同开始日/合同结束日不为空且与原来的不同、审批状态从【审批中】变成【已审批】、维修合同不为空
    		// if (((nObj.Contract_Start_Date__c != null && nObj.Contract_Start_Date__c != nObj.Old_Contract_Start_Date__c) || 
    		if ((nObj.Contract_Start_Date__c != null && nObj.Contract_Start_Date__c != nObj.Old_Contract_Start_Date__c) &&
    			// (nObj.Contract_End_Date__c != null && nObj.Contract_End_Date__c != nObj.Old_Contract_End_Date__c)) && 
    			nObj.Status__c == '已审批' && nObj.Maintenance_Contract__c != null && oObj.Status__c == '审批中') {
    			// 存放维修合同Id
    			mcId.add(nObj.Maintenance_Contract__c);
    			// 存放合同开始日、合同结束日
    			startMap.put(nObj.Maintenance_Contract__c, nObj.Contract_Start_Date__c);
    			endMap.put(nObj.Maintenance_Contract__c, nObj.Contract_End_Date__c);
    		}
    	}

    	// 维修合同Id有值，查询并更新
    	if (mcId.size() > 0) {
    		// 查询当前维修合同
        	List<Maintenance_Contract__c> mcList = [SELECT Id,Contract_End_Date__c,Contract_Start_Date__c From Maintenance_Contract__c WHERE Id IN:mcId];
        	
        	// 赋值并更新
        	if (mcList.size() > 0) {
	    		List<Maintenance_Contract__c> upList = new List<Maintenance_Contract__c>();
	    		// 赋值合同开始日、合同结束日
	    		for (Maintenance_Contract__c nObj : mcList) {
	    			nObj.Contract_Start_Date__c = startMap.get(nObj.Id);
	    			nObj.Contract_End_Date__c = endMap.get(nObj.Id);
	    			upList.add(nObj);
	    			System.debug('zheli===================================='+upList);
	    		}
	    		// 更新维修合同数据
	    		update upList;
	    	}
    	}

    }
}