From 24f0ce147c8daef39ec437d5def9d089ea5b1839 Mon Sep 17 00:00:00 2001
From: king <18310653075@163.com>
Date: 星期五, 05 六月 2020 17:23:53 +0800
Subject: [PATCH] 2020-06-05

---
 src/utils/utils.js |  480 +++++++++++++++++++++++++++++++++++++++++++++--------------
 1 files changed, 366 insertions(+), 114 deletions(-)

diff --git a/src/utils/utils.js b/src/utils/utils.js
index eed79e0..8b14dfd 100644
--- a/src/utils/utils.js
+++ b/src/utils/utils.js
@@ -2,8 +2,6 @@
 import md5 from 'md5'
 import options from '@/store/options.js'
 
-const service = window.GLOB.service ? (/\/$/.test(window.GLOB.service) ? window.GLOB.service : window.GLOB.service + '/') : ''
-
 export default class Utils {
   /**
    * @description 鐢熸垚32浣島uid string + 鏃堕棿
@@ -67,10 +65,10 @@
       {key: 'alter', reg: /(^|\s)alter\s/ig},
       {key: 'truncate', reg: /(^|\s)truncate\s/ig},
       {key: 'if', reg: /(^|\s)if\s/ig},
-      {key: 'exec', reg: /exec/ig},
-      {key: 'OBJECT', reg: /object/ig},
-      {key: 'sys.', reg: /sys\./ig},
-      {key: 'kill', reg: /kill/ig}
+      {key: 'exec', reg: /(^|\s)exec(\s|\()/ig},
+      {key: 'OBJECT', reg: /(^|\s)object(\s|\()/ig},
+      {key: 'sys.', reg: /(^|\s)sys\./ig},
+      {key: 'kill', reg: /(^|\s)kill\s/ig}
     ]
 
     if (type === 'customscript') {
@@ -193,7 +191,76 @@
         value: search.initval,
         required: search.required === 'true'
       }
-      if (item.type === 'date') {
+
+      if (item.type === 'group') {
+        let copy = JSON.parse(JSON.stringify(item))
+        copy.key = search.datefield
+
+        item.value = search.initval && search.initval[0] ? search.initval[0] : ''
+        item.match = '='
+        item.forbid = true
+        
+        copy.type = 'daterange'
+        copy.match = 'between'
+        copy.value = ''
+
+        if (search.initval && search.initval.length > 0) {
+          let _type = search.initval[0]
+          let _val = search.initval[1]
+
+          if (_type === 'day') {
+            copy.value = [moment().subtract(_val, 'days').format('YYYY-MM-DD'),
+              moment().subtract(_val, 'days').format('YYYY-MM-DD')]
+          } else if (_type === 'week') {
+            copy.value = [moment().subtract(_val * 7, 'days').startOf('week').format('YYYY-MM-DD'),
+              moment().subtract(_val * 7, 'days').endOf('week').format('YYYY-MM-DD')]
+          } else if (_type === 'month') {
+            copy.value = [moment().subtract(_val, 'month').startOf('month').format('YYYY-MM-DD'),
+              moment().subtract(_val, 'month').endOf('month').format('YYYY-MM-DD')]
+          } else if (_type === 'quarter') {
+            let _differ = parseInt(moment().format('MM')) % 3
+            let _pdiffer = 0
+            let _ndiffer = 0
+
+            // 宸�艰绠�
+            switch(_differ) {
+              case 0:
+                _pdiffer = 2
+                _ndiffer = 0
+                break
+              case 1:
+                _pdiffer = 0
+                _ndiffer = -2
+                break
+              case 2:
+                _pdiffer = 1
+                _ndiffer = -1
+                break
+              default:
+            }
+
+            copy.value = [moment().subtract(_pdiffer + _val * 3, 'month').startOf('month').format('YYYY-MM-DD'),
+              moment().subtract(_ndiffer + _val * 3, 'month').endOf('month').format('YYYY-MM-DD')]
+          } else if (_type === 'year') {
+            let _year = parseInt(moment().format('YYYY')) - _val
+            copy.value = [_year + '-01-01', _year + '-12-31']
+          } else if (_type === 'customized') {
+            try {
+              _val = JSON.parse(_val)
+            } catch {
+              _val = [0, 0]
+            }
+            copy.value = [moment().subtract(_val[0], 'days').format('YYYY-MM-DD'),
+              moment().subtract(_val[1], 'days').format('YYYY-MM-DD')]
+          }
+        }
+
+        if (search.transfer === 'true') {
+          newsearches.push(item)
+        }
+        newsearches.push(copy)
+        return
+      } else if (item.type === 'date') {
         item.value = item.value ? moment().subtract(item.value, 'days').format('YYYY-MM-DD') : ''
       } else if (item.type === 'datemonth') {
         item.value = item.value ? moment().subtract(item.value, 'month').format('YYYY-MM') : ''
@@ -244,14 +311,14 @@
         }
 
         if (newsearches[item.key]) {
-          newsearches[item.key + '1'] = _val ? _val + timetail : null
+          newsearches[item.key + '1'] = _val ? _val + timetail : ''
         } else {
-          newsearches[item.key] = _val ? _val + timetail : null
+          newsearches[item.key] = _val ? _val + timetail : ''
         }
       } else if (item.type === 'datemonth') {
         // 鏈�-杩囨护鏉′欢锛屼粠鏈堝紑濮嬭嚦缁撴潫
-        let _startval = null
-        let _endval = null
+        let _startval = ''
+        let _endval = ''
 
         if (item.value) {
           _startval = moment(item.value, 'YYYY-MM').startOf('month').format('YYYY-MM-DD') + ' 00:00:00.000'
@@ -266,16 +333,16 @@
           _endval = moment(item.value[1], 'YYYY-MM-DD').add(1, 'days').format('YYYY-MM-DD')
         }
 
-        newsearches[item.key] = item.value ? item.value[0] + ' 00:00:00.000' : null
-        newsearches[item.key + '1'] = item.value ? _endval + ' 00:00:00.000' : null
+        newsearches[item.key] = item.value ? item.value[0] + ' 00:00:00.000' : ''
+        newsearches[item.key + '1'] = item.value ? _endval + ' 00:00:00.000' : ''
       } else if (item.type === 'daterange') {
         let _endval = ''
         if (item.value) {
           _endval = moment(item.value[1], 'YYYY-MM-DD').add(1, 'days').format('YYYY-MM-DD')
         }
 
-        newsearches[item.key] = item.value ? item.value[0] + ' 00:00:00.000' : null
-        newsearches[item.key + '1'] = item.value ? _endval + ' 00:00:00.000' : null
+        newsearches[item.key] = item.value ? item.value[0] + ' 00:00:00.000' : ''
+        newsearches[item.key + '1'] = item.value ? _endval + ' 00:00:00.000' : ''
       } else if (item.type === 'text') {
         item.key.split(',').forEach(field => { // 缁煎悎鎼滅储锛屾墍瀛楁鎷兼帴
           newsearches[field] = item.value
@@ -306,7 +373,7 @@
 
     let searchText = ''
     searches.forEach(item => {
-      if (!item.value || (item.type === 'multiselect' && item.value.length === 0)) return
+      if (item.forbid || !item.value || (item.type === 'multiselect' && item.value.length === 0)) return
       
       searchText += (searchText !== '' ? ' AND ' : '')
       if (item.type === 'text') {
@@ -360,6 +427,95 @@
   }
 
   /**
+   * @description 鎷兼帴鎼滅储鏉′欢main
+   * @param {Array}   searches     鎼滅储鏉′欢
+   * @return {String}  searchText  鎷兼帴缁撴灉
+   */
+  static getAllSearchOptions (searches) {
+    if (!searches || searches.length === 0) return []
+
+    let options = []
+    let fieldmap = new Map()
+    searches.forEach(search => {
+      let item = {
+        key: search.key,
+        match: search.match,
+        type: search.type,
+        label: search.label,
+        value: search.value,
+        required: search.required
+      }
+
+      if (fieldmap.has(item.key)) {
+        item.key = item.key + '1'
+      }
+
+      fieldmap.set(item.key, true)
+
+      if (item.type === 'date') {
+        if (['>=', '>'].includes(item.match)) {
+          item.value = item.value ? item.value + ' 00:00:00.000' : '1970-01-01 00:00:00.000'
+        } else if (['<=', '<'].includes(item.match)) {
+          item.value = item.value ? moment(item.value, 'YYYY-MM-DD').add(1, 'days').format('YYYY-MM-DD') + ' 00:00:00.000' : '2050-01-01 00:00:00.000'
+        }
+
+        options.push(item)
+      } else if (item.type === 'datemonth') {
+        let _startval = item.value ? moment(item.value, 'YYYY-MM').startOf('month').format('YYYY-MM-DD') + ' 00:00:00.000' : '1970-01-01 00:00:00.000'
+        let _endval = item.value ? moment(item.value, 'YYYY-MM').endOf('month').add(1, 'days').format('YYYY-MM-DD') + ' 00:00:00.000' : '2050-01-01 00:00:00.000'
+
+        let copy = JSON.parse(JSON.stringify(item))
+        copy.key = copy.key + '1'
+        copy.value = _endval
+        
+        item.value = _startval
+
+        options.push(item)
+        options.push(copy)
+      } else if (item.type === 'dateweek') {
+        let _startval = item.value && item.value[0] ? moment(item.value[0], 'YYYY-MM-DD').format('YYYY-MM-DD') + ' 00:00:00.000' : '1970-01-01 00:00:00.000'
+        let _endval = item.value && item.value[1] ? moment(item.value[1], 'YYYY-MM-DD').add(1, 'days').format('YYYY-MM-DD') + ' 00:00:00.000' : '2050-01-01 00:00:00.000'
+
+        let copy = JSON.parse(JSON.stringify(item))
+        copy.key = copy.key + '1'
+        copy.value = _endval
+        
+        item.value = _startval
+
+        options.push(item)
+        options.push(copy)
+      } else if (item.type === 'daterange') {
+        let _startval = item.value && item.value[0] ? item.value[0] + ' 00:00:00.000' : '1970-01-01 00:00:00.000'
+        let _endval = item.value && item.value[1] ? moment(item.value[1], 'YYYY-MM-DD').add(1, 'days').format('YYYY-MM-DD') + ' 00:00:00.000' : '2050-01-01 00:00:00.000'
+
+        let copy = JSON.parse(JSON.stringify(item))
+        copy.key = copy.key + '1'
+        copy.value = _endval
+        
+        item.value = _startval
+
+        options.push(item)
+        options.push(copy)
+      } else if (item.type === 'multiselect') {
+        item.value = item.value ? item.value.join(',') : item.value
+
+        options.push(item)
+      } else if (item.type === 'text') {
+        item.key.split(',').forEach(field => { // 缁煎悎鎼滅储锛屾墍瀛楁鎷兼帴
+          let cell = JSON.parse(JSON.stringify(item))
+          cell.key = field
+
+          options.push(item)
+        })
+      } else {
+        options.push(item)
+      }
+    })
+
+    return options
+  }
+
+  /**
    * @description 鎷兼帴鎼滅储鏉′欢datamanage
    * @param {Array}   searches     鎼滅储鏉′欢
    * @return {String}  searchText  鎷兼帴缁撴灉
@@ -395,9 +551,9 @@
 
     let baseurl = ''
     if (process.env.NODE_ENV === 'production') {
-      baseurl = document.location.origin + '/' + service
+      baseurl = document.location.origin + '/' + window.GLOB.service
     } else {
-      baseurl = 'http://qingqiumarket.cn/' + service
+      baseurl = window.GLOB.location + window.GLOB.service
     }
     // if (!/Content\/images\/upload\//.test(url)) {
     //   baseurl = baseurl + 'Content/images/upload/'
@@ -418,7 +574,7 @@
     if (options.cloudServiceApi) {
       baseurl = options.cloudServiceApi.replace(/webapi(.*)$/, '')
     } else {
-      baseurl = document.location.origin + '/' + service
+      baseurl = document.location.origin + '/' + window.GLOB.service
     }
 
     return url.match(/^http/) || url.match(/^\/\//) ? url : baseurl + url
@@ -700,25 +856,26 @@
     let _prevCustomScript = '' // 榛樿sql鍓嶆墽琛岃剼鏈�
     let _backCustomScript = '' // 榛樿sql鍚庢墽琛岃剼鏈�
 
-    if (verify.scripts) {
-      verify.scripts.forEach(item => {
-        if (item.status === 'false') return
+    verify.scripts && verify.scripts.forEach(item => {
+      if (item.status === 'false') return
 
-        if (item.position === 'init') {
-          _initCustomScript += `
-          ${item.sql}
-          `
-        } else if (item.position === 'front') {
-          _prevCustomScript += `
-          ${item.sql}
-          `
-        } else {
-          _backCustomScript += `
-          ${item.sql}
-          `
-        }
-      })
-    }
+      if (item.position === 'init') {
+        _initCustomScript += `
+        /* 鑷畾涔夎剼鏈� */
+        ${item.sql}
+        `
+      } else if (item.position === 'front') {
+        _prevCustomScript += `
+        /* 鑷畾涔夎剼鏈� */
+        ${item.sql}
+        `
+      } else {
+        _backCustomScript += `
+        /* 鑷畾涔夎剼鏈� */
+        ${item.sql}
+        `
+      }
+    })
 
     // 闇�瑕佸0鏄庣殑鍙橀噺闆�
     // let _vars = ['tbid', 'ErrorCode', 'retmsg', 'BillCode', 'BVoucher', 'FIBVoucherDate', 'FiYear', 'UserName', 'FullName', 'ID', 'BID', 'LoginUID', 'SessionUid', 'UserID', 'Appkey']
@@ -728,7 +885,8 @@
     let primaryKey = setting.primaryKey || 'id'
 
     // 绯荤粺鍙橀噺澹版槑涓庤缃垵濮嬪��
-    let _sql = `Declare @tbid nvarchar(50),@ErrorCode nvarchar(50),@retmsg nvarchar(4000),@BillCode nvarchar(50),@BVoucher nvarchar(50),@FIBVoucherDate nvarchar(50), @FiYear nvarchar(50), @UserName nvarchar(50),@FullName nvarchar(50),@ModularDetailCode nvarchar(50)
+    let _sql = `/* 绯荤粺鐢熸垚 */
+        Declare @tbid nvarchar(50),@ErrorCode nvarchar(50),@retmsg nvarchar(4000),@BillCode nvarchar(50),@BVoucher nvarchar(50),@FIBVoucherDate nvarchar(50), @FiYear nvarchar(50), @UserName nvarchar(50),@FullName nvarchar(50),@ModularDetailCode nvarchar(50)
       `
 
     // let _initvars = ['ID', 'BID', 'LoginUID', 'SessionUid', 'UserID', 'Appkey'] // 宸茶祴鍊煎瓧娈甸泦
@@ -745,8 +903,15 @@
         if (!_initvars.includes(_key)) {
           _initvars.push(_key)
 
-          if (form.type === 'number' && typeof(form.value) === 'number') {
-            _initfields.push(`@${_key}=${form.value}`)
+          if (form.type === 'number') {
+            let val = form.value
+            if (typeof(val) !== 'number') {
+              val = parseFloat(val)
+              if (isNaN(val)) {
+                val = 0
+              }
+            }
+            _initfields.push(`@${_key}=${val}`)
           } else {
             _initfields.push(`@${_key}='${form.value}'`)
           }
@@ -772,8 +937,8 @@
       })
     }
 
-    // 娣诲姞鏁版嵁涓瓧娈碉紝琛ㄥ崟鍊间紭鍏�(鎸夐挳涓嶉�夎鏃惰烦杩�)
-    if (data && btn.Ot !== 'notRequired') {
+    // 娣诲姞鏁版嵁涓瓧娈碉紝琛ㄥ崟鍊间紭鍏�(鎸夐挳涓嶉�夎鎴栧琛屾嫾鎺ユ椂璺宠繃)
+    if (data && btn.Ot !== 'notRequired' && btn.Ot !== 'requiredOnce') {
       _formFieldValue = {...data, ..._formFieldValue}
 
       if (logcolumns && logcolumns.length > 0) {
@@ -785,7 +950,13 @@
 
             let _val = data.hasOwnProperty(col.field) ? data[col.field] : ''
 
-            if (col.type === 'number' && typeof(_val) === 'number') {
+            if (col.type === 'number') {
+              if (typeof(_val) !== 'number') {
+                _val = parseFloat(_val)
+                if (isNaN(_val)) {
+                  _val = 0
+                }
+              }
               _initfields.push(`@${_key}=${_val}`)
             } else {
               _initfields.push(`@${_key}='${_val}'`)
@@ -817,8 +988,9 @@
     // 鍙橀噺澹版槑
     _declarefields = _declarefields.join(',')
     if (_declarefields) {
-      _sql += `,${_declarefields}
-        `
+      _sql = `/* 绯荤粺鐢熸垚 */
+        Declare @tbid nvarchar(50),@ErrorCode nvarchar(50),@retmsg nvarchar(4000),@BillCode nvarchar(50),@BVoucher nvarchar(50),@FIBVoucherDate nvarchar(50), @FiYear nvarchar(50), @UserName nvarchar(50),@FullName nvarchar(50),@ModularDetailCode nvarchar(50),${_declarefields}
+      `
     }
 
     // 鍙橀噺璧嬪��
@@ -851,8 +1023,10 @@
     }
 
     // 鍒濆鍖栧嚟璇佸強鐢ㄦ埛淇℃伅瀛楁
-    _sql += `select @BVoucher='',@FIBVoucherDate='',@FiYear='',@ErrorCode='',@retmsg='',@UserName='${userName}', @FullName='${fullName}'
-      `
+    _sql += `
+        /* 鍑瘉鍙婄敤鎴蜂俊鎭垵濮嬪寲璧嬪�� */
+        select @BVoucher='',@FIBVoucherDate='',@FiYear='',@ErrorCode='',@retmsg='',@UserName='${userName}', @FullName='${fullName}'
+        `
 
     if (_initCustomScript) {
       _sql += _initCustomScript
@@ -860,7 +1034,9 @@
 
     // 鍚敤璐︽湡楠岃瘉
     if (verify.accountdate === 'true') {
-      _sql += `exec s_FIBVoucherDateCheck @ErrorCode=@ErrorCode OUTPUT,@retmsg=@retmsg OUTPUT
+      _sql += `
+        /* 璐︽湡楠岃瘉 */
+        exec s_FIBVoucherDateCheck @ErrorCode=@ErrorCode OUTPUT,@retmsg=@retmsg OUTPUT
         if @ErrorCode!=''
           GOTO aaa
         `
@@ -873,8 +1049,15 @@
         datasource = '(' + datasource + ') tb'
       }
 
-      _sql += `select @tbid='', @ErrorCode='',@retmsg=''
-        select @tbid=${primaryKey} from ${datasource} where ${primaryKey} ='${primaryId}'
+      let _ID = '=@ID@'
+      if (btn.Ot === 'requiredOnce') {
+        _ID = ' in (select ID from  dbo.SplitComma(@ID@))'
+      }
+
+      _sql += `
+        /* 澶辨晥楠岃瘉 */
+        select @tbid='', @ErrorCode='',@retmsg=''
+        select @tbid=${primaryKey} from ${datasource} where ${primaryKey} ${_ID}
         If @tbid=''
         Begin
           select @ErrorCode='E',@retmsg='鏁版嵁宸插け鏁�'
@@ -886,17 +1069,19 @@
     // 姣旇緝楠岃瘉
     if (verify.contrasts && verify.contrasts.length > 0) {
       verify.contrasts.forEach(item => {
-        _sql += `If ${item.frontfield} ${item.operator} ${item.backfield}
-          Begin
-            select @ErrorCode='${item.errorCode}',@retmsg='${item.errmsg}'
-              goto aaa
-          end
-          `
+        _sql += `
+        /* 姣旇緝楠岃瘉 */
+        If ${item.frontfield} ${item.operator} ${item.backfield}
+        Begin
+          select @ErrorCode='${item.errorCode}',@retmsg='${item.errmsg}'
+            goto aaa
+        end
+        `
       })
     }
 
-    // 鍞竴鎬ч獙璇侊紝蹇呴』瀛樺湪琛ㄥ崟锛堣〃鍗曞瓨鍦ㄦ椂锛屼富閿潎涓哄崟鍊硷級,蹇呴』濉啓鏁版嵁婧�
-    if (formdata && verify.uniques && verify.uniques.length > 0) {
+    // 鍞竴鎬ч獙璇侊紝蹇呴』瀛樺湪琛ㄥ崟锛堣〃鍗曞瓨鍦ㄦ椂锛屼富閿潎涓哄崟鍊硷級,蹇呴』濉啓鏁版嵁婧愶紝澶氳鎷兼帴鏃朵笉鍙敤
+    if (formdata && verify.uniques && verify.uniques.length > 0 && btn.Ot !== 'requiredOnce') {
       let hasBid = false // 妫�楠岃〃鍗曞強鍒楀瓧娈典腑鏄惁鏈塨id
       let _keys_ = Object.keys(_formFieldValue).map(key => key.toLowerCase())
       if (_keys_.includes('bid')) {
@@ -911,9 +1096,7 @@
 
         item.field.split(',').forEach((_field, index) => {
           let _fval = `'${_formFieldValue[_field]}'`
-          // if (['id', 'bid', 'loginuid', 'sessionuid', 'userid', 'appkey'].includes(_field.toLowerCase())) {
-          //   _fval = '@' + _field + '@'
-          // }
+
           if (_field.toLowerCase() === 'bid' && !hasBid) { // 琛ㄥ崟涓病鏈塨id鍒欎娇鐢ㄧ郴缁焍id鍙橀噺
             _fval = '@BID@'
           }
@@ -932,30 +1115,54 @@
           _fieldValue.push(`${primaryKey} !='${primaryId}'`)
         }
 
-        _sql += `select @tbid='', @ErrorCode='',@retmsg=''
-          select @tbid='X' from ${btn.sql} where ${_fieldValue.join(' and ')}${_verifyType}
-          If @tbid!=''
-          Begin
-            select @ErrorCode='${item.errorCode}',@retmsg='${_value.join(', ')} 宸插瓨鍦�'
-            goto aaa
-          end
-          `
+        _sql += `
+        /* 鍞竴鎬ч獙璇� */
+        select @tbid='', @ErrorCode='',@retmsg=''
+        select @tbid='X' from ${btn.sql} where ${_fieldValue.join(' and ')}${_verifyType}
+        If @tbid!=''
+        Begin
+          select @ErrorCode='${item.errorCode}',@retmsg='${_value.join(', ')} 宸插瓨鍦�'
+          goto aaa
+        end
+        `
+      })
+    } else if (verify.uniques && verify.uniques.length > 0 && btn.Ot === 'requiredOnce' && setting.dataresource) {
+      let datasource = setting.dataresource
+      if (/\s/.test(datasource)) { // 鎷兼帴鍒悕
+        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
+        
+        If @tbid!=''
+        Begin
+          Set @ErrorCode='E' Set @retmsg='${item.fieldlabel} 鍊间笉鍞竴'
+          goto aaa
+        end
+        `
       })
     }
     
     // 鑷畾涔夐獙璇�
-    if (verify.customverifys && verify.customverifys.length > 0) {
-      verify.customverifys.forEach(item => {        
-        _sql += `select @tbid='', @ErrorCode='',@retmsg=''
-          select top 1 @tbid='X' from (${item.sql}) a
-          If @tbid ${item.result === 'true' ? '!=' : '='}''
-          Begin
-            select @ErrorCode='${item.errorCode}',@retmsg='${item.errmsg}'
-            goto aaa
-          end
-          `
-      })
-    }
+    verify.customverifys && verify.customverifys.forEach(item => {        
+      _sql += `
+        /* 鑷畾涔夐獙璇� */
+        select @tbid='', @ErrorCode='',@retmsg=''
+        select top 1 @tbid='X' from (${item.sql}) a
+        If @tbid ${item.result === 'true' ? '!=' : '='}''
+        Begin
+          select @ErrorCode='${item.errorCode}',@retmsg='${item.errmsg}'
+          goto aaa
+        end
+        `
+    })
 
     // 鍗曞彿鐢熸垚锛屼娇鐢ㄤ笂绾d锛圔ID锛夋垨鍒楄〃鏁版嵁锛屽0鏄庡彉閲忥紙妫�楠岋級
     let _billcodesSql  = ''
@@ -990,21 +1197,23 @@
           _vars.push(_key)
         }
 
-        _billcodesSql += `${_declare}
-          select @BillCode='', @${_key}='', @ModularDetailCode=''
-          ${_lpline}
-          exec s_get_BillCode
-            @ModularDetailCode=${_ModularDetailCode},
-            @Type=${item.Type},
-            @TypeCharOne='${item.TypeCharOne}',
-            @TypeCharTwo ='${item.TypeCharTwo}',
-            @BillCode =@BillCode output,
-            @ErrorCode =@ErrorCode output, 
-            @retmsg=@retmsg output
-          if @ErrorCode!=''
-            goto aaa
-          set @${_key}=@BillCode
-          `
+        _billcodesSql += `
+        /* 鍗曞彿鐢熸垚 */
+        ${_declare}
+        select @BillCode='', @${_key}='', @ModularDetailCode=''
+        ${_lpline}
+        exec s_get_BillCode
+          @ModularDetailCode=${_ModularDetailCode},
+          @Type=${item.Type},
+          @TypeCharOne='${item.TypeCharOne}',
+          @TypeCharTwo ='${item.TypeCharTwo}',
+          @BillCode =@BillCode output,
+          @ErrorCode =@ErrorCode output, 
+          @retmsg=@retmsg output
+        if @ErrorCode!=''
+          goto aaa
+        set @${_key}=@BillCode
+        `
       })
 
       if (_actionType !== 'insertOrUpdate') {
@@ -1020,7 +1229,9 @@
 
       hasvoucher = true
 
-      _sql += `exec s_BVoucher_Create
+      _sql += `
+        /* 鍒涘缓鍑瘉 */
+        exec s_BVoucher_Create
           @Bill ='${data[_voucher.linkField]}',
           @BVoucherType ='${_voucher.BVoucherType}',
           @VoucherTypeOne ='${_voucher.VoucherTypeOne}',
@@ -1038,8 +1249,6 @@
           GOTO aaa
         `
     }
-
-    let primaryKeyName = ['id', 'bid', 'loginuid', 'sessionuid', 'userid', 'appkey'].includes(primaryKey.toLowerCase()) ? primaryKey + '@' : primaryKey
 
     let _insertsql = ''
     if (_actionType === 'insert' || _actionType === 'insertOrUpdate') { // 娣诲姞璇彞
@@ -1085,7 +1294,7 @@
     }
 
     let _updatesql = ''
-    if (_actionType === 'update' || _actionType === 'insertOrUpdate') { // 淇敼璇彞
+    if (_actionType === 'update' || _actionType === 'audit' || _actionType === 'insertOrUpdate') { // 淇敼璇彞
       let _form = []
       let _arr = []
 
@@ -1094,12 +1303,22 @@
         _form.push(item.key + '=@' + item.key)
       })
 
-      if (!_arr.includes('modifydate')) {
-        _form.push('modifydate=getdate()')
+      if (_actionType === 'audit') {
+        if (!_arr.includes('submitdate')) {
+          _form.push('submitdate=getdate()')
+        }
+        if (!_arr.includes('submituserid')) {
+          _form.push('submituserid=@userid@')
+        }
+      } else {
+        if (!_arr.includes('modifydate')) {
+          _form.push('modifydate=getdate()')
+        }
+        if (!_arr.includes('modifyuserid')) {
+          _form.push('modifyuserid=@userid@')
+        }
       }
-      if (!_arr.includes('modifyuserid')) {
-        _form.push('modifyuserid=@userid@')
-      }
+      
       if (hasvoucher) {
         if (!_arr.includes('bvoucher')) {
           _form.push('BVoucher=@BVoucher')
@@ -1112,7 +1331,13 @@
         }
       }
       _form = _form.join(',')
-      _updatesql = `update ${btn.sql} set ${_form} where ${primaryKey}=@${primaryKeyName};`
+
+      let _ID = '=@ID@'
+      if (btn.Ot === 'requiredOnce') {
+        _ID = ' in (select ID from  dbo.SplitComma(@ID@))'
+      }
+
+      _updatesql = `update ${btn.sql} set ${_form} where ${primaryKey}${_ID};`
     }
 
     if (_prevCustomScript) {
@@ -1121,11 +1346,22 @@
 
     // 娣诲姞銆佷慨鏀广�侀�昏緫鍒犻櫎銆佺墿鐞嗗垹闄�
     if (_actionType === 'insert') {
-      _sql += _insertsql
-    } else if (_actionType === 'update') {
-      _sql += _updatesql
+      _sql += `
+        /* 榛樿sql */
+        ${_insertsql}`
+    } else if (_actionType === 'update' || _actionType === 'audit') {
+      _sql += `
+        /* 榛樿sql */
+        ${_updatesql}`
     } else if (_actionType === 'LogicDelete') { // 閫昏緫鍒犻櫎
-      _sql += `update ${btn.sql} set deleted=1,modifydate=getdate(),modifyuserid=@userid@ where ${primaryKey}=@${primaryKeyName};`
+      let _ID = '=@ID@'
+      if (btn.Ot === 'requiredOnce') {
+        _ID = ' in (select ID from  dbo.SplitComma(@ID@))'
+      }
+
+      _sql += `
+        /* 榛樿sql */
+        update ${btn.sql} set deleted=1,modifydate=getdate(),modifyuserid=@userid@ where ${primaryKey}${_ID};`
     
     } else if (_actionType === 'delete') {      // 鐗╃悊鍒犻櫎
       let _msg = ''
@@ -1138,9 +1374,19 @@
           }
         })
       }
-      _sql += `insert into snote (remark,createuserid,CreateUser,CreateStaff) select left('鍒犻櫎琛�:${btn.sql} 鏁版嵁: ${_msg}${primaryKey}='+@${primaryKeyName},200),@userid@,@username,@fullname delete ${btn.sql} where ${primaryKey}=@${primaryKeyName};`
+
+      let _ID = '=@ID@'
+      if (btn.Ot === 'requiredOnce') {
+        _ID = ' in (select ID from  dbo.SplitComma(@ID@))'
+      }
+
+      _sql += `
+        /* 榛樿sql */
+        insert into snote (remark,createuserid,CreateUser,CreateStaff) select left('鍒犻櫎琛�:${btn.sql} 鏁版嵁: ${_msg}${primaryKey}='+@ID@,200),@userid@,@username,@fullname delete ${btn.sql} where ${primaryKey}${_ID};`
     } else if (_actionType === 'insertOrUpdate') {
-      _sql += `select @tbid=''
+      _sql += `
+        /* 榛樿sql */
+        select @tbid=''
         select @tbid='X' from ${btn.sql} where ${primaryKey}=@ID@
         if @tbid=''
           begin
@@ -1159,7 +1405,9 @@
     }
 
     _sql += `
-      aaa: select @ErrorCode as ErrorCode,@retmsg as retmsg`
+        aaa: select @ErrorCode as ErrorCode,@retmsg as retmsg`
+
+    _sql = _sql.replace(/\n\s{8}/ig, '\n')
     console.log(_sql)
     return _sql
   }
@@ -1325,7 +1573,11 @@
 
     if (!_vars.includes(primaryKey.toLowerCase())) {
       _vars.push(primaryKey.toLowerCase())
-      formParam = `mchr13k@${primaryKey} nvarchar(50)='',`
+      let _type = '50'
+      if (btn.Ot === 'requiredOnce') { // 澶氳鎷兼帴鏃讹紝涓婚敭璁句负max
+        _type = 'max'
+      }
+      formParam = `mchr13k@${primaryKey} nvarchar(${_type})='',`
     }
 
     if (param.fields && param.fields.length > 0) {

--
Gitblit v1.8.0