高章伟
2022-03-18 a0bc3e3af59aed35334bdc38256a7a1dfe8aecf3
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/**
 * Select Id From Contact Where RecordTypeId = '01210000000QfWdAAK' and Strategic_dept_Class__c != null
 */
public without sharing class ContactTriggerHandler extends Oly_TriggerHandler {
    private Map<Id, Contact> newMap;
    private Map<Id, Contact> oldMap;
    private List<Contact> newList;
    private List<Contact> oldList;
    public ContactTriggerHandler() {
        this.newMap = (Map<Id, Contact>) Trigger.newMap;
        this.oldMap = (Map<Id, Contact>) Trigger.oldMap;
        this.newList = (List<Contact>) Trigger.new;
        this.oldList = (List<Contact>) Trigger.old;
    }
 
    protected override void afterInsert() {
        syncToAgencyContact();
        updateDealerNum();
    }
 
    protected override void afterUpdate() {
        syncToAgencyContact();
        updateDealerNum();
 
    }
    protected override void beforeUpdate() {
        // 服务技师 2018/12/24  Start
        UpdateProcessingWork();
        // 服务技师 2018/12/24  end
       
    }
 
 
    protected override void afterDelete() {
        syncToAgencyContactDelete();
        updateDealerNum();
    }
 
    //
    /**
     * @author 张玉山
     * @DateTime 2019-03-11T13:49:10+0800
     * 根据服务技师填写情况更新客户人员上的开展工作字段
     */
    private void UpdateProcessingWork() {
        for ( Contact temContact : newList) {
            if (System.label.UpdateServiceHistory.equals('true')) {
 
                string ProcessingWork = '';
                string ProcessingWorkWithoutNumber = '';
                if (temContact.ServiceBookInput__c != 0) {
                    ProcessingWork += '维修委托书填写' + temContact.ServiceBookInput__c + '次';
                    ProcessingWorkWithoutNumber +=  '维修委托书填写';
 
                }
                if (temContact.InspectTime__c != 0) {
                    if (String.isBlank(ProcessingWork)) {
                        ProcessingWork += '点检' + temContact.InspectTime__c + '次';
                        ProcessingWorkWithoutNumber +=  '点检';
                    } else {
                        ProcessingWork += ',点检' + temContact.InspectTime__c + '次';
                        ProcessingWorkWithoutNumber +=  ',点检';
 
                    }
                }
                if (temContact.InspectEquipmentTime__c != 0) {
                    if (String.isBlank(ProcessingWork)) {
                        ProcessingWork += '点检设备' + temContact.InspectEquipmentTime__c + '个';
                        ProcessingWorkWithoutNumber +=  '点检设备';
                    } else {
                        ProcessingWork += ',点检设备' + temContact.InspectEquipmentTime__c + '个';
                        ProcessingWorkWithoutNumber +=  ',点检设备';
                    }
 
                }
                if (temContact.VisitTime__c != 0) {
                    if (String.isBlank(ProcessingWork)) {
                        ProcessingWork += '上门' + temContact.VisitTime__c + '次';
                        ProcessingWorkWithoutNumber +=  '上门';
                    } else {
                        ProcessingWork += ',上门' + temContact.VisitTime__c + '次';
                        ProcessingWorkWithoutNumber +=  ',上门';
                    }
 
                }
                if (temContact.pollingTime__c != 0) {
                    if (String.isBlank(ProcessingWork)) {
                        ProcessingWork += '巡检' + temContact.pollingTime__c + '次';
                        ProcessingWorkWithoutNumber +=  '巡检';
                    } else {
                        ProcessingWork += ',巡检' + temContact.pollingTime__c + '次';
                        ProcessingWorkWithoutNumber +=  ',巡检';
                    }
 
                }
                if (temContact.TeachingTime__c != 0) {
                    if (String.isBlank(ProcessingWork)) {
                        ProcessingWork += '培训次数' + temContact.TeachingTime__c + '次';
                        ProcessingWorkWithoutNumber +=  '培训次数';
                    } else {
                        ProcessingWork += ',培训次数' + temContact.TeachingTime__c + '次';
                        ProcessingWorkWithoutNumber +=  ',培训次数';
                    }
                }
                //if (!String.isBlank(ProcessingWork)) {
                //    ProcessingWork += '。';
                //    ProcessingWorkWithoutNumber += '。';
                //}
                temContact.ProcessingWork__c = ProcessingWork;
                temContact.ProcessingWorkWithoutNumber__c = ProcessingWorkWithoutNumber;
 
 
            }
        }
 
    }
    // 服务技师 2018/12/24  end
    private void syncToAgencyContact() {
        Map<Id, Agency_Contact__c> targetContactMap = new Map<Id, Agency_Contact__c>();
        for (Contact nObj : newList) {
            if (nObj.RecordTypeId == '01210000000QfWdAAK'                // Doctor
                    && String.isBlank(nObj.Strategic_dept_Class__c) == false
               ) {
                if (Trigger.isInsert
                        || (Trigger.isUpdate
                            //                        && (
                            //                            oldMap.get(nObj.Id).LastName                != nObj.LastName
                            //                         || oldMap.get(nObj.Id).FirstName               != nObj.FirstName
                            //                         || oldMap.get(nObj.Id).Strategic_dept_Class__c != nObj.Strategic_dept_Class__c
                            //                         || oldMap.get(nObj.Id).Type__c                 != nObj.Type__c
                            //                         || oldMap.get(nObj.Id).Doctor_Division1__c     != nObj.Doctor_Division1__c
                            //                        )
                           )
                   ) {
                    Agency_Contact__c acObj = new Agency_Contact__c(Agency_ID__c = '000000000000000'
                            , Contact__c          = nObj.Id
                                                    , ContactId18__c      = nObj.Id
                                                            , Agency_Hospital__c  = null
                                                                    , Name                = nObj.LastName + ((String.isBlank(nObj.FirstName) == false) ? ' ' + nObj.FirstName : '')
                                                                            , Department_Class__c = nObj.Strategic_dept_Class__c
                                                                                    , Type__c             = nObj.Type__c
                                                                                            , Doctor_Division1__c = nObj.Doctor_Division1__c
                                                                   );
                    targetContactMap.put(nObj.Id, acObj);
                }
            }
        }
        if (targetContactMap.size() > 0) {
            upsert targetContactMap.values() ContactId18__c;
        }
    }
 
    private void syncToAgencyContactDelete() {
        List<Id> cIdList = new List<Id>();
        for (Contact oObj : oldList) {
            cIdList.add(oObj.Id);
        }
        if (cIdList.size() > 0) {
            List<Agency_Contact__c> acList = [Select Id From Agency_Contact__c
                                              Where Contact__c = :null and Agency_ID__c = '000000000000000'];
            if (acList.size() > 0) {
                delete acList;
            }
        }
    }
 
    //更新经销商用户人数字段 精琢技术 pk 2021-08-26 start
    private void updateDealerNum(){
        Set<Id> accountSet = new Set<Id>();
        Map<Id,Account> acMap = new Map<Id,Account>();
            if(Trigger.isUpdate){
               for(Contact contactnew : newList){
                    if(contactnew.Agency_User__c != oldMap.get(contactnew.Id).Agency_User__c || (System.Label.onlyUpdate == '1' && UserInfo.getUserId() == '00510000005sEEMAA2')){
                        accountSet.add(contactnew.AccountId);
                    }
               }
            }
 
            if(Trigger.isInsert){
                for(Contact contactnew : newList){
                    if(contactnew.Agency_User__c){
                        accountSet.add(contactnew.AccountId);
                    }
                }
            }
        
        
        if(Trigger.isDelete){
            for(Contact contactold : oldList){
                if(contactold.Agency_User__c){
                    accountSet.add(contactold.AccountId);
                }
            }
        }
       
        if(accountSet.size()>0){
            List<AggregateResult> contactList = [select count(id) ctn ,AccountId accid from Contact where Agency_User__c = true and AccountId = :accountSet and RecordTypeId = '01210000000QfWi' group by AccountId];
            for(AggregateResult ar : contactList){
                String accid = (String)ar.get('accid');
                Account account = new Account();
                account.id = accid;
                account.Dealer_Num__c = (Integer)ar.get('ctn');
                acMap.put(accid, account);
            }
 
            for (Id accountId : accountSet) {
                if (!acMap.containsKey(accountId)) {
                    Account account = new Account();
                    account.id = accountId;
                    account.Dealer_Num__c = 0;
                    acMap.put(accountId, account);
                }
            }
            
            if(acMap.size() > 0){
              update acMap.values();
            }
        }
 
 
    }
     //更新经销商用户人数字段 精琢技术 pk 2021-08-26 end
}