page.js 9.4 KB

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