index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. import React from 'react'
  2. import { Form, Input, Row, Col, Switch, Button, Modal, message, Select, Icon } from 'antd'
  3. import AceEditor from 'react-ace'
  4. import { create, test, groupList } from '../service'
  5. import 'ace-builds/src-noconflict/mode-java'
  6. import 'ace-builds/src-noconflict/theme-monokai'
  7. import { connect } from 'dva'
  8. import router from 'umi/router'
  9. const formItemLayout = {
  10. layout: 'vertical'
  11. }
  12. @connect(({ user }) => ({
  13. currentUser: user.currentUser
  14. }))
  15. @Form.create()
  16. class Add extends React.PureComponent {
  17. state = {
  18. type: 0,
  19. content: '',
  20. enabled: false,
  21. timeTaskEnabled: false,
  22. showTest: false, // 是否展示测试内容
  23. testContent: '', // 测试数据
  24. testResult: '', // 测试结果
  25. dos: [],
  26. groupList: [],
  27. group: '',
  28. features: ''
  29. }
  30. componentDidMount () {
  31. groupList({ pageNum: 999 }).then(res => this.setState({ groupList: res.data.list, group: res.data.list[0].name, features: res.data.list[0].features }))
  32. }
  33. create = () => {
  34. this.props.form.validateFields((err, values) => {
  35. if (this.state.type === 4) {
  36. values.content = JSON.stringify({ group: this.state.group, dos: this.state.dos })
  37. }
  38. if (!err) {
  39. create({
  40. ...values,
  41. createUser: this.props.currentUser.name, // 创建人
  42. updateUser: this.props.currentUser.name // 最后修改人
  43. }).then(res => {
  44. console.log(res)
  45. if (res.code === 0) {
  46. this.setState({ ...values })
  47. Modal.success({
  48. title: '提交成功',
  49. content: '决策代码编译正常,点击“测试按钮”可以继续测试决策逻辑'
  50. })
  51. // 跳转到列表页
  52. router.push('/fengkong/strategies/strategies')
  53. }
  54. })
  55. }
  56. })
  57. }
  58. test =() => {
  59. if (!this.state.name) {
  60. message.warn('请先提交决策内容!')
  61. return false
  62. }
  63. if (this.state.timeTaskEnabled) {
  64. message.warn('定时启用不能测试,请先关闭!')
  65. return false
  66. }
  67. test({
  68. name: this.state.name,
  69. type: this.state.type,
  70. input: JSON.parse(this.state.testContent)
  71. }).then(res => {
  72. if (res.code === 0) {
  73. this.setState({ testResult: JSON.stringify(res) })
  74. }
  75. })
  76. }
  77. addDos = () => {
  78. const dos = this.state.dos.slice()
  79. dos.push({
  80. r: { ep: '', f: '', v: '' }, ep: '', v: ''
  81. })
  82. this.setState({ dos: dos })
  83. }
  84. changeDos = (index, key, value) => {
  85. const dos = this.state.dos.slice()
  86. const keyList = key.split('.')
  87. if (keyList.length > 1) {
  88. dos[index][keyList[0]][keyList[1]] = value
  89. } else {
  90. dos[index][keyList[0]] = value
  91. }
  92. this.setState({ dos: dos })
  93. }
  94. removeDos = (index) => {
  95. const dos = this.state.dos.slice()
  96. dos.slice(index, 1)
  97. this.setState({ dos: dos })
  98. }
  99. render () {
  100. const { getFieldDecorator } = this.props.form
  101. return (
  102. <div>
  103. <h1>新增决策</h1>
  104. <Row>
  105. <Col span={12}>
  106. <Form {...formItemLayout}>
  107. <Form.Item label="决策名称">
  108. {getFieldDecorator('name')(<Input placeholder="全英文或英文带下划线" />)}
  109. </Form.Item>
  110. <Form.Item label="决策描述">
  111. {getFieldDecorator('description')(<Input.TextArea placeholder="多行输入" />)}
  112. </Form.Item>
  113. <Form.Item label="决策类型">
  114. {getFieldDecorator('type', { initialValue: this.state.type })(
  115. <Select placeholder={'请选择类型'} onChange={value => this.setState({ type: value })}>
  116. <Select.Option value= {0}>策略</Select.Option>
  117. <Select.Option value= {1}>流程</Select.Option>
  118. <Select.Option value= {2}>模型</Select.Option>
  119. <Select.Option value= {3}>调度</Select.Option>
  120. <Select.Option value= {4}>规则</Select.Option>
  121. </Select>)
  122. }
  123. </Form.Item>
  124. {
  125. this.state.type === 4 && (
  126. <Form.Item label="特征分组">
  127. {getFieldDecorator('group', { initialValue: this.state.group })(
  128. <Select placeholder={'请选择分组'} onChange={value => this.setState({ group: value }, () => this.setState({ dos: [], features: this.state.groupList.filter(item => item.name === value)[0].features }))}>
  129. {
  130. this.state.groupList.map(item => <Select.Option value={item.name}>{item.name}</Select.Option>)
  131. }
  132. </Select>)
  133. }
  134. </Form.Item>
  135. )
  136. }
  137. <Form.Item label="是否启用">
  138. {getFieldDecorator('enabled', {
  139. initialValue: this.state.enabled
  140. })(<Switch checked={this.state.enabled} onChange={checked => this.setState({ enabled: checked })} />)}
  141. </Form.Item>
  142. {
  143. this.state.type === 3 && (
  144. <Form.Item label="定时是否启用">
  145. {getFieldDecorator('timeTaskEnabled', {
  146. initialValue: this.state.timeTaskEnabled
  147. })(<Switch checked={this.state.timeTaskEnabled} onChange={checked => this.setState({ timeTaskEnabled: checked })} />)}
  148. </Form.Item>
  149. )
  150. }
  151. {
  152. this.state.type === 4 && (
  153. <Form.Item label="决策规则开发">
  154. <Button onClick={this.addDos}>点击创建规则</Button>
  155. {
  156. this.state.dos.map((item, index) => (
  157. <div style={{ margin: '10px 0' }}>
  158. <Select value={item.r.f} style={{ width: '150px' }} onChange={(value) => this.changeDos(index, 'r.f', value)}>
  159. {
  160. this.state.features.split(',').map(item => (<Select.Option value={item}>{item}</Select.Option>))
  161. }
  162. </Select>&emsp;
  163. <Select value={item.r.ep} style={{ width: '60px' }} onChange={(value) => this.changeDos(index, 'r.ep', value)}>
  164. <Select.Option value=">">{'>'}</Select.Option>
  165. <Select.Option value=">=">{'>='}</Select.Option>
  166. <Select.Option value="<">{'<'}</Select.Option>
  167. <Select.Option value="<=">{'<='}</Select.Option>
  168. <Select.Option value="!=">{'!='}</Select.Option>
  169. </Select>&emsp;
  170. <Input defaultValue={item.r.v} style={{ width: '50px' }} onChange={(e) => this.changeDos(index, 'r.v', e.target.value)} />&emsp;
  171. <Select value={item.ep} style={{ width: '50px' }} onChange={(value) => this.changeDos(index, 'ep', value)}>
  172. <Select.Option value="+">{'+'}</Select.Option>
  173. <Select.Option value="-">{'-'}</Select.Option>
  174. <Select.Option value="*">{'*'}</Select.Option>
  175. <Select.Option value="/">{'/'}</Select.Option>
  176. </Select>&emsp;
  177. <Input defaultValue={item.v} style={{ width: '50px' }} onChange={(e) => this.changeDos(index, 'v', e.target.value)} />&emsp;
  178. <Icon
  179. type="minus-circle-o"
  180. theme="twoTone"
  181. twoToneColor="#eb2f96"
  182. onClick={() => this.removeDos(index)}
  183. />
  184. </div>
  185. ))
  186. }
  187. </Form.Item>
  188. )
  189. }
  190. {
  191. this.state.type !== 4 && (<Form.Item label="决策逻辑开发">
  192. {getFieldDecorator('content')(
  193. <AceEditor
  194. style={{ width: this.state.showTest ? '100%' : '200%' }}
  195. mode="java"
  196. theme="monokai"
  197. name="content"
  198. onChange={(value) => this.setState({ content: value })}
  199. fontSize={14}
  200. showPrintMargin={true}
  201. showGutter={true}
  202. highlightActiveLine={true}
  203. value={''}
  204. setOptions={{
  205. enableBasicAutocompletion: true,
  206. enableLiveAutocompletion: true,
  207. enableSnippets: true,
  208. showLineNumbers: true,
  209. tabSize: 2
  210. }}
  211. />
  212. )}
  213. </Form.Item>)
  214. }
  215. <Form.Item>
  216. <Button type="primary" onClick={this.create}>提交</Button>&emsp;
  217. <Button onClick={() => this.setState({ showTest: true })}>测试</Button>
  218. </Form.Item>
  219. </Form>
  220. </Col>
  221. <Col span={12}>
  222. <div style={{ paddingLeft: '20px', display: this.state.showTest ? 'block' : 'none' }}>
  223. <Form.Item label="输入数据">
  224. <AceEditor
  225. mode="json"
  226. theme="monokai"
  227. onChange={(value) => this.setState({ testContent: value })}
  228. fontSize={14}
  229. showPrintMargin={true}
  230. showGutter={true}
  231. highlightActiveLine={true}
  232. value={this.state.testContent}
  233. height="300px"
  234. setOptions={{
  235. enableBasicAutocompletion: true,
  236. enableLiveAutocompletion: true,
  237. enableSnippets: true,
  238. showLineNumbers: true,
  239. tabSize: 2
  240. }}
  241. />
  242. </Form.Item>
  243. <Button type="primary" onClick={this.test}>执行测试</Button>&emsp;
  244. <Form.Item label="输出结果">
  245. <AceEditor
  246. mode="json"
  247. theme="monokai"
  248. fontSize={14}
  249. showPrintMargin={true}
  250. showGutter={true}
  251. highlightActiveLine={true}
  252. value={this.state.testResult}
  253. setOptions={{
  254. enableBasicAutocompletion: true,
  255. enableLiveAutocompletion: true,
  256. enableSnippets: true,
  257. showLineNumbers: true,
  258. tabSize: 2
  259. }}
  260. />
  261. </Form.Item>
  262. </div>
  263. </Col>
  264. </Row>
  265. </div>
  266. )
  267. }
  268. }
  269. export default Add