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
|
* @return {Object} param
|
*/
|
static getQueryDataParams (setting, arrFields, search = [], orderBy = '', pageIndex = 1, pageSize = 10, BID, menuType, id) {
|
let param = null
|
|
if (setting.interType === 'system' || (setting.interType === 'custom' && setting.requestMode === 'system')) {
|
param = this.getDefaultQueryParam(setting, arrFields, search, orderBy, pageIndex, pageSize, menuType, id)
|
} else {
|
param = this.getCustomQueryParam(setting, search, orderBy, pageIndex, pageSize, menuType, id)
|
}
|
|
if (BID) {
|
param.BID = BID
|
}
|
// 数据管理权限
|
if (sessionStorage.getItem('dataM') === 'true') {
|
param.dataM = 'Y'
|
}
|
|
return param
|
}
|
|
/**
|
* @description 获取用户自定义存储过程传参
|
*/
|
static getCustomQueryParam (setting, search, orderBy, pageIndex, pageSize, menuType, id) {
|
let param = Utils.formatCustomMainSearch(search)
|
|
if (orderBy) {
|
param.OrderCol = orderBy
|
}
|
|
if (id) {
|
param.ID = id
|
} else if (setting.laypage) {
|
param.PageIndex = pageIndex
|
param.PageSize = pageSize
|
}
|
|
if (setting.interType === 'inner' || (setting.interType === 'custom' && setting.requestMode === '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, id) {
|
let param = {
|
func: 'sPC_Get_TableData',
|
obj_name: 'data',
|
exec_type: 'y',
|
arr_field: arrFields,
|
default_sql: setting.execute ? 'true' : 'false'
|
}
|
|
let userName = sessionStorage.getItem('User_Name') || ''
|
let fullName = sessionStorage.getItem('Full_Name') || ''
|
|
if (sessionStorage.getItem('isEditState') === 'true') {
|
userName = sessionStorage.getItem('CloudUserName') || ''
|
fullName = sessionStorage.getItem('CloudFullName') || ''
|
}
|
|
let _dataresource = setting.dataresource
|
let _customScript = ''
|
|
if (setting.customScript) {
|
_customScript = `declare @ErrorCode nvarchar(50),@retmsg nvarchar(4000),@UserName nvarchar(50),@FullName nvarchar(50)
|
Select @ErrorCode='',@retmsg ='',@UserName='${userName}', @FullName='${fullName}'
|
${setting.customScript}
|
`
|
}
|
|
let regoptions = null
|
if (setting.queryType === 'statistics' || _customScript) {
|
let allSearch = Utils.getAllSearchOptions(search)
|
let userName = sessionStorage.getItem('User_Name') || ''
|
let fullName = sessionStorage.getItem('Full_Name') || ''
|
|
if (sessionStorage.getItem('isEditState') === 'true') {
|
userName = sessionStorage.getItem('CloudUserName') || ''
|
fullName = sessionStorage.getItem('CloudFullName') || ''
|
}
|
|
regoptions = allSearch.map(item => {
|
return {
|
reg: new RegExp('@' + item.key + '@', 'ig'),
|
value: `'${item.value}'`
|
}
|
})
|
regoptions.push({
|
reg: new RegExp('@userName@', 'ig'),
|
value: userName
|
}, {
|
reg: new RegExp('@fullName@', 'ig'),
|
value: fullName
|
}, {
|
reg: new RegExp('@orderBy@', 'ig'),
|
value: orderBy
|
}, {
|
reg: new RegExp('@pageSize@', 'ig'),
|
value: setting.laypage ? pageSize : '9999'
|
}, {
|
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 && !id) {
|
_search = Utils.joinMainSearchkey(search)
|
if (_search) {
|
_search = 'where ' + _search
|
}
|
} else if (_dataresource && id) {
|
_search = Utils.joinMainSearchkey(search)
|
_search = `where ${_search ? _search + ' AND ' : ''} ${setting.primaryKey || 'ID'}='${id}'`
|
}
|
|
if (_customScript) {
|
regoptions.forEach(item => {
|
_customScript = _customScript.replace(item.reg, item.value)
|
})
|
}
|
|
let LText = ''
|
let DateCount = ''
|
|
if (_dataresource && setting.laypage && orderBy && !id) {
|
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.info(`${LText ? '' : '/*不执行默认sql*/\n'}${_customScript}`)
|
LText && console.info(LText)
|
}
|
|
param.custom_script = Utils.formatOptions(_customScript)
|
param.LText = Utils.formatOptions(LText)
|
param.DateCount = Utils.formatOptions(DateCount)
|
|
param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss')
|
param.secretkey = Utils.encrypt('', param.timestamp)
|
|
if (menuType === 'HS') { // 云端数据验证
|
param.open_key = Utils.encryptOpenKey(param.secretkey, param.timestamp)
|
}
|
|
return param
|
}
|
|
/**
|
* @description 获取系统存储过程 sPC_Get_TableData 合计值的参数
|
*/
|
static getStatQueryDataParams (setting, statFields, search, orderBy, BID, menuType) {
|
let param = {
|
func: 'sPC_Get_TableData',
|
obj_name: 'data',
|
exec_type: 'y',
|
arr_field: statFields.map(col => col.field).join(','),
|
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)
|
let userName = sessionStorage.getItem('User_Name') || ''
|
let fullName = sessionStorage.getItem('Full_Name') || ''
|
|
if (sessionStorage.getItem('isEditState') === 'true') {
|
userName = sessionStorage.getItem('CloudUserName') || ''
|
fullName = sessionStorage.getItem('CloudFullName') || ''
|
}
|
|
regoptions = allSearch.map(item => {
|
return {
|
reg: new RegExp('@' + item.key + '@', 'ig'),
|
value: `'${item.value}'`
|
}
|
})
|
regoptions.push({
|
reg: new RegExp('@userName@', 'ig'),
|
value: userName
|
}, {
|
reg: new RegExp('@fullName@', 'ig'),
|
value: fullName
|
}, {
|
reg: new RegExp('@orderBy@', 'ig'),
|
value: orderBy
|
}, {
|
reg: new RegExp('@pageSize@', 'ig'),
|
value: 999999
|
}, {
|
reg: new RegExp('@pageIndex@', 'ig'),
|
value: 1
|
})
|
}
|
|
let _search = Utils.joinMainSearchkey(search)
|
if (_search) {
|
_search = 'where ' + _search
|
}
|
|
if (setting.queryType === 'statistics') { // 统计数据源,内容替换
|
regoptions.forEach(item => {
|
_dataresource = _dataresource.replace(item.reg, item.value)
|
})
|
}
|
|
if (_customScript) {
|
regoptions.forEach(item => {
|
_customScript = _customScript.replace(item.reg, item.value)
|
})
|
}
|
|
let LText = ` select ${statFields.map(col => `isnull(sum(${col.field}),0) as ${col.field}`).join(',')} from ${_dataresource} ${_search} `
|
|
if (_customScript) {
|
LText = `${LText}
|
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.info(`${LText ? '' : '/*不执行默认sql*/\n'}${_customScript}`)
|
LText && console.info(LText)
|
}
|
|
param.custom_script = Utils.formatOptions(_customScript)
|
param.LText = Utils.formatOptions(LText)
|
param.DateCount = ''
|
|
param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss')
|
param.secretkey = Utils.encrypt('', param.timestamp)
|
|
if (menuType === 'HS') { // 云端数据验证
|
param.open_key = Utils.encryptOpenKey(param.secretkey, param.timestamp)
|
}
|
|
if (BID) {
|
param.BID = BID
|
}
|
|
// 数据管理权限
|
if (sessionStorage.getItem('dataM') === 'true') {
|
param.dataM = 'Y'
|
}
|
|
return param
|
}
|
|
/**
|
* @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
|
* @return {Object} param
|
*/
|
static getPrevQueryParams (setting, search = [], BID, menuType) {
|
let param = null
|
|
if (setting.procMode !== 'inner') {
|
param = this.getDefaultPrevQueryParam(setting, search, menuType)
|
} else {
|
param = Utils.formatCustomMainSearch(search)
|
param.func = setting.prevFunc || ''
|
}
|
|
if (BID) {
|
param.BID = BID
|
}
|
|
return param
|
}
|
|
/**
|
* @description 获取系统存储过程 sPC_Get_TableData 的参数
|
*/
|
static getDefaultPrevQueryParam (setting, search, menuType) {
|
let param = {
|
func: 'sPC_TableData_InUpDe',
|
exec_type: 'y',
|
script_type: 'Y'
|
}
|
|
let sql = ''
|
let userName = sessionStorage.getItem('User_Name') || ''
|
let fullName = sessionStorage.getItem('Full_Name') || ''
|
|
if (sessionStorage.getItem('isEditState') === 'true') {
|
userName = sessionStorage.getItem('CloudUserName') || ''
|
fullName = sessionStorage.getItem('CloudFullName') || ''
|
}
|
|
setting.preScripts.forEach(item => {
|
if (item.status === 'false') return
|
sql += `${item.sql}
|
`
|
})
|
|
if (sql) {
|
sql = `/*前置脚本*/
|
declare @ErrorCode nvarchar(50),@retmsg nvarchar(4000),@UserName nvarchar(50),@FullName nvarchar(50)
|
Select @ErrorCode='',@retmsg ='',@UserName='${userName}', @FullName='${fullName}'
|
${sql}
|
aaa:
|
if @ErrorCode!=''
|
insert into tmp_err_retmsg (ID, ErrorCode, retmsg, CreateUserID) select @time_id@,@ErrorCode, @retmsg,@UserID@
|
`
|
|
let allSearch = Utils.getAllSearchOptions(search)
|
let regoptions = allSearch.map(item => {
|
return {
|
reg: new RegExp('@' + item.key + '@', 'ig'),
|
value: `'${item.value}'`
|
}
|
})
|
regoptions.push({
|
reg: new RegExp('@userName@', 'ig'),
|
value: userName
|
}, {
|
reg: new RegExp('@fullName@', 'ig'),
|
value: fullName
|
})
|
|
regoptions.forEach(item => {
|
sql = sql.replace(item.reg, item.value)
|
})
|
|
// 测试系统打印查询语句
|
if ((options.sysType === 'local' && !window.GLOB.systemType) || window.debugger === true) {
|
console.info(sql.replace(/\n\s{8}/ig, '\n'))
|
}
|
}
|
|
if (sessionStorage.getItem('dataM') === 'true') { // 数据权限
|
sql = sql.replace(/\$@/ig, '/*')
|
sql = sql.replace(/@\$/ig, '*/')
|
} else {
|
sql = sql.replace(/@\$|\$@/ig, '')
|
}
|
|
param.LText = Utils.formatOptions(sql)
|
param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss')
|
param.secretkey = Utils.encrypt('', param.timestamp)
|
|
if (menuType === 'HS') { // 函数 sPC_TableData_InUpDe 云端验证
|
param.open_key = Utils.encryptOpenKey(param.secretkey, param.timestamp)
|
}
|
|
param.menuname = setting.MenuName || ''
|
|
return param
|
}
|
|
/**
|
* @description 获取系统存储过程 sPC_Get_TableData 的参数
|
*/
|
static getCallBackQueryParams (setting, sql, errSql) {
|
let param = {
|
func: 'sPC_TableData_InUpDe',
|
exec_type: 'y',
|
}
|
let _prevCustomScript = `declare @ErrorCode nvarchar(50),@retmsg nvarchar(4000)
|
Select @ErrorCode='',@retmsg=''
|
${errSql}
|
`
|
let _backCustomScript = `
|
`
|
|
setting.cbScripts.forEach(script => {
|
if (script.status === 'false') return
|
|
if (script.position === 'front') {
|
_prevCustomScript += `
|
/* 自定义脚本 */
|
${script.sql}
|
`
|
} else {
|
_backCustomScript += `
|
/* 自定义脚本 */
|
${script.sql}
|
`
|
}
|
})
|
|
_backCustomScript += `
|
aaa: select @ErrorCode as ErrorCode,@retmsg as retmsg`
|
|
sql = _prevCustomScript + sql
|
sql = sql + _backCustomScript
|
|
if ((window.GLOB.systemType !== 'production' && options.sysType !== 'cloud') || window.debugger === true) {
|
console.info(sql.replace(/\n\s{8}/ig, '\n'))
|
}
|
|
param.LText = sql
|
|
if (sessionStorage.getItem('dataM') === 'true') { // 数据权限
|
param.LText = param.LText.replace(/\$@/ig, '/*')
|
param.LText = param.LText.replace(/@\$/ig, '*/')
|
} else {
|
param.LText = param.LText.replace(/@\$|\$@/ig, '')
|
}
|
|
param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss')
|
param.secretkey = Utils.encrypt('', param.timestamp)
|
param.LText = Utils.formatOptions(param.LText)
|
param.menuname = setting.MenuName || ''
|
|
return param
|
}
|
}
|