123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980 |
- /**
- * Created by Syusuke on 2017/3/17.
- */
- //转换excel相关
- //Excel中人材机的数据不存在合并列
- const TransferExcel = (function () {
- const $transferModal = $('#transfer');
- const $file = $('#transfer-file');
- const $transfer = $('#transferConfirm');
- const $exportSpread = $('#exportSpread');
- let exportSpread = null;
- let transferData = [];
- //--以防需要可配置的情况
- const fixedCount = 4; //固定列数(顺序号、项目、单位、代号)
- const beginCol = 0;//起始的列
- //固定的这几列的列号映射是动态的,因为有一些子表,这些列进行了合并列的处理
- let colMapping = {
- serialNo: 0,
- name: 1,
- unit: 2,
- code: 3,
- consumeAmt: 4, //消耗量开始的列
- };
- //---
- function isUnDef(v) {
- return typeof v === 'undefined' || v === null;
- }
- //取消所有空格
- function trimAll(v) {
- return typeof v === 'string' ? v.replace(/\s/g, '') : v;
- }
- //单元格是否有数据
- function cellHasData(cell) {
- return cell && cell.value;
- }
- //行是否有数据
- //@param {Object}rowData(行数据) {Array}deduct(排除的列)
- function rowHasData(rowData, deduct = null) {
- for (let col in rowData) {
- if (deduct && deduct.includes(col)) {
- continue;
- }
- let cell = rowData[col];
- if (cell && cell.value) {
- return true;
- }
- }
- return false;
- }
- //行为列头行 //第一列为数值,且后面其他列有数据(不单纯认为是第二列,因为怕有合并序号列)
- function headerRow(rowData) {
- return rowData[0] && rowData[0].value && /^\d+$/.test(rowData[0].value) && rowHasData(rowData, ['0']);
- }
- //去除定额子表数组中的空行(有的表格是在 单位:xx 下面还跟着一些垃圾数据,导致subRation的range范围判定变大了,但是四列后面的数据是空的)
- function simplifyRationTable(arr) {
- let target = [];
- for (let subArr of arr) {
- let emptyRow = subArr.every(function (ele) {
- return ele === null;
- });
- if (emptyRow) {
- continue;
- }
- target.push(subArr);
- }
- return target;
- }
- //将二维数组转换成每个元素都有值的二维数据(去没值元素,因为前四列有可能合并列)
- function simplifyGljTable(arr) {
- let target = [];
- for (let subArr of arr) {
- let subTarget = [];
- for (let ele of subArr) {
- if (ele !== null) {
- subTarget.push(ele);
- }
- }
- target.push(subTarget);
- }
- return target;
- }
- //获取前固定四列的动态列映射(解决这几列可能有合并列的问题)
- function getColMapping(rowData, colCount) {
- function getUndefinedField(obj) {
- let needFields = ['serialNo', 'name', 'unit', 'code', 'consumeAmt'];
- for (let field of needFields) {
- if (!obj.hasOwnProperty(field)) {
- return field;
- }
- }
- return null;
- }
- //假定序号列为第一列
- let mapping = {serialNo: 0};
- for (let i = 1; i < colCount; i++) {
- let col = beginCol + i,
- cell = rowData[col];
- //还没设置的字段(必须要有的是serialNo, name, unit, code, consumeAmt)
- if (cell && cell.value) {
- let field = getUndefinedField(mapping);
- if (field) {
- mapping[field] = col;
- }
- if (typeof mapping.consumeAmt !== 'undefined') {
- return mapping;
- }
- }
- }
- return null;
- }
-
- //获取表头定额名称数据(最后一行为定额末位编码)
- //合并的单元格处理:为了更好的让定额获得对应的定额名称,将合并单元格做填值处理, eg: [a]['合并'] = [a][a]
- //@param {Object}dataTable {Object}range(表头的范围,row, col, rowCount, colCount) {Array}spans(spread解析Excel后的合并数组)
- //@return {Array}
- function getSubRationTable(dataTable, range, spans) {
- let subTable = [];
- for (let i = 0; i < range.rowCount; i++) {
- subTable.push(Array(range.colCount).fill(null));
- }
- //获取合并的单元格填充数据
- let fillArr = [];
- for (let i = 0; i < range.rowCount; i++) {
- let row = range.row + i;
- if (!dataTable[row]) {
- continue;
- }
- for (let j = 0; j < range.colCount; j++) {
- let col = range.col + j;
- let cell = dataTable[row][col];
- //只有有值的单元格,判断合并才有意义,有值且合并列的单元格,每列的值认为都是一样的
- if (cellHasData(cell)) {
- //是否合并了单元格
- let span = spans.find(function (data) {
- return data.row === row && data.col === col;
- });
- //这个fillData给subData填值用
- let fillData = {value: trimAll(cell.value), range: {row: i, col: j}};
- if (span) {
- fillData.range.rowCount = span.rowCount;
- fillData.range.colCount = span.colCount;
- } else {
- fillData.range.rowCount = 1;
- fillData.range.colCount = 1;
- }
- fillArr.push(fillData);
- }
- }
- }
- //将数据填充到subData中
- for (let fillData of fillArr) {
- //合并行不需要向下填值(否则会重复)
- let row = fillData.range.row;
- //合并列需要向右填值
- for (let j = 0; j < fillData.range.colCount; j++) {
- let col = fillData.range.col + j;
- subTable[row][col] = trimAll(fillData.value);
- }
- }
- return simplifyRationTable(subTable);
- }
- //获取工料机子表数据(序号至最末消耗量)
- //@param {Object}dataTable {Object}range(工料机子表的范围,row, col, rowCount, colCount)
- //@return {Array}
- function getSubGljTable(dataTable, range) {
- let gljTable = [];
- for (let i = 0; i < range.rowCount; i++) {
- gljTable.push(Array(range.colCount).fill(null));
- }
- for (let i = 0; i < range.rowCount; i++) {
- let row = range.row + i;
- if (!dataTable[row]) {
- continue;
- }
- for (let j = 0; j < range.colCount; j++) {
- let col = range.col + j;
- let cell = dataTable[row][col];
- if (cellHasData(cell)) {
- gljTable[i][j] = cell.value;
- }
- }
- }
- //工料机数据每个单元格应该都有值,没有的为合并列造成,这里将没值的单元格去除
- return simplifyGljTable(gljTable);
- }
- /*
- * 从原本的excel中提取数据
- * 需要的数据结构:eg: [{code: '1-1-1', name: '伐树', unit: '表列单位', subTable: [{subGlj: [], subRation: []}]}]
- * subTable为定额数据对应的子表数据,其中有定额名称子表、工料机子表
- * */
- function extractDataFromExcel(sheetData) {
- let dataTable = sheetData.data.dataTable,
- spans = sheetData.spans;
- //行数据是定额行 eg: 1-1-1 人工挖土方
- //@param {Number}row
- //@return {Object || Null} eg: {code: '1-1-1', name: '人工挖土方'}
- function rationRow(row) {
- let cell = dataTable[row][beginCol];
- if (!cell || !cell.value || typeof cell.value !== 'string') {
- return false;
- }
- let v = trimAll(cell.value);
- //[\-,—,一] 这里是因为pdf转出来的excel(需要转换的excel)会吧"-"显示成各种奇怪的横杆
- //第一数值限制在3位以内,防止有: 2019-05-01等日期干扰
- let reg = /^(\d{1,3}[\-,_,—,一]{1}\d+[\-,_,—,一]{1}\d+)(\w{0,}[\u4e00-\u9fa5]{1,})/;
- let match = reg.exec(v);
- if (match && match.length === 3 && match[0] && match[1] && match[2]) {
- return {code: match[1].replace(/[_,—,一]/g, '-'), name: match[2]};
- }
- return null;
- }
- //单位数据行 eg: 单位:表列单位
- //@return {String || Null} eg: '表列单位'
- function unitRow(row) {
- let cell = dataTable[row][beginCol];
- if (!cell || !cell.value || typeof cell.value !== 'string') {
- return false;
- }
- let v = trimAll(cell.value);
- let reg = /单位[\:, :]([\w+, \u4e00-\u9fa5]{0,})/;
- let match = reg.exec(v);
- if (match && match.length === 2 && match[0] && match[1]) {
- return match[1];
- }
- return null;
- }
- //行数据是人材机数据行
- //表头后,某行第一列为数值,第二列有值,则该行为紧接着表头的人材机数据行
- //@return {Boolean}
- function rowIsGlj(rowData) {
- let numberCell = rowData[colMapping.serialNo],
- valueCell = rowData[colMapping.name];
- return numberCell && numberCell.value && /^\d+$/.test(numberCell.value) && valueCell && Boolean(valueCell.value);
- }
- //连续有数据的列数(每个子表格中工料机的数据列数)
- //由于序号、项目、单位、代号可能存在合并列,因此从消耗量列(消耗量列不会合并列,消耗量对应的列号已在getColMapping获取)开始统计
- function getDataCount(row) {
- let consumeAmtCol = colMapping.consumeAmt;
- for (let col = consumeAmtCol; col < sheetData.columnCount; col++) {
- let cell = dataTable[row][col];
- if (!cell || !cell.value) {
- return col;
- }
- }
- return sheetData.columnCount;
- }
- //获取表格的子表范围(定额名称表头范围、工料机数据范围)
- //遇到单位:xx行rowA后,获取紧跟其后的表格,该表格最少要有一行工料机数据行rowB,表头范围则为rowA至rowB
- //@param {Number}beginRow
- function getTableRange(beginRow) {
- let hasTHead = false,
- hasTable = false,
- hasMapping = false; //是否获取过固定列映射
- let range = {
- subRation: {},
- subGlj: {},
- };
- for (let row = beginRow; row < sheetData.rowCount; row++) {
- if (!dataTable[row]) {
- continue;
- }
- //第一个有数据的行,为表头第一行
- if (rowHasData(dataTable[row]) && !hasTHead) {
- hasTHead = true;
- range.subRation.row = row;
- }
- //获取当前子表的固定列映射
- if (hasTHead && !hasTable && !hasMapping && headerRow(dataTable[row])) {
- hasMapping = true;
- colMapping = getColMapping(dataTable[row], sheetData.columnCount);
- if (!colMapping) {
- return null;
- }
- }
- //第一条工料机数据行
- if (hasTHead && !hasTable && colMapping && rowIsGlj(dataTable[row])) {
- hasTable = true;
- range.subGlj.row = row;
- range.subGlj.col = 0;
- range.subRation.col = colMapping.consumeAmt;
- range.subRation.rowCount = range.subGlj.row - range.subRation.row;
- range.subGlj.colCount = getDataCount(row);
- range.subRation.colCount = range.subGlj.colCount - colMapping.consumeAmt;
- }
- if (hasTable && !rowIsGlj(dataTable[row])) {
- range.subGlj.rowCount = row - range.subGlj.row;
- return range;
- }
- if (hasTable && (row === sheetData.rowCount - 1 || !dataTable[row + 1])) {
- range.subGlj.rowCount = row - range.subGlj.row + 1;
- return range;
- }
- }
- return null;
- }
- //分析整个表
- let extractData = [];
- //定额行后必须要跟着单位行
- let hasUnit = false;
- for (let row = 0; row < sheetData.rowCount; row++) {
- if (!dataTable[row] || !rowHasData(dataTable[row])) {
- continue;
- }
- let rationData = rationRow(row);
- if (rationData) {
- if (!hasUnit) {
- extractData.pop();
- }
- hasUnit = false;
- //转换数据数组的每个元素,为定额基本数据:不完整的code、不完整的name、unit、和子表门构成
- //subTable: [{subRation: [], subGlj: []}],subRation为每个子表表头数据(排除掉了顺序号至代号),
- //subGlj为每个子表工料机数据
- let basicData = {
- code: rationData.code,
- name: rationData.name,
- unit: null,
- subTable: []
- };
- extractData.push(basicData);
- }
- let unitData = unitRow(row);
- if (unitData) {
- hasUnit = true;
- let thisBasicData = extractData[extractData.length - 1];
- if (thisBasicData) {
- if (!thisBasicData.unit) {
- thisBasicData.unit = unitData;
- }
- //获取表格数据
- let range = getTableRange(row + 1);
- if (range) {
- let subRationTable = getSubRationTable(dataTable, range.subRation, spans),
- subGljTable = getSubGljTable(dataTable, range.subGlj);
- thisBasicData.subTable.push({subRation: subRationTable, subGlj: subGljTable});
- //跳过其中的行
- row = range.subGlj.row + range.subGlj.rowCount - 1;
- }
- }
- }
- }
- return extractData;
- }
- /*
- * 转换数据,将提取出来的数据转换成另外一种数据结构,便于转化Excel的结构
- * 需要的数据结构: eg: [{code: '1-1-1-1', name: '伐树xxx', unit: '表列单位', gljList: [{code,name,unit,comsumeAmt}]}]
- * */
- function transferDataFromExtract(extractData) {
- //从一个提取的数据(1定额数据及其子表数据)中获取一份转换数据
- function transfer(source) {
- //以完整定额编码为属性,因为1个定额可能会有多张子表,且完整定额编码相同,子工料机不同,方便直接添加后续的工料机
- let temp = {},
- target = [];
- let basicCode = source.code,
- basicName = source.name,
- basicUnit = source.unit;
- //处理消耗量,可能有(3.5) 和 - 的情况, 处理:(3.5) => 3.5 - => 0
- function handleConsumeAmt(consumeAmt) {
- if (typeof consumeAmt === 'string') {
- consumeAmt = trimAll(consumeAmt);
- consumeAmt = consumeAmt.replace(/[\-,_,—,一,\(,\),(,)]/g, '');
- if (!consumeAmt) {
- return 0;
- }
- }
- return consumeAmt;
- }
- //从工料机子表中获取工料机数据, index为定额编码对应的下标索引
- function getGljList(gljTable, index) {
- let gljList = [];
- //获取的工料机对应列
- let gljColMapping = {
- name: 1,
- unit: 2,
- code: 3,
- consumeAmtCol: fixedCount + index
- };
- for (let rowData of gljTable) {
- //工料机数据必须要有名称、单位、编码
- if (!rowData[gljColMapping.name] ||
- !rowData[gljColMapping.unit] ||
- !rowData[gljColMapping.code]) {
- continue;
- }
- let consumeAmt = isUnDef(rowData[gljColMapping.consumeAmtCol]) ? 0 : handleConsumeAmt(rowData[gljColMapping.consumeAmtCol]);
- gljList.push({
- name: rowData[gljColMapping.name],
- unit: rowData[gljColMapping.unit],
- code: rowData[gljColMapping.code],
- consumeAmt: consumeAmt,
- });
- }
- return gljList;
- }
- //拼接定额工料机数据
- for (let table of source.subTable) {
- let rationTable = table.subRation;
- if (!rationTable || rationTable.length === 0) {
- continue;
- }
- let lastRationCodes = rationTable.pop(); //定额子表,最后一行是末位定额编码
- for (let i = 0; i < lastRationCodes.length; i++) {
- let lastCode = lastRationCodes[i];
- //拼接定额编码
- let compleCode = `${basicCode}-${lastCode}`,
- gljList = getGljList(table.subGlj, i);
- if (!temp[compleCode]) { //该定额不存在
- temp[compleCode] = {
- code: compleCode,
- unit: basicUnit,
- name: basicName,
- gljList: gljList
- };
- } else { //该定额已存在,则追加工料机
- temp[compleCode].gljList = temp[compleCode].gljList.concat(gljList);
- }
- //拼接定额名称
- for (let rationNameRow of rationTable) {
- if (rationNameRow[i]) {
- temp[compleCode].name += ` ${rationNameRow[i]}`;
- }
- }
- }
- }
- //将temp对象转换为数据
- for (let code in temp) {
- target.push(temp[code]);
- }
- return target;
- }
- let transferData = [];
- for (let data of extractData) {
- let unitTargetData = transfer(data);
- transferData = transferData.concat(unitTargetData);
- }
- return transferData;
- }
- //导入Excel
- function exportToExcel(transferData, fileName) {
- $.bootstrapLoading.start();
- setTimeout(function () {
- if (exportSpread) {
- exportSpread.destroy();
- }
- exportSpread = new GC.Spread.Sheets.Workbook($exportSpread[0], {sheetCount: 1});
- let sheet = exportSpread.getSheet(0);
- sheet.suspendPaint();
- sheet.suspendEvent();
- //往表格填值
- let curRow = 0,
- fillCol = {
- code: 1,
- name: 2,
- unit: 3,
- consumeAmt: 4
- };
- function getRowCount() {
- let count = 0;
- for (let data of transferData) {
- count += 1 + data.gljList.length;
- }
- return count;
- }
- sheet.setRowCount(getRowCount());
- for (let data of transferData) {
- sheet.setValue(curRow, 0, '定额');
- sheet.setValue(curRow, fillCol.code, data.code);
- sheet.setValue(curRow, fillCol.name, data.name);
- sheet.setValue(curRow, fillCol.unit, data.unit);
- curRow++;
- for (let glj of data.gljList) {
- sheet.setValue(curRow, fillCol.code, glj.code);
- sheet.setValue(curRow, fillCol.name, glj.name);
- sheet.setValue(curRow, fillCol.unit, glj.unit);
- sheet.setValue(curRow, fillCol.consumeAmt, glj.consumeAmt);
- curRow++;
- }
- }
- sheet.resumeEvent();
- sheet.resumePaint();
- let json = exportSpread.toJSON();
- let excelIo = new GC.Spread.Excel.IO();
- excelIo.save(json, function(blob) {
- saveAs(blob, fileName);
- $.bootstrapLoading.end();
- $transferModal.modal('hide');
- }, function(e) {
- $.bootstrapLoading.end();
- $transferModal.modal('hide');
- console.log(e);
- });
- }, 200);
- }
- function eventListener() {
- $transferModal.on('hidden.bs.modal', function () {
- $file.val('');
- transferData = [];
- });
- //导入excel,提取并转换定额数据
- $file.change(function () {
- $transfer.addClass('disabled');
- let file = $(this)[0];
- let excelFile = file.files[0];
- if(excelFile) {
- let xlsReg = /xls$/g;
- if(excelFile.name && xlsReg.test(excelFile.name)){
- alert('请选择xlsx文件');
- $(this).val('');
- return;
- }
- $.bootstrapLoading.start();
- $('#loadingPage').css('z-index', '2000');
- //前端解析excel数据
- let excelIo = new GC.Spread.Excel.IO();
- let sDate = +new Date();
- excelIo.open(excelFile, function (json) {
- console.log(json);
- let extractData = extractDataFromExcel(json.sheets.Sheet1);
- transferData = transferDataFromExtract(extractData);
- console.log(`解析Excel文件时间:${+new Date() - sDate}`);
- $.bootstrapLoading.end();
- $transfer.removeClass('disabled');
- }, function (e) {
- $.bootstrapLoading.end();
- $transfer.removeClass('disabled');
- alert(e.errorMessage);
- });
- }
- });
- //确认转换,导出转换的Excel
- $transfer.click(function () {
- if (!transferData || transferData.length === 0) {
- //没有转换数据
- alert('没有转换数据');
- return;
- }
- let fileName = '转换数据.xlsx';
- if ($file[0].files && $file[0].files[0] && $file[0].files[0].name) {
- fileName = '转换数据' + $file[0].files[0].name;
- }
- exportToExcel(transferData, fileName);
- });
- }
- return {eventListener}
- })();
- $(function () {
- TransferExcel.eventListener();
- let dispNameArr;
- let preDeleteId = null;
- let deleteCount = 0;
- let selCompilationId,
- compilationsArr = [];
- $('#del').on('hidden.bs.modal', function () {
- deleteCount = 0;
- });
- getAllRationLib(function (dispNames) {
- dispNameArr = dispNames;
- //添加
- $('#addBtn').click(function () {
- let compilationName = $('#compilationSels option:selected').text();
- let compilationId = $('#compilationSels option:selected').val();
- let gljLibName = $('#gljLibSels option:selected').text();
- let gljLibId = $('#gljLibSels option:selected').val();
- let libName = $('#libNameTxt').val();
- if(libName.trim().length === 0){
- alert('名称不可为空!');
- $('#libNameTxt').val('')
- }
- else if(dispNames.indexOf(libName) !== -1){
- alert('此定额库已存在!');
- $('#libNameTxt').val('')
- }
- else if(compilationName.trim().length === 0){
- alert('编办不可为空!');
- }
- else if(gljLibName.trim().length === 0){
- alert("请选择工料机库!");
- }
- else{
- let newRationLib = {};
- newRationLib.dispName = libName;
- newRationLib.compilationId = compilationId;
- newRationLib.compilationName = compilationName;
- newRationLib.gljLib = gljLibId;
- newRationLib.creator = userAccount;
- newRationLib.appType = "建筑";
- $('#libNameTxt').val('');
- createRationLib(newRationLib, dispNameArr);
- }
- });
- //重命名
- $("#showArea").on("click", "[data-target = '#edit']", function(){
- let renameId = $(this).parent().parent().attr("id");
- $('#renameText').val($(this).parent().parent().find('td:first-child').text());
- $("#renameA").attr("renameId", renameId);
- });
- $("#renameA").click(function(){
- let newName = $("#renameText").val();
- let libId = $(this).attr("renameId");
- let jqSel = "#" + libId + " td:first" + " a";
- let orgName = $(jqSel).text();
- if(newName.trim().length === 0){
- alert("名称不可为空!");
- $("#renameText").val('');
- }
- else if(dispNameArr.indexOf(newName) !== -1){
- alert("该定额库已存在!");
- $("#renameText").val('');
- }
- else{
- renameRationLib({ID: libId, newName: newName, orgName: orgName}, dispNameArr);
- }
- });
- $('#edit').on('shown.bs.modal', function () {
- setTimeout(function () {
- $('#renameText').focus();
- }, 100);
- });
- $('#add').on('shown.bs.modal', function () {
- setTimeout(function () {
- $('#libNameTxt').focus();
- }, 100);
- });
- $('#add').on('hidden.bs.modal', function () {
- $('#libNameTxt').val('');
- });
- //删除
- $("#showArea").on("click", "[data-target = '#del']", function(){
- let deleteId = $(this).parent().parent().attr("id");
- $("#deleteA").attr("deleteId", deleteId);
- let delLibName = $(`#${deleteId}`).find('td:first').text();
- $('#del').find('.modal-body h5').text(`准备删除 “${delLibName}”,会导致已引用此库的地方出错,确定要删除吗?`);
- });
- $("#deleteA").click(function(){
- let deleteId = $(this).attr("deleteId");
- if(preDeleteId && preDeleteId !== deleteId){
- deleteCount = 0;
- }
- preDeleteId = deleteId;
- deleteCount++;
- let jqSel = "#" + deleteId + " td:first" + " a";
- let libName = $(jqSel).text();
- if(deleteCount === 3){
- deleteCount = 0;
- removeRationLib({libId: deleteId, libName: libName}, dispNameArr);
- $('#del').modal('hide');
- }
- });
- //全部计算
- $("#showArea").on("click", "[data-target = '#reCalcAll']", function(){
- let recalcId = $(this).parent().parent().attr("id");
- $("#reCalcConfirm").attr("recalcId", recalcId);
- });
- $("#reCalcConfirm").click(function(){
- $('#reCalcConfirm').addClass('disabled');
- $.bootstrapLoading.start();
- let recalcId = $(this).attr("recalcId");
- CommonAjax.post('/rationRepository/api/reCalcAll', {rationRepId: recalcId}, function (rstData) {
- $.bootstrapLoading.end();
- $('#reCalcAll').modal('hide');
- $('#reCalcConfirm').removeClass('disabled');
- }, function () {
- $.bootstrapLoading.end();
- $('#reCalcAll').modal('hide');
- $('#reCalcConfirm').removeClass('disabled')
- });
- });
- });
- getCompilationList(function (data) {
- compilationsArr = data.compilation;
- });
- // 导入原始数据按钮
- let rationRepId = 0;
- $("#showArea").on("click", ".import-source", function () {
- let id = $(this).data("id");
- id = parseInt(id);
- if (isNaN(id) || id <= 0) {
- return false;
- }
- rationRepId = id;
- $("#import").modal("show");
- });
- // 导入内部数据
- $("#showArea").on("click", ".import-data", function () {
- let id = $(this).data("id");
- id = parseInt(id);
- if (isNaN(id) || id <= 0) {
- return false;
- }
- rationRepId = id;
- $("#import2").modal("show");
- });
- // 导入原始数据确认
- $("#source-import,#data-import").click(function() {
- $.bootstrapLoading.start();
- const self = $(this);
- const type = self.is("#source-import") ? 'source_file' : 'import_data';
- const dialog = type === 'source_file' ? $("#import") : $("#import2");
- try {
- let formData = new FormData();
- let file = $("input[name='"+ type +"']")[0];
- if (file.files.length <= 0) {
- throw '请选择文件!';
- }
- formData.append('file', file.files[0]);
- // 获取定额库id
- if (rationRepId <= 0) {
- return false;
- }
- formData.append('rationRepId', rationRepId);
- formData.append('type', type);
- $.ajax({
- url: '/rationRepository/api/upload',
- type: 'POST',
- data: formData,
- cache: false,
- contentType: false,
- processData: false,
- beforeSend: function() {
- self.attr('disabled', 'disabled');
- self.text('上传中...');
- },
- success: function(response){
- self.removeAttr('disabled');
- self.text('确定导入');
- if (response.err === 0) {
- $.bootstrapLoading.end();
- const message = response.msg !== undefined ? response.msg : '';
- if (message !== '') {
- alert(message);
- }
- // 成功则关闭窗体
- dialog.modal("hide");
- } else {
- $.bootstrapLoading.end();
- const message = response.msg !== undefined ? response.msg : '上传失败!';
- alert(message);
- }
- },
- error: function(){
- $.bootstrapLoading.end();
- alert("与服务器通信发生错误");
- self.removeAttr('disabled');
- self.text('确定导入');
- }
- });
- } catch(error) {
- alert(error);
- }
- });
- // 导出数据
- $("#showArea").on("click", ".export", function () {
- let id = $(this).data("id");
- id = parseInt(id);
- if (isNaN(id) || id <= 0) {
- return false;
- }
- window.location.href = '/rationRepository/api/export?rationRepId=' + id;
- });
- //设置补充定额库章节树模板
- $("#showArea").on("click", ".set-comple", function () {
- let id = $(this).data("id");
- id = parseInt(id);
- if (isNaN(id) || id <= 0) {
- return false;
- }
- rationRepId = id;
- $('#templateA').addClass('disabled');
- $('#template').modal('show');
- $('#compilations').empty();
- for (let data of compilationsArr) {
- let $opt = $(`<option value="${data._id}">${data.name}</option>`);
- $('#compilations').append($opt);
- }
- $('#compilations').change();
- });
- $('#compilations').change(function () {
- selCompilationId = $(this).select().val();
- CommonAjax.get(`api/sectionTemplateCount/${selCompilationId}`, function (rstData) {
- rstData.data.count > 0 ?
- $('#templateText').text('该费用定额下已有定额章节树模板数据,是否确认覆盖数据?') :
- $('#templateText').text('确认是否将此库的章节树设置成该费用定额下补充定额章节树模板?');
- $('#templateA').removeClass('disabled');
- });
- });
- $('#templateA').click(function () {
- if (rationRepId <= 0 && selCompilationId) {
- return false;
- }
- $.bootstrapLoading.start();
- CommonAjax.post('api/initSectionTemplate', {rationLibId: rationRepId, compilationId: selCompilationId}, function () {
- $.bootstrapLoading.end();
- $('#template').modal('hide');
- }, function () {
- $.bootstrapLoading.end();
- $('#template').modal('hide');
- });
- });
- });
- function getAllRationLib(callback){
- $.ajax({
- type: 'post',
- url: 'api/getRationDisplayNames',
- dataType: 'json',
- success: function (result) {
- let dispNames = [];
- if(result.data.length > 0){
- for(let i = 0; i < result.data.length; i++){
- storageUtil.setSessionCache("RationGrp","repositoryID_" + result.data[i].ID, result.data[i].dispName);
- if(result.data[i].gljLib){
- storageUtil.setSessionCache("gljLib","repositoryID_" + result.data[i].ID, result.data[i].gljLib);
- }
- let id = result.data[i].ID;
- let libName = result.data[i].dispName;
- let createDate = result.data[i].createDate.split(' ')[0];
- let compilationName = result.data[i].compilationName;
- dispNames.push(result.data[i].dispName);
- $("#showArea").append(
- "<tr id='"+id+"'>" +
- "<td><a href='/rationRepository/ration?repository=" + id +"'>"+libName+"</a></td>" +
- "<td>"+compilationName+" </td>" +
- "<td>"+createDate+" </td>" +
- "<td><a href='javascript:void(0);' data-toggle='modal' data-target='#edit' title='编辑'>" +
- "<i class='fa fa-pencil-square-o'></i></a> <a href='javascript:void(0);' data-toggle='modal' data-target='#del' class='text-danger' title='删除'>" +
- "<i class='fa fa-remove'></i></a>" +
- " <a href='javascript:void(0);' data-toggle='modal' data-target='#reCalcAll' title='全部计算'><i class='fa fa-calculator'></i></a></td>"+
- "<td><a class='btn btn-secondary btn-sm import-source' href='javacript:void(0);' data-id='"+ id +"' title='导入原始数据'><i class='fa fa-sign-in fa-rotate-90'></i>导入</a></td>" +
- "<td><a class='btn btn-success btn-sm export' href='javacript:void(0);' data-toggle='modal' data-id='"+ id +"' data-target='#emport' title='导出内部数据'><i class='fa fa-sign-out fa-rotate-270'></i>导出</a> " +
- "<a class='btn btn-secondary btn-sm import-data' href='javacript:void(0);' data-id='"+ id +"' title='导入内部数据'><i class='fa fa-sign-in fa-rotate-90'></i>导入</a></td>" +
- "<td><a class='btn btn-secondary btn-sm transfer-excel' data-toggle='modal' data-target='#transfer' href='javacript:void(0);' data-id='"+ id +"' title='转换Excel'><i class='fa fa-sign-in fa-rotate-90'></i>转换</a></td>" +
- "<td><a class='btn btn-secondary btn-sm set-comple' href='javacript:void(0);' data-id='"+ id +"' title='将章节树设为补充模板数据'><i class='fa fa-sign-in fa-rotate-90'></i>设置</a></td>" +
- "</tr>");
- $("#tempId").attr("id", id);
- }
- }
- callback(dispNames);
- }
- });
- }
- function getCompilationList(callback){
- $.ajax({
- type: 'post',
- url: 'api/getCompilationList',
- dataType: 'json',
- success: function (result) {
- //addoptions
- for(let i = 0; i < result.data.compilation.length; i++){
- let $option = $("<option >"+ result.data.compilation[i].name +"</option>");
- $option.val( result.data.compilation[i]._id);
- $('#compilationSels').append($option);
- }
- //初始工料机库选项
- if(result.data.compilation.length > 0 && result.data.gljLibs.length > 0){
- let compilationId = result.data.compilation[0]._id;
- //console.log(compilationId);
- let gljLibOps = getGljLibOps(compilationId, result.data.gljLibs);
- for(let i = 0; i < gljLibOps.length; i++){
- let $option = $("<option >"+ gljLibOps[i].dispName +"</option>");
- $option.val(gljLibOps[i].ID);
- $('#gljLibSels').append($option);
- }
- }
- $('#compilationSels').on("change", function () {
- //刷新工料机库选项
- $('#gljLibSels').children().remove();
- let newGljLibOps = getGljLibOps(this.selectedOptions[0].value, result.data.gljLibs);
- for(let i = 0; i < newGljLibOps.length; i++){
- let $option = $("<option >"+ newGljLibOps[i].dispName +"</option>");
- $option.val(newGljLibOps[i].ID);
- $('#gljLibSels').append($option);
- }
- });
- callback(result.data);
- }
- });
- }
- function getGljLibOps(compilationId, gljLibs){
- let rst = [];
- for(let i = 0; i < gljLibs.length; i++){
- if(gljLibs[i]){
- if(compilationId === gljLibs[i].compilationId){
- rst.push(gljLibs[i]);
- }
- }
- }
- return rst;
- }
- function createRationLib(rationObj, dispNamesArr){
- $.ajax({
- type: 'post',
- url: 'api/addRationRepository',
- data: {rationRepObj: JSON.stringify(rationObj)},
- dataType: 'json',
- success: function (result) {
- if(result.data){
- storageUtil.setSessionCache("RationGrp","repositoryID_" + result.data.ID, result.data.dispName);
- if(result.data.gljLib){
- storageUtil.setSessionCache("gljLib","repositoryID_" + result.data.ID, result.data.gljLib);
- }
- let id = result.data.ID;
- let libName = result.data.dispName;
- let createDate = result.data.createDate.split(' ')[0];
- let compilationName = result.data.compilationName;
- dispNamesArr.push(libName);
- $("#showArea").append(
- "<tr id='"+id+"'>" +
- "<td><a href='/rationRepository/ration?repository=" + id +"'>"+libName+"</a></td>" +
- "<td>"+compilationName+" </td>" +
- "<td>"+createDate+" </td>" +
- "<td><a href='javascript:void(0);' data-toggle='modal' data-target='#edit' title='编辑'>" +
- "<i class='fa fa-pencil-square-o'></i></a> <a href='javascript:void(0);' data-toggle='modal' data-target='#del' class='text-danger' title='删除'>" +
- "<i class='fa fa-remove'></i></a>" +
- " <a href='javascript:void(0);' data-toggle='modal' data-target='#reCalcAll' title='全部计算'><i class='fa fa-calculator'></i></a>"+
- "<td><a class='btn btn-secondary btn-sm import-source' href='javacript:void(0);' data-id='"+ id +"' title='导入原始数据'><i class='fa fa-sign-in fa-rotate-90'></i>导入</a></td>" +
- "<td><a class='btn btn-success btn-sm export' href='javacript:void(0);' data-toggle='modal' data-id='"+ id +"' data-target='#emport' title='导出内部数据'><i class='fa fa-sign-out fa-rotate-270'></i>导出</a> " +
- "<a class='btn btn-secondary btn-sm import-data' href='javacript:void(0);' data-id='"+ id +"' title='导入内部数据'><i class='fa fa-sign-in fa-rotate-90'></i>导入</a></td>" +
- "<td><a class='btn btn-secondary btn-sm transfer-excel' data-toggle='modal' data-target='#transfer' href='javacript:void(0);' data-id='"+ id +"' title='转换Excel'><i class='fa fa-sign-in fa-rotate-90'></i>转换</a></td>" +
- "<td><a class='btn btn-secondary btn-sm set-comple' href='javacript:void(0);' data-id='"+ id +"' title='将章节树设为补充模板数据'><i class='fa fa-sign-in fa-rotate-90'></i>设置</a></td>" +
- "</tr>");
- }
- $('#cancelBtn').click();
- }
- })
- }
- function renameRationLib(renameObj, dispNames){
- $.ajax({
- type: 'post',
- url: 'api/editRationLibs',
- data: {oprtor: userAccount, renameObj: JSON.stringify(renameObj)},
- dataType: 'json',
- success: function (result) {
- if(!result.error){
- let jqSel = "#" + renameObj.ID + " td:first" + " a";
- $(jqSel).text(renameObj.newName);
- let index = dispNames.indexOf(renameObj.orgName);
- dispNames.splice(index, 1);
- dispNames.splice(index, 0, renameObj.newName);
- }
- $('#editCancelBtn').click();
- $('#renameText').val('');
- }
- })
- }
- function removeRationLib(delObj, dispNames){
- $.bootstrapLoading.start();
- $.ajax({
- type: 'post',
- url: 'api/deleteRationLibs',
- data: {oprtor: userAccount, libId: delObj.libId},
- dataType: 'json',
- success: function (result) {
- if(!result.error){
- var jqSel = "#"+ delObj.libId;
- $(jqSel).remove();
- let index = dispNames.indexOf(delObj.libName);
- dispNames.splice(index, 1);
- $('#delCancelBtn').click();
- }
- $.bootstrapLoading.end();
- },
- error: function () {
- alert('删除失败');
- $.bootstrapLoading.end();
- }
- })
- }
|