king
2023-08-27 d340a56429b12c3c893762730638631e147e047c
src/templates/zshare/createfunc/index.jsx
@@ -4,203 +4,125 @@
import moment from 'moment'
import Utils from '@/utils/utils.js'
import FuncUtils from './utils.js'
import Api from '@/api'
// import './index.scss'
class CreateFunc extends Component {
  static propTypes = {
    trigger: PropTypes.func
    getMsg: PropTypes.func
  }
  state = {
    loading: false,
    loading: false
  }
  exec = (innerFunc, newLText, DelText) => {
  trigger = () => {
    this.props.getMsg().then(config => {
      let newLText = ''
      let func = ''
      if (config.$type === 'table') {
        func = config.func
        newLText = FuncUtils.getTableFunc(config)
      } else if (config.$type === 'excelIn') {
        func = config.func
        newLText = FuncUtils.getexcelInfunc(config)
      } else if (config.$type === 'btn') {
        func = config.func
        newLText = FuncUtils.getfunc(config)
      }
      if (!newLText || !func) return
      this.createExec(func, newLText)
    })
  }
  createExec = (func, newLText) => {
    let dropfunc = `IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID('${func}') AND type in (N'P', N'PC'))  mdrpk PROCEDURE ${func}`
    // 删除存储过程
    let droparam = {
      func: 'sPC_TableData_InUpDe',
      LText: Utils.formatOptions(dropfunc),
      TypeCharOne: 'proc' // 删除或创建存储过程标志
    }
    droparam.timestamp = moment().format('YYYY-MM-DD HH:mm:ss')
    droparam.secretkey = Utils.encrypt(droparam.LText, droparam.timestamp)
    droparam.open_key = Utils.encryptOpenKey(droparam.secretkey, droparam.timestamp)
    // 新建存储过程
    let _param = {
      func: 'sPC_TableData_InUpDe',
      LText: Utils.formatOptions(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)
    let saveParam = {
      func: 's_proc_save',
      sql_script: window.btoa(window.encodeURIComponent(newLText)),
      proc_name: func,
      save_type: 'auto'
    }
    this.setState({
      loading: true
    })
    this.createExec(innerFunc, newLText, DelText)
  }
  createExec = (innerFunc, newLText, DelText) => {
    let isExist = false // 存储过程是否存在
    let cloudText = ''  // 云端存储结果
    let localfunc = ''  // 本地存储过程
    new Promise(resolve => {
      // 获取云端存储过程信息
      Api.getCloudConfig({
        func: 'sPC_Get_TVP',
        TVPName: innerFunc
      }).then(result => {
    Api.genericInterface(droparam).then(res => {
      if (!res.status) {
        notification.warning({
          top: 92,
          message: res.message,
          duration: 5
        })
        this.setState({
          loading: false
        })
        return
      }
      Api.genericInterface(_param).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.getCloudConfig({
          func: 'sPC_TVP_InUp',
          TVPName: innerFunc,
          TVPText: newLText,
          TypeName: 'P'
        })
      } else {
        return new Promise(resolve => {
          Api.getCloudConfig({ // 添加现有的本地存储过程至云端
            func: 'sPC_TVP_InUp',
            TVPName: innerFunc,
            TVPText: localfunc,
            TypeName: 'P'
          }).then(result => {
            if (result.status) {
              Api.getCloudConfig({
                func: 'sPC_TVP_InUp', // 添加最新的存储过程至云端
                TVPName: innerFunc,
                TVPText: newLText,
                TypeName: 'P'
              }).then(response => {
                resolve(response)
              })
            } else {
              resolve(result)
            }
          this.setState({
            loading: false
          })
          return
        }
        Api.genericInterface(saveParam).then(response => {
          this.setState({loading: false})
          if (!response.status) {
            notification.warning({
              top: 92,
              message: response.message,
              duration: 5
            })
          } else {
            notification.success({
              top: 92,
              message: '创建成功。',
              duration: 5
            })
          }
        })
      }
    }).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}
        onClick={this.trigger}
        loading={this.state.loading}
      >
        创建存储过程