From d5e92a5383860b5fcd449507028b84bddf3a41f2 Mon Sep 17 00:00:00 2001
From: king <18310653075@163.com>
Date: 星期二, 14 四月 2020 17:32:52 +0800
Subject: [PATCH] 2020-04-14

---
 src/utils/utils.js |  155 ++++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 138 insertions(+), 17 deletions(-)

diff --git a/src/utils/utils.js b/src/utils/utils.js
index 6a5c97a..bef3bb4 100644
--- a/src/utils/utils.js
+++ b/src/utils/utils.js
@@ -164,8 +164,8 @@
     })
 
     // 1銆佹浛鎹�%绗︼紙鏁版嵁搴撲腑瑙f瀽鍚巗ql鎶ラ敊锛夛紝2銆佸幓闄ゆ敹灏惧浣欑┖鏍�
-    value = value.replace(/%/ig, 'mpercent')
-    value = value.replace(/(^\s|\s$)/ig, '')
+    value = value.replace(/%/ig, ' mpercent ')
+    // value = value.replace(/(^\s|\s$)/ig, '')
 
     // 1銆乪ncode缂栫爜锛堜腑鏂囧瓧绗﹁秴鍑篵ase64鍔犲瘑鑼冨洿锛夛紝2銆乥ase64鍔犲瘑
     value = window.btoa(window.encodeURIComponent(value))
@@ -505,7 +505,7 @@
             })
           }
         } else if (/^int/ig.test(col.type)) {
-          if (!val) {
+          if (!val && val !== 0) {
             let _error =  _position + dict['main.excel.content.emptyerror']
             errors.push(_error)
           } else {
@@ -523,7 +523,7 @@
             }
           }
         } else if (/^Decimal/ig.test(col.type)) {
-          if (!val) {
+          if (!val && val !== 0) {
             let _error =  _position + dict['main.excel.content.emptyerror']
             errors.push(_error)
           } else {
@@ -565,6 +565,48 @@
     let _sql = ''
 
     if (item.intertype === 'inner' && !item.innerFunc) {
+      let _uniquesql = ''
+      if (btn.uniques && btn.uniques.length > 0) {
+        btn.uniques.forEach(unique => {
+          if (unique.status === 'false') return
+
+          let _fields = unique.field.split(',')
+          let _fields_ = _fields.map(_field => {
+            return `a.${_field}=b.${_field}`
+          })
+          _fields_ = _fields_.join(' and ')
+
+          if (unique.verifyType !== 'physical') {
+            _fields_ += ' and b.deleted=0'
+          }
+
+          _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
+          `
+        })
+
+        if (_uniquesql) {
+          _uniquesql = 'Declare @tbid Nvarchar(512)' + _uniquesql
+        }
+      }
+
       let declarefields = []
       let fields = []
 
@@ -603,6 +645,7 @@
       
       Insert into  @${item.sheet} (${fields},jskey)
       ${_Ltext}
+      ${_uniquesql}
       ${_insert}
       Delete @${item.sheet}
 
@@ -624,7 +667,7 @@
    * @return {String} type   鎵ц绫诲瀷
    * @return {String} table  琛ㄥ悕
    */
-  static getSysDefaultSql (btn, setting, formdata, param, data, logcolumns) {
+  static getSysDefaultSql (btn, setting, formdata, param, data, logcolumns, tab) {
     let primaryId = param.ID
     let BID = param.BID
     let verify = btn.verify || {}
@@ -807,6 +850,7 @@
         let _fieldValue = []                     // 琛ㄥ崟閿�煎field=value
         let _value = []                          // 琛ㄥ崟鍊硷紝鐢ㄤ簬閿欒鎻愮ず
         let _labels = item.fieldlabel.split(',') // 琛ㄥ崟鎻愮ず鏂囧瓧
+        let arr = [] // 楠岃瘉涓婚敭
 
         item.field.split(',').forEach((_field, index) => {
           let _fval = `'${_formFieldValue[_field]}'`
@@ -816,6 +860,8 @@
           if (_field.toLowerCase() === 'bid' && !hasBid) { // 琛ㄥ崟涓病鏈塨id鍒欎娇鐢ㄧ郴缁焍id鍙橀噺
             _fval = '@BID@'
           }
+          arr.push(_field.toLowerCase())
+
           _fieldValue.push(`${_field}=${_fval}`)
           _value.push(`${_labels[index] || ''}锛�${_formFieldValue[_field] || ''}`)
         })
@@ -825,8 +871,12 @@
           _verifyType = ' and deleted=0'
         }
 
+        if (!arr.includes(primaryKey.toLowerCase())) {
+          _fieldValue.push(`${primaryKey} !='${primaryId}'`)
+        }
+
         _sql += `select @tbid='', @ErrorCode='',@retmsg=''
-          select @tbid='X' from ${btn.sql} where ${_fieldValue.join(' and ')} and ${primaryKey} !='${primaryId}'${_verifyType}
+          select @tbid='X' from ${btn.sql} where ${_fieldValue.join(' and ')}${_verifyType}
           If @tbid!=''
           Begin
             select @ErrorCode='${item.errorCode}',@retmsg='${_value.join(', ')} 宸插瓨鍦�'
@@ -857,9 +907,9 @@
         let _lpline = ''
         if (item.TypeCharOne === 'Lp') {
           if (item.linkField === 'BID' && BID) { // 鏇挎崲bid
-            _lpline = `set @ModularDetailCode= 'Lp'+ right('${btn.uuid}'+@BID@,48)`
+            _lpline = `set @ModularDetailCode= 'Lp'+ right('${item.mark || btn.uuid}'+@BID@,48)`
           } else {
-            _lpline = `set @ModularDetailCode= 'Lp'+ right('${btn.uuid}'+@${item.linkField},48)`
+            _lpline = `set @ModularDetailCode= 'Lp'+ right('${item.mark || btn.uuid}'+@${item.linkField},48)`
           }
           _ModularDetailCode = '@ModularDetailCode'
         } else if (item.TypeCharOne === 'BN') {
@@ -914,6 +964,9 @@
           @VoucherTypeOne ='${_voucher.VoucherTypeOne}',
           @VoucherTypeTwo ='${_voucher.VoucherTypeTwo}',
           @Type =${_voucher.Type},
+          @UserID=@UserID@,
+          @Username=@Username,
+          @FullName=@FullName,
           @BVoucher =@BVoucher OUTPUT ,
           @FIBVoucherDate =@FIBVoucherDate OUTPUT ,
           @FiYear =@FiYear OUTPUT ,
@@ -932,8 +985,9 @@
       _actionType = btn.sqlType
     }
 
-    // 娣诲姞銆佷慨鏀广�侀�昏緫鍒犻櫎銆佺墿鐞嗗垹闄�
-    if (_actionType === 'insert') {
+    let _insertsql = ''
+    let _updatesql = ''
+    if (_actionType === 'insert' || _actionType === 'insertOrUpdate') { // 娣诲姞璇彞
       let keys = []
       let values = []
 
@@ -950,8 +1004,8 @@
         }
       })
 
-      if (!keys.includes(primaryKey)) {
-        keys.push(primaryKey)
+      if (!keys.includes(primaryKey.toLowerCase())) {
+        keys.push(primaryKey.toLowerCase())
         values.push('\'' + primaryId + '\'')
       }
       if (!keys.includes('createuserid')) {
@@ -967,14 +1021,18 @@
         values.push('@fullname')
       }
       if (!keys.includes('bid')) {
-        keys.push('bid')
+        if (tab && tab.foreignKey && !keys.includes(tab.foreignKey.toLowerCase())) {
+          keys.push(tab.foreignKey)
+        } else {
+          keys.push('bid')
+        }
         values.push('@BID@')
       }
 
       keys = keys.join(',')
       values = values.join(',')
-      _sql += `insert into ${btn.sql} (${keys}) select ${values};`
-    } else if (_actionType === 'update') {
+      _insertsql = `insert into ${btn.sql} (${keys}) select ${values};`
+    } else if (_actionType === 'update' || _actionType === 'insertOrUpdate') { // 淇敼璇彞
       let _form = []
       let _arr = []
 
@@ -1009,7 +1067,14 @@
       }
 
       _form = _form.join(',')
-      _sql += `update ${btn.sql} set ${_form} where ${primaryKey}=@${primaryKeyName};`
+      _updatesql = `update ${btn.sql} set ${_form} where ${primaryKey}=@${primaryKeyName};`
+    }
+
+    // 娣诲姞銆佷慨鏀广�侀�昏緫鍒犻櫎銆佺墿鐞嗗垹闄�
+    if (_actionType === 'insert') {
+      _sql += _insertsql
+    } else if (_actionType === 'update') {
+      _sql += _updatesql
     } else if (_actionType === 'LogicDelete') { // 閫昏緫鍒犻櫎
       _sql += `update ${btn.sql} set deleted=1,modifydate=getdate(),modifyuserid=@userid@ where ${primaryKey}=@${primaryKeyName};`
     
@@ -1025,6 +1090,14 @@
         })
       }
       _sql += `insert into snote (remark,createuserid,CreateUser,CreateStaff) select '鍒犻櫎琛�:${btn.sql} 鏁版嵁: ${_msg}${primaryKey}='+@${primaryKeyName},@userid@,@username,@fullname delete ${btn.sql} where ${primaryKey}=@${primaryKeyName};`
+    } else if (_actionType === 'insertOrUpdate') {
+      _sql += `select @tbid=''
+        select @tbid='X' from ${btn.sql} where ${primaryKey}=@ID@
+        if @tbid=''
+          ${_insertsql}
+        else
+          ${_updatesql}
+      `
     }
 
     // 鎷兼帴鑷畾涔夎剼鏈�
@@ -1243,6 +1316,10 @@
         
         update ${param.name} set ${field3},modifydate=getdate(),modifyuserid=@UserID
       `
+    } else if (btn.OpenType === 'prompt' || btn.OpenType === 'exec') {
+      form = `
+        update ${param.name} set ModifyDate=getdate(),ModifyUserID=@UserID where ${primaryKey}=@${primaryKey}
+      `
     }
 
     if (columns) {
@@ -1359,6 +1436,50 @@
   static getexcelInfunc (param, btn, menu) {
     let _verify = btn.verify
 
+    let _uniquesql = ''
+    if (_verify.uniques && _verify.uniques.length > 0) {
+      _verify.uniques.forEach(unique => {
+        if (unique.status === 'false') return
+
+        let _fields = unique.field.split(',')
+        let _fields_ = _fields.map(_field => {
+          return `a.${_field}=b.${_field}`
+        })
+        _fields_ = _fields_.join(' and ')
+
+        if (unique.verifyType !== 'physical') {
+          _fields_ += ' and b.deleted=0'
+        }
+
+        _uniquesql += `
+        Set @tbid=''
+        Select top 1 @tbid=${_fields.join('+\' \'+')} from (select 1 as n,${unique.field} from @${btn.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  @${btn.sheet} a
+        Inner join ${btn.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 = []
 
@@ -1378,7 +1499,7 @@
 
       exec s_KeyWords_Replace
       @LText=@LText, @BID=@BID,@LoginUID=@LoginUID,@SessionUid=@SessionUid,@UserID=@UserID,@ID=@ID
-
+      ${_uniquesql}
       Insert into ${btn.sheet} (${fields},createuserid,createuser,createstaff,bid) 
       Select ${fields},@userid,@username,@fullname,@BID From @${btn.sheet}
 

--
Gitblit v1.8.0