page.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. import React from 'react';
  2. import { Tabs, Form, Row, Col, InputNumber, Button, Switch, DatePicker } from 'antd';
  3. import './index.less';
  4. import Page from '@src/containers/Page';
  5. import Block from '@src/components/Block';
  6. import EditTableCell from '@src/components/EditTableCell';
  7. import { getMap, flattenObject } from '@src/services/Tools';
  8. import { asyncSMessage } from '@src/services/AsyncTools';
  9. import TableLayout from '@src/layouts/TableLayout';
  10. import { QuestionDifficult } from '../../../../Constant';
  11. import { System } from '../../../stores/system';
  12. // import { Examination } from '../../../stores/examination';
  13. import { Exercise } from '../../../stores/exercise';
  14. export default class extends Page {
  15. constructor(props) {
  16. super(props);
  17. this.exerciseColumns = [{
  18. title: '学科',
  19. dataIndex: 'title',
  20. }].concat(QuestionDifficult.map(row => {
  21. return {
  22. title: row.label,
  23. dataIndex: row.value,
  24. render: (text, result) => {
  25. const { exercise = {} } = this.state;
  26. return <EditTableCell value={(exercise[result.id] || {})[row.value] || 0} onChange={(v) => {
  27. this.changeMapValue('exercise', result.id, row.value, v);
  28. }} />;
  29. },
  30. };
  31. }));
  32. this.examinationColumns = [{
  33. title: '学科',
  34. dataIndex: 'title',
  35. }, {
  36. title: '题目数',
  37. dataIndex: 'number',
  38. render: (text, result) => {
  39. const { examination = {} } = this.state;
  40. return (examination[result.extend] || {}).number || 0;
  41. // return <EditTableCell value={(examination[result.extend] || {}).number || 0} onChange={(v) => {
  42. // this.changeMapValue('examination', result.extend, 'number', v);
  43. // }} />;
  44. },
  45. }, {
  46. title: '做题时间',
  47. dataIndex: 'time',
  48. render: (text, result) => {
  49. const { examination = {} } = this.state;
  50. return Number((examination[result.extend] || {}).time || 0) / 60;
  51. // return <EditTableCell value={(examination[result.extend] || {}).time || 0} onChange={(v) => {
  52. // this.changeMapValue('examination', result.extend, 'time', v);
  53. // }} />;
  54. },
  55. }];
  56. this.state.tab = 'exercise';
  57. }
  58. initData() {
  59. Promise.all(this.structExamination(), this.structExercise())
  60. .then(() => {
  61. return this.refresh(this.state.tab);
  62. });
  63. }
  64. refresh(tab) {
  65. if (tab === 'exercise') {
  66. return Promise.all([this.refreshScoreSwitch(), this.refreshExercise(), this.refreshSentence(), this.refreshTextbook()]);
  67. }
  68. if (tab === 'examination') {
  69. return this.refreshExamination();
  70. }
  71. if (tab === 'filter') {
  72. return this.refreshFilter();
  73. }
  74. if (tab === 'paper') {
  75. return this.refreshExercisePaperAuto();
  76. }
  77. return Promise.reject();
  78. }
  79. refreshScoreSwitch() {
  80. return System.getScoreSwitch().then(result => {
  81. this.setState({ score: result || {} });
  82. });
  83. }
  84. refreshExercise() {
  85. return System.getExerciseTime().then((result) => {
  86. this.setState({ exercise: result });
  87. });
  88. }
  89. refreshExercisePaperAuto() {
  90. return System.getExercisePaperAuto().then((result) => {
  91. this.setState({ exercisePaperAuto: result });
  92. });
  93. }
  94. refreshSentence() {
  95. return System.getSentenceTime().then((result) => {
  96. this.setState({ sentence: result || {} });
  97. const { form } = this.props;
  98. form.setFieldsValue(flattenObject(result, 'sentence'));
  99. });
  100. }
  101. refreshTextbook() {
  102. return System.getTextbookTime().then((result) => {
  103. this.setState({ textbook: result || {} });
  104. const { form } = this.props;
  105. form.setFieldsValue(flattenObject(result, 'textbook'));
  106. });
  107. }
  108. refreshExamination() {
  109. return System.getExaminationTime().then((result) => {
  110. this.setState({ examination: result });
  111. });
  112. }
  113. refreshFilter() {
  114. return System.getFilterTime().then((result) => {
  115. this.setState({ filter: result || {} });
  116. const { form } = this.props;
  117. form.setFieldsValue(flattenObject(result, 'filter'));
  118. });
  119. }
  120. structExercise() {
  121. return Exercise.allStruct().then(result => {
  122. const list = result.map(row => { row.title = `${row.titleZh}/${row.titleEn}`; return row; });
  123. const map = getMap(list, 'id');
  124. this.setState({
  125. exerciseList: list.filter(row => row.level === 2).map(row => {
  126. const parent = map[row.parentId];
  127. row.title = `${parent.title}-${row.title}`;
  128. return row;
  129. }),
  130. });
  131. });
  132. }
  133. structExamination() {
  134. return Exercise.allStruct().then(result => {
  135. const list = result.map(row => { row.title = `${row.titleZh}/${row.titleEn}`; return row; });
  136. this.setState({
  137. examinationList: list.filter(row => row.level === 1 && row.isExamination),
  138. });
  139. });
  140. }
  141. changeMapValue(field, index, key, value) {
  142. const data = this.state[field] || {};
  143. data[index] = data[index] || {};
  144. data[index][key] = value;
  145. this.setState({ [field]: data });
  146. }
  147. changeValue(field, key, value) {
  148. const data = this.state[field] || {};
  149. data[key] = value;
  150. this.setState({ [field]: data });
  151. }
  152. submit(tab) {
  153. let handler;
  154. if (tab === 'exercise') {
  155. handler = this.submitExercise()
  156. .then(() => {
  157. return this.submitTextbook();
  158. })
  159. .then(() => {
  160. return this.submitSentence();
  161. });
  162. }
  163. if (tab === 'examination') {
  164. handler = this.submitExamination();
  165. }
  166. if (tab === 'filter') {
  167. handler = this.submitFilter();
  168. }
  169. if (tab === 'paper') {
  170. handler = this.submitExercisePaperAuto();
  171. }
  172. handler.then(() => {
  173. asyncSMessage('保存成功');
  174. });
  175. }
  176. submitScore() {
  177. const { score } = this.state;
  178. return System.setScoreSwitch(score);
  179. }
  180. submitExercise() {
  181. const { exercise } = this.state;
  182. return System.setExerciseTime(exercise);
  183. }
  184. submitExercisePaperAuto() {
  185. const { exercisePaperAuto } = this.state;
  186. return System.setExercisePaperAuto(exercisePaperAuto);
  187. }
  188. submitSentence() {
  189. const { sentence } = this.state;
  190. return System.setSentenceTime(sentence);
  191. }
  192. submitTextbook() {
  193. const { textbook } = this.state;
  194. return System.setTextbookTime(textbook);
  195. }
  196. submitExamination() {
  197. const { examination } = this.state;
  198. return System.setExaminationTime(examination);
  199. }
  200. submitFilter() {
  201. const { form } = this.props;
  202. return new Promise((resolve, reject) => {
  203. form.validateFields(['filter'], (err, values) => {
  204. if (!err) {
  205. System.setFilterTime(values.filter)
  206. .then(() => {
  207. resolve();
  208. })
  209. .catch((e) => {
  210. reject(e);
  211. });
  212. }
  213. });
  214. });
  215. }
  216. renderAll() {
  217. const { score = {} } = this.state;
  218. return <Form>
  219. <Row>
  220. <Col span={8} offset={16}>
  221. <Form.Item labelCol={{ span: 12 }} wrapperCol={{ span: 10 }} label='是否启动全站数据'>
  222. <Switch checked={score.all} checkedChildren='启用' unCheckedChildren='未启用' onChange={(value) => {
  223. score.all = value;
  224. this.setState({ score });
  225. this.submitScore();
  226. }} />
  227. </Form.Item>
  228. </Col>
  229. </Row>
  230. </Form>;
  231. }
  232. renderExercise() {
  233. return <TableLayout
  234. columns={this.exerciseColumns}
  235. list={this.state.exerciseList}
  236. pagination={false}
  237. loading={this.props.core.loading}
  238. />;
  239. }
  240. renderOther() {
  241. const { getFieldDecorator } = this.props.form;
  242. return <Form>
  243. <Row>
  244. <Col span={12}>
  245. <Form.Item labelCol={{ span: 8 }} wrapperCol={{ span: 16 }} label='长难句'>
  246. {getFieldDecorator('sentence.time', {
  247. rules: [
  248. { required: true, message: '输入每题预估时间' },
  249. ],
  250. })(
  251. <InputNumber placeholder='请输入每题预估时间' addonAfter="s" onChange={(value) => {
  252. this.changeValue('sentence', 'time', value);
  253. }} style={{ width: '200px' }} />,
  254. )}
  255. </Form.Item>
  256. </Col>
  257. <Col span={12}>
  258. <Form.Item labelCol={{ span: 8 }} wrapperCol={{ span: 16 }} label='数据机经'>
  259. {getFieldDecorator('textbook.time', {
  260. rules: [
  261. { required: true, message: '输入每题预估时间' },
  262. ],
  263. })(
  264. <InputNumber placeholder='请输入每题预估时间' addonAfter="s" onChange={(value) => {
  265. this.changeValue('textbook', 'time', value);
  266. }} style={{ width: '200px' }} />,
  267. )}
  268. </Form.Item>
  269. </Col>
  270. </Row>
  271. </Form>;
  272. }
  273. renderFilterTime() {
  274. const { getFieldDecorator } = this.props.form;
  275. return <Form>
  276. <Row>
  277. <Col span={12}>
  278. <Form.Item labelCol={{ span: 8 }} wrapperCol={{ span: 16 }} label='最短时间/题'>
  279. {getFieldDecorator('filter.min', {
  280. rules: [
  281. { required: true, message: '输入最短做题时间' },
  282. ],
  283. })(
  284. <InputNumber placeholder='请输入最短做题时间' addonAfter="s" onChange={(value) => {
  285. this.changeValue('filter', 'min', value);
  286. }} style={{ width: '200px' }} />,
  287. )}
  288. </Form.Item>
  289. </Col>
  290. <Col span={12}>
  291. <Form.Item labelCol={{ span: 8 }} wrapperCol={{ span: 16 }} label='最长时间/题'>
  292. {getFieldDecorator('filter.max', {
  293. rules: [
  294. { required: true, message: '输入最长做题时间' },
  295. ],
  296. })(
  297. <InputNumber placeholder='请输入最长做题时间' addonAfter="s" onChange={(value) => {
  298. this.changeValue('filter', 'max', value);
  299. }} style={{ width: '200px' }} />,
  300. )}
  301. </Form.Item>
  302. </Col>
  303. </Row>
  304. </Form>;
  305. }
  306. renderExercisePaperAuto() {
  307. const { getFieldDecorator } = this.props.form;
  308. return <Form>
  309. <Row>
  310. <Col span={12}>
  311. <Form.Item labelCol={{ span: 10 }} wrapperCol={{ span: 14 }} label='下次错误率组卷'>
  312. {getFieldDecorator('exercisePaper.date', {
  313. rules: [
  314. { required: true, message: '请输入下次练习错误率组卷时间' },
  315. ],
  316. })(
  317. <DatePicker placeholder='请输入下次练习错误率组卷时间' onChange={(value) => {
  318. this.changeValue('exercisePaper', 'date', value);
  319. }} />,
  320. )}
  321. </Form.Item>
  322. </Col>
  323. </Row>
  324. </Form>;
  325. }
  326. renderExamination() {
  327. return <TableLayout
  328. columns={this.examinationColumns}
  329. list={this.state.examinationList}
  330. pagination={false}
  331. loading={this.props.core.loading}
  332. />;
  333. }
  334. renderView() {
  335. const { tab } = this.state;
  336. return <Block>
  337. <Tabs activeKey={tab} onChange={(value) => {
  338. this.setState({ tab: value, selectedKeys: [], checkedKeys: [] });
  339. this.refresh(value);
  340. }}>
  341. <Tabs.TabPane tab="预估做题时间" key="exercise">
  342. {this.renderAll()}
  343. {this.renderExercise()}
  344. {this.renderOther()}
  345. </Tabs.TabPane>
  346. <Tabs.TabPane tab="数据剔除时间" key="filter">
  347. {this.renderFilterTime()}
  348. </Tabs.TabPane>
  349. <Tabs.TabPane tab="预估考试时间" key="examination">
  350. {this.renderExamination()}
  351. </Tabs.TabPane>
  352. <Tabs.TabPane tab="组卷" key="paper">
  353. {this.renderExercisePaperAuto()}
  354. </Tabs.TabPane>
  355. </Tabs>
  356. {tab !== 'examination' && <Row type="flex" justify="center">
  357. <Col>
  358. <Button type="primary" onClick={() => {
  359. this.submit(tab);
  360. }}>保存</Button>
  361. </Col>
  362. </Row>}
  363. </Block>;
  364. }
  365. }