king
2023-02-24 5d8eb51419d2c1b7dc88dd0cbde5c8341cceda2f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
 
import moment from 'moment'
 
export default class SettingUtils {
  /**
   * @description 生成页面查询语句
   */
  static getDebugSql (verify, scripts, searches, Utils, timestamp) {
    let sql = ''
    let _dataresource = verify.dataresource || ''
    let regoptions = this.getRegOptions(searches)
    let _search = this.formatSearch(searches)
    _search = Utils.joinMainSearchkey(_search)
 
    _search = _search.replace(/@\$@/ig, '')
    _search = _search ? 'where ' + _search : ''
 
    let arr_field = []
    verify.columns.forEach(item => {
      if (item.Column !== '$Index') {
        arr_field.push(item.Column)
      }
    })
    arr_field = arr_field.join(',')
 
    if (!arr_field) {
      arr_field = '*'
    }
 
    let _customScript = ''
    scripts && scripts.forEach(script => {
      if (script.status === 'false') return
      _customScript += `
      ${script.sql}
      `
    })
 
    if (_customScript) {
      _customScript = `declare @ErrorCode nvarchar(50),@retmsg nvarchar(4000),@UserName nvarchar(50),@FullName nvarchar(50),@RoleID nvarchar(512),@mk_departmentcode nvarchar(512),@mk_organization nvarchar(512),@mk_user_type nvarchar(20),@mk_nation nvarchar(50),@mk_province nvarchar(50),@mk_city nvarchar(50),@mk_district nvarchar(50),@mk_address nvarchar(100) select @ErrorCode='',@retmsg =''
        ${_customScript}
      `
    }
 
    if (verify.defaultSql === 'false') {
      _dataresource = ''
    }
    
    _dataresource = _dataresource.replace(/@(BID|ID|LoginUID|SessionUid|UserID|Appkey|time_id|typename)@/ig, `'${timestamp}'`)
    _customScript = _customScript.replace(/@(BID|ID|LoginUID|SessionUid|UserID|Appkey|time_id|typename)@/ig, `'${timestamp}'`)
    _dataresource = _dataresource.replace(/@\$|\$@/ig, '')
    _customScript = _customScript.replace(/@\$|\$@/ig, '')
 
    // 外联数据库替换
    if (window.GLOB.externalDatabase !== null) {
      _dataresource = _dataresource.replace(/@db@/ig, window.GLOB.externalDatabase)
      _customScript = _customScript.replace(/@db@/ig, window.GLOB.externalDatabase)
    }
    
    // 正则替换
    let _regoptions = regoptions.map(item => {
      return {
        reg: new RegExp('@' + item.key + '@', 'ig'),
        value: `'0'`,
        type: item.type || ''
      }
    })
 
    _regoptions.push({
      reg: new RegExp('@userName@', 'ig'),
      value: `''`
    }, {
      reg: new RegExp('@fullName@', 'ig'),
      value: `''`
    }, {
      reg: new RegExp('@orderBy@', 'ig'),
      value: verify.order
    }, {
      reg: new RegExp('@pageSize@', 'ig'),
      value: 10
    }, {
      reg: new RegExp('@pageIndex@', 'ig'),
      value: 1
    })
 
    if (verify.queryType === 'statistics' && _dataresource) {
      _regoptions.forEach(item => {
        _dataresource = _dataresource.replace(item.reg, item.value)
      })
      _search = ''
    } else if (_dataresource) {
      _regoptions.forEach(item => {
        if (item.type !== 'url') return
        _dataresource = _dataresource.replace(item.reg, item.value)
      })
    }
 
    if (_customScript) {
      _regoptions.forEach(item => {
        _customScript = _customScript.replace(item.reg, item.value)
      })
    }
 
    // 数据源处理, 存在显示列时 
    if (_dataresource) {
      if (/\s/.test(_dataresource)) {
        _dataresource = '(' + _dataresource + ') tb'
      }
 
      if (verify.order) {
        _dataresource = `/*system_query*/select ${arr_field} from (select ${arr_field} ,ROW_NUMBER() over(order by ${verify.order}) as rows from ${_dataresource} ${_search}) tmptable order by tmptable.rows`
      } else {
        _dataresource = `/*system_query*/select ${arr_field} from ${_dataresource} ${_search}`
      }
    }
 
    if (_customScript) {
      sql = `/* sql 验证 */
        ${_customScript}
        ${_dataresource}
        aaa:
        if @ErrorCode!=''
          insert into tmp_err_retmsg (ID, ErrorCode, retmsg, CreateUserID) select @time_id@,@ErrorCode, @retmsg,@UserID@
      `
    } else {
      sql = `/* sql 验证 */
        declare @ErrorCode nvarchar(50),@retmsg nvarchar(4000),@UserName nvarchar(50),@FullName nvarchar(50),@RoleID nvarchar(512),@mk_departmentcode nvarchar(512),@mk_organization nvarchar(512),@mk_user_type nvarchar(20),@mk_nation nvarchar(50),@mk_province nvarchar(50),@mk_city nvarchar(50),@mk_district nvarchar(50),@mk_address nvarchar(100) select @ErrorCode='',@retmsg =''
        ${_dataresource}`
    }
    sql = sql.replace(/\n\s{8}/ig, '\n')
    console.info(sql)
    sql = sql.replace(/\n/g, ' ')
 
    return sql
  }
 
  /**
   * @description 获取全部搜索条件
   * @param {Array} searches 搜索条件数组
   */
  static formatSearch (searches) {
    if (!searches || searches.length === 0) return []
 
    let newsearches = []
    searches.forEach(search => {
      if (!search.field) return
      
      let item = {
        key: search.field,
        match: search.match,
        type: search.type,
        label: search.label,
        value: search.initval,
        required: search.required === 'true'
      }
      
      if (item.type === 'group') {
        item.key = search.datefield
        item.type = 'daterange'
        item.match = 'between'
        item.value = [moment().format('YYYY-MM-DD'), moment().format('YYYY-MM-DD')].join(',')
 
        newsearches.push(item)
        return
      } else if (item.type === 'date') {
        item.value = moment().format('YYYY-MM-DD')
      } else if (item.type === 'datemonth') {
        item.value = moment().format('YYYY-MM')
      } else if (item.type === 'dateweek') {
        item.value = moment().format('YYYY-MM-DD')
      } else if (item.type === 'daterange') {
        item.value = [moment().format('YYYY-MM-DD'), moment().format('YYYY-MM-DD')].join(',')
      } else if (item.type === 'multiselect' || (item.type === 'checkcard' && search.multiple === 'true')) {
        item.type = 'multi'
        item.value = '@$@'
      } else {
        item.value = '@$@'
      }
      newsearches.push(item)
    })
    
    return newsearches
  }
  
  /**
   * @description 获取搜索用于正则替换
   * @param {Array}   searches     搜索条件
   * @return {String}  searchText  拼接结果
   */
  static getRegOptions (searches) {
    if (!searches || searches.length === 0) return []
 
    let options = []
    let fieldmap = new Map()
    searches.forEach(search => {
      let item = {
        key: search.field,
        value: '0'
      }
 
      if (fieldmap.has(item.key)) {
        item.key = item.key + '1'
      }
 
      fieldmap.set(item.key, true)
 
      if (search.type === 'group') {
        options.push({
          key: search.field,
          value: '0'
        })
        options.push({
          key: search.datefield,
          value: '0'
        })
        options.push({
          key: search.datefield + '1',
          value: '0'
        })
        options.push(item)
      } else if (['datemonth', 'dateweek', 'daterange'].includes(search.type)) {
        options.push(item)
        options.push({
          key: item.key + '1',
          value: '0'
        })
      } else if (search.type === 'text' || search.type === 'select') {
        item.key.split(',').forEach(field => {
          let cell = JSON.parse(JSON.stringify(item))
          cell.key = field
 
          options.push(cell)
        })
      } else {
        options.push(item)
      }
    })
 
    return options
  }
}