From 02a05c4e82e74c290e80ae7710fbb394ed7cac7a Mon Sep 17 00:00:00 2001
From: king <18310653075@163.com>
Date: 星期三, 15 四月 2020 12:51:04 +0800
Subject: [PATCH] 2020-04-15

---
 src/utils/utils.js |  182 +++++++++++++++++++++++++++++++++++++++------
 1 files changed, 157 insertions(+), 25 deletions(-)

diff --git a/src/utils/utils.js b/src/utils/utils.js
index 6a5c97a..4b68be0 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,11 +667,17 @@
    * @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 || {}
     let _formFieldValue = {}
+    let _actionType = null
+
+    if (verify.default !== 'false') { // 鍒ゆ柇鏄惁浣跨敤榛樿sql
+      _actionType = btn.sqlType
+    }
+
     // 闇�瑕佸0鏄庣殑鍙橀噺闆�
     // let _vars = ['tbid', 'ErrorCode', 'retmsg', 'BillCode', 'BVoucher', 'FIBVoucherDate', 'FiYear', 'UserName', 'FullName', 'ID', 'BID', 'LoginUID', 'SessionUid', 'UserID', 'Appkey']
     let _vars = ['tbid', 'errorcode', 'retmsg', 'billcode', 'bvoucher', 'fibvoucherdate', 'fiyear', 'username', 'fullname', 'modulardetailcode']
@@ -807,6 +856,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 +866,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 +877,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(', ')} 宸插瓨鍦�'
@@ -851,15 +907,16 @@
     }
 
     // 鍗曞彿鐢熸垚锛屼娇鐢ㄤ笂绾d锛圔ID锛夋垨鍒楄〃鏁版嵁锛屽0鏄庡彉閲忥紙妫�楠岋級
+    let _billcodesSql  = ''
     if (verify.billcodes && verify.billcodes.length > 0) {
       verify.billcodes.forEach(item => {
         let _ModularDetailCode = ''
         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') {
@@ -882,7 +939,7 @@
           _vars.push(_key)
         }
 
-        _sql += `${_declare}
+        _billcodesSql += `${_declare}
           select @BillCode='', @${_key}='', @ModularDetailCode=''
           ${_lpline}
           exec s_get_BillCode
@@ -898,6 +955,10 @@
           set @${_key}=@BillCode
           `
       })
+
+      if (_actionType !== 'insertOrUpdate') {
+        _sql += _billcodesSql
+      }
     }
 
     let hasvoucher = false
@@ -914,6 +975,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 ,
@@ -926,14 +990,8 @@
 
     let primaryKeyName = ['id', 'bid', 'loginuid', 'sessionuid', 'userid', 'appkey'].includes(primaryKey.toLowerCase()) ? primaryKey + '@' : primaryKey
 
-    let _actionType = null
-
-    if (verify.default !== 'false') { // 鍒ゆ柇鏄惁浣跨敤榛樿sql
-      _actionType = btn.sqlType
-    }
-
-    // 娣诲姞銆佷慨鏀广�侀�昏緫鍒犻櫎銆佺墿鐞嗗垹闄�
-    if (_actionType === 'insert') {
+    let _insertsql = ''
+    if (_actionType === 'insert' || _actionType === 'insertOrUpdate') { // 娣诲姞璇彞
       let keys = []
       let values = []
 
@@ -950,8 +1008,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 +1025,21 @@
         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};`
+    }
+
+    let _updatesql = ''
+    if (_actionType === 'update' || _actionType === 'insertOrUpdate') { // 淇敼璇彞
       let _form = []
       let _arr = []
 
@@ -1007,9 +1072,15 @@
           _form.push('FiYear=@FiYear')
         }
       }
-
       _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 +1096,19 @@
         })
       }
       _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=''
+          begin
+          ${_billcodesSql}
+          ${_insertsql}
+          end
+        else
+          begin
+          ${_updatesql}
+          end
+      `
     }
 
     // 鎷兼帴鑷畾涔夎剼鏈�
@@ -1243,6 +1327,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 +1447,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 +1510,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