binxie
2024-01-18 b1d36ea3e6653e59bd767aa192c688ee0d9d4c58
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/* 备品智能化 add by dzk
   OPD申请单顺序调整页面
*/
import { LightningElement, track, wire,api } from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import initRenApply  from '@salesforce/apex/OPDManualSortController.initRenApply';
import saveRenApply  from '@salesforce/apex/OPDManualSortController.saveRenApply';
import selectRental  from '@salesforce/apex/OPDManualSortController.selectRental';
import lwcCSS from '@salesforce/resourceUrl/lwcCSS';
import {loadStyle} from 'lightning/platformResourceLoader';
 
export default class lexOPDManualSort extends LightningElement {
    @track data = [];
    @track selectedRows = [];
    @track selectAll = false;
    @track userProfileCheck = false;
    @track userProfileSystemCheck = true;
    @track showSpinner = false;
    @track oldData = [];
    @track UpdateCheckNum = 0;
    @track isNotSave = false;
    @track selectSalesdept = [];
    @track oldStartData = [];
    @track retrievedDataCount = 0;
    @wire(initRenApply)
    wiredRenApplyMethod({ data, error }) {
        if (data) {
            this.Salesdept = data.Salesdept;
            this.selectSalesdept = [
                { label: this.Salesdept, value: this.Salesdept }
            ];
            this.userProfileCheck = data.userProfileCheck;
            this.data = data.renApply;
            this.oldData = this.data;
            if(data.userProfileSystemCheck === true){
                this.userProfileSystemCheck = false;
                this.selectSalesdept = [
                    { label: '医疗西南营业本部', value: '医疗西南营业本部' },
                    { label: '医疗西北营业本部', value: '医疗西北营业本部' },
                    { label: '医疗华南营业本部', value: '医疗华南营业本部' },
                    { label: '医疗华东营业本部', value: '医疗华东营业本部' },
                    { label: '医疗华北营业本部', value: '医疗华北营业本部' },
                    { label: '医疗东北营业本部', value: '医疗东北营业本部' },
                    { label: '医疗产品培训本部', value: '医疗产品培训本部' },
                    { label: '消化·呼吸内镜事业本部', value: '消化·呼吸内镜事业本部' },
                    { label: '外科事业本部', value: '外科事业本部' },
                    { label: 'Solution本部', value: 'Solution本部' },
                    { label: 'MA本部', value: 'MA本部' },
                ];
            }
           
            if(data.weekDayCheck === false){
                this.userProfileCheck = false;
                const event = new ShowToastEvent({
                    message:
                    "未在排序调整规定时间内。",
                    variant : "error" 
                });
                this.data = [];
                this.dispatchEvent(event);
            } else if(data.userProfileCheck === false){
                const event = new ShowToastEvent({
                    message:
                    "当前用户不具备排序权限。",
                    variant : "error" 
                });
                this.data = [];
                this.dispatchEvent(event);
            }else{
                this.selectRentalNow(this.Salesdept);
                this.selectRentalOld(this.Salesdept);
                this.retrievedDataCount = data.renApply.length;
                console.log('retrievedDataCount------' +data.renApply.length);
            }
        } else if (error) {
            console.log('RenApplyMethod错误------' + JSON.stringify(error));
        }
    }
 
    connectedCallback() {
        Promise.all([
            loadStyle(this, lwcCSS)
        ]);
    }
    handleSelectAll(event) {
        this.selectAll = event.target.checked;
        this.data = this.data.map((row) => ({ ...row, selected: this.selectAll,isupdate:!this.selectAll }));
        if(this.selectAll){
            this.selectedRows = this.data;
        }else{
            this.selectedRows = [];
        }
  
    }
 
    handleSelect(event) {
        const itemId = event.target.dataset.id;
        const selected = event.target.checked;
        this.data = this.data.map((item) => {
            if (item.id === itemId) {
                return { ...item, selected: selected, isupdate:!selected  };
            }
            return item;
        });
 
        // 更新选中的数据集合
        if (selected) {
            const selectedItem = this.data.find((item) => item.id === itemId);
            this.selectedRows = [...this.selectedRows, selectedItem];
        } else {
            this.selectedRows = this.selectedRows.filter((item) => item.id !== itemId);
        }
        
        if(this.selectedRows.length === this.data.length){
            this.selectAll = true;
        }else{
           this.selectAll = false; 
        }
    }
 
    // 数据上移
    moveUp(event) {
        if(this.isNotSave){
            this.showToast(this.isNotSave);
        }else{
            if (this.selectedRows.length === 1) {
                const selectedRow = this.selectedRows[0].id;
                const index = this.data.findIndex(row => row.id === selectedRow);
                if (index > 0) {
                    this.swapRows(index, index - 1);
                }
                this.data.forEach((row,index) => {
                    row.Num = index + 1;
                });
                this.oldData = this.data;
            }else{
                const event = new ShowToastEvent({
                    message:
                    "上移请选择单条数据。",
                    variant : "error" 
                });
                this.dispatchEvent(event);
            }
        }
    }
 
    // 数据下移
    moveDown(event) {
        if(this.isNotSave){
            this.showToast(this.isNotSave);
        }else{
            if (this.selectedRows.length === 1) {
                const selectedRow = this.selectedRows[0].id;
                const index = this.data.findIndex(row => row.id === selectedRow);
                if (index < this.data.length - 1) {
                    this.swapRows(index, index + 1);
                }
                this.data.forEach((row,index) => {
                    row.Num = index + 1;
                });
                this.oldData = this.data;
            }else{
                const event = new ShowToastEvent({
                    message:
                    "下移请选择单条数据。",
                    variant : "error" 
                });
                this.dispatchEvent(event);
            }
        }
    }
    // 数据置顶
    moveTop(event) {
        if(this.isNotSave){
            this.showToast(this.isNotSave);
        }else{
            if (this.selectedRows.length === 1) {
            const selectedRow = this.selectedRows[0].id;
            const index = this.data.findIndex(row => row.id === selectedRow);
            if (index > 0) {
                const firstData = this.data[index];
                this.data.splice(index,1);
                this.data.unshift(firstData);
                this.data[0].UpdateCheck = true;
            }
            this.data.forEach((row,index) => {
                row.Num = index + 1;
            });
            this.oldData = this.data;
            }else{
                const event = new ShowToastEvent({
                    message:
                    "置顶请选择单条数据。",
                    variant : "error" 
                });
                this.dispatchEvent(event);
            }
        }   
    }
    // 数据置底
    moveBottom(event) {
        if(this.isNotSave){
            this.showToast(this.isNotSave);
        }else{
            if (this.selectedRows.length === 1) {
                const selectedRow = this.selectedRows[0].id;
                const index = this.data.findIndex(row => row.id === selectedRow);
                if (index < this.data.length - 1) {
                    this.data.push(this.data[index]);
                    this.data.splice(index,1);
                    this.data[this.data.length-1].UpdateCheck = true;
                }
                this.data.forEach((row,index) => {
                    row.Num = index + 1;
                });
                this.oldData = this.data;
            }else{
                const event = new ShowToastEvent({
                    message:
                    "置底时请选择单条数据。",
                    variant : "error" 
                });
                this.dispatchEvent(event);
            }
        }
    }
 
 
    // 上移、下移、置顶、置地数据交换
    swapRows(indexA, indexB) {
        this.data[indexA].UpdateCheck = true;
        const temp = this.data[indexA];
        this.data[indexA] = this.data[indexB];
        this.data[indexB] = temp;
        this.data = [...this.data];
        
    }
 
    @track changes = [];
    @track oldDataCopy = [];
    // 页面更新
    moveAll(event){
         if(this.selectedRows.length == 0){
            const event = new ShowToastEvent({
                message:
                "未勾选排序数据。",
                variant : "error" 
            });
            this.dispatchEvent(event);
            this.showSpinner = false;
            return;
        }
        this.showToast(this.isNotSave);
 
        // 清空排序修改集合
        if(this.isNotSave == false){
            const data = this.data;
            this.pageUpdate(data);
        }
        
    }  
 
 
    pageUpdate(nowData){
        const toSet = new Set();
        const modifiedData = [];
        this.changes = [];
        let oldData = JSON.parse(JSON.stringify(this.oldData));
        nowData.forEach((row,index) => {
        //将选中的数据,修改前与修改后的排序放入集合
            oldData.forEach((old) => {
                if (row.selected == true && row.id == old.id ) {
                    const integerId = parseInt(old.Num, 10);
                    const integerNum = parseInt(row.Num, 10);
                    this.changes.push({ from: integerId, to: integerNum });
                }   
            });
        });
        this.changes.forEach((change) => {
            toSet.add(change.to);
        }); 
 
        // 如果输入的排序存在重复
        if (toSet.size !== this.changes.length && toSet.size > 0) {
            const event = new ShowToastEvent({
                message:
                "输入的排序不合理,请检查排序。",
                variant : "error" 
            });
            this.changes = [];
            this.dispatchEvent(event);
            this.showSpinner = false;
            return;
        } else{
            // 将修改后的数据放入集合,并从原集合中清除
            for (const change of this.changes) {
                const item = oldData.find(item => item.Num === change.from);
                modifiedData.push({ Num: change.to, 
                                    id: item.id, 
                                    Name: item.Name, 
                                    OPDPlan: item.OPDPlan,
                                    OPDPlanId: item.OPDPlanId,
                                    Hospital: item.Hospital,
                                    OCSM: item.OCSM,
                                    OPDUseDate: item.OPDUseDate,
                                    RequestDate: item.RequestDate,
                                    Reson: item.Reson,
                                    OPDType: item.OPDType,
                                    OPDPlanStatus: item.OPDPlanStatus,
                                    RAStatus: item.RAStatus,
                                    RentalAssistantName: item.RentalAssistantName,
                                    InternalLocation: item.InternalLocation,
                                    RequestReturnDay: item.RequestReturnDay,
                                    OrdNumFlag: item.OrdNumFlag,
                                    UpdateCheck: true,
                                    isupdate: false,
                                    selected: true
                                });
                const index = oldData.findIndex(item => item.Num === change.from);
                if (index !== -1) {
                    try {
                        oldData.splice(index,1);
                    } catch (error) {
                        console.error('splice删除错误----------' + error );
                    }
                }
            }
            
            // 将修改排序的数据与未修改排序的数据按序号进行重新排序
            modifiedData.sort((a, b) => a.Num - b.Num);
            oldData.sort((a, b) => a.Num - b.Num);
            // 将未修改排序的数据按1开始进行重新排序
            try {
                oldData.forEach((row,index) => {
                    row.Num = index + 1;
                });
            }catch (error) {
                console.error('旧数据排序----------' + error );
            }
            // 将未修改的数据向修改的数据集合添加    
            for (const item of oldData) {
                let newItem = { Num: item.Num, 
                                id: item.id,
                                Name: item.Name, 
                                OPDPlan: item.OPDPlan,
                                OPDPlanId: item.OPDPlanId,
                                Hospital: item.Hospital,
                                OCSM: item.OCSM,
                                OPDUseDate: item.OPDUseDate,
                                Reson: item.Reson,
                                RequestDate: item.RequestDate,
                                OrdNumFlag: item.OrdNumFlag,
                                OPDType: item.OPDType,
                                OPDPlanStatus: item.OPDPlanStatus,
                                RAStatus: item.RAStatus,
                                RentalAssistantName: item.RentalAssistantName,
                                InternalLocation: item.InternalLocation,
                                RequestReturnDay: item.RequestReturnDay,
                                OrdNumFlag: item.OrdNumFlag,
                                UpdateCheck: item.UpdateCheck,
                                isupdate: true,
                                selected: false};
                // 如果未修改数据的序号与修改数据的序号存在重复,则序号+1
                while (modifiedData.some(dataItem => dataItem.Num === newItem.Num)) {
                    newItem.Num++;
                }
                modifiedData.push(newItem);
            }
            modifiedData.sort((a, b) => a.Num - b.Num);
            
            modifiedData.forEach((item, index) => {
                item.Num = index + 1;
            });
            this.data = modifiedData;
            this.changes = [];
            this.oldData = modifiedData;
            // this.isNotSave = true;
        }
        // 打印修改后的数据集合和原集合
    }
 
    @track isRepeat = false;
    handleSequenceChange(event) {
        const id = event.target.dataset.id;
        const newValue = event.target.value;
  
        const toSet = new Set();
        this.data = this.data.map((row,index) => {
            if (row.id === id) {
                if(newValue < 1){
                    event.target.value = row.Num;
                    const event1 = new ShowToastEvent({
                        message:
                        "输入的排序不得小于1。",
                        variant : "error" 
                    });
                    this.dispatchEvent(event1);
                    
                }
                else{
                    return { ...row, Num: newValue };
                }
            }
            return row;
        });
        // 记录被修改的序号值和整条数据
    }
    saveAll(event){
        this.showSpinner = true;
        this.pageUpdate(this.data);
        this.checkOrdNumFlag();
        if(this.UpdateCheckNum === 0){
            const event = new ShowToastEvent({
                message:
                "页面排序未发生变化。",
                variant : "error" 
            });
            this.dispatchEvent(event);
            this.showSpinner = false;
            return;
        }
        
 
        saveRenApply({ records: JSON.stringify(this.data)}).then(result => {
            this.showSpinner = false;
            // this.data = result;
            window.location.reload();
             // 处理 Apex 方法的返回结果
        }).catch(error => {
            // 处理错误
            console.error('saveRenApply发生错误:', error);
        });
 
    }
 
    handleSalesdeptChange(event) {
        this.Salesdept = event.target.value;
    }
    handleSearch(event){
        this.selectRentalNow(this.Salesdept);
    }
 
 
    checkOrdNumFlag(){
        this.UpdateCheckNum = 0;
        this.data.forEach((rowNow,indexNow) => {
            if(rowNow.selected === true){
                if(this.data[indexNow].Name !== this.oldStartData[indexNow].Name){
                    rowNow.OrdNumFlag = true;
                    this.UpdateCheckNum = 1;
                }
            }
        });
    }
 
    selectRentalOld(Salesdept){
        selectRental({ Salesdept: this.Salesdept }).then(result => {
            this.oldStartData = result.renApply;
             // 处理 Apex 方法的返回结果
        }).catch(error => {
            // 处理错误
            console.error('selectRentalOld发生错误:', error);
        });
    }
 
    selectRentalNow(Salesdept){
        this.showSpinner = true;
        selectRental({ Salesdept: this.Salesdept }).then(result => {
            this.data = result.renApply;
            this.showSpinner = false;
            this.selectedRows = [];
            this.oldData = result.renApply;
            this.selectRentalOld(this.Salesdept);
            this.selectAll = false;
            this.retrievedDataCount = result.renApply.length;
             // 处理 Apex 方法的返回结果
        }).catch(error => {
            // 处理错误
            console.error('selectRentalNow发生错误:', error);
        });
    }
    // 验证进行操作时,是否存在页面更新未保存
    showToast(isNotSave) {
        if(isNotSave){
            const event = new ShowToastEvent({
                title: '',
                message: "页面调整顺序后未进行保存",
                variant: "error"
            });
            this.dispatchEvent(event);
            return;
        }
    }
}