page.js 10 KB

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