public without sharing class RequestTednerDocHandler extends Oly_TriggerHandler {
    private Map<Id, Request_tedner_doc__c> newMap;
    private Map<Id, Request_tedner_doc__c> oldMap;
    private List<Request_tedner_doc__c> newList;
    private List<Request_tedner_doc__c> oldList;
    private List<String> qisIdList;

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

    protected override void afterInsert() {
        updateTask();
    }
    protected override void afterUpdate() {
        updateTask();
    }

    private void updateTask() {

    	List<String> requestIdList = new List<String>();
    	// Map<String, Request_tedner_doc__c> requestMap = new Map<String, Request_tedner_doc__c>();

    	for (Request_tedner_doc__c rtd : newList) {
            if ((Trigger.isInsert ||
            	 	oldMap.get(rtd.Id).get('Status__c') != rtd.Status__c)) {
            	// 授权申请ID
            	RequestIdList.add(rtd.Id);
            }
        }

        if (RequestIdList != null && RequestIdList.size() > 0) {

        	String baseUrl = URL.getSalesforceBaseUrl().toExternalForm();
        	List<Task> updTask = new List<Task>();
        	List<Task> delTask = new List<Task>();
        	Map<String, Task> tskMap = new Map<String,Task>();

        	List<Task> lstTask = [ Select Id, Subject, OwnerId, Description, ActivityDate, RequestOpp__c
        							 From Task
        						    Where RequestOpp__c In :RequestIdList
                                     limit 10
        						 ];

        	for(Task tsk : lstTask) {
                tskMap.put(String.valueOf(tsk.RequestOpp__c) + tsk.OwnerId, tsk);
            }

    		for (Request_tedner_doc__c rtd : newList) {

    			if ((Trigger.isInsert ||
        	 			oldMap.get(rtd.Id).get('Status__c') != rtd.Status__c) && rtd.Status__c == '已提交' ) {

    				// 提交委托的时候，更新或新增任务
    				Task tsk = new Task();
    				String key = rtd.Id + String.valueof(rtd.Request_target__c);

    				if (tskMap.containsKey(key)) {
    					tsk = tskMap.get(key);
    				}

    				String todoSubject = '授权委托：' + rtd.Prospect_number__c;
            		String description = rtd.Tender_request_reason__c;

            		tsk.OwnerId = rtd.Request_target__c;
                	tsk.Subject = todoSubject;
                	tsk.Description = description;
                	tsk.ActivityDate = Date.today();
                	tsk.RequestOpp__c = rtd.Id;
                	tsk.ReportUrlText__c = baseUrl + '/' + rtd.Id;

                	updTask.add(tsk);

        		} else if(Trigger.isUpdate &&
        	 			oldMap.get(rtd.Id).get('Status__c') == '已提交' && rtd.Status__c == '草案中' ){

        			// 取消委托的时候，将任务删除
    				String key = rtd.Id + String.valueof(rtd.Request_target__c);

    				if (tskMap.containsKey(key)) {
    					delTask.add(tskMap.get(key));
    				}
        		}
    		}

            if (updTask.size() > 0) {
                upsert updTask;
            }
            if (delTask.size() > 0) {
                delete delTask;
            }
        }
    }
}