page.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. submit() {
  140. const { form } = this.props;
  141. form.validateFields((err) => {
  142. if (!err) {
  143. const data = form.getFieldsValue();
  144. let handler;
  145. data.questionNoIds = this.state.questionNoIds;
  146. data.paperModule = this.state.data.paperModule;
  147. data.courseModule = this.state.data.courseModule;
  148. if (!data.id) {
  149. handler = Preview.add(data);
  150. } else {
  151. handler = Preview.edit(data);
  152. }
  153. handler.then((result) => {
  154. asyncSMessage('保存成功');
  155. if (data.id) {
  156. linkTo(`/course/preview/detail/${data.id}`);
  157. } else {
  158. linkTo(`/course/preview/detail/${result.id}`);
  159. }
  160. }).catch((e) => {
  161. if (e.result) form.setFields(formatFormError(data, e.result));
  162. });
  163. }
  164. });
  165. }
  166. addOnlineAction() {
  167. const { data } = this.state;
  168. asyncForm('添加', this.onlineList, {}, info => {
  169. if (info.time.length > 0) {
  170. info.startTime = info.time[0].format('YYYY-MM-DD HH:mm:ss');
  171. info.endTime = info.time[1].format('YYYY-MM-DD HH:mm:ss');
  172. }
  173. return Preview.addAssign(Object.assign({ paperModule: data.paperModule, paperId: data.id, courseModule: data.courseModule }, info)).then(() => {
  174. asyncSMessage('添加成功!');
  175. this.refresh();
  176. });
  177. }).catch(err => {
  178. console.log(err);
  179. });
  180. }
  181. addVsAction() {
  182. const { data } = this.state;
  183. asyncForm('添加', this.vsList, {}, info => {
  184. if (info.time.length > 0) {
  185. info.startTime = info.time[0].format('YYYY-MM-DD HH:mm:ss');
  186. info.endTime = info.time[1].format('YYYY-MM-DD HH:mm:ss');
  187. }
  188. return User.listCourseAppointment({ courseId: data.courseId, userId: info.userId, startTime: info.startTime, endTime: info.endTime }).then(result => {
  189. if (result.total === 0) {
  190. return Promise.reject(new Error('没有找到相关的预约记录'));
  191. } if (result.total > 1) {
  192. return Promise.reject(new Error('时间段内存在多条预约记录'));
  193. }
  194. return Preview.addAssign(Object.assign({ paperModule: data.paperModule, paperId: data.id, courseModule: data.courseModule }, info)).then(() => {
  195. asyncSMessage('添加成功!');
  196. this.refresh();
  197. });
  198. });
  199. }).catch(err => {
  200. console.log(err);
  201. });
  202. }
  203. renderBase() {
  204. const { data = {}, questionNoIds, questionType } = this.state;
  205. const { getFieldDecorator } = this.props.form;
  206. return <Block>
  207. <Form>
  208. {getFieldDecorator('id')(<input hidden />)}
  209. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='选择课程'>
  210. {getFieldDecorator('courseId', {
  211. rules: [
  212. { required: true, message: '请选择课程' },
  213. ],
  214. })(
  215. <Select {...this.state.courseId} disabled={data.id} placeholder='请选择课程' onChange={(v) => {
  216. this.refreshCourse(v);
  217. }} />,
  218. )}
  219. </Form.Item>
  220. {data.courseModule === 'video' && <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='选择课时'>
  221. {getFieldDecorator('courseNo', {
  222. rules: [
  223. { required: true, message: '请选择课时' },
  224. ],
  225. })(
  226. <Select select={this.state.courseNo} disabled={data.id || !data.courseId} placeholder='请选择课时' />,
  227. )}
  228. </Form.Item>}
  229. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='题型'>
  230. {getFieldDecorator('questionType', {
  231. rules: [
  232. { required: true, message: '请选择题型' },
  233. ],
  234. })(
  235. <Select select={questionType} placeholder='请选择题型' />,
  236. )}
  237. </Form.Item>
  238. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='作业标题'>
  239. {getFieldDecorator('title', {
  240. rules: [
  241. { required: true, message: '请输入作业标题' },
  242. ],
  243. })(
  244. <Input placeholder='请输入作业标题' />,
  245. )}
  246. </Form.Item>
  247. </Form>
  248. {data.paperModule && <Association title='选择题目' ids={questionNoIds} field={'questionNoIds'} module={data.paperModule} modal={false} onConfirm={(info) => {
  249. this.setState({ questionNoIds: info.questionNoIds });
  250. return Promise.resolve();
  251. }} />}
  252. </Block>;
  253. }
  254. renderOnline() {
  255. return <Block>
  256. <h1><Button onClick={() => {
  257. this.addOnlineAction();
  258. }}>添加指定做题人</Button></h1>
  259. </Block>;
  260. }
  261. renderVs() {
  262. return <Block>
  263. <h1><Button onClick={() => {
  264. this.addVsAction();
  265. }}>添加指定做题人</Button></h1>
  266. </Block>;
  267. }
  268. renderContent() {
  269. const { data } = this.state;
  270. switch (data.courseModule) {
  271. case 'video':
  272. return [];
  273. case 'online':
  274. return [this.renderOnline()];
  275. case 'vs':
  276. return [this.renderVs()];
  277. default:
  278. return <div />;
  279. }
  280. }
  281. renderView() {
  282. const { data = {} } = this.state;
  283. return <div flex>
  284. {this.renderBase()}
  285. <Row type="flex" justify="center">
  286. <Col>
  287. <Button type="primary" onClick={() => {
  288. this.submit();
  289. }}>保存</Button>
  290. </Col>
  291. </Row>
  292. {data.id > 0 && this.renderContent()}
  293. </div>;
  294. }
  295. }