king
2020-09-14 76427d51a079a5fd1f45bf7188249e7a4647ae05
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
import moment from 'moment'
import options from '@/store/options.js'
import Utils from './utils.js'
 
export default class DataUtils {
  /**
   * @description 数据源名称,用于统一查询
   * @param {Object}   setting      数据源设置
   * @param {String}   arrFields    查询字段
   * @param {Array}    search       搜索条件
   * @param {String}   orderBy      排序方式
   * @param {Number}   pageIndex    页码
   * @param {Number}   pageSize     每页数量
   * @param {String}   BID          上级ID
   * @param {String}   menuType     菜单类型,普通菜单与HS
   * @param {Boolean}  dataManager  数据权限
   * @return {Object}  param
   */
  static getQueryDataParams (setting, arrFields, search = [], orderBy = '', pageIndex = 1, pageSize = 10, BID, menuType, dataManager) {
    let param = null
 
    if (setting.interType === 'system' || (setting.interType === 'inner' && !setting.innerFunc)) {
      param = this.getDefaultQueryParam(setting, arrFields, search, orderBy, pageIndex, pageSize, menuType)
    } else {
      param = this.getCustomQueryParam(setting, search, orderBy, pageIndex, pageSize, menuType)
    }
 
    if (BID) {
      param.BID = BID
    }
    // 数据管理权限
    if (dataManager) {
      param.dataM = 'Y'
    }
 
    return param
  }
 
  /**
   * @description 获取用户自定义存储过程传参
   */
  static getCustomQueryParam (setting, search, orderBy, pageIndex, pageSize, menuType) {
    let param = Utils.formatCustomMainSearch(search)
 
    if (orderBy) {
      param.OrderCol = orderBy
    }
 
    if (setting.laypage) {
      param.PageIndex = pageIndex
      param.PageSize = pageSize
    }
 
    if (setting.interType === 'inner') {
      param.func = setting.innerFunc
    } else {
      if (menuType === 'HS') {
        if (setting.sysInterface === 'true' && options.cloudServiceApi) {
          param.rduri = options.cloudServiceApi
        } else if (setting.sysInterface !== 'true') {
          param.rduri = setting.interface
        }
      } else {
        if (setting.sysInterface === 'true' && window.GLOB.mainSystemApi) {
          param.rduri = window.GLOB.mainSystemApi
        } else if (setting.sysInterface !== 'true') {
          param.rduri = setting.interface
        }
      }
 
      if (setting.outerFunc) {
        param.func = setting.outerFunc
      }
    }
 
    return param
  }
 
  /**
   * @description 获取系统存储过程 sPC_Get_TableData 的参数
   */
  static getDefaultQueryParam (setting, arrFields, search, orderBy, pageIndex, pageSize, menuType) {
    let param = {
      func: 'sPC_Get_TableData',
      obj_name: 'data',
      arr_field: arrFields,
      default_sql: setting.execute ? 'true' : 'false'
    }
    
    let _dataresource = setting.dataresource
    let _customScript = ''
    
    if (setting.customScript) {
      _customScript = `declare @ErrorCode nvarchar(50),@retmsg nvarchar(4000) select @ErrorCode='',@retmsg =''
        ${setting.customScript}
      `
    }
 
    let regoptions = null
    if (setting.queryType === 'statistics' || _customScript) {
      let allSearch = Utils.getAllSearchOptions(search)
 
      regoptions = allSearch.map(item => {
        return {
          reg: new RegExp('@' + item.key + '@', 'ig'),
          value: `'${item.value}'`
        }
      })
      regoptions.push({
        reg: new RegExp('@orderBy@', 'ig'),
        value: orderBy
      }, {
        reg: new RegExp('@pageSize@', 'ig'),
        value: pageSize
      }, {
        reg: new RegExp('@pageIndex@', 'ig'),
        value: pageIndex
      })
    }
 
    let _search = ''
    
    if (setting.queryType === 'statistics' && _dataresource) { // 统计数据源,内容替换
      regoptions.forEach(item => {
        _dataresource = _dataresource.replace(item.reg, item.value)
      })
    } else if (_dataresource) {
      _search = Utils.joinMainSearchkey(search)
      if (_search) {
        _search = 'where ' + _search
      }
    }
 
    if (_customScript) {
      regoptions.forEach(item => {
        _customScript = _customScript.replace(item.reg, item.value)
      })
    }
 
    let LText = ''
    let DateCount = ''
 
    if (_dataresource && setting.laypage && orderBy) {
      LText = ` select top ${pageSize} ${arrFields} from (select ${arrFields} ,ROW_NUMBER() over(order by ${orderBy}) as rows from ${_dataresource} ${_search}) tmptable where rows > ${pageSize * (pageIndex - 1)} order by tmptable.rows `
      DateCount = `select count(1) as total from ${_dataresource} ${_search}`
    } else if (_dataresource && orderBy) {
      LText = ` select ${arrFields} from (select ${arrFields} ,ROW_NUMBER() over(order by ${orderBy}) as rows from ${_dataresource} ${_search}) tmptable order by tmptable.rows `
    } else if (_dataresource) {
      LText = ` select ${arrFields} from ${_dataresource} ${_search}  `
    }
 
    if (_customScript) {
      if (LText) {
        LText = `${LText}
          aaa:
          if @ErrorCode!=''
            insert into tmp_err_retmsg (ID, ErrorCode, retmsg, CreateUserID) select @time_id@,@ErrorCode, @retmsg,@UserID@ 
        `
      } else {
        _customScript = `${_customScript}
          aaa:
          if @ErrorCode!=''
            insert into tmp_err_retmsg (ID, ErrorCode, retmsg, CreateUserID) select @time_id@,@ErrorCode, @retmsg,@UserID@
        `
      }
    }
 
    // 测试系统打印查询语句
    if ((options.sysType === 'local' && !window.GLOB.systemType) || window.debugger === true) {
      _customScript &&  console.log(`${LText ? '' : '/*不执行默认sql*/\n'}${_customScript}`)
      LText &&  console.log(LText)
    }
    
    param.custom_script = Utils.formatOptions(_customScript)
    param.LText = Utils.formatOptions(LText)
    param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss')
    param.secretkey = Utils.encrypt(param.LText, param.timestamp)
    param.DateCount = Utils.formatOptions(DateCount)
 
    if (menuType === 'HS') { // 云端数据验证
      param.open_key = Utils.encryptOpenKey(param.secretkey, param.timestamp)
    }
 
    return param
  }
}