/* 备品智能化 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; } } }