king
2025-02-06 d1cd5af5adb53e91efdd278328e1b6f8ad834fb5
src/menu/components/table/edit-table/columns/tableIn/customscript/index.jsx
@@ -1,9 +1,8 @@
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { Form, Row, Col, Button, notification, Modal, Tooltip, Radio, Select, Switch } from 'antd'
import { Form, Row, Col, Button, notification, Tooltip, Radio, Select, Switch } from 'antd'
import { QuestionCircleOutlined } from '@ant-design/icons'
import Api from '@/api'
import { checkSQL } from '@/utils/utils-custom.js'
import CodeMirror from '@/templates/zshare/codemirror'
// import './index.scss'
@@ -67,20 +66,12 @@
  }
  handleConfirm = () => {
    const { type, btn } = this.props
    const { type } = this.props
    const { editItem, skip } = this.state
    // 表单提交时检查输入值是否正确
    this.props.form.validateFieldsAndScroll((err, values) => {
      if (type === 'fullscreen' && err) {
        notification.warning({
          top: 92,
          message: '请输入sql!',
          duration: 5
        })
        return
      }
      if (!err) {
        if (/^[\s\n]+$/.test(values.sql)) {
        if (!values.sql || /^[\s\n]+$/.test(values.sql)) {
          notification.warning({
            top: 92,
            message: '请输入sql!',
@@ -97,92 +88,41 @@
        let pass = checkSQL(values.sql, 'customscript')
        if (!pass) return
        if (!pass && !skip) return
        let sheet = btn.sheet.replace(/(.*)\.(.*)\.|@db@/ig, '')
        let tail = `
          drop table #${sheet}
          aaa: select @ErrorCode as ErrorCode,@retmsg as retmsg
        `
        let _initCustomScript = '' // 初始化脚本
        let _prevCustomScript = '' // 默认sql前执行脚本
        let _backCustomScript = '' // 默认sql后执行脚本
        this.props.scripts.forEach(item => {
          if (item.status === 'false') return
          if (item.position === 'init') {
            _initCustomScript += `
            /* 初始化脚本 */
            ${values.uuid === item.uuid ? values.sql : item.sql}
            `
          } else if (item.position === 'front') {
            _prevCustomScript += `
            /* 默认sql前脚本 */
            ${values.uuid === item.uuid ? values.sql : item.sql}
            `
          } else {
            _backCustomScript += `
            /* 默认sql后脚本 */
            ${values.uuid === item.uuid ? values.sql : item.sql}
            `
          }
        })
        if (!values.uuid) {
          if (values.position === 'init') {
            _initCustomScript += `
            /* 初始化脚本 */
            ${values.sql}
            `
          } else if (values.position === 'front') {
            _prevCustomScript += `
            /* 默认sql前脚本 */
            ${values.sql}
            `
          } else {
            _backCustomScript += `
            /* 默认sql后脚本 */
            ${values.sql}
            `
          }
        if (/@ID@/ig.test(values.sql) && !skip) {
          notification.warning({
            top: 92,
            message: '脚本中不可使用@ID@!',
            duration: 5
          })
          return
        }
        let sql = this.state.verifySql + _initCustomScript + _prevCustomScript + _backCustomScript + tail
        sql = sql.replace(/@\$|\$@/ig, '')
        if (skip) {
          this.setState({
            editItem: null,
            skip: false
          }, () => {
            this.props.scriptsChange(values)
            skip: false,
            editItem: null
          })
          this.props.form.setFieldsValue({
            sql: ' '
            sql: ''
          })
          this.props.scriptsChange(values)
        } else {
          this.setState({loading: true})
          Api.sDebug(sql).then(res => {
            if (res.status || res.ErrCode === '-2') {
              this.setState({
                loading: false,
                editItem: null
              }, () => {
                this.props.scriptsChange(values)
              })
              this.props.form.setFieldsValue({
                sql: ' '
              })
            } else {
              this.setState({loading: false})
  
              Modal.error({
                title: res.message
              })
            }
          this.props.scriptsChange(values, () => {
            this.setState({
              loading: false,
              editItem: null
            })
            this.props.form.setFieldsValue({
              sql: ''
            })
          }, () => {
            this.setState({
              loading: false
            })
          })
        }
      }
@@ -200,14 +140,18 @@
  }
  selectScript = (value, option) => {
    const { usefulfields, btn } = this.props
    const { usefulfields, btn, setting } = this.props
    let _value = ''
    if (value === 'default') {
      let fields = usefulfields.map(col => col.field).join(',')
      let upFields = usefulfields.map(col => `${col.field}=t.${col.field}`).join(',')
      
      if (fields) {
        fields = fields + ','
      }
      if (upFields) {
        upFields = upFields + ','
      }
      let database = btn.sheet.match(/(.*)\.(.*)\.|@db@/ig) || ''
@@ -215,7 +159,20 @@
      database = database ? (database[0] || '') : ''
      _value = `Insert into ${database}${sheet} (${fields}createuserid,createuser,createstaff,bid)\nSelect ${fields}@userid@,@username,@fullname,@BID@ From #${sheet}`
      _value = `update a set ${upFields}modifydate=getdate(),modifyuserid=@UserID@,modifyuser=@username,modifystaff=@fullname,deleted=0
      from (select * from #${sheet} where data_type='upt') t
      inner join ${database}${sheet} a on t.jskey=a.${setting.primaryKey || 'id'}
      update a set deleted=1,modifydate=getdate(),modifyuserid=@UserID@,modifyuser=@username,modifystaff=@fullname
      from (select * from #${sheet} where data_type='del') t
      inner join ${database}${sheet} a on t.jskey=a.${setting.primaryKey || 'id'}
      delete t from #${sheet} t inner join ${database}${sheet} a on t.jskey=a.${setting.primaryKey || 'id'}
      Insert into ${database}${sheet} (${fields}createuserid,createuser,createstaff,bid)
      Select ${fields}@UserID@,@username,@fullname,@BID@ From #${sheet}
      `
      _value = _value.replace(/\s{6}[fiuds]/ig, (word) => word.replace(/\s{6}/ig, ''))
    } else {
      _value = value
    }
@@ -266,12 +223,12 @@
          </Col> : null}
          {!type ? <Col span={10}>
            <Form.Item label="报错字段" style={{margin: 0, whiteSpace: 'nowrap'}}>
              errorcode(增加后缀NT表示数据不回滚,如ENT、NNT、FNT、NMNT), retmsg
              errorcode, retmsg
            </Form.Item>
          </Col> : null}
          {!type ? <Col span={24} className="sqlfield">
            <Form.Item label="可用字段">
              <Tooltip mouseLeaveDelay={0.3} mouseEnterDelay={0.3} placement="top" title={'公共值,请按照@xxx@格式使用。'}><span style={{color: '#1890ff'}}>BID, ID, LoginUID, SessionUid, UserID, Appkey, time_id</span></Tooltip>,&nbsp;
              <Tooltip mouseLeaveDelay={0.3} mouseEnterDelay={0.3} placement="top" title={'公共值,请按照@xxx@格式使用。'}><span style={{color: '#1890ff'}}>BID, LoginUID, SessionUid, UserID, Appkey, lang, typename, datam, time_id</span></Tooltip>,&nbsp;
              <Tooltip mouseLeaveDelay={0.3} mouseEnterDelay={0.3} placement="top" title={'系统变量,系统会定义变量并赋值。'}><span style={{color: '#fa8c16'}}>UserName, FullName, RoleID, mk_departmentcode, mk_organization, mk_user_type, mk_nation, mk_province, mk_city, mk_district, mk_address</span></Tooltip>,&nbsp;
              {usefulfields},data_type(注:jskey为主键值,新增时前端生成;data_type为操作类型,新增 - add、修改 - upt、删除 - del)
            </Form.Item>
@@ -329,15 +286,9 @@
            <Switch checked={skip} size="small" onChange={() => this.setState({skip: !skip})}/>
          </Col>
          <Col span={24} className="sql">
            <Form.Item label="sql">
            <Form.Item label="sql" required>
              {getFieldDecorator('sql', {
                initialValue: '',
                rules: [
                  {
                    required: true,
                    message: '请输入sql!'
                  }
                ]
                initialValue: ''
              })(<CodeMirror />)}
            </Form.Item>
          </Col>