import data from '@/assets/data'; // userdata for testing use const subKeyWords = [ 'sublength', 'subwidth', 'subthickness', 'subquantity', 'subarea', ]; const totalKeyWords = [ 'totallength', 'totalwidth', 'totalthickness', 'totalquantity', 'totalarea', ]; const orderDetailsKeys: string[] = []; const banCaiHeaderKeys: string[] = []; const banCaiDetailsKeys: string[] = []; const temRowHolder: string[] = []; const banCaiTemplateIndexHolder = []; for (const key in data) { if (key !== 'boards') { orderDetailsKeys.push(key); } } for (const key in data.boards[0]) { if (key !== 'boardInfos') { banCaiHeaderKeys.push(key); } } for (const key of Object.keys(data.boards[0].boardInfos[0])) { banCaiDetailsKeys.push(key); } const Tool = { addRow: (that: any, rowId: number) => { if (that.selectedRow) { for (const index in that.infos) { if (that.selectedRow === that.infos[index].rowId) { that.infos.splice(Number(index), 0, { rowId, rowTitle: rowId.toString(), cells: [], }); that.selectedRow = 0; break; } } } else { that.infos.push({ rowId: (rowId += 1), rowTitle: rowId.toString(), cells: [], }); } }, deleteRow: (that: any) => { if (!that.selectedRow) { return alert('请选择要删除行'); } that.infos = that.infos.filter( (item: any) => item.rowId !== that.selectedRow, ); that.selectedRow = 0; }, addCell: (that: any, cellId: number) => { if (!that.selectedRow) { return alert('请选择要添加单元的行'); } for (const row of that.infos) { if (that.selectedRow === row.rowId) { row.cells.push({ cellId, title: '列' + cellId, colspan: 0, height: 30, textAlign: 'center', fontSize: 16, fontStyle: 'normal', }); } } }, deleteCell: (that: any) => { // 9000 is initial cell Id. if (that.selectedCell === 9000) { return alert('请选择要删除的单元'); } for (const row of that.infos) { for (const cell in row.cells) { if (row.cells[cell].cellId === that.selectedCell) { row.cells.splice(Number(cell), 1); } } } that.selectedCell = 9000; }, addTotalAmount: (query) => { let total = 0; const query1 = query.slice(5); for (const board of data.boards) { for (const eachBoardInfo of board.boardInfos) { if (Object.keys(eachBoardInfo).indexOf(query1) !== -1) { total += eachBoardInfo[query1]; } } } if (Number.isInteger(total)) { return total; } else { return total.toFixed(3); } }, addSubAmount: (query, board) => { let sub = 0; const query1 = query.slice(3); for (const eachBoardInfo of board.boardInfos) { if (Object.keys(eachBoardInfo).indexOf(query1) !== -1) { sub += eachBoardInfo[query1]; } } if (Number.isInteger(sub)) { return sub; } else { return sub.toFixed(3); } }, addBoardInfoAmount: (that, board) => { for (const row of that.infos) { for (const cell of row.cells) { if (cell.title === 'bancaiinfoamount') { cell.title = board.boardInfos.length; } } } }, copyBanCaiTemplate: (that: any) => { let temStartLineIndex = Number(that.banCaiHeadStartLineIndex); const banCaiHeadTemplateHolder = []; while (temStartLineIndex <= Number(that.banCaiHeadEndLineIndex)) { banCaiHeadTemplateHolder.push( that.copySingleRow(that.infos[temStartLineIndex].rowId), ); temStartLineIndex += 1; } return banCaiHeadTemplateHolder; }, copyRemainingForm: (that: any) => { let remainingIndex = Number(that.banCaiHeadEndLineIndex) + 1; const remainingFormHolder = []; for (remainingIndex; remainingIndex < that.infos.length; remainingIndex++) { if (that.infos[remainingIndex].cells.length !== 0) { remainingFormHolder.push(that.infos[remainingIndex]); } } for (const eachRemainingRow of remainingFormHolder) { for (const row of that.infos) { if (eachRemainingRow === row) { that.infos.splice(that.infos.indexOf(row), 1); } } } return remainingFormHolder; }, pasteRemainingForm: (that, remainingFormHolder) => { for (const eachRemainingRow of remainingFormHolder) { for (const row of that.infos) { if (row.cells.length === 0) { that.infos.splice(that.infos.indexOf(row), 1, eachRemainingRow); break; } } for (const row of that.infos) { for (const cell of row.cells) { if (totalKeyWords.indexOf(cell.title) !== -1) { cell.title = that.addTotalAmount(cell.title); } } } } }, pasteBanCaiHeadTemplate: (that, copyBanCaiTemplate: any) => { const temCopyBanCaiTemplate = copyBanCaiTemplate; for (const banCaiRow of temCopyBanCaiTemplate) { for (const row of that.infos) { if (row.cells.length === 0) { that.infos.splice(that.infos.indexOf(row), 0, banCaiRow); break; } } } }, pasteBanCaiBody: (that, boardIndex: number) => { const boardInfosTemplateHolder = []; const copyBanCaiBodyTemplateIndex: any = that.copyBanCaiBodyTemplate(); for (const i of data.boards[boardIndex].boardInfos) { boardInfosTemplateHolder.push( that.copySingleRow(that.infos[copyBanCaiBodyTemplateIndex].rowId), ); } for (let i = 0; i < boardInfosTemplateHolder.length; i++) { for (const cell of boardInfosTemplateHolder[i].cells) { for (const key in data.boards[boardIndex].boardInfos[i]) { if (key === cell.title) { cell.title = data.boards[boardIndex].boardInfos[i][key]; } } } } that.infos.splice(copyBanCaiBodyTemplateIndex, 1); for (let i = 0; i < boardInfosTemplateHolder.length; i++) { that.infos.splice( copyBanCaiBodyTemplateIndex + i, 0, boardInfosTemplateHolder[i], ); } }, addBanCaiHeader: (that, index: number) => { for (const row of that.infos) { for (const cell of row.cells) { if (banCaiHeaderKeys.indexOf(cell.title) !== -1) { if (temRowHolder.indexOf(row) === -1) { temRowHolder.push(that.copySingleRow(row.rowId)); } cell.title = data.boards[index][cell.title]; } } } }, addOrderDetails: (that) => { for (const row of that.infos) { for (const cell of row.cells) { if (orderDetailsKeys.indexOf(cell.title) !== -1) { cell.title = data[cell.title]; } } } }, addsubQuantity: (that, board: any) => { for (const row of that.infos) { for (const cell of row.cells) { if (subKeyWords.indexOf(cell.title) !== -1) { cell.title = that.addSubAmount(cell.title, board); } } } }, }; export { Tool };