page.js 10.0 KB

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