高章伟
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
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
global class MaintenanceContractWebService {
    // 用户点击上传sap按钮,更新状态到契约,自动触发上传sap接口程序
    WebService static String up2sap(String mcid) {
        List<Maintenance_Contract__c> mcList = [select id, Status__c, Maintenance_Contract_No__c , Hospital__c , RecordTypeId, RecordType.DeveloperName,Is_RecognitionModel__c,Recognition_Model_Type__c,upload_to_RM_time__c,old_Is_RecognitionModel__c,Is_Recognition_Model_True__c from Maintenance_Contract__c where id = :mcid ];
        if (mcList.size() == 0) {
            return '维修合同不存在,请确认维修合同ID。';
        }
        Maintenance_Contract__c mc = mcList[0];
        if (mc.Maintenance_Contract_No__c == null || mc.Maintenance_Contract_No__c == '') {
            return Schema.SObjectType.Maintenance_Contract__c.fields.Maintenance_Contract_No__c.label + '为空,不能上传sap';
        }
        //update   wangweipeng                    2021/11/17   start
        //用于判断当前合同是否需要认款
        boolean flag = false;
        //判断是否需要认款合同
        if(mc.old_Is_RecognitionModel__c){
            //判断当前合同是否已经认款完成
            if(!mc.Is_Recognition_Model_True__c){
                flag = true;
            }
        }
        //update   wangweipeng                    2021/11/17   end
        try {
            if(flag){
                mc.upload_to_RM_time__c = Datetime.now();//上传认款合同时间
                //mc.old_Is_RecognitionModel__c = mc.Is_RecognitionModel__c;
            }else{
                mc.Status__c = '契約';
                mc.upload_to_sap_time__c = Datetime.now();
 
                //2021-01-14  mzy  add LJPH-BWY5QB  合同状态为合同中时 ,将客户上的 维修合同用户类型  设为true ,合同上的 用户类型 为 新用户, 以后维修合同用户类型 都为 true 合同上的 用户类型 为 既有用户
                //只有合同是维修合同和服务合同的时候进入
                if (!mc.RecordType.DeveloperName.equals('VM_Contract')) {
                    List<Account> acList =  [select id, MaintenanceContractUserType__c from Account where id = :mc.Hospital__c];
                    if (acList != null && acList.size() > 0) {
                        Account hp = acList[0];
                        //老用户
                        if (hp.MaintenanceContractUserType__c == true) {
                            mc.UserType__c = '既有用户';
                        } else {
                            //新用户
                            hp.MaintenanceContractUserType__c = true;
                            mc.UserType__c = '新用户';
                            update hp;
                        }
                    }
                }
            }
            update mc;
            //2021-01-14  mzy  add  LJPH-BWY5QB  合同状态为合同中时 ,将客户上的 维修合同用户类型  设为true ,合同上的 用户类型 为 新用户, 以后维修合同用户类型 都为 true 合同上的 用户类型 为 既有用户
        } catch (Exception ex) {
            return ex.getMessage();
        }
 
        return '1';
    }
    // fxk 点击上传SAP按钮自动生成点检计划  2021/7/22 Start
    WebService static String Check_plan(List<String> mcidList) {
        System.debug('1234567890');
        List<Maintenance_Contract__c> MCList = [select Id, Contract_Start_Date__c, Service_contract_target_number__c,
                                                Actual_Execution_Quantity_Summary__c,
                                                //2021-09-08  mzy  FSE分批点检如果有遗漏设备的提醒设置  设置点检计划的合同担当 start
                                                Service_Contract_Staff__c,Contract_Range__c,Contract_Period__c,
                                                //2021-09-08  mzy  FSE分批点检如果有遗漏设备的提醒设置 设置点检计划的合同担当  end
                                                Contract_End_Date__c, Inspection_Time__c
                                                from Maintenance_Contract__c where Id in: mcidList];
 
        // Inspection_Report__c CheckPlanR = [select Id, Inspection_asset_number__c, Approved_date__c,
        //                                    Failourassetnumber__c
        //                                    from Inspection_Report__c where Contract__c = : mcid];
        // Inspection_Item__c CheckPlanI = [select Id, ItemStatus__c, Failourassetnumber__c
        // from Inspection_Item__c where Maintance_Static_His__c = : mcid];
        System.debug('MCInfoList:' + MCList);
        // 存新创建的点检计划
        List<Inspectup_Plan__c> IPInfoList = new List<Inspectup_Plan__c>();
        // 1.合同总月数
        // 2.合同总点检次数
        // 3.每次点检月份区间
        // 遍历总月数{
        //  满一年点检日期区间
        //  不满一年点检日期区间
        // // 总年数
        // Integer Allyear = Integer.valueOf(PageMCEElement.Contract_End_Date__c.year() - PageMCEElement.Contract_Start_Date__c.year());
        // System.debug('Zongnianshu' + Allyear);
        // // 剩余月数
        // Integer RestMonth = Integer.valueOf(PageMCEElement.Contract_End_Date__c.month() - PageMCEElement.Contract_Start_Date__c.month());
        // // 剩余月应该点检的次数
        // Integer RestMonth_check = RestMonth / (12 / Integer.valueOf(PageMCEElement.Inspection_Time__c));
        // // 合同点检总次数
        // Integer All_check = Allyear * Integer.valueOf(PageMCEElement.Inspection_Time__c) + RestMonth_check;
        // 总点检次数
        // Integer All_check;
        //合同总月数
        // Integer Contract_validMonth;
        // Integer CheckPlan;
        Map<Id, Integer> MCTOCheckMap = new Map<Id, Integer>();
        for (Maintenance_Contract__c Mc : MCList) {
            Integer All_check = 0;
            if (MC.Inspection_Time__c != null && MC.Inspection_Time__c != '') {
                Integer CheckPlan = Integer.valueOf(MC.Inspection_Time__c);
                //合同总月数
                Integer Contract_validMonth = Integer.valueOf(MC.Contract_Range__c);
                // add 一年内点检区间不够生成一次点检计划,默认生成一个点检计划,计划区间为合同开始日-结束日 2021/10/25 fxk Star
                if((CheckPlan == 1 && Contract_validMonth < 9) 
                    || (CheckPlan == 2 && Contract_validMonth < 6) 
                    || (CheckPlan == 3 && Contract_validMonth < 4)
                    || (CheckPlan == 4 && Contract_validMonth < 3)){
                    All_check = 1;
                }else{
                // add 一年内点检区间不够生成一次点检计划,默认生成一个点检计划,计划区间为合同开始日-结束日 2021/10/25 fxk end
                    if (Contract_validMonth - Integer.valueOf(Contract_validMonth / 12) * 12 >= 6 && CheckPlan == 1) {
                        All_check = (Contract_validMonth * Integer.valueOf(MC.Inspection_Time__c)) / 12 + 1;
                    } else {
                        All_check = (Contract_validMonth * Integer.valueOf(MC.Inspection_Time__c)) / 12;
                    }
                    System.debug('-------Contract_validMonth:' + Contract_validMonth);
                }
                MCTOCheckMap.put(Mc.Id, All_check);
            }
        }
 
        // 合同点检总次数
        Map<Integer, List<Integer>> tempMapCheck = new Map<Integer, List<Integer>>();
        // 如果签一年合同有效期不是(1-12月)则它的点检的月份应为(点检次数为1)
        // Integer[] a1 = new Integer[] {9};
        // Integer[] a2 = new Integer[] {6};
        // Integer[] a3 = new Integer[] {4};
        // Integer[] a4 = new Integer[] {3};
 
        List<Integer> checkMonth = new List<Integer> {0, 9, 6, 4, 3};
 
        // tempMapCheck.put(1, a1);
        // tempMapCheck.put(2, a2);
        // tempMapCheck.put(3, a3);
        // tempMapCheck.put(4, a4);
        // gzw 计数初始化
        // Integer nameNo = 0;
        try {
            for (Maintenance_Contract__c Mc : MCList) {
                // gzw 计数初始化
                Integer nameNo = 0;
                if (MC.Inspection_Time__c != null && MC.Inspection_Time__c != '') {
                    // 总点检次数
                    Integer All_check = 0;
                    if (MCTOCheckMap.containsKey(Mc.Id)) {
                        All_check = MCTOCheckMap.get(Mc.Id);
                    }
                    // 年点检次数
                    Integer CheckPlan = Integer.valueOf(MC.Inspection_Time__c);
                    //合同总月数
                    //update 2021/11/11 fxk star
                    Integer Contract_validMonth = Integer.valueOf(MC.Contract_Period__c);
                    //Integer Contract_validMonth = MC.Contract_Start_Date__c.monthsBetween(MC.Contract_End_Date__c);
                    //update 2021/11/11 fxk end
                    for (Integer i = 1; i <= All_check; i++ ) {
 
                        if (i * 12 <= Contract_validMonth) {
                            if (CheckPlan == 1) {
                                Integer keys = checkMonth[CheckPlan];
                                System.debug('------3-------' + keys);
                                // for (Integer keys : tempMapCheck.get(CheckPlan)) {
                                for (Integer j = 1; j <= CheckPlan; j++ ) {
                                    System.debug('------5-------' + i);
                                    // Integer keys = checkMonth[CheckPlan]
                                    Integer keys1 = checkMonth[CheckPlan];
                                    System.debug('------4-------' + keys1);
                                    // 计划开始日
                                    Date startDate;
                                    Date endDate;
                                    Date taskTime;
                                    // if (Contract_validMonth <= 12) {
                                    //     startDate = MC.Contract_Start_Date__c.addMonths((i - 1) * 12 + (keys - keys1));
                                    //     taskTime = startDate.addMonths(5);
                                    // }else 
                                    if(i>1){
                                        startDate = MC.Contract_Start_Date__c.addMonths((i - 1) * 12 + (keys - keys1)-3);
                                        taskTime = startDate.addMonths(8);
                                    }else{
                                        startDate = MC.Contract_Start_Date__c.addMonths((i - 1) * 12 + (keys - keys1));
                                        taskTime = startDate.addMonths(5);
                                    }
                                    
                                    System.debug('------1-------' + startDate);
                                    // 计划结束日
                                    endDate = MC.Contract_Start_Date__c.addMonths((i - 1) * 12 + keys).addDays(-1);
                                    System.debug('------2-------' + endDate);
                                    keys += keys1;
                                    System.debug('------6-------' + keys);
                                    //防止点检计划结束日大于合同结束日
                                    if (endDate > MC.Contract_End_Date__c ) {
                                        endDate =  MC.Contract_End_Date__c;
                                    }
                                    nameNo++;
                                    //2021-09-02 mzy update start
                                    //发送任务的日期改成跟着实施期限的日期和合同期限的日期走,不要是固定的那个月的1号
                                    //Date taskTime = Date.newInstance(endDate.year(), endDate.month() - 1, 1);
                                    //如果是1年点检1次的话则是合同开始日+5个月
                                    // Date taskTime = endDate.addMonths(-1);
                                    // if (CheckPlan == 1) {
                                    //taskTime = MC.Contract_Start_Date__c.addMonths(5);
 
                                    // }
                                    //2021-09-02 mzy  update end
                                    System.debug('1234567890:' + nameNo);
                                    planInfo(IPInfoList, nameNo, MC, startDate, endDate, taskTime);
                                }
                            } else {
                                Integer keys = checkMonth[CheckPlan];
                                System.debug('------3-------' + keys);
                                // for (Integer keys : tempMapCheck.get(CheckPlan)) {
                                for (Integer j = 1; j <= CheckPlan; j++ ) {
                                    System.debug('------5-------' + i);
                                    // Integer keys = checkMonth[CheckPlan]
                                    Integer keys1 = checkMonth[CheckPlan];
                                    System.debug('------4-------' + keys1);
                                    // 计划开始日
                                    Date startDate = MC.Contract_Start_Date__c.addMonths((i - 1) * 12 + (keys - keys1));
                                    System.debug('------1-------' + startDate);
                                    // 计划结束日
                                    Date endDate = MC.Contract_Start_Date__c.addMonths((i - 1) * 12 + keys).addDays(-1);
                                    System.debug('------2-------' + endDate);
                                    keys += keys1;
                                    System.debug('------6-------' + keys);
                                    //防止点检计划结束日大于合同结束日
                                    if (endDate > MC.Contract_End_Date__c ) {
                                        endDate =  MC.Contract_End_Date__c;
                                    }
                                    nameNo++;
                                    //2021-09-02 mzy update start
                                    //发送任务的日期改成跟着实施期限的日期和合同期限的日期走,不要是固定的那个月的1号
                                    //Date taskTime = Date.newInstance(endDate.year(), endDate.month() - 1, 1);
                                    //如果是1年点检1次的话则是合同开始日+5个月
                                    Date taskTime = endDate.addMonths(-1);
                                    // if (CheckPlan == 1) {
                                    //     //taskTime = MC.Contract_Start_Date__c.addMonths(5);
                                    //     taskTime = startDate.addMonths(5);
                                    // }
                                    //2021-09-02 mzy  update end
                                    System.debug('1234567890:' + nameNo);
                                    planInfo(IPInfoList, nameNo, MC, startDate, endDate, taskTime);
                                }
                            }
                        } else {
                            // 剩余月份应该点检的次数
                            Integer num;
                            if (CheckPlan == 1) {
                                System.debug('-------2-----' + CheckPlan);
                                if ((Contract_validMonth - (i - 1) * 12) >= 6 ) {
                                    System.debug('--------1--------' + (Contract_validMonth - (i - 1) * 12));
                                    num = 1;
                                    for (Integer j = 0 ; j < num ; j++) {
                                        Integer keys = checkMonth[CheckPlan];
                                        Date startDate;
                                        if (Contract_validMonth < 12) {
                                            startDate = MC.Contract_Start_Date__c.addMonths((i - 1) * 12 + (keys * j));
                                        }else{
                                            startDate = MC.Contract_Start_Date__c.addMonths((i - 1) * 12 + (keys * j) - 3);
                                        }
                                        // Date endDate = MC.Contract_Start_Date__c.addMonths((i - 1) * 12 + (keys1 + keys1 * j)).addDays(-1);
                                        Date endDate = MC.Contract_End_Date__c;
                                        nameNo++;
                                        //2021-09-02 mzy update start
                                        //发送任务的日期改成跟着实施期限的日期和合同期限的日期走,不要是固定的那个月的1号
                                        //Date taskTime = Date.newInstance(MC.Contract_End_Date__c.year(), MC.Contract_End_Date__c.month() - 2, 1);
                                        Date taskTime = MC.Contract_End_Date__c.addMonths(-2);
                                        //2021-09-02 mzy  update end
                                        planInfo(IPInfoList, nameNo, MC, startDate, endDate, taskTime);
                                    }
                                }
                                // add 一年内点检区间不够生成一次点检计划,默认生成一个点检计划,计划区间为合同开始日-结束日 2021/10/25 fxk Star
                                if ( Contract_validMonth < 6 ) {
                                    Date startDate = MC.Contract_Start_Date__c;
                                    Date endDate = MC.Contract_End_Date__c;
                                    nameNo = 1;
                                    Date taskTime = MC.Contract_End_Date__c.addMonths(-2);
                                    planInfo(IPInfoList, nameNo, MC, startDate, endDate, taskTime);
                                }
                                // add 一年内点检区间不够生成一次点检计划,默认生成一个点检计划,计划区间为合同开始日-结束日 2021/10/25 fxk end
                            } else {
                                // add 一年内点检区间不够生成一次点检计划,默认生成一个点检计划,计划区间为合同开始日-结束日 2021/10/25 fxk Star
                                if(Contract_validMonth < checkMonth[CheckPlan]){
                                    Date startDate = MC.Contract_Start_Date__c;
                                    Date endDate = MC.Contract_End_Date__c;
                                    nameNo = 1;
                                    Date taskTime = endDate.addMonths(-1);
                                    planInfo(IPInfoList, nameNo, MC, startDate, endDate, taskTime);
                                }else{
                                // add 一年内点检区间不够生成一次点检计划,默认生成一个点检计划,计划区间为合同开始日-结束日 2021/10/25 fxk end
                                    num = Integer.valueOf((Contract_validMonth - (i - 1) * 12) * CheckPlan / 12);
                                    for (Integer j = 0 ; j < num ; j++) {
                                        Integer keys = checkMonth[CheckPlan];
                                        Date startDate = MC.Contract_Start_Date__c.addMonths((i - 1) * 12 + (keys * j));
                                        Date endDate = MC.Contract_Start_Date__c.addMonths((i - 1) * 12 + (keys + keys * j)).addDays(-1);
 
                                        //防止点检计划结束日大于合同结束日
                                        if (endDate > MC.Contract_End_Date__c ) {
                                            endDate =  MC.Contract_End_Date__c;
                                        }
                                        nameNo++;
                                        //2021-09-02 mzy update start
                                        //发送任务的日期改成跟着实施期限的日期和合同期限的日期走,不要是固定的那个月的1号
                                        //Date taskTime = Date.newInstance(endDate.year(), endDate.month() - 1, 1);
                                        Date taskTime = endDate.addMonths(-1);
                                        //2021-09-02 mzy  update end
                                        planInfo(IPInfoList, nameNo, MC, startDate, endDate, taskTime);
                                    }
                                }
                            }
                            // nameNo = 0;
                            // break;
                        }
                    }
                }
            }
            System.debug('1234567887654321' + MCList);
            if (MCList.size() > 0 ) {
                update MCList;
            }
            System.debug('12345678' + IPInfoList);
            if (IPInfoList.size() > 0) {
                insert IPInfoList;
                // Integer a = Integer.valueOf('asdfg12345');
            }
        } catch (Exception ex) {
            return ex.getMessage();
        }
        return '1';
    }
    public static List<Inspectup_Plan__c> planInfo(List<Inspectup_Plan__c> IPInfoList, Integer nameNo, Maintenance_Contract__c MC, Date startDate, Date endDate, Date taskTime) {
        // List<Inspectup_Plan__c> IPInfoList = new List<Inspectup_Plan__c>();
        Inspectup_Plan__c IPInfo = new Inspectup_Plan__c();
        IPInfo.Name = '第' + nameNo + '次计划';
        IPInfo.Maintenance_Contract__c = MC.Id;
        IPInfo.Planned_Start_Date__c = startDate;
        IPInfo.Planned_End_Date__c = endDate;
        IPInfo.Task_sending_time__c = taskTime;
        IPInfo.Chack_Plan_NO__c = String.valueOf(nameNo);
        //2021-09-08  mzy  FSE分批点检如果有遗漏设备的提醒设置  设置点检计划的合同担当,为了发邮件时使用 start
        IPInfo.Service_Contract_Staff__c  = MC.Service_Contract_Staff__c;
        //2021-09-08  mzy  FSE分批点检如果有遗漏设备的提醒设置  设置点检计划的合同担当,为了发邮件时使用 end
 
        IPInfoList.add(IPInfo);
        return IPInfoList;
    }
    // fxk 点击上传SAP按钮自动生成点检计划  2021/7/22 End
}