page.js 11 KB

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