page.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. import React from 'react';
  2. import { Tabs, Form, Row, Col, InputNumber, Button } 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. const { exercise = {} } = this.state;
  22. return {
  23. title: row.label,
  24. dataIndex: row.value,
  25. render: (text, result) => {
  26. return <EditTableCell value={(exercise[result.id] || {})[text] || 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 <EditTableCell value={(examination[result.extend] || {}).number || 0} onChange={(v) => {
  41. this.changeMapValue('examination', result.extend, 'number', v);
  42. }} />;
  43. },
  44. }, {
  45. title: '做题时间',
  46. dataIndex: 'time',
  47. render: (text, result) => {
  48. const { examination = {} } = this.state;
  49. return <EditTableCell value={(examination[result.extend] || {}).time || 0} onChange={(v) => {
  50. this.changeMapValue('examination', result.extend, 'time', v);
  51. }} />;
  52. },
  53. }];
  54. this.state.tab = 'exercise';
  55. }
  56. initData() {
  57. Promise.all(this.structExamination(), this.structExercise())
  58. .then(() => {
  59. return this.refresh(this.state.tab);
  60. });
  61. }
  62. refresh(tab) {
  63. if (tab === 'exercise') {
  64. return Promise.all([this.refreshExercise(), this.refreshTextbook()]);
  65. }
  66. if (tab === 'examination') {
  67. return this.refreshExamination();
  68. }
  69. if (tab === 'filter') {
  70. return this.refreshFilter();
  71. }
  72. return Promise.reject();
  73. }
  74. refreshExercise() {
  75. return System.getExerciseTime().then((result) => {
  76. this.setState({ exercise: result || {} });
  77. });
  78. }
  79. refreshSentence() {
  80. return System.getSentenceTime().then((result) => {
  81. this.setState({ sentence: result || {} });
  82. const { form } = this.props;
  83. form.setFieldsValue(flattenObject(result, 'sentence'));
  84. });
  85. }
  86. refreshTextbook() {
  87. return System.getSentenceTime().then((result) => {
  88. this.setState({ textbook: result || {} });
  89. const { form } = this.props;
  90. form.setFieldsValue(flattenObject(result, 'textbook'));
  91. });
  92. }
  93. refreshExamination() {
  94. return System.getExaminationTime().then((result) => {
  95. this.setState({ examination: result || {} });
  96. });
  97. }
  98. refreshFilter() {
  99. return System.getFilterTime().then((result) => {
  100. this.setState({ filter: result || {} });
  101. const { form } = this.props;
  102. form.setFieldsValue(flattenObject(result, 'filter'));
  103. });
  104. }
  105. structExercise() {
  106. return Exercise.allStruct().then(result => {
  107. const list = result.map(row => { row.title = `${row.titleZh}/${row.titleEn}`; return row; });
  108. const map = getMap(list, 'id');
  109. this.setState({
  110. exerciseList: list.filter(row => row.level === 2).map(row => {
  111. const parent = map[row.parentId];
  112. row.title = `${parent.title}-${row.title}`;
  113. return row;
  114. }),
  115. });
  116. });
  117. }
  118. structExamination() {
  119. return Exercise.allStruct().then(result => {
  120. const list = result.map(row => { row.title = `${row.titleZh}/${row.titleEn}`; return row; });
  121. this.setState({
  122. examinationList: list.filter(row => row.level === 1 && row.isExamination),
  123. });
  124. });
  125. }
  126. changeMapValue(field, index, key, value) {
  127. const data = this.state[field] || {};
  128. data[index] = data[index] || {};
  129. data[index][key] = value;
  130. this.setState({ [field]: data });
  131. }
  132. changeValue(field, key, value) {
  133. const data = this.state[field] || {};
  134. data[key] = value;
  135. this.setState({ [field]: data });
  136. }
  137. submit(tab) {
  138. let handler;
  139. if (tab === 'exercise') {
  140. handler = this.submitExercise();
  141. handler = handler.then(() => {
  142. // return this.submitSentence();
  143. return this.submitTextbook();
  144. });
  145. }
  146. if (tab === 'examination') {
  147. handler = this.submitExamination();
  148. }
  149. if (tab === 'filter') {
  150. handler = this.submitFilter();
  151. }
  152. handler.then(() => {
  153. asyncSMessage('保存成功');
  154. });
  155. }
  156. submitExercise() {
  157. const { exercise } = this.state;
  158. return System.setExerciseTime(exercise);
  159. }
  160. submitSentence() {
  161. const { sentence } = this.state;
  162. return System.setSentenceTime(sentence);
  163. }
  164. submitTextbook() {
  165. const { textbook } = this.state;
  166. return System.setTextbookTime(textbook);
  167. }
  168. submitExamination() {
  169. const { examination } = this.state;
  170. return System.setExaminationTime(examination);
  171. }
  172. submitFilter() {
  173. const { form } = this.props;
  174. return new Promise((resolve, reject) => {
  175. form.validateFields(['filter'], (err, values) => {
  176. if (!err) {
  177. System.setFilterTime(values.filter)
  178. .then(() => {
  179. resolve();
  180. })
  181. .catch((e) => {
  182. reject(e);
  183. });
  184. }
  185. });
  186. });
  187. }
  188. renderExercise() {
  189. return <TableLayout
  190. columns={this.exerciseColumns}
  191. list={this.state.exerciseList}
  192. pagination={false}
  193. loading={this.props.core.loading}
  194. />;
  195. }
  196. renderSentence() {
  197. const { getFieldDecorator } = this.props.form;
  198. return <Form>
  199. <Row>
  200. <Col span={12}>
  201. <Form.Item labelCol={{ span: 8 }} wrapperCol={{ span: 16 }} label='每题预估时间'>
  202. {getFieldDecorator('sentence.time', {
  203. rules: [
  204. { required: true, message: '输入每题预估时间' },
  205. ],
  206. })(
  207. <InputNumber placeholder='请输入每题预估时间' addonAfter="s" onChange={(value) => {
  208. this.changeValue('sentence', 'time', value);
  209. }} style={{ width: '200px' }} />,
  210. )}
  211. </Form.Item>
  212. </Col>
  213. </Row>
  214. </Form>;
  215. }
  216. renderTextbook() {
  217. const { getFieldDecorator } = this.props.form;
  218. return <Form>
  219. <Row>
  220. <Col span={12}>
  221. <Form.Item labelCol={{ span: 8 }} wrapperCol={{ span: 16 }} label='每题预估时间'>
  222. {getFieldDecorator('sentence.time', {
  223. rules: [
  224. { required: true, message: '输入每题预估时间' },
  225. ],
  226. })(
  227. <InputNumber placeholder='请输入每题预估时间' addonAfter="s" onChange={(value) => {
  228. this.changeValue('sentence', 'time', value);
  229. }} style={{ width: '200px' }} />,
  230. )}
  231. </Form.Item>
  232. </Col>
  233. </Row>
  234. </Form>;
  235. }
  236. renderFilterTime() {
  237. const { getFieldDecorator } = this.props.form;
  238. return <Form>
  239. <Row>
  240. <Col span={12}>
  241. <Form.Item labelCol={{ span: 8 }} wrapperCol={{ span: 16 }} label='最短时间/题'>
  242. {getFieldDecorator('filter.min', {
  243. rules: [
  244. { required: true, message: '输入最短做题时间' },
  245. ],
  246. })(
  247. <InputNumber placeholder='请输入最短做题时间' addonAfter="s" onChange={(value) => {
  248. this.changeValue('filter', 'min', value);
  249. }} style={{ width: '200px' }} />,
  250. )}
  251. </Form.Item>
  252. </Col>
  253. <Col span={12}>
  254. <Form.Item labelCol={{ span: 8 }} wrapperCol={{ span: 16 }} label='最长时间/题'>
  255. {getFieldDecorator('filter.max', {
  256. rules: [
  257. { required: true, message: '输入最长做题时间' },
  258. ],
  259. })(
  260. <InputNumber placeholder='请输入最长做题时间' addonAfter="s" onChange={(value) => {
  261. this.changeValue('filter', 'max', value);
  262. }} style={{ width: '200px' }} />,
  263. )}
  264. </Form.Item>
  265. </Col>
  266. </Row>
  267. </Form>;
  268. }
  269. renderExamination() {
  270. return <TableLayout
  271. columns={this.examinationColumns}
  272. list={this.state.examinationList}
  273. pagination={false}
  274. loading={this.props.core.loading}
  275. />;
  276. }
  277. renderView() {
  278. const { tab } = this.state;
  279. return <Block><Tabs activeKey={tab} onChange={(value) => {
  280. this.setState({ tab: value, selectedKeys: [], checkedKeys: [] });
  281. this.refresh(value);
  282. }}>
  283. <Tabs.TabPane tab="预估做题时间" key="exercise">
  284. <h1>练习设置</h1>
  285. {this.renderExercise()}
  286. <h1>机经设置</h1>
  287. {this.renderTextbook()}
  288. </Tabs.TabPane>
  289. <Tabs.TabPane tab="数据剔除时间" key="filter">
  290. {this.renderFilterTime()}
  291. </Tabs.TabPane>
  292. <Tabs.TabPane tab="预估考试时间" key="examination">
  293. {this.renderExamination()}
  294. </Tabs.TabPane>
  295. </Tabs>
  296. <Row type="flex" justify="center">
  297. <Col>
  298. <Button type="primary" onClick={() => {
  299. this.submit(tab);
  300. }}>保存</Button>
  301. </Col>
  302. </Row>
  303. </Block>;
  304. }
  305. }