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 | 220 +++++++++++++++++++++++++++++++++++------------------- 1 files changed, 141 insertions(+), 79 deletions(-) diff --git a/src/utils/utils.js b/src/utils/utils.js index 771b170..9c2063c 100644 --- a/src/utils/utils.js +++ b/src/utils/utils.js @@ -37,11 +37,20 @@ /** * @description md5鍔犲瘑 * @return {String} str 鍔犲瘑涓� - * @return {String} timestamp 鏃堕棿鎴� + * @return {String} timestamp 鏃堕棿鎴� + * @return {Boolean} isopenkey 鏄惁涓轰簯绔瘑閽� */ - static encrypt (str, timestamp) { - let salt = 'mingke' // 鐩愬�� - let _str = str + salt + timestamp + static encrypt (str, timestamp, isopenkey) { + let salt1 = 'mingke' // sql璇硶鐩愬�� + let salt2 = 'open_key' // 浜戠鏁版嵁鎿嶄綔鐩愬�� + let _str = '' + + if (isopenkey) { + _str = salt2 + timestamp + str + } else { + _str = str + salt1 + timestamp + } + if (_str.length > 8000) { _str = _str.slice(_str.length - 8000) } @@ -624,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 @@ -639,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'] + ' ' @@ -723,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' @@ -736,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 = '' @@ -747,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') { @@ -757,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('; ') } } @@ -1033,7 +1085,7 @@ // 鍒濆鍖栧嚟璇佸強鐢ㄦ埛淇℃伅瀛楁 _sql += ` /* 鍑瘉鍙婄敤鎴蜂俊鎭垵濮嬪寲璧嬪�� */ - select @BVoucher='',@FIBVoucherDate='',@FiYear='',@ErrorCode='',@retmsg='',@UserName='${userName}', @FullName='${fullName}' + select @BVoucher='',@FIBVoucherDate='',@FiYear='',@ErrorCode='',@retmsg='',@UserName='${userName}', @FullName='${fullName}', @BillCode='', @ModularDetailCode='' ` if (_initCustomScript) { @@ -1057,21 +1109,31 @@ datasource = '(' + datasource + ') tb' } - let _ID = '=@ID@' if (btn.Ot === 'requiredOnce') { - _ID = ' in (select ID from dbo.SplitComma(@ID@))' - } - - _sql += ` + _sql += ` /* 澶辨晥楠岃瘉 */ select @tbid='', @ErrorCode='',@retmsg='' - select @tbid=${primaryKey} from ${datasource} where ${primaryKey} ${_ID} + select @tbid='X' from ${datasource} right join (select ID from dbo.SplitComma(@ID@)) sp + on tb.id =sp.id where tb.id is null + + If @tbid!='' + Begin + select @ErrorCode='E',@retmsg='鏁版嵁宸插け鏁�' + goto aaa + end + ` + } else { + _sql += ` + /* 澶辨晥楠岃瘉 */ + select @tbid='', @ErrorCode='',@retmsg='' + select @tbid=${primaryKey} from ${datasource} where ${primaryKey}=@ID@ If @tbid='' Begin select @ErrorCode='E',@retmsg='鏁版嵁宸插け鏁�' goto aaa end ` + } } // 姣旇緝楠岃瘉 @@ -1143,16 +1205,16 @@ let datasource = setting.dataresource if (/\s/.test(datasource)) { // 鎷兼帴鍒悕 datasource = '(' + datasource + ') tb' + } else { + datasource = datasource + ' tb' } verify.uniques.forEach(item => { - let _primaryKey = item.field.split(',').includes(primaryKey) ? '' : ',' + primaryKey - _sql += ` /* 鍚岀被鏁版嵁楠岃瘉 */ Set @tbid='' - Select top 1 @tbid='X' from (select ${item.field}${_primaryKey},1 as n from ${datasource} ) tb inner join (select ID from dbo.SplitComma(@ID@)) sp on tb.${primaryKey}=sp.ID 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