From 48a18736c461ad730bd264b0ac7b40b68a0e33a1 Mon Sep 17 00:00:00 2001 From: king <18310653075@163.com> Date: 星期日, 28 六月 2020 20:30:20 +0800 Subject: [PATCH] 2020-06-28 --- src/utils/utils.js | 175 ++++++++++++++++++++++++++++++++++++---------------------- 1 files changed, 109 insertions(+), 66 deletions(-) diff --git a/src/utils/utils.js b/src/utils/utils.js index 8042fad..9c2063c 100644 --- a/src/utils/utils.js +++ b/src/utils/utils.js @@ -633,6 +633,13 @@ static getExcelInSql (item, data, dict) { let btn = item.verify let keys = ['delete', 'drop', 'insert', 'truncate', 'update'] + let userName = sessionStorage.getItem('User_Name') || '' + let fullName = sessionStorage.getItem('Full_Name') || '' + + if (sessionStorage.getItem('isEditState') === 'true') { + userName = sessionStorage.getItem('CloudUserName') || '' + fullName = sessionStorage.getItem('CloudFullName') || '' + } let errors = [] let _topline = btn.range || 0 @@ -648,22 +655,28 @@ if (script.position === 'init') { _initCustomScript += ` - ${script.sql} - ` + /* 鑷畾涔夎剼鏈� */ + ${script.sql} + ` } else if (script.position === 'front') { _prevCustomScript += ` - ${script.sql} - ` + /* 鑷畾涔夎剼鏈� */ + ${script.sql} + ` } else { _backCustomScript += ` - ${script.sql} - ` + /* 鑷畾涔夎剼鏈� */ + ${script.sql} + ` } }) } let _Ltext = data.map((item, lindex) => { - let vals = btn.columns.map((col, cindex) => { + let vals = [] + btn.columns.forEach((col, cindex) => { + if (col.import === 'false') return + let val = item[col.Column] !== undefined ? item[col.Column] : '' let _position = (_topline + lindex + 1) + dict['main.excel.line'] + ' ' + (cindex + 1) + dict['main.excel.column'] + ' ' @@ -732,9 +745,28 @@ errors.push(_error) } } + } else if (col.type === 'date') { + val = val.replace(/(^\s*$)|\t*|\v*/ig, '') + + if (!val && col.required === 'true') { // 蹇呭~鏍¢獙 + let _error = _position + dict['main.excel.content.emptyerror'] + 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) + } + }) + } } - - return `'${val}' as ${col.Column}` + + if (col.type === 'date') { + vals.push(`case when ${col.Column}='' then '1900-01-01' else dateadd(day,cast('${val}' as int)-2,'1900-01-01') end as ${col.Column}`) + } else { + vals.push(`'${val}' as ${col.Column}`) + } }) let _lineIndex = '0000' + (lindex + 1) + '0' @@ -745,9 +777,14 @@ return `Select ${vals.join(',')}` }) - _Ltext = _Ltext.join(' Union all ') + let result = [] + for(let i = 0; i < _Ltext.length; i += 20) { + result.push(_Ltext.slice(i, i + 20)) + } let _sql = '' + let _sqlInsert = '' + let _sqlBottom = '' if (item.intertype === 'inner' && !item.innerFunc) { let _uniquesql = '' @@ -756,9 +793,8 @@ if (unique.status === 'false') return let _fields = unique.field.split(',') - let _fields_ = _fields.map(_field => { - return `a.${_field}=b.${_field}` - }) + let _fields_ = _fields.map(_field => `a.${_field}=b.${_field}`) + let _afields = _fields.map(_field => `a.${_field}`) _fields_ = _fields_.join(' and ') if (unique.verifyType !== 'physical') { @@ -766,81 +802,88 @@ } _uniquesql += ` - Set @tbid='' - Select top 1 @tbid=${_fields.join('+\' \'+')} from (select 1 as n,${unique.field} from @${item.sheet} ) a group by ${unique.field} having sum(n)>1 - - If @tbid!='' - Begin - select @ErrorCode='${unique.errorCode}',@retmsg=@tbid+' 閲嶅' - goto aaa - end - - Set @tbid='' - Select top 1 @tbid=${_fields.join('+\' \'+')} from @${item.sheet} a - Inner join ${item.sheet} b on ${_fields_} - - If @tbid!='' - Begin - select @ErrorCode='${unique.errorCode}',@retmsg=@tbid+' 涓庡凡鏈夋暟鎹噸澶�' - goto aaa - end - ` + /* 閲嶅鎬ч獙璇� */ + Set @tbid='' + Select top 1 @tbid=${_fields.join('+\' \'+')} from (select 1 as n,${unique.field} from @${item.sheet} ) a group by ${unique.field} having sum(n)>1 + + If @tbid!='' + Begin + select @ErrorCode='${unique.errorCode}',@retmsg=@tbid+' 閲嶅' + goto aaa + end + + Set @tbid='' + Select top 1 @tbid=${_afields.join('+\' \'+')} from @${item.sheet} a Inner join ${item.sheet} b on ${_fields_} + + If @tbid!='' + Begin + select @ErrorCode='${unique.errorCode}',@retmsg=@tbid+' 涓庡凡鏈夋暟鎹噸澶�' + goto aaa + end + ` }) - - if (_uniquesql) { - _uniquesql = 'Declare @tbid Nvarchar(512)' + _uniquesql - } } let declarefields = [] let fields = [] btn.columns.forEach(col => { - declarefields.push(`${col.Column} ${col.type}`) + if (col.type === 'date') { + declarefields.push(`${col.Column} Nvarchar(50)`) + } else { + declarefields.push(`${col.Column} ${col.type}`) + } fields.push(col.Column) }) fields = fields.join(',') let _insert = '' - - if (_prevCustomScript) { - _insert += _prevCustomScript - } - if (btn.default !== 'false') { - _insert += ` + _insert = ` + /* 榛樿sql */ Insert into ${item.sheet} (${fields},createuserid,createuser,createstaff,bid) Select ${fields},@userid@,@username,@fullname,@BID@ From @${item.sheet} ` } - if (_backCustomScript) { - _insert += _backCustomScript - } - - _sql = `declare @${item.sheet} table (${declarefields.join(',')},jskey nvarchar(50) ) - Declare @UserName nvarchar(50),@FullName nvarchar(50),@ErrorCode nvarchar(50),@retmsg nvarchar(4000) - - Select @ErrorCode='', @retmsg='' + _sql = ` + /* 绯荤粺鐢熸垚 */ + declare @${item.sheet} table (${declarefields.join(',')},jskey nvarchar(50) ) + Declare @UserName nvarchar(50),@FullName nvarchar(50),@ErrorCode nvarchar(50),@retmsg nvarchar(4000),@tbid Nvarchar(512) + + Select @ErrorCode='', @retmsg='', @UserName='${userName}', @FullName='${fullName}' + ${_initCustomScript} + ` + _sqlInsert = `Insert into @${item.sheet} (${fields},jskey)` + _sqlBottom = ` + /* 榛樿sql */ + delete tmp_excel_in where upid=@upid@ + + delete tmp_excel_in where datediff(day,createdate,getdate())>15 + ${_uniquesql} + ${_prevCustomScript} + ${_insert} + ${_backCustomScript} + Delete @${item.sheet} + + aaa: select @ErrorCode as ErrorCode,@retmsg as retmsg` - select @UserName=UserName,@FullName=FullName from SUsers where UID=@UserID@ - ${_initCustomScript} - Insert into @${item.sheet} (${fields},jskey) - ${_Ltext} - ${_uniquesql} - ${_insert} - Delete @${item.sheet} - - aaa: select @ErrorCode as ErrorCode,@retmsg as retmsg` - - } else { - _sql = _Ltext + let fsql = `${_sql}${_sqlInsert}${_sqlBottom}` + fsql = fsql.replace(/\n\s{8}/ig, '\n') + console.log(fsql) } - - console.log(_sql) + return { sql: _sql, + lines: result.map((list, index) => { + return { + Ltext: window.btoa(window.encodeURIComponent(list.join(' Union all '))), + Sort: (index + 1) * 10 + } + }), + insert: _sqlInsert, + bottom: _sqlBottom, errors: errors.join('; ') } } @@ -1171,7 +1214,7 @@ /* 鍚岀被鏁版嵁楠岃瘉 */ Set @tbid='' - Select top 1 @tbid='X' from (select distinct ${item.field},1 as n from ${datasource} inner join (select ID from dbo.SplitComma(@ID@)) sp on tb.${primaryKey}=sp.ID ) a group by ${item.field} having sum(n)>1 + Select top 1 @tbid='X' from (select distinct ${item.field},1 as n from ${datasource} inner join (select ID from dbo.SplitComma(@ID@)) sp on tb.${primaryKey}=sp.ID ) a having sum(n)>1 If @tbid!='' Begin -- Gitblit v1.8.0