高章伟
2022-02-18 8b5f4c6c281cfa548f92de52c8021e37aa81901e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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;
            }
        }
    }
}