page.js 8.5 KB

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