king
2025-02-12 c8c8bdf0c362ca76253d8e0cbb0dbaa13efad4a3
src/templates/sharecomponent/actioncomponent/verifyexcelin/index.jsx
@@ -19,7 +19,9 @@
const { confirm } = Modal
const { Search } = Input
const { Paragraph } = Typography
const EditTable = asyncComponent(() => import('@/templates/zshare/editTable'))
const CodeMirror = asyncComponent(() => import('@/templates/zshare/codemirror'))
const FullScripts = asyncComponent(() => import('@/templates/zshare/verifycard/fullScripts'))
class VerifyCard extends Component {
@@ -76,6 +78,8 @@
          { value: 'Decimal(18,2)', text: 'Decimal(18,2)' },
          { value: 'Decimal(18,4)', text: 'Decimal(18,4)' },
          { value: 'Decimal(18,6)', text: 'Decimal(18,6)' },
          { value: 'Decimal(18,8)', text: 'Decimal(18,8)' },
          { value: 'Decimal(18,10)', text: 'Decimal(18,10)' },
          { value: 'date', text: 'date' },
          { value: 'datetime', text: 'datetime' }
        ]
@@ -117,7 +121,7 @@
        unlimit: true,
        editable: true,
        keyVals: ['Int', 'Decimal(18,0)', 'Decimal(18,2)', 'Decimal(18,4)', 'Decimal(18,6)'],
        render: (text, record) => /^Decimal/ig.test(record.type) || /^int/ig.test(record.type) ? text : ''
        render: (text, record) => record.required === 'true' && (/^Decimal/ig.test(record.type) || /^int/ig.test(record.type)) ? text : ''
        
      },
      {
@@ -129,7 +133,7 @@
        unlimit: true,
        editable: true,
        keyVals: ['Int', 'Decimal(18,0)', 'Decimal(18,2)', 'Decimal(18,4)', 'Decimal(18,6)'],
        render: (text, record) => /^Decimal/ig.test(record.type) || /^int/ig.test(record.type) ? text : ''
        render: (text, record) => record.required === 'true' && (/^Decimal/ig.test(record.type) || /^int/ig.test(record.type)) ? text : ''
      }
    ],
    uniqueColumns: [
@@ -203,6 +207,89 @@
            </div>
          )
      },
      {
        dataIndex: 'sqlRender',
        render: (record) => {
          let columns = this.state.verify.columns
          let textFields = []
          let numberFields = []
          let dateFields = []
          columns.forEach((col) => {
            if (/Nvarchar/ig.test(col.type)) {
              textFields.push(col.Column)
            } else if (/Decimal|int/ig.test(col.type)) {
              numberFields.push(col.Column)
            } else if (/date/ig.test(col.type)) {
              dateFields.push(col.Column)
            }
          })
          let _fields = record.field.split(',')
          let _fields_ = _fields.map(_field => `a.${_field}=b.${_field}`)
          _fields_ = _fields_.join(' and ')
          let _where = []
          _fields.forEach(f => {
            if (textFields.includes(f)) {
              _where.push(`${f}!=''`)
            } else if (numberFields.includes(f)) {
              _where.push(`${f}!=0`)
            } else if (dateFields.includes(f)) {
              _where.push(`${f}>'1949-10-01'`)
            }
          })
          _where = _where.length ? `where ${_where.join(' and ')} ` : ''
          if (record.verifyType === 'logic' || record.verifyType === 'logic_temp') {
            _fields_ += ' and b.deleted=0'
          }
          let _afields = []
          _fields = _fields.map(f => {
            if (numberFields.includes(f)) {
              _afields.push(`cast(a.${f} as nvarchar(50))`)
              return `cast(${f} as nvarchar(50))`
            } else if (dateFields.includes(f)) {
              _afields.push(`CONVERT(nvarchar(50), a.${f}, 21)`)
              return `CONVERT(nvarchar(50), ${f}, 21)`
            }
            _afields.push(`a.${f}`)
            return f
          })
          let _sheet = this.props.card.sheet
          let database = _sheet.match(/(.*)\.(.*)\.|@db@/ig) || ''
          let sheet = _sheet.replace(/(.*)\.(.*)\.|@db@/ig, '')
          database = database ? (database[0] || '') : ''
          let sql = `
          /* 重复性验证 */
          Set @tbid=''
          Select top 1 @tbid=${_fields.join('+\' \'+')} from (select 1 as n,${record.field} from #${sheet} ${_where}) a group by ${record.field} having sum(n)>1
          If @tbid!=''
          Begin
            select @ErrorCode='${record.errorCode}',@retmsg=@tbid+' 重复'
            goto aaa
          end
          ${record.verifyType.indexOf('temp') === -1 ? `Set @tbid=''
          Select top 1 @tbid=${_afields.join('+\' \'+')} from ${_where ? `(select * from #${sheet} ${_where})` : `#${sheet}`} a Inner join ${database}${sheet} b on ${_fields_}
          If @tbid!=''
          Begin
            select @ErrorCode='${record.errorCode}',@retmsg=@tbid+' 与已有数据重复'
            goto aaa
          end` : ''}
          `
          return sql.split(/\n\s{10}/ig).map(n => n.replace(/^\s{2}/ig, '&nbsp;&nbsp;'))
        }
      }
    ],
    scriptsColumns: [
      {
@@ -307,17 +394,27 @@
      _verify.range = 1
    }
    _verify.excelHandle = _verify.excelHandle || 'false'
    _verify.default = _verify.default || 'true'
    _verify.sheet = _verify.sheet || 'Sheet1'
    _verify.range = _verify.range || 0
    _verify.columns = _columns
    _verify.scripts = _verify.scripts || []
    _verify.uniques = _verify.uniques || []
    if (window.GLOB.process && card.intertype === 'system') {
      _verify.workFlow = _verify.workFlow || 'false'
      _verify.flowType = 'start'
      _verify.flowSql = _verify.flowSql || 'true'
    } else {
      delete _verify.workFlow
      delete _verify.flowType
      delete _verify.flowSql
    }
    this.setState({
      searchKey: '',
      verify: {
        ..._verify,
        default: _verify.default || 'true',
        sheet: _verify.sheet || 'Sheet1',
        range: _verify.range || 0,
        columns: _columns,
        scripts: _verify.scripts || [],
        uniques: _verify.uniques || []
      }
      verify: _verify
    }, () => {
      this.resetUniqueColumns()
    })
@@ -501,35 +598,37 @@
  clearField = () => {
    const { verify } = this.state
    const _this = this
    const that = this
    confirm({
      content: `确定清空Excel列吗?`,
      onOk() {
        _this.setState({
        that.setState({
          verify: {
            ...verify,
            columns: []
          }
        }, () => {
          _this.resetUniqueColumns()
          that.resetUniqueColumns()
        })
      },
      onCancel() {}
    })
  }
  columnChange = (values) => {
  columnChange = (values, callback) => {
    let verify = JSON.parse(JSON.stringify(this.state.verify))
    let fields = verify.columns.map(item => item.Column)
    if (fields.includes(values.Column)) {
    let fields = verify.columns.map(item => item.Column.toLowerCase())
    if (fields.includes(values.Column.toLowerCase())) {
      notification.warning({
        top: 92,
        message: values.Column + '字段已存在!',
        duration: 5
      })
      return
    } else {
      callback()
    }
    values.uuid = Utils.getuuid()
@@ -712,33 +811,44 @@
        if (!err) {
          let _verify = {...verify, ...values}
          if (_verify.excelHandle !== 'true') {
            delete _verify.excel_func
          }
          if (_verify.default === 'false' && _verify.scripts.length === 0) {
            notification.warning({
              top: 92,
              message: '不执行默认sql时,必须设置自定义脚本!',
              duration: 5
            })
            return
          }
          let cols = _verify.columns.map(col => col.Column.toLowerCase())
          cols = Array.from(new Set(cols))
          let error = ''
          if (_verify.columns.length === 0) {
            notification.warning({
              top: 92,
              message: '请设置Excel列字段!',
              duration: 5
            })
            return
            error = '请设置Excel列字段!'
          } else if (_verify.columns.length > cols.length) {
            notification.warning({
              top: 92,
              message: 'Excel列字段名,不可重复!',
              duration: 5
            })
            return
            error = 'Excel列字段名,不可重复!'
          } else if (cols.includes('bid')) {
            error = 'bid字段为保留字,不可使用!'
          } else if (cols.includes('jskey')) {
            error = 'jskey字段为保留字,不可使用!'
          } else if (_verify.range === 1) {
            let tEmptys = _verify.columns.filter(op => !op.Text)
            if (tEmptys.length > 0) {
              notification.warning({
                top: 92,
                message: '忽略首行时,会使用Text值校验Excel首行内容,Text值与Excel表首行内容相同,且均不可为空!',
                duration: 5
              })
              return
              error = '忽略首行时,会使用Text值校验Excel首行内容,Text值与Excel表首行内容相同,且均不可为空!'
            }
          }
          if (error) {
            notification.warning({
              top: 92,
              message: error,
              duration: 5
            })
            return
          }
          _verify.columns.sort((a, b) => {
@@ -771,22 +881,17 @@
            resolve(_verify)
          }
        } else {
          notification.warning({
            top: 92,
            message: '请设置Excel表名!',
            duration: 5
          })
          this.setState({activeKey: 'basemsg'})
        }
      })
    })
  }
  onOptionChange = (e, key) => {
  onOptionChange = (value, key) => {
    const { verify } = this.state
    let value = e.target.value
    this.setState({
      verify: {...verify, default: value}
      verify: {...verify, [key]: value}
    })
  }
@@ -867,7 +972,31 @@
          <TabPane tab="基础验证" key="basemsg">
            <Form {...formItemLayout}>
              <Row gutter={24}>
                {card.intertype === 'system' ? <Col span={8}>
                  <Form.Item label="默认sql">
                    <Radio.Group value={verify.default} onChange={(e) => this.onOptionChange(e.target.value, 'default')}>
                      <Radio value="true">执行</Radio>
                      <Radio value="false">不执行</Radio>
                    </Radio.Group>
                  </Form.Item>
                </Col> : null}
                <Col span={8}>
                  <Form.Item label={
                    <Tooltip placement="topRight" title="自定义验证Excel格式,可用参数为 XLSX、workbook、btn、callback。">
                      <QuestionCircleOutlined className="mk-form-tip" />
                      导入格式
                    </Tooltip>
                  }>
                    {getFieldDecorator('excelHandle', {
                      initialValue: verify.excelHandle
                    })(
                    <Radio.Group onChange={(e) => this.onOptionChange(e.target.value, 'excelHandle')}>
                      <Radio value="false">默认</Radio>
                      <Radio value="true">自定义</Radio>
                    </Radio.Group>)}
                  </Form.Item>
                </Col>
                {verify.excelHandle !== 'true' ? <Col span={8}>
                  <Form.Item label={
                    <Tooltip placement="bottomLeft" title="导入时工作表名与excel中必须一致,注:工作表名为Sheet1且excel中仅有一个工作表时不进行表名验证。">
                      <QuestionCircleOutlined className="mk-form-tip" />
@@ -884,7 +1013,7 @@
                      ]
                    })(<Input placeholder="" autoComplete="off" />)}
                  </Form.Item>
                </Col>
                </Col> : null}
                <Col span={8}>
                  <Form.Item label={
                    <Tooltip placement="bottomLeft" title="忽略首行时,会校验excel中表头名称与excel列设置是否一致。">
@@ -897,9 +1026,35 @@
                    })(<InputNumber min={0} max={100} precision={0} />)}
                  </Form.Item>
                </Col>
                {card.intertype === 'system' ? <Col span={8}>
                  <Form.Item label={'默认sql'}>
                    <Radio.Group value={verify.default} onChange={this.onOptionChange}>
                {verify.excelHandle === 'true' ? <Col span={24} style={{paddingLeft: '30px'}}>
                  <Form.Item wrapperCol={ {xs: { span: 24 }, sm: { span: 24 }} } label="">
                    {getFieldDecorator('excel_func', {
                      initialValue: verify.excel_func || '',
                      rules: [
                        {
                          required: true,
                          message: '请填写自定义逻辑!'
                        }
                      ]
                    })(<CodeMirror mode="text/javascript" theme="cobalt" />)}
                  </Form.Item>
                </Col> : null}
                {window.GLOB.process && card.intertype === 'system' ? <Col span={8}>
                  <Form.Item label={
                    <Tooltip placement="bottomLeft" title="导入Excel工作流仅支持发起流程。">
                      <QuestionCircleOutlined className="mk-form-tip" />
                      工作流
                    </Tooltip>
                  }>
                    <Radio.Group value={verify.workFlow} onChange={(e) => {this.onOptionChange(e.target.value, 'workFlow')}}>
                      <Radio value="true">开启</Radio>
                      <Radio value="false">不开启</Radio>
                    </Radio.Group>
                  </Form.Item>
                </Col> : null}
                {verify.workFlow === 'true' ? <Col span={8}>
                  <Form.Item label="默认sql(工作流)">
                    <Radio.Group value={verify.flowSql} onChange={(e) => {this.onOptionChange(e.target.value, 'flowSql')}}>
                      <Radio value="true">执行</Radio>
                      <Radio value="false">不执行</Radio>
                    </Radio.Group>
@@ -921,7 +1076,7 @@
            <Button className="excel-col-add mk-red" title="清空Excel列" onClick={this.clearField}>
              清空Excel列
            </Button>
            <Col style={{fontSize: '12px', color: '#757575', paddingLeft: '10px'}} span={24}>注:数值类型(int 或 decimal),内容为必填;最大值和最小值在类型为数值时有效。导入-初始化:用于excel中不存在,导入时需要初始化的字段</Col>
            <Col style={{fontSize: '12px', color: '#757575', paddingLeft: '10px'}} span={24}>注:数值类型(int 或 decimal),内容为必填;最大值和最小值在类型为数值时(必填)有效。导入-初始化:用于excel中不存在,导入时需要初始化的字段</Col>
            <EditTable actions={['edit', 'move', 'copy', 'del', 'extra:required:是否必填']} searchKey={searchKey} type="excelcolumn" data={verify.columns} columns={excelColumns} onChange={this.changeColumns}/>
          </TabPane>
          {card.intertype === 'system' ? <TabPane tab={
@@ -931,7 +1086,7 @@
            </span>
          } key="unique">
            <UniqueForm fields={verify.columns} uniqueChange={this.uniqueChange}/>
            <EditTable actions={['edit', 'move', 'del', 'status']} data={verify.uniques} columns={uniqueColumns} onChange={this.changeUniques}/>
            <EditTable actions={['edit', 'move', 'del', 'status', 'sql']} data={verify.uniques} columns={uniqueColumns} onChange={this.changeUniques}/>
          </TabPane> : null}
          {card.intertype === 'system' ? <TabPane tab={
            <span>
@@ -951,6 +1106,7 @@
                btn={this.props.card}
                usefulfields={verify.columns}
                scripts={verify.scripts}
                workFlow={verify.workFlow}
                systemScripts={this.state.systemScripts}
                scriptsChange={this.scriptsChange}
                wrappedComponentRef={(inst) => this.scriptsFullForm = inst}
@@ -960,6 +1116,7 @@
              btn={this.props.card}
              usefulfields={verify.columns}
              scripts={verify.scripts}
              workFlow={verify.workFlow}
              systemScripts={this.state.systemScripts}
              scriptsChange={this.scriptsChange}
              wrappedComponentRef={(inst) => this.scriptsForm = inst}