import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import { Button, notification } from 'antd'
|
import moment from 'moment'
|
|
import Utils from '@/utils/utils.js'
|
import Api from '@/api'
|
// import './index.scss'
|
|
class CreateFunc extends Component {
|
static propTypes = {
|
trigger: PropTypes.func
|
}
|
|
state = {
|
loading: false,
|
}
|
|
exec = (innerFunc, newLText, DelText) => {
|
this.setState({
|
loading: true
|
})
|
|
this.createExec(innerFunc, newLText, DelText)
|
}
|
|
createExec = (innerFunc, newLText, DelText) => {
|
let isExist = false // 存储过程是否存在
|
let cloudText = '' // 云端存储结果
|
let localfunc = '' // 本地存储过程
|
|
new Promise(resolve => {
|
// 获取云端存储过程信息
|
Api.getSystemConfig({
|
func: 'sPC_Get_TVP',
|
TVPName: innerFunc
|
}).then(result => {
|
if (!result.status) {
|
notification.warning({
|
top: 92,
|
message: result.message,
|
duration: 5
|
})
|
resolve(false)
|
} else {
|
cloudText = result.TVPText
|
resolve(true)
|
}
|
})
|
}).then(res => {
|
if (!res) return res
|
// 获取本地存储过程信息
|
|
let _param = {
|
func: 's_get_userproc',
|
LText: innerFunc
|
}
|
_param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss')
|
_param.secretkey = Utils.encrypt(_param.LText, _param.timestamp)
|
|
return Api.genericInterface(_param)
|
}).then(res => {
|
if (!res) return res
|
|
// 处理本地结果
|
if (!res.status) {
|
notification.warning({
|
top: 92,
|
message: res.message,
|
duration: 5
|
})
|
return false
|
} else {
|
isExist = true
|
localfunc = Utils.formatOptions(res.Ltext)
|
return true
|
}
|
}).then(res => {
|
if (!res) return res
|
|
// 根据本地及远端结果判断执行动作
|
if ((newLText === localfunc) && (newLText === cloudText)) {
|
return 'drop'
|
} else if (!localfunc || (cloudText === localfunc)) {
|
// 本地存储过程不存在,或云端和本地存储过程一致时,将新的存储过程更新至云端
|
return Api.getSystemConfig({
|
func: 'sPC_TVP_InUp',
|
TVPName: innerFunc,
|
TVPText: newLText,
|
TypeName: 'P'
|
})
|
} else {
|
return new Promise(resolve => {
|
Api.getSystemConfig({ // 添加现有的本地存储过程至云端
|
func: 'sPC_TVP_InUp',
|
TVPName: innerFunc,
|
TVPText: localfunc,
|
TypeName: 'P'
|
}).then(result => {
|
if (result.status) {
|
Api.getSystemConfig({
|
func: 'sPC_TVP_InUp', // 添加最新的存储过程至云端
|
TVPName: innerFunc,
|
TVPText: newLText,
|
TypeName: 'P'
|
}).then(response => {
|
resolve(response)
|
})
|
} else {
|
resolve(result)
|
}
|
})
|
})
|
}
|
}).then(res => {
|
if (!res || res === 'drop') return res
|
|
// 处理云端更新结果
|
if (!res.status) {
|
notification.warning({
|
top: 92,
|
message: res.message,
|
duration: 5
|
})
|
return false
|
} else if (isExist) {
|
return 'drop'
|
} else {
|
return 'create'
|
}
|
}).then(res => {
|
if (!res || res === 'create') return res
|
|
// 删除存储过程
|
let _param = {
|
func: 'sPC_TableData_InUpDe',
|
LText: DelText,
|
TypeCharOne: 'proc' // 删除或创建存储过程标志
|
}
|
|
_param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss')
|
_param.secretkey = Utils.encrypt(_param.LText, _param.timestamp)
|
_param.open_key = Utils.encryptOpenKey(_param.secretkey, _param.timestamp)
|
|
return Api.genericInterface(_param)
|
}).then(res => {
|
if (!res || res === 'create') return res
|
|
// 删除结果处理
|
if (!res.status) {
|
notification.warning({
|
top: 92,
|
message: res.message,
|
duration: 5
|
})
|
return false
|
} else {
|
return true
|
}
|
}).then(res => {
|
if (!res) return res
|
|
// 新建存储过程
|
let _param = {
|
func: 'sPC_TableData_InUpDe',
|
LText: newLText,
|
TypeCharOne: 'proc'
|
}
|
_param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss')
|
_param.secretkey = Utils.encrypt(_param.LText, _param.timestamp)
|
_param.open_key = Utils.encryptOpenKey(_param.secretkey, _param.timestamp)
|
|
return Api.genericInterface(_param)
|
}).then(res => {
|
this.setState({
|
loading: false
|
})
|
|
if (!res) return res
|
|
// 处理新建结果
|
if (!res.status) {
|
notification.warning({
|
top: 92,
|
message: res.message,
|
duration: 5
|
})
|
} else {
|
notification.success({
|
top: 92,
|
message: '创建成功',
|
duration: 2
|
})
|
}
|
})
|
}
|
|
render() {
|
|
return (
|
<Button
|
className="mk-btn mk-purple"
|
onClick={this.props.trigger}
|
loading={this.state.loading}
|
>
|
创建存储过程
|
</Button>
|
)
|
}
|
}
|
|
export default CreateFunc
|