From 8904592cf12f091aece5d6fc564fd8478fc8988b Mon Sep 17 00:00:00 2001 From: king <18310653075@163.com> Date: 星期四, 13 二月 2020 20:04:56 +0800 Subject: [PATCH] 2020-02-13 --- src/utils/utils.js | 137 ++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 133 insertions(+), 4 deletions(-) diff --git a/src/utils/utils.js b/src/utils/utils.js index 5bc7055..3bf9737 100644 --- a/src/utils/utils.js +++ b/src/utils/utils.js @@ -441,6 +441,131 @@ } /** + * @description 鑾峰彇excel瀵煎叆鍙傛暟 + * @return {String} btn 鎸夐挳 + * @return {String} data excel鏁版嵁 + */ + static getExcelInSql (item, data, dict) { + let btn = item.verify + let keys = ['delete', 'drop', 'insert', 'truncate', 'update'] + + let errors = [] + let _topline = btn.range || 0 + let _Ltext = data.map((item, lindex) => { + let vals = btn.columns.map((col, cindex) => { + let val = item[col.Column] !== undefined ? item[col.Column] : '' + let _position = (_topline + lindex + 1) + dict['main.excel.line'] + ' ' + (cindex + 1) + dict['main.excel.column'] + ' ' + + if (/^Nvarchar/ig.test(col.type)) { + if (typeof(val) === 'number') { + val = val.toString() + } + + val = val.replace(/(^\s*$)|\t*|\v*/ig, '') + + let limitlen = col.type.match(/\d+/)[0] + + if (!val && col.required === 'true') { // 蹇呭~鏍¢獙 + let _error = _position + dict['main.excel.content.emptyerror'] + errors.push(_error) + } else if (val.length > limitlen) { // 闀垮害鏍¢獙 + let _error = _position + dict['main.excel.content.maxlimit'] + errors.push(_error) + } else { // 鍏抽敭瀛楁牎楠� + keys.forEach(key => { + let _patten = new RegExp('(^' + key + '\\s+)|(\\s+' + key + '\\s+)', 'ig') + if (_patten.test(val)) { + let _error = _position + dict['main.excel.includekey'] + key + errors.push(_error) + } + }) + } + } else if (/^int/ig.test(col.type)) { + if (typeof(val) !== 'number' || parseInt(val) < parseFloat(val)) { // 妫�楠屾槸鍚︿负鏁存暟 + let _error = _position + dict['main.excel.content.interror'] + errors.push(_error) + } else if ((col.min || col.min === 0) && val < col.min) { // 鏈�灏忓�兼楠� + let _error = _position + dict['main.excel.content.limitmin'] + errors.push(_error) + } else if ((col.max || col.max === 0) && val > col.max) { // 鏈�澶у�兼楠� + let _error = _position + dict['main.excel.content.limitmax'] + errors.push(_error) + } + } else if (/^Decimal/ig.test(col.type)) { + let _val = val + '' + _val = _val.split('.') + let limitlen = col.type.match(/\d+/ig)[1] + + if (typeof(val) !== 'number') { // 妫�楠屾槸鍚︿负娴偣鏁� + let _error = _position + dict['main.excel.content.floaterror'] + errors.push(_error) + } else if (_val[0].length > 18) { // 妫�楠屾暣鏁颁綅 + let _error = _position + dict['main.excel.content.floatIntover'] + errors.push(_error) + } else if (_val[1] && _val[1].length > limitlen) { // 鏈�灏忓�兼楠� + let _error = _position + dict['main.excel.content.floatPointover'] + errors.push(_error) + } else if ((col.min || col.min === 0) && val < col.min) { // 鏈�灏忓�兼楠� + let _error = _position + dict['main.excel.content.limitmin'] + errors.push(_error) + } else if ((col.max || col.max === 0) && val > col.max) { // 鏈�澶у�兼楠� + let _error = _position + dict['main.excel.content.limitmax'] + errors.push(_error) + } + } + + return `'${val}' as ${col.Column}` + }) + + if (!item.innerFunc) { + vals.push(`@upid+'${this.getuuid()}' as jskey`) + } + + return `Select ${vals.join(',')}` + }) + + _Ltext = _Ltext.join(' Union all ') + + let _sql = '' + + if (!item.innerFunc) { + let declarefields = [] + let fields = [] + let timestamp = new Date().getTime() + + btn.columns.forEach(col => { + declarefields.push(`${col.Column} ${col.type}`) + fields.push(col.Column) + }) + + fields = fields.join(',') + + _sql = `declare @${btn.sheet} table (${declarefields.join(',')},jskey nvarchar(50) ) + Declare @UserName nvarchar(50),@FullName nvarchar(50) ,@upid nvarchar(50) + select @UserName=UserName,@FullName=FullName from SUsers where UID=@UserID@ + + set @upid='${timestamp}' + + Insert into @${btn.sheet} (${fields},jskey) + ${_Ltext} + + Insert into ${btn.sheet} (${fields},createuserid,createuser,createstaff,bid,upid) + Select ${fields},@userid@,@username,@fullname,@BID@,@upid From @${btn.sheet} + + Delete @${btn.sheet}` + + } else { + _sql = _Ltext + } + + console.log(_sql) + return { + sql: _sql, + errors: errors.join('; ') + } + } + + /** * @description 浣跨敤绯荤粺鍑芥暟鏃讹紙sPC_TableData_InUpDe 锛夛紝鐢熸垚sql璇彞 * @return {String} type 鎵ц绫诲瀷 * @return {String} table 琛ㄥ悕 @@ -552,10 +677,10 @@ ` } - // 娣诲姞鏃朵富閿负绌� - if (btn.sqlType === 'insert') { - primaryId = '' - } + // 娣诲姞鏃朵富閿负绌� 鏀逛负鍓嶅彴鐢熸垚 + // if (btn.sqlType === 'insert') { + // primaryId = '' + // } // 鍘婚櫎绂佺敤鐨勯獙璇� if (verify.contrasts) { @@ -759,6 +884,10 @@ } }) + if (!keys.includes(primaryKey)) { + keys.push(primaryKey) + values.push('\'' + primaryId + '\'') + } if (!keys.includes('createuserid')) { keys.push('createuserid') values.push('@userid@') -- Gitblit v1.8.0