page.js 10 KB

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