sunxia
2022-04-18 85b18058da15575cbf98fc0227d231eb205bc30d
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
trigger QISTrigger on QIS_Report__c ( before insert,after insert, after update, before update, after delete) {
    if((!Test.isRunningTest())&&UserInfo.getUserId()==System.Label.ByPassTrigger){
        return;
    }
System.debug( '-----:QIS起動');
 
    // QISの画像を設定する
    if (Trigger.isBefore) {
        if(Trigger.new.size() == 1 && Trigger.isUpdate){
            List<OlympusCalendar__c> workday = [
                    select Id, Date__c, IsWorkDay__c 
                      from OlympusCalendar__c 
                     where Date__c >= :Date.Today()
                       and IsWorkDay__c = 1
                     order by Date__c
                     limit 3];
            if(workday.size()==3){
                Date selectDate = workday[2].Date__c;
                for (QIS_Report__c newTarget : Trigger.new) {
                    if(newTarget.PAE_AE_DeadLine_Date__c == null && newTarget.QIS_Submit_day__c == Date.today()){
                        newTarget.PAE_AE_DeadLine_Date__c = selectDate;
                    }
                }
            }
            
            
        }
        
        for (QIS_Report__c newTarget : Trigger.new) {
            newTarget.Owner_System__c = newTarget.OwnerId;
        }
        QISTrigger.setPhotoImgText(Trigger.new, Trigger.old, Trigger.newMap, Trigger.oldMap);
        
    }
    
    // 2020/11/18 songxiaoqi 取的询价的最终用户的管理编码  start 
    list<String> CnecID = new list<String>();
    if (Trigger.isBefore) {
        if (Trigger.isUpdate || Trigger.isInsert) {
            for(QIS_Report__c local : Trigger.new) {
                QIS_Report__c old = null;
                if(Trigger.isUpdate) {
                    old = Trigger.oldMap.get(local.Id);
                } //&& local.contract_number_ET__c != old.contract_number_ET__c
                // LHJ WLIG-C2V9UY Start
                // if (Trigger.isInsert || (Trigger.isUpdate && local.contract_number_ET__c != null && local.contract_number_ET__c != old.contract_number_ET__c)){
                //     CnecID.add(local.contract_number_ET__c);
                // }
                // if (Trigger.isUpdate && old.contract_number_ET__c !=  local.contract_number_ET__c && local.Managementtext__c != null && local.contract_number_ET__c == null ) {
                //     local.Managementtext__c = null;                                 
                // }
                if (Trigger.isInsert || (Trigger.isUpdate && local.capital_or_consumable__c == '耗材' && local.contract_number__c != old.contract_number__c)){
                    CnecID.add(local.contract_number__c);
                }
                // LHJ WLIG-C2V9UY End
            }    
        }
    }
    
    if(CnecID.size() > 0){
        Map<String,Statu_Achievements__c>  opstatumap = new Map<String,Statu_Achievements__c>(); 
        List<Statu_Achievements__c>  opstatuList = [select id,
                                                        //合同编号
                                                        ContractNO__c,
                                                        //询价最终用户是的管理号码
                                                        Opportunity__r.Account.Management_Code__c,
                                                        //询价最终用户爷爷的管理号码
                                                        Opportunity__r.Account.Parent.Parent.Management_Code__c,
                                                        //记录类型名
                                                        Opportunity__r.Account.RecordType.DeveloperName
                                                    from Statu_Achievements__c where ContractNO__c in : CnecID
                                                    ];               
        for (Statu_Achievements__c  optsta : opstatuList){
            opstatumap.put(optsta.ContractNO__c,optsta);
        }
        if (Trigger.isBefore) {
            if (Trigger.isUpdate || Trigger.isInsert){
                for (QIS_Report__c orc : Trigger.new){
                    // LHJ WLIG-C2V9UY Start
                    // if(orc.contract_number_ET__c != null && opstatumap.containsKey(orc.contract_number_ET__c)) {
                    //     //查看客户的记录类型 如果客户名是合同的话就取他parent的管理编码 如果是科室的话就取parent的parent的管理编码
                    //     if(opstatumap.get(orc.contract_number_ET__c).Opportunity__r.Account.RecordType.DeveloperName == 'AgencyContract') {
                    //         if (opstatumap.get(orc.contract_number_ET__c).Opportunity__r.Account.Management_Code__c != orc.Managementtext__c) {
                    //             orc.Managementtext__c = opstatumap.get(orc.contract_number_ET__c).Opportunity__r.Account.Management_Code__c;    
                    //         }
                    //     }else{
                    //         if(opstatumap.get(orc.contract_number_ET__c).Opportunity__r.Account.Parent.Parent.Management_Code__c != orc.Managementtext__c) {
                    //             orc.Managementtext__c = opstatumap.get(orc.contract_number_ET__c).Opportunity__r.Account.Parent.Parent.Management_Code__c;    
                    //         }
                    //     } 
                    // }
                    if(orc.capital_or_consumable__c == '耗材' && opstatumap.containsKey(orc.contract_number__c)) {
                        //查看客户的记录类型 如果客户名是合同的话就取他parent的管理编码 如果是科室的话就取parent的parent的管理编码
                        if(opstatumap.get(orc.contract_number__c).Opportunity__r.Account.RecordType.DeveloperName == 'AgencyContract') {
                            if (opstatumap.get(orc.contract_number__c).Opportunity__r.Account.Management_Code__c != orc.Managementtext__c) {
                                orc.Managementtext__c = opstatumap.get(orc.contract_number__c).Opportunity__r.Account.Management_Code__c;    
                            }
                        }else{
                            if(opstatumap.get(orc.contract_number__c).Opportunity__r.Account.Parent.Parent.Management_Code__c != orc.Managementtext__c) {
                                orc.Managementtext__c = opstatumap.get(orc.contract_number__c).Opportunity__r.Account.Parent.Parent.Management_Code__c;    
                            }
                        } 
                    }
                    // LHJ WLIG-C2V9UY End
                }   
            }    
        }       
    }
        // 2020/11/18 songxiaoqi 取的询价的最终用户的管理编码  end
    
    // ----------------------------------------------------------------------
    // これより、On-Callの「On-Call跟进结果」を更新するロジック
    // ----------------------------------------------------------------------
    
    if ( ( Trigger.isBefore && ( Trigger.isInsert || Trigger.isUpdate)) || ( Trigger.isAfter && Trigger.isDelete)) {  
    
        // 処理対象となるQISを全部確保
        List<QIS_Report__c> qiss = ( Trigger.isBefore ? Trigger.New : new List<QIS_Report__c>());
        Set<Id> cncallIds = new Set<Id>();
        for ( QIS_Report__c qis : ( Trigger.isBefore ? Trigger.New : Trigger.Old)) {
            cncallIds.add( qis.Source_OnCall__c);
        }
        
        // 処理対象となるオンコールを全部確保
        List<On_Call__c> oncalls = OncallFinalStatusManager.getOncallList(cncallIds);
    
        // 処理対象となる修理を全部確保
        List<Repair__c> repairs = OncallFinalStatusManager.getRepairListFromOncalls(oncalls);
        
        // FinalStatus更新処理
        OncallFinalStatusManager.setFinalStatus( oncalls, repairs, qiss);
        
        OncallFinalStatusManager.updOncalls(oncalls);
    }
    
    // ----------------------------------------------------------------------
    // ここまで、On-Callの「On-Call跟进结果」を更新するロジック
    // ----------------------------------------------------------------------
    
/*
    // ----------------------------------------------------------------------
    // これより、ユーザの経理項目をQISにセット
    // ----------------------------------------------------------------------
    
    if (!Trigger.isDelete) {
        // ログインユーザーからプレセス用の上司を設定
        User loginUser = null;
        for (QIS_Report__c qis : Trigger.new) {
            if (loginUser == null) {
                loginUser = [SELECT Id, Name, ManagerId, SalesManager__c, BuchangApprovalManagerSales__c, JingliApprovalManager__c, BuchangApprovalManager__c, ZongjianApprovalManager__c FROM User WHERE Id = :UserInfo.getUserId()];
            }
            qis.Owner_Manager__c = loginUser.ManagerId == null ? loginUser.Id : loginUser.ManagerId;
            qis.SalesManager__c = loginUser.SalesManager__c == null ? loginUser.Id : loginUser.SalesManager__c;
            qis.BuchangApprovalManagerSales__c = loginUser.BuchangApprovalManagerSales__c == null ? loginUser.Id : loginUser.BuchangApprovalManagerSales__c;
            qis.JingliApprovalManager__c = loginUser.JingliApprovalManager__c == null ? loginUser.Id : loginUser.JingliApprovalManager__c;
            qis.BuchangApprovalManager__c = loginUser.BuchangApprovalManager__c == null ? loginUser.Id : loginUser.BuchangApprovalManager__c;
            qis.ZongjianApprovalManager__c = loginUser.ZongjianApprovalManager__c == null ? loginUser.Id : loginUser.ZongjianApprovalManager__c;
        }
    }
*/
// Add by HANCHAO 8.25 for approve
    List<String> IDlist = new List<String>();
    if(Trigger.isBefore && Trigger.isUpdate ){
        for(QIS_Report__c qis : Trigger.new){
            QIS_Report__c old = new QIS_Report__c();
            old = Trigger.oldMap.get(qis.Id);
            if(qis.RC__c !=old.RC__c){
                IDlist.add(qis.RC__c);
            }
        }
    }
    List<User> Decider = [SELECT Id, Name, JingliApprovalManager__c, BuchangApprovalManager__c, ZongjianApprovalManager__c, SalesManager__c, BuchangApprovalManagerSales__c FROM User WHERE Id = :IDlist];
    Map<ID,User> QIStoUser = new Map<ID,User>();
    for(user us : Decider){
        if(QIStoUser.containsKey(us.Id)){
            continue;
        }else{
            QIStoUser.put(us.Id, us);
        }
    }
    if (Trigger.isBefore && Trigger.isUpdate) {
        for (QIS_Report__c qis : Trigger.new) {
            QIS_Report__c  old = Trigger.oldMap.get(qis.Id);
            // DOJ2.1 gzw AwareDate公式考文本 start
            if (qis.AwareDate__c != qis.Aware_date__c &&
                (qis.Aware_date__c >= Date.newInstance(2021, 9, 1) || 
                qis.AwareDate__c >= Date.newInstance(2021, 9, 1) )) {
                qis.Aware_date__c = qis.AwareDate__c;
            }
            // DOJ2.1 gzw AwareDate公式考文本 end
 
            User usl = new User();
 
 
            if(old.RC__c != qis.RC__c){
                if(QIStoUser.get(qis.RC__c)!=null){
                usl = QIStoUser.get(qis.RC__c);
                }else{
                   continue;
                }
             
             // LZHU-BNM4SP 20200420 Start
             //qis.ApproveManager__c  = usl.JingliApprovalManager__c == null ? qis.RC__c : usl.JingliApprovalManager__c;
             //qis.ApproveBuZhang__c  = usl.BuchangApprovalManager__c == null ? qis.RC__c : usl.BuchangApprovalManager__c;
             qis.ApproveManager__c  = usl.SalesManager__c == null ? qis.RC__c : usl.SalesManager__c;
             qis.ApproveBuZhang__c  = usl.BuchangApprovalManagerSales__c == null ? qis.RC__c : usl.BuchangApprovalManagerSales__c;
             // LZHU-BNM4SP 20200420 End
             qis.AppeoveZongJian__c = usl.ZongjianApprovalManager__c == null ? qis.RC__c : usl.ZongjianApprovalManager__c;  
             } 
            
            // HWAG-AWW5GU 字段:未收到实物提醒用邮件  设值
            String email = null;
            if (qis.Salesdepartment__c.contains('华东')) {
                email = 'OCM-HD-member@olympus.com.cn';
            } else if (qis.Salesdepartment__c.contains('东北')) {
                email = 'OCM-DB-member@olympus.com.cn';
            } else if (qis.Salesdepartment__c.contains('西北') || qis.Salesdepartment__c.contains('华北')) {
                email = 'OCM-BF-member@olympus.com.cn';
            } else if (qis.Salesdepartment__c.contains('华南') || qis.Salesdepartment__c.contains('西南')) {
                email = 'OCM-NF-member@olympus.com.cn';
            }
            if (qis.Not_Get_Asset_Remind__c != email) {
                qis.Not_Get_Asset_Remind__c = email;
            }
        }
    }
    // 注释 by Gzw 20200922 Complaint_Number从aws获取,OSH_Affirmant点击按钮时记录相关信息 start
   //  //20200221 ljh add start
   //  if ( Trigger.isBefore && Trigger.isUpdate || ( Trigger.isAfter && Trigger.isDelete)) { 
   //      List<QIS_Report__c> newList = Trigger.new;
   //      Map<Id,QIS_Report__c> newMap = Trigger.newMap;
   //      //newMap.values();
   //      List<QIS_Report__c> oldList = Trigger.old;
   //      Map<Id,QIS_Report__c> oldMap = Trigger.oldMap;
        
   //      for(QIS_Report__c qisr : newList){
   //          QIS_Report__c old = oldMap.get(qisr.Id);
   //          if(qisr.Complaint_Number__c != old.Complaint_Number__c ){
   //              qisr.OSH_Affirmant__c = UserInfo.getUserId();
   //              qisr.OSH_ConfirmationDate__c = Date.today();
   //          }
   //      }
   // }
    // 注释 by Gzw 20200922 Complaint_Number从aws获取,OSH_Affirmant点击按钮时记录相关信息 end
   if (Trigger.isBefore) {
       List<QIS_Report__c> newList = Trigger.new;
       Date d = Date.valueOf(System.Label.dateJudge);
       for(QIS_Report__c qisr : newList){
           if(qisr.QIS_Submit_day__c > d ){
               qisr.isAE_Profile__c = qisr.IS_AE__c;
               if(!String.isBlank(qisr.IS_PAE__c)){
                   qisr.isPAE_Profile__c = qisr.IS_PAE__c;
               }
           }
       }
   
   }
    //20200221 ljh add end  
    // 20211011 XinHongLu 小程序-报修子单创建QIS 后 回更 报修子单 状态 Start
    if (Trigger.isAfter && Trigger.isInsert) {
        List<RepairSubOrder__c> updateRepairSubOrderList = new List<RepairSubOrder__c>();
        List<QIS_Report__c> newList = Trigger.new;
 
        for(QIS_Report__c qisr : newList){
            if (String.isNotBlank(qisr.QisSubOrder__c)) {
                RepairSubOrder__c repairSubOrder = new RepairSubOrder__c();
                repairSubOrder.Id = qisr.QisSubOrder__c;
                repairSubOrder.Status__c = '已转QIS';
                updateRepairSubOrderList.add(repairSubOrder);
            }
        }
 
        if (updateRepairSubOrderList.size() >0) {
            update updateRepairSubOrderList;
        }
    }
    // 20211011 XinHongLu 小程序-报修子单创建QIS 后 回更 报修子单 状态 End
     
    //wangweipeng        LJPH-C7ZBSE          2021/10/27              start
    //当“产品分类1=ET,ENG;产品分类2=耗材”时,系统通知发送给如下4位同事:陈水榕、陈维汉、高治明、李俊彪
    //系统通知邮件包含:【QIS申请】、【QIS判定结果通知】、【QIS判定结果(OSH)】、【QIS取消申请】
    if (Trigger.isBefore && (Trigger.isInsert || Trigger.isUpdate)) {
        List<QIS_Report__c> newList = Trigger.new;
        for(QIS_Report__c qrc : newList){
            if(String.isNotBlank(qrc.nonyushohin__c)){
                if(qrc.ET_QIS_SEND_EMAIL__c){
                    if(String.isBlank(qrc.ET_QIS_SEND_EMAIL1__c)){
                        if (NFMUtil.isSandbox()) {  //测试环境
                            qrc.ET_QIS_SEND_EMAIL1__c = 'Shuirong_Chen@olympus.com.cn.stagefull';
                            qrc.ET_QIS_SEND_EMAIL2__c = 'Junbiao_Li@olympus.com.cn.stagefull';
                            qrc.ET_QIS_SEND_EMAIL3__c = 'Weihan_Chen@olympus.com.cn.stagefull';
                            qrc.ET_QIS_SEND_EMAIL4__c = 'Zhiming_Gao@olympus.com.cn.stagefull';
                        } else {  //正式环境
                            qrc.ET_QIS_SEND_EMAIL1__c = 'Shuirong_Chen@olympus.com.cn';
                            qrc.ET_QIS_SEND_EMAIL2__c = 'Junbiao_Li@olympus.com.cn';
                            qrc.ET_QIS_SEND_EMAIL3__c = 'Weihan_Chen@olympus.com.cn';
                            qrc.ET_QIS_SEND_EMAIL4__c = 'Zhiming_Gao@olympus.com.cn';
                        }
                    }
                }else{
                    if(String.isNotBlank(qrc.ET_QIS_SEND_EMAIL1__c)){
                        qrc.ET_QIS_SEND_EMAIL1__c = '';
                        qrc.ET_QIS_SEND_EMAIL2__c = '';
                        qrc.ET_QIS_SEND_EMAIL3__c = '';
                        qrc.ET_QIS_SEND_EMAIL4__c = '';
                    }
                }
            }else{
                if(String.isNotBlank(qrc.ET_QIS_SEND_EMAIL1__c)){
                    qrc.ET_QIS_SEND_EMAIL1__c = '';
                    qrc.ET_QIS_SEND_EMAIL2__c = '';
                    qrc.ET_QIS_SEND_EMAIL3__c = '';
                    qrc.ET_QIS_SEND_EMAIL4__c = '';
                }
            }
        }
    }   
    //wangweipeng        LJPH-C7ZBSE          2021/10/27              start
}