page.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. import React from 'react';
  2. import { Form, Input, Button, Row, Col } from 'antd';
  3. import './index.less';
  4. import Page from '@src/containers/Page';
  5. import Block from '@src/components/Block';
  6. import Select from '@src/components/Select';
  7. import { formatFormError, bindSearch, formatDate, generateSearch } from '@src/services/Tools';
  8. import { asyncSMessage, asyncForm } from '@src/services/AsyncTools';
  9. import { QuestionType } from '../../../../Constant';
  10. import Association from '../../../components/Association';
  11. import { Preview } from '../../../stores/preview';
  12. import { Course } from '../../../stores/course';
  13. import { User } from '../../../stores/user';
  14. export default class extends Page {
  15. initState() {
  16. return {
  17. questionType: QuestionType,
  18. };
  19. }
  20. init() {
  21. this.onlineList = [{
  22. key: 'courseTime',
  23. type: 'select',
  24. select: [],
  25. name: '时间段',
  26. }, {
  27. key: 'time',
  28. type: 'daterange',
  29. name: '完成时间',
  30. }];
  31. this.vsList = [{
  32. key: 'userId',
  33. type: 'select',
  34. select: [],
  35. name: '学生',
  36. }, {
  37. key: 'time',
  38. type: 'daterange',
  39. name: '完成时间',
  40. }, {
  41. key: 'title',
  42. type: 'input',
  43. name: '作业标题',
  44. }];
  45. }
  46. initData() {
  47. const { id } = this.params;
  48. const { form } = this.props;
  49. const { module } = this.state.search;
  50. let handler;
  51. if (id) {
  52. handler = Preview.get({ id });
  53. } else {
  54. handler = Promise.resolve({ questionNoIds: [], courseModule: module });
  55. }
  56. handler
  57. .then(result => {
  58. const { questionNoIds } = result;
  59. generateSearch('courseId', {}, this, (search) => {
  60. return Course.list(Object.assign({ courseModule: result.courseModule }, search));
  61. }, (row) => {
  62. return {
  63. title: row.title,
  64. value: row.id,
  65. };
  66. }, result.courseId, null);
  67. form.getFieldDecorator('courseNo');
  68. form.setFieldsValue(result);
  69. this.setState({ module: result.courseModule, data: result, questionNoIds });
  70. this.refresh();
  71. if (result.courseId) this.refreshCourse(result.courseId);
  72. });
  73. }
  74. refresh() {
  75. const { data } = this.state;
  76. switch (data.courseModule) {
  77. case 'video':
  78. this.refreshNo(data.courseId);
  79. break;
  80. case 'online':
  81. bindSearch(this.onlineList, 'courseTime', this, (search) => {
  82. return Course.listTime(Object.assign({ courseId: data.courseId }, search));
  83. }, (row) => {
  84. return {
  85. title: `${formatDate(row.startTime, 'YYYY-MM-DD')}~${formatDate(row.endTime, 'YYYY-MM-DD')}`,
  86. value: row.id,
  87. };
  88. }, null, null);
  89. break;
  90. case 'vs':
  91. bindSearch(this.vsList, 'userId', this, (search) => {
  92. return User.list(search);
  93. }, (row) => {
  94. return {
  95. title: `${row.nickname}(${row.mobile})`,
  96. value: row.id,
  97. };
  98. }, null, null);
  99. break;
  100. default:
  101. }
  102. }
  103. refreshCourse(id) {
  104. if (!id) return;
  105. Course.get({ id }).then((info) => {
  106. const { data } = this.state;
  107. // 设置长难句paperModule信息
  108. data.paperModule = info.extend === 'sentence' ? 'sentence' : 'exercise';
  109. data.courseId = id;
  110. this.setState({ data });
  111. if (data.courseModule === 'video') {
  112. this.refreshNo(id);
  113. }
  114. this.refreshType(info.extend);
  115. });
  116. }
  117. refreshNo(id) {
  118. if (!id) return;
  119. Course.allNo({ courseId: id }).then(result => {
  120. this.setState({
  121. courseNo: result.map((row) => {
  122. return {
  123. title: `${row.title}(${row.no})`,
  124. value: row.id,
  125. };
  126. }),
  127. });
  128. });
  129. }
  130. refreshType(questionType) {
  131. if (questionType) {
  132. const { setFieldsValue } = this.props.form;
  133. setFieldsValue({ questionType });
  134. this.setState({ questionType: QuestionType.filter(row => row.value === questionType) });
  135. } else {
  136. this.setState({ questionType: QuestionType });
  137. }
  138. }
  139. onRefreshType(questionType) {
  140. this.setState({ currentQuestionType: questionType });
  141. }
  142. submit() {
  143. const { form } = this.props;
  144. form.validateFields((err) => {
  145. if (!err) {
  146. const data = form.getFieldsValue();
  147. let handler;
  148. data.questionNoIds = this.state.questionNoIds;
  149. data.paperModule = this.state.data.paperModule;
  150. data.courseModule = this.state.data.courseModule;
  151. if (!data.id) {
  152. handler = Preview.add(data);
  153. } else {
  154. handler = Preview.edit(data);
  155. }
  156. handler.then((result) => {
  157. asyncSMessage('保存成功');
  158. if (data.id) {
  159. linkTo(`/course/preview/detail/${data.id}`);
  160. } else {
  161. linkTo(`/course/preview/detail/${result.id}`);
  162. }
  163. }).catch((e) => {
  164. if (e.result) {
  165. form.setFields(formatFormError(data, e.result));
  166. } else {
  167. asyncSMessage(e.message, 'error');
  168. }
  169. });
  170. }
  171. });
  172. }
  173. addOnlineAction() {
  174. const { data } = this.state;
  175. asyncForm('添加', this.onlineList, {}, info => {
  176. if (info.time.length > 0) {
  177. info.startTime = info.time[0].format('YYYY-MM-DD');
  178. info.endTime = info.time[1].format('YYYY-MM-DD');
  179. }
  180. return Preview.addAssign(Object.assign({ paperModule: data.paperModule, paperId: data.id, courseModule: data.courseModule }, info)).then(() => {
  181. asyncSMessage('添加成功!');
  182. this.refresh();
  183. });
  184. }).catch(err => {
  185. console.log(err);
  186. });
  187. }
  188. addVsAction() {
  189. const { data } = this.state;
  190. asyncForm('添加', this.vsList, {}, info => {
  191. if (info.time.length > 0) {
  192. info.startTime = info.time[0].format('YYYY-MM-DD');
  193. info.endTime = info.time[1].format('YYYY-MM-DD');
  194. }
  195. return User.listCourseAppointment({ courseId: data.courseId, userId: info.userId, startTime: info.startTime, endTime: info.endTime }).then(result => {
  196. if (result.total === 0) {
  197. return Promise.reject(new Error('没有找到相关的预约记录'));
  198. } if (result.total > 1) {
  199. return Promise.reject(new Error('时间段内存在多条预约记录'));
  200. }
  201. return Preview.addAssign(Object.assign({ paperModule: data.paperModule, paperId: data.id, courseModule: data.courseModule }, info)).then(() => {
  202. asyncSMessage('添加成功!');
  203. this.refresh();
  204. });
  205. });
  206. }).catch(err => {
  207. console.log(err);
  208. });
  209. }
  210. renderBase() {
  211. const { data = {}, questionNoIds, questionType, currentQuestionType } = this.state;
  212. const { getFieldDecorator } = this.props.form;
  213. return <Block>
  214. <Form>
  215. {getFieldDecorator('id')(<input hidden />)}
  216. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='选择课程'>
  217. {getFieldDecorator('courseId', {
  218. rules: [
  219. { required: true, message: '请选择课程' },
  220. ],
  221. })(
  222. <Select {...this.state.courseId} disabled={data.id} placeholder='请选择课程' onChange={(v) => {
  223. this.refreshCourse(v);
  224. }} />,
  225. )}
  226. </Form.Item>
  227. {data.courseModule === 'video' && <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='选择课时'>
  228. {getFieldDecorator('courseNo', {
  229. rules: [
  230. { required: true, message: '请选择课时' },
  231. ],
  232. })(
  233. <Select select={this.state.courseNo} disabled={data.id || !data.courseId} placeholder='请选择课时' />,
  234. )}
  235. </Form.Item>}
  236. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='题型'>
  237. {getFieldDecorator('questionType', {
  238. rules: [
  239. { required: true, message: '请选择题型' },
  240. ],
  241. })(
  242. <Select select={questionType} placeholder='请选择题型' onChange={(v) => {
  243. this.onRefreshType(v);
  244. }} />,
  245. )}
  246. </Form.Item>
  247. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='作业标题'>
  248. {getFieldDecorator('title', {
  249. rules: [
  250. { required: true, message: '请输入作业标题' },
  251. ],
  252. })(
  253. <Input placeholder='请输入作业标题' />,
  254. )}
  255. </Form.Item>
  256. </Form>
  257. {data.paperModule && <Association title='选择题目' ids={questionNoIds} field={'questionNoIds'} questionType={currentQuestionType} module={data.paperModule} modal={false} onConfirm={(info) => {
  258. this.setState({ questionNoIds: info.questionNoIds });
  259. return Promise.resolve();
  260. }} />}
  261. </Block>;
  262. }
  263. renderOnline() {
  264. return <Block>
  265. <h1><Button onClick={() => {
  266. this.addOnlineAction();
  267. }}>添加指定做题人</Button></h1>
  268. </Block>;
  269. }
  270. renderVs() {
  271. return <Block>
  272. <h1><Button onClick={() => {
  273. this.addVsAction();
  274. }}>添加指定做题人</Button></h1>
  275. </Block>;
  276. }
  277. renderContent() {
  278. const { data } = this.state;
  279. switch (data.courseModule) {
  280. case 'video':
  281. return [];
  282. case 'online':
  283. return [this.renderOnline()];
  284. case 'vs':
  285. return [this.renderVs()];
  286. default:
  287. return <div />;
  288. }
  289. }
  290. renderView() {
  291. const { data = {} } = this.state;
  292. return <div flex>
  293. {this.renderBase()}
  294. <Row type="flex" justify="center">
  295. <Col>
  296. <Button type="primary" onClick={() => {
  297. this.submit();
  298. }}>保存</Button>
  299. </Col>
  300. </Row>
  301. {data.id > 0 && this.renderContent()}
  302. </div>;
  303. }
  304. }