page.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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.id] || {}).number || 0} onChange={(v) => {
  41. this.changeMapValue('examination', result.id, '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.id] || {}).time || 0} onChange={(v) => {
  50. this.changeValue('examination', result.id, '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 this.refreshExercise();
  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. refreshExamination() {
  80. return System.getExaminationTime().then((result) => {
  81. this.setState({ examination: result || {} });
  82. });
  83. }
  84. refreshFilter() {
  85. return System.getFilterTime().then((result) => {
  86. this.setState({ filter: result });
  87. const { form } = this.props;
  88. form.setFieldsValue(flattenObject(result, 'filter'));
  89. });
  90. }
  91. structExercise() {
  92. return Exercise.allStruct().then(result => {
  93. const list = result.map(row => { row.title = `${row.titleZh}/${row.titleEn}`; return row; });
  94. const map = getMap(list, 'id');
  95. this.setState({
  96. exerciseList: list.filter(row => row.level === 2).map(row => {
  97. const parent = map[row.parentId];
  98. row.title = `${parent.title}-${row.title}`;
  99. return row;
  100. }),
  101. });
  102. });
  103. }
  104. structExamination() {
  105. return Examination.allStruct().then(result => {
  106. const list = result.map(row => { row.title = `${row.titleZh}/${row.titleEn}`; return row; });
  107. this.setState({
  108. examinationList: list.filter(row => row.level === 1),
  109. });
  110. });
  111. }
  112. changeMapValue(field, index, key, value) {
  113. const data = this.state[field] || {};
  114. data[index] = data[index] || {};
  115. data[index][key] = value;
  116. this.setState({ [field]: data });
  117. }
  118. changeValue(field, key, value) {
  119. const data = this.state[field] || {};
  120. data[key] = value;
  121. this.setState({ [field]: data });
  122. }
  123. submit(tab) {
  124. let handler;
  125. if (tab === 'exercise') {
  126. handler = this.submitExercise();
  127. }
  128. if (tab === 'examination') {
  129. handler = this.submitExamination();
  130. }
  131. if (tab === 'filter') {
  132. handler = this.submitFilter();
  133. }
  134. handler.then(() => {
  135. asyncSMessage('保存成功');
  136. });
  137. }
  138. submitExercise() {
  139. const { exercise } = this.state;
  140. return System.setExerciseTime(exercise);
  141. }
  142. submitExamination() {
  143. const { examination } = this.state;
  144. return System.setExaminationTime(examination);
  145. }
  146. submitFilter() {
  147. const { form } = this.props;
  148. return new Promise((resolve, reject) => {
  149. form.validateFields(['filter'], (err, values) => {
  150. if (!err) {
  151. System.setFilterTime(values.filter)
  152. .then(() => {
  153. resolve();
  154. })
  155. .catch((e) => {
  156. reject(e);
  157. });
  158. }
  159. });
  160. });
  161. }
  162. renderExercise() {
  163. return <TableLayout
  164. columns={this.exerciseColumns}
  165. list={this.state.exerciseList}
  166. pagination={false}
  167. loading={this.props.core.loading}
  168. />;
  169. }
  170. renderFilterTime() {
  171. const { getFieldDecorator } = this.props.form;
  172. return <Form>
  173. <Row>
  174. <Col span={12}>
  175. <Form.Item labelCol={{ span: 8 }} wrapperCol={{ span: 16 }} label='最短时间/题'>
  176. {getFieldDecorator('filter.min', {
  177. rules: [
  178. { required: true, message: '输入最短做题时间' },
  179. ],
  180. })(
  181. <InputNumber placeholder='请输入最短做题时间' addonAfter="s" onChange={(value) => {
  182. this.changeValue('filter', 'min', value);
  183. }} style={{ width: '200px' }} />,
  184. )}
  185. </Form.Item>
  186. </Col>
  187. <Col span={12}>
  188. <Form.Item labelCol={{ span: 8 }} wrapperCol={{ span: 16 }} label='最长时间/题'>
  189. {getFieldDecorator('filter.max', {
  190. rules: [
  191. { required: true, message: '输入最长做题时间' },
  192. ],
  193. })(
  194. <InputNumber placeholder='请输入最长做题时间' addonAfter="s" onChange={(value) => {
  195. this.changeValue('filter', 'max', value);
  196. }} style={{ width: '200px' }} />,
  197. )}
  198. </Form.Item>
  199. </Col>
  200. </Row>
  201. </Form>;
  202. }
  203. renderExamination() {
  204. return <TableLayout
  205. columns={this.exerciseColumns}
  206. list={this.state.exerciseList}
  207. pagination={false}
  208. loading={this.props.core.loading}
  209. />;
  210. }
  211. renderView() {
  212. const { tab } = this.state;
  213. return <Block><Tabs activeKey={tab} onChange={(value) => {
  214. this.setState({ tab: value, selectedKeys: [], checkedKeys: [] });
  215. this.refresh(value);
  216. }}>
  217. <Tabs.TabPane tab="预估做题时间" key="exercise">
  218. {this.renderExercise()}
  219. </Tabs.TabPane>
  220. <Tabs.TabPane tab="数据剔除时间" key="filter">
  221. {this.renderFilterTime()}
  222. </Tabs.TabPane>
  223. <Tabs.TabPane tab="预估考试时间" key="examination">
  224. {this.renderExamination()}
  225. </Tabs.TabPane>
  226. </Tabs>
  227. <Row type="flex" justify="center">
  228. <Col>
  229. <Button type="primary" onClick={() => {
  230. this.submit(tab);
  231. }}>保存</Button>
  232. </Col>
  233. </Row>
  234. </Block>;
  235. }
  236. }