/** * 配合比相关 * * @author CaiAoLin * @date 2017/7/10 * @version */ let mixRatioHeader = []; let mixRatioSheet = null; let mixRatioSpread = null; let currentTag = ''; let mixRatioRightClickTarget = null; let isDeleting = false; $(document).ready(function() { initMixRatioExcel(); // 初始化右键相关 initMixRatioRightClick(); // 切换tab触发refresh $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) { currentTag = $(e.target).data('name'); // 获取工料机当前选中的行号 let projectGLJId = 0; if (currentTag === "mix-ratio" && mixRatioSpread !== null) { // 筛选数据显示(显示混凝土、砂浆、配合比) filterDataByType([GLJTypeConst.CONCRETE, GLJTypeConst.MORTAR, GLJTypeConst.MIX_RATIO]); projectGLJId = getActiveProjectGLJId(); // 获取数据 getRatioData(projectGLJId, getMixRatio); } }); }); /** * 初始化excel * * @return {void} */ function initMixRatioExcel() { mixRatioHeader = [ {name: '编码', field: 'code', visible: true}, {name: '名称', field: 'name', visible: true}, {name: '单位', field: 'unit_price.unit', visible: true}, {name: 'ID', field: 'id', visible: false}, {name: '类型', field: 'unit_price.type', visible: false}, {name: '基价单价', field: "unit_price.base_price", visible: true}, {name: '调整基价', field: 'adjust_price', visible: true}, {name: '市场单价', field: "unit_price.market_price", visible: true}, {name: '消耗量', field: 'consumption', visible: true, validator: 'number'}, {name: 'CID', field: 'mix_ratio_id', visible: false}, ]; let setting = { header: [] }; let columnInfo = []; for(let tmp of mixRatioHeader) { setting.header.push({headerName: tmp.name, headerWidth: 120}); columnInfo.push({name: tmp.field, displayName: tmp.name, visible: tmp.visible, cellType: tmp.cellType, size: 120}); } mixRatioSpread = sheetCommonObj.buildSheet(document.getElementById("mix-ratio"), setting, 3); mixRatioSpread.options.scrollbarShowMax = true; mixRatioSpread.options.scrollbarMaxAlign = true; mixRatioSpread.options.showHorizontalScrollbar = true; mixRatioSheet = mixRatioSpread.getActiveSheet(); // 编码列号 let codeColumn = getFieldColumn(mixRatioHeader, 'code'); // 单位列号 let unitColumn = getFieldColumn(mixRatioHeader, 'unit_price.unit'); // 消耗量列号 let consumptionColumn = getFieldColumn(mixRatioHeader, 'consumption'); // 居中样式 let centerStyleSetting = {hAlign: 1}; mixRatioSheet.setStyle(-1, codeColumn, getStyle(centerStyleSetting), GC.Spread.Sheets.SheetArea.viewport); mixRatioSheet.setStyle(-1, unitColumn, getStyle(centerStyleSetting), GC.Spread.Sheets.SheetArea.viewport); // 设置表单不可编辑 mixRatioSheet.options.isProtected = true; // 设置可编辑列 mixRatioSheet.getRange(-1, consumptionColumn, -1, 1).locked(false); // 绑定数据格式 mixRatioSheet.autoGenerateColumns = false; mixRatioSheet.bindColumns(columnInfo); // 绑定事件 mixRatioSheet.bind(GC.Spread.Sheets.Events.ValueChanged, updateConsumption); } /** * 设置特殊单元格数据 * * @param {object} sourceData * @return {void} */ function ratioSpecialColumn(sourceData) { let rowCounter = 0; // 获取市场单价列号 let consumptionColumn = getFieldColumn(mixRatioHeader, 'consumption'); let idColumn = getFieldColumn(mixRatioHeader, 'mix_ratio_id'); for(let data of sourceData) { // 把消耗量从对象中抽离出来 if (data.ratio_data.consumption !== undefined) { mixRatioSheet.setValue(rowCounter, consumptionColumn, data.ratio_data.consumption); mixRatioSheet.setValue(rowCounter, idColumn, data.ratio_data.id); } rowCounter++; } } /** * 初始化右键 * * @return {void} */ function initMixRatioRightClick() { $.contextMenu({ selector: '#mix-ratio', build: function ($trigger, e) { mixRatioRightClickTarget = SheetDataHelper.safeRightClickSelection($trigger, e, mixRatioSpread); return mixRatioRightClickTarget.hitTestType === GC.Spread.Sheets.SheetArea.viewport || mixRatioRightClickTarget.hitTestType === GC.Spread.Sheets.SheetArea.rowHeader; }, items: { "deleteMixRatio": { name: "删除", icon: 'fa-trash-o', disabled: function () { return mixRatioRightClickTarget.row === undefined; }, callback: function (key, opt) { let row = mixRatioRightClickTarget.row; let idColumn = getFieldColumn(mixRatioHeader, 'mix_ratio_id'); let deleteId = mixRatioSheet.getValue(row, idColumn); deleteMixRatio(deleteId, row); } }, } }); } /** * 获取对应的配合比方法 * * @param {Object} response * @param {String} type * @return {void|boolean} */ function getMixRatio(response, type) { switch (type) { case 'success': response.data = JSON.parse(response.data); // 设置数据 mixRatioSheet.setDataSource(response.data); ratioSpecialColumn(response.data); mixRatioSpread.refresh(); break; case 'fail': mixRatioSheet.setDataSource(null); mixRatioSpread.refresh(); alert('不存在对应数据'); break; } } /** * 删除配合比数据 * * @param {Number} id * @param {Number} row * @return {void | boolean} */ function deleteMixRatio(id, row) { id = parseInt(id); if (isNaN(id) || id <= 0) { alert('参数错误!'); } if (isDeleting) { return false; } // 获取当前行的消耗量 let consumptionColumn = getFieldColumn(mixRatioHeader, 'consumption'); let consumption = mixRatioSheet.getValue(row, consumptionColumn); $.ajax({ url: '/glj/delete-ratio', type: 'post', data: {id: id}, dataType: 'json', error: function() { isDeleting = false; alert('服务器繁忙'); }, beforeSend: function() { isDeleting = true; }, success: function(response) { if (response.err === 0) { // 删除成功后重新计算父工料机相关信息 let [parentMarketPrice, parentBasePrice] = getBaseAndMarketPrice('delete', row); let info = { parentMarketPrice: parentMarketPrice, parentBasePrice: parentBasePrice, change: -consumption }; mixRatioSheet.setValue(row, consumptionColumn, 0); mixRatioSuccess('', info); } } }); } /** * 更新消耗量 * * @param {object} element * @param {object} info * @return {void|boolean} */ function updateConsumption(element, info) { // 获取修改的数据 let column = info.col; let row = info.row; let field = mixRatioHeader[column] !== undefined && mixRatioHeader[column].field !== undefined ? mixRatioHeader[column].field : ''; if (field === '') { return false; } // 防止快速同时提交 if (isChanging) { return false; } // 校验数据 let validator = mixRatioHeader[column].validator !== undefined ? mixRatioHeader[column].validator : null; let value = info.newValue; if (validator && !checkData(validator, value)) { alert('数据格式错误,请重新输入!'); mixRatioSheet.setValue(row, column, info.oldValue); return false; } // 获取id let idColumn = getFieldColumn(mixRatioHeader, 'mix_ratio_id'); if (idColumn < 0) { return false; } let id = mixRatioSheet.getValue(row, idColumn); let [parentMarketPrice, parentBasePrice] = getBaseAndMarketPrice('modify', row, info.newValue); $.ajax({ url: '/glj/update', type: 'post', data: {id: id, field: 'mix_ratio.' + field, value: value, market_price: parentMarketPrice, base_price: parentBasePrice}, dataType: 'json', error: function() { alert('数据传输有误!'); isChanging = false; mixRatioSheet.setValue(row, column, info.oldValue); }, beforeSend: function() { isChanging = true; }, success: function(response) { isChanging = false; // 修改失败则恢复原值 if (response.err !== 0) { mixRatioSheet.setValue(row, column, info.oldValue); alert('更改数据失败!'); } else { info.parentMarketPrice = parentMarketPrice; info.parentBasePrice = parentBasePrice; info.change = info.newValue - info.oldValue; mixRatioSuccess(field, info); } } }); } /** * 成功事件 * * @param {string} field * @param {object} info * @return {void} */ function mixRatioSuccess(field, info) { // 成功则对相应的总消耗量进行设置 let activeGLJRow = gljSheet.getActiveRowIndex(); setGLJCellByField(activeGLJRow, 'quantity', info.change, true); // 设置父级3个价格 setGLJCellByField(activeGLJRow, 'unit_price.market_price', info.parentMarketPrice, false); setGLJCellByField(activeGLJRow, 'unit_price.base_price', info.parentBasePrice, false); setGLJCellByField(activeGLJRow, 'adjust_price', info.parentBasePrice, false); } /** * 计算市场单价基价单价 * * @param {String} scene * @param {Number} affectRow * @param {Number} newValue * @return {Array} */ function getBaseAndMarketPrice(scene, affectRow, newValue = 0) { // 计算父级3个价格 let maxRow = mixRatioSheet.getRowCount(); // 获取对应列的列号 let marketPriceColumn = getFieldColumn(mixRatioHeader, 'unit_price.market_price'); let consumptionColumn = getFieldColumn(mixRatioHeader, 'consumption'); let basePriceColumn = getFieldColumn(mixRatioHeader, 'unit_price.base_price'); let parentMarketPrice = 0; let parentBasePrice = 0; for(let i = 0; i < maxRow; i++) { // 获取市场单价 let marketPrice = mixRatioSheet.getValue(i, marketPriceColumn); // 获取基价单价 let basePrice = mixRatioSheet.getValue(i, basePriceColumn); // 如果是删除则忽略即将被删除的行数据 if (scene === 'delete' && affectRow === i) { continue; } // 获取消耗量(如果是当前修改的行则替换数据) let consumption = i === affectRow ? newValue : mixRatioSheet.getValue(i, consumptionColumn); parentMarketPrice += consumption * marketPrice; parentBasePrice += consumption * basePrice; } parentMarketPrice = parentMarketPrice.toDecimal(2); parentBasePrice = parentBasePrice.toDecimal(2); return [parentMarketPrice, parentBasePrice] }