define(function(require, exports, module) { var BaseWindow = require('system/views/pages/window'); require('css!./funcScriptwin.css'); var tpl = require('text!apps/edp/frd/frd004/funcScriptwin.tpl'); function Window(args) { this.options = { title: args.dbType + '脚本查看器', content: tpl, width: 800, height: 600, modal: true, closed: true, closable: true, data: null }; this.args = args; BaseWindow.call(this, this.options); } inherits(Window, BaseWindow); Window.prototype.init = function() { var self = this; }; Window.prototype.select = function() { this.close(); }; Window.prototype.doOp = function(opCode) { var isHandled = false; switch (opCode) { case 'ok': this.select(); break; case 'exit': this.$txtScript.select(); break; case 'refresh': this.refresh(); break; case 'export': this.exportScript(); break; } }; Window.prototype.refresh = function() { var self = this; this.dsMaster.search(); }; Window.prototype.customerize = function($container) { var self = this; this.$txtScript = this.getElement('.txtscript'); this.$scriptNameInput = this.getElement('.edp-frd-script-name-input'); var $btnOk = this.getElement('.btn-script-ok'); if ($btnOk) { $btnOk.on('click', function() { self.doOp('ok'); }); } var $btnCancel = this.getElement('.btn-script-cancel'); if ($btnCancel) { $btnCancel.click(function() { self.doOp('exit'); }); } var $btnExport = this.getElement('.btn-script-export'); if ($btnExport) { $btnExport.click(function() { self.doOp('export'); }); } this.$scriptNameInput.val(this.scriptName); self.generateScript(); }; Window.prototype.open = function() { var self = this; self.makeScriptName(); BaseWindow.prototype.open.call(self); }; Window.prototype.makeScriptName = function() { this.scriptName = Store.dbScriptsProjectNo + '07_' + this.args.dsMaster.currentRow['ModuleCode'] + '_预置功能' + this.args.dsMaster.currentRow['FuncCode'] + '.sql'; }; Window.prototype.generateScript = function() { switch (this.args.scriptType) { case '0': //覆盖型脚本 this.createDataScript('0'); break; case '1': //校验型脚本(暂不支持) this.createDataScript('1'); break; } }; Window.prototype.exportScript = function() { var self = this, scriptContent = self.$txtScript.val(), scriptName = self.$scriptNameInput.val(), blob = new Blob( [scriptContent], {type: "text/plain;charset=" + document.characterSet} ), url = window.URL.createObjectURL(blob), download_link = $('.export-script')[0]; Store.services.fileExport({ filename: scriptName, content: scriptContent }, function (isok, data) { if (isok) { download_link.href = Store.host + data.path; download_link.download = scriptName; download_link.click(); window.URL.revokeObjectURL(url); } }) }; Window.prototype.createDataScript = function(scriptType) { var self = this; var dataScriptTpl = require('text!apps/edp/frd/frd004/funcDataScript.tpl'); if (self.args.dbType === 'mysql') { dataScriptTpl = require('text!apps/edp/frd/frd004/funcDataScriptMySql.tpl'); } var funcCode = self.args.dsMaster.currentRow['FuncCode']; var funcName = self.args.dsMaster.currentRow['FuncName']; var dataScript = dataScriptTpl.replace(/#FUNCCODE/g, funcCode); dataScript = dataScript.replace(/#FUNCNAME/g, funcName); //获取功能字典表数据 var dataSql = getDataScript(self, self.args.dsMaster, scriptType); dataScript = dataScript.replace(/#FUNCMAIN/g, dataSql); dataSql = getDataScript(self, self.args.dsFuncObj, scriptType); dataScript = dataScript.replace(/#FUNCOBJ/g, dataSql); dataSql = getDataScript(self, self.args.dsFuncOp, scriptType); dataScript = dataScript.replace(/#FUNCOPER/g, dataSql); dataSql = getDataScript(self, self.args.dsFuncRec, scriptType); dataScript = dataScript.replace(/#FUNCRES/g, dataSql); dataSql = getDataScript(self, self.args.dsFuncOrgRight, scriptType); dataScript = dataScript.replace(/#FUNCORGCTRL/g, dataSql); self.$txtScript.val(dataScript); }; function getDataScript(self, dataSouce, scriptType) { var sqlCols = ''; var sqlValues = ''; var sqlText = ''; var sqlWhere = "FuncCode" + "='" + self.args.dsMaster.currentRow['FuncCode'] + "' "; //组合成删除插入语句(功能字典表) if (dataSouce.datasetName === self.args.dsMaster['datasetName'] && scriptType === '0') { _.each(dataSouce.columns, function(item, index) { var fieldName = item['fieldName']; var dataType = item['dataType']; var value = dataSouce.currentRow[fieldName]; //列集合 if (sqlCols.length > 0) { sqlCols += ","; } sqlCols += fieldName; //列值集合 if (sqlValues.length > 0) { sqlValues += ','; } if (dataType === Store.Enums.DataType.NUMBER || dataType === Store.Enums.DataType.TIMESTAMP) { sqlValues += value; } else { sqlValues += getStrValue(value); } }); sqlText = sqlText + 'Delete From ' + dataSouce.datasetName + " Where " + sqlWhere + ';\n'; sqlText = sqlText + 'insert into ' + dataSouce.datasetName + ' (' + sqlCols + ') Values(' + sqlValues + ');\n'; } // 组合成删除插入语句(功能对象表)、(功能操作表)、(功能资源表)、(功能组织权表) if (scriptType === '0' && dataSouce.datasetName !== self.args.dsMaster['datasetName']) { sqlText = getColumnSet(dataSouce, sqlText, sqlWhere); } return sqlText; } function getColumnSet(dataSouce, sqlText, sqlWhere) { if (dataSouce.rows.length === 0) { sqlText = sqlText + 'Delete From ' + dataSouce.datasetName + " Where " + sqlWhere + ';\n'; sqlText = sqlText + '-- no data,no insert\n'; return sqlText; } sqlText = sqlText + 'Delete From ' + dataSouce.datasetName + " Where " + sqlWhere + ';\n'; _.each(dataSouce.rows, function(row, rowIndex) { var sqlCols = ''; var sqlValues = ''; _.each(dataSouce.columns, function(item, index) { var fieldName = item['fieldName']; var dataType = item['dataType']; var value = row[fieldName]; //列集合 if (sqlCols.length > 0) { sqlCols += ","; } sqlCols += fieldName; //列值集合 if (sqlValues.length > 0) { sqlValues += ','; } if (dataType === Store.Enums.DataType.NUMBER || dataType === Store.Enums.DataType.TIMESTAMP) { sqlValues += value; } else { sqlValues += getStrValue(value); } }); sqlText = sqlText + 'insert into ' + dataSouce.datasetName + ' (' + sqlCols + ') Values(' + sqlValues + ');\n'; }); return sqlText; } function getStrValue(value) { if (value) { value = value.replace(/'/g,"''"); return "'" + value + "'"; } else { return 'null'; } } return Window; });