page.js 13 KB

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