TaskCreaterView.jsx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. import React, { useState, useEffect, useRef } from 'react'
  2. import { Form, Select, Button, Space, message } from 'antd'
  3. import TaskForm from './TaskForm'
  4. import TaskChartEditor from './TaskChartEditor'
  5. import styled from 'styled-components'
  6. import { useNavigate, useLocation } from 'react-router'
  7. import {
  8. getJmHomeworkList,
  9. createJmJob,
  10. getJmJobInfo,
  11. updateJmJobInfo,
  12. } from '../services'
  13. const TaskCreater = styled.div`
  14. margin: 20px;
  15. background: #fff;
  16. height: 100%;
  17. padding: 15px 25px;
  18. .tasktype_label {
  19. padding-left: 12px;
  20. border-left: 3px solid #1881da;
  21. font-size: 14px;
  22. font-family: PingFangSC-Medium, PingFang SC;
  23. font-weight: 500;
  24. color: #4a4a4a;
  25. line-height: 20px;
  26. letter-spacing: 1px;
  27. }
  28. .tasktype_form {
  29. margin-top: 20px;
  30. }
  31. .tasktype_btns {
  32. margin: 20px 20px 0px;
  33. }
  34. `
  35. const layout = {
  36. labelCol: {
  37. span: 2,
  38. },
  39. wrapperCol: {
  40. span: 10,
  41. },
  42. }
  43. const FormItem = Form.Item
  44. const { Option } = Select
  45. const TaskCreaterView = () => {
  46. // 路由导航
  47. const navigate = useNavigate()
  48. const { state } = useLocation()
  49. const [taskType, setTaskType] = useState(null)
  50. const [graphRef, setGraphRef] = useState(null)
  51. const [jmWorkList, setJmWorkList] = useState([])
  52. const [taskInfo, setTaskInfo] = useState(null)
  53. const [taskTypeForm] = Form.useForm()
  54. const [taskForm] = Form.useForm()
  55. const cronRef = useRef()
  56. useEffect(() => {
  57. if (state) {
  58. fetchJmJobInfo(state.id)
  59. }
  60. }, [state])
  61. //初始化作业列表
  62. useEffect(() => {
  63. fetchJmWorkList()
  64. }, [])
  65. const fetchJmJobInfo = async id => {
  66. const { data } = await getJmJobInfo(id)
  67. if (data.code === 200) {
  68. const { type } = data.data
  69. taskTypeForm.setFieldValue('taskType', type)
  70. setTaskType(type === '单作业离线任务' ? 'singleTask' : 'multitasking')
  71. setTaskInfo(data.data)
  72. }
  73. }
  74. const onTaskTypeChange = value => {
  75. setTaskType(value)
  76. }
  77. const onGraphRef = ref => {
  78. setGraphRef(ref)
  79. }
  80. const fetchJmWorkList = async () => {
  81. const { data } = await getJmHomeworkList('test')
  82. if (data.code === 200) {
  83. setJmWorkList(data.data)
  84. } else {
  85. message.error(data.msg)
  86. }
  87. }
  88. const onSubmit = () => {
  89. const taskFormData = taskForm.getFieldsValue()
  90. taskTypeForm
  91. .validateFields()
  92. .then(() => {
  93. taskForm
  94. .validateFields()
  95. .then(async () => {
  96. let nodes = []
  97. let edges = []
  98. let type = ''
  99. if (taskType === 'singleTask') {
  100. type = '单作业离线任务'
  101. nodes = [
  102. {
  103. id: 1,
  104. homework_id: taskFormData.job,
  105. homework_name: jmWorkList.find(
  106. item => item.id === taskFormData.job
  107. ).name,
  108. start_point: 1,
  109. },
  110. ]
  111. } else {
  112. type = '多作业离线任务'
  113. const cells = handleNodes()
  114. nodes = cells.nodes
  115. edges = cells.edges
  116. }
  117. const params = {
  118. name: taskFormData.taskName,
  119. type,
  120. tag: taskFormData.taskTag,
  121. cron_type: taskFormData.executionCycle,
  122. user_id: 'test',
  123. user_name: 'test',
  124. project_id: 'test',
  125. nodes,
  126. edges,
  127. }
  128. if (taskType === 'multitasking') {
  129. params['json_str'] = JSON.stringify(
  130. graphRef?.state.taskGraph.toJSON()
  131. )
  132. }
  133. const cron_data = cronRef.current?.getCronData
  134. if (taskFormData.executionCycle === 2) {
  135. params['cron_expression'] = cron_data
  136. }
  137. // 单位非空判断
  138. if (
  139. taskFormData.executionCycle === 1 ||
  140. cron_data?.minute ||
  141. cron_data?.hour ||
  142. cron_data?.week ||
  143. cron_data?.month ||
  144. cron_data?.cron_expression
  145. ) {
  146. let res = {}
  147. if (state !== null) {
  148. params['id'] = state.id
  149. res = await updateJmJobInfo(params)
  150. } else {
  151. res = await createJmJob(params)
  152. }
  153. if (res.data.code === 200) {
  154. navigate(-1)
  155. } else {
  156. message.error(res.data.msg)
  157. }
  158. } else {
  159. message.error('定时规则数据有误')
  160. }
  161. })
  162. .catch(err => {
  163. message.error('请检查表单数据是否完整')
  164. })
  165. })
  166. .catch(err => {
  167. message.error('请检查表单数据是否完整')
  168. })
  169. }
  170. const handleNodes = () => {
  171. const cells = graphRef?.state.taskGraph.getCells()
  172. const nodes = []
  173. const edges = []
  174. cells.forEach(cell => {
  175. if (cell.shape === 'task-node') {
  176. nodes.push({
  177. id: cell.id,
  178. homework_id: cell.data.homework_id,
  179. homework_name: cell.data.label,
  180. // start_point: cell.data.type === 'work' ? 1 : 0,
  181. })
  182. } else {
  183. edges.push({ source: cell.source.cell, target: cell.target.cell })
  184. }
  185. })
  186. return { nodes, edges }
  187. }
  188. return (
  189. <TaskCreater>
  190. <div className="tasktype_label">配置任务类型:</div>
  191. <Form {...layout} form={taskTypeForm} className="tasktype_form">
  192. <FormItem
  193. name="taskType"
  194. label="选择任务类型"
  195. rules={[{ required: true, message: '请选择任务类型!' }]}>
  196. <Select
  197. placeholder="选择任务类型"
  198. onChange={onTaskTypeChange}
  199. allowClear>
  200. <Option value="singleTask">单作业离线任务</Option>
  201. <Option value="multitasking">多作业离线任务</Option>
  202. </Select>
  203. </FormItem>
  204. </Form>
  205. <div className="tasktype_label">配置任务信息:</div>
  206. <TaskForm
  207. taskForm={taskForm}
  208. taskType={taskType}
  209. jmWorkList={jmWorkList}
  210. taskInfo={taskInfo}
  211. ref={cronRef}
  212. />
  213. {taskType === 'multitasking' && (
  214. <div className="tasktype_label">作业编排:</div>
  215. )}
  216. {taskType === 'multitasking' && (
  217. <TaskChartEditor
  218. onRef={onGraphRef}
  219. jmWorkList={jmWorkList}
  220. json={taskInfo?.json_str}
  221. />
  222. )}
  223. <Space className="tasktype_btns">
  224. <Button onClick={() => navigate(-1)}>取消</Button>
  225. <Button onClick={onSubmit}>提交</Button>
  226. </Space>
  227. </TaskCreater>
  228. )
  229. }
  230. export default TaskCreaterView