page.js 9.0 KB

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