page.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. import React from 'react';
  2. import moment from 'moment';
  3. import './index.less';
  4. import Page from '@src/containers/Page';
  5. import Block from '@src/components/Block';
  6. import FilterLayout from '@src/layouts/FilterLayout';
  7. import ActionLayout from '@src/layouts/ActionLayout';
  8. import TableLayout from '@src/layouts/TableLayout';
  9. import { formatDate, bindSearch, getMap } from '@src/services/Tools';
  10. import { asyncSMessage, asyncForm } from '@src/services/AsyncTools';
  11. import { CourseSource, CourseStatus } from '../../../../Constant';
  12. import { Course } from '../../../stores/course';
  13. import { User } from '../../../stores/user';
  14. const CourseSourceMap = getMap(CourseSource, 'value', 'label');
  15. const CourseStatusMap = getMap(CourseStatus, 'value', 'label');
  16. export default class extends Page {
  17. init() {
  18. this.onlineAction = [{
  19. key: 'addOnlineStudent',
  20. name: '添加学员',
  21. }];
  22. this.onlineFilter = [{
  23. key: 'id',
  24. type: 'hidden',
  25. }, {
  26. key: 'timeId',
  27. type: 'select',
  28. allowClear: true,
  29. select: [],
  30. number: true,
  31. name: '时间段',
  32. }, {
  33. key: 'userId',
  34. type: 'select',
  35. name: '学员',
  36. allowClear: true,
  37. select: [],
  38. number: true,
  39. placeholder: '请输入',
  40. }];
  41. this.onlineList = [{
  42. key: 'timeId',
  43. type: 'select',
  44. select: [],
  45. name: '时间段',
  46. }, {
  47. key: 'userIds',
  48. type: 'multiple',
  49. name: '学员',
  50. allowClear: true,
  51. select: [],
  52. number: true,
  53. placeholder: '请输入',
  54. }];
  55. this.onlineColumns = [{
  56. title: '时间段',
  57. dataIndex: 'time',
  58. render: (text) => {
  59. return `${formatDate(text.startTime, 'YYYY-MM-DD')}~${formatDate(text.endTime, 'YYYY-MM-DD')}`;
  60. },
  61. }, {
  62. title: '学员id',
  63. dataIndex: 'userId',
  64. }, {
  65. title: '学员名称',
  66. dataIndex: 'user.nickname',
  67. }, {
  68. title: '手机号',
  69. dataIndex: 'user.mobile',
  70. }, {
  71. title: '操作',
  72. dataIndex: 'handler',
  73. render: (text, record) => {
  74. return <div className="table-button">
  75. {<a onClick={() => {
  76. this.deleteOnlineStudent(record);
  77. }}>删除</a>}
  78. </div>;
  79. },
  80. }];
  81. this.vsAction = [{
  82. key: 'addVsStudent',
  83. name: '添加学员',
  84. }];
  85. this.vsList = [{
  86. key: 'id',
  87. type: 'hidden',
  88. }, {
  89. key: 'userId',
  90. type: 'select',
  91. name: '学员',
  92. select: [],
  93. number: true,
  94. placeholder: '请输入',
  95. }, {
  96. key: 'time',
  97. type: 'daterange',
  98. name: '有效期',
  99. }, {
  100. key: 'teacherId',
  101. type: 'select',
  102. select: [],
  103. name: '老师',
  104. }, {
  105. key: 'vsNumber',
  106. type: 'number',
  107. name: '课时数',
  108. }];
  109. this.vsColumns = [{
  110. title: '学员名称',
  111. dataIndex: 'user.nickname',
  112. }, {
  113. title: '有效期',
  114. dataIndex: 'time',
  115. render: (text, record) => {
  116. return record.isUsed ? `${formatDate(record.useStartTime, 'YYYY-MM-DD')}~${formatDate(record.useEndTime, 'YYYY-MM-DD')}` : '';
  117. },
  118. }, {
  119. title: '添加方式',
  120. dataIndex: 'source',
  121. render: (text) => {
  122. return CourseSourceMap[text] || text;
  123. },
  124. }, {
  125. title: '授课老师',
  126. dataIndex: 'teacher.realname',
  127. }, {
  128. title: '状态',
  129. dataIndex: 'status',
  130. render: (text, record) => {
  131. let status = 0;
  132. if (record.isUsed) {
  133. const end = new Date(record.useEndTime);
  134. status = 1;
  135. if (new Date().getTime() > end.getTime()) {
  136. status = 2;
  137. }
  138. }
  139. return CourseStatusMap[status] || status;
  140. },
  141. }, {
  142. title: '操作',
  143. dataIndex: 'handler',
  144. render: (text, record) => {
  145. return <div className="table-button">
  146. {<a onClick={() => {
  147. this.changeVs(record);
  148. }}>修改</a>}
  149. </div>;
  150. },
  151. }];
  152. }
  153. initData() {
  154. const { id } = this.params;
  155. let handler;
  156. if (id) {
  157. handler = Course.get({ id });
  158. }
  159. handler
  160. .then(result => {
  161. this.setState({ module: result.courseModule });
  162. this.setState({ data: result });
  163. this.refresh();
  164. });
  165. }
  166. refresh() {
  167. const { id } = this.params;
  168. const { module } = this.state;
  169. switch (module) {
  170. case 'video':
  171. break;
  172. case 'online':
  173. bindSearch(this.onlineFilter, 'timeId', this, (search) => {
  174. return Course.listTime(Object.assign({ courseId: id }, search));
  175. }, (row) => {
  176. return {
  177. title: `${formatDate(row.startTime, 'YYYY-MM-DD')}~${formatDate(row.endTime, 'YYYY-MM-DD')}`,
  178. value: row.id,
  179. };
  180. }, this.state.search.timeId ? Number(this.state.search.timeId) : null, null);
  181. bindSearch(this.onlineFilter, 'userId', this, (search) => {
  182. return User.list(search);
  183. }, (row) => {
  184. return {
  185. title: `${row.nickname}(${row.mobile})`,
  186. value: row.id,
  187. };
  188. }, null, null);
  189. bindSearch(this.onlineList, 'timeId', this, (search) => {
  190. return Course.listTime(Object.assign({ courseId: id }, search));
  191. }, (row) => {
  192. return {
  193. title: `${formatDate(row.startTime, 'YYYY-MM-DD')}~${formatDate(row.endTime, 'YYYY-MM-DD')}`,
  194. value: row.id,
  195. };
  196. }, null, null);
  197. bindSearch(this.onlineList, 'userIds', this, (search) => {
  198. return User.list(search);
  199. }, (row) => {
  200. return {
  201. title: `${row.nickname}(${row.mobile})`,
  202. value: row.id,
  203. };
  204. }, [], null);
  205. this.refreshOnline();
  206. break;
  207. case 'vs':
  208. this.refreshVs();
  209. break;
  210. default:
  211. }
  212. }
  213. refreshOnline() {
  214. const { id } = this.params;
  215. Course.listStudentOnline(Object.assign({ courseId: id }, this.state.search)).then(result => {
  216. this.setTableData(result.list, result.total);
  217. });
  218. }
  219. refreshVs() {
  220. const { id } = this.params;
  221. Course.listStudentVs(Object.assign({ courseId: id }, this.state.search)).then(result => {
  222. this.setTableData(result.list, result.total);
  223. });
  224. }
  225. addOnlineStudentAction() {
  226. const { id } = this.params;
  227. asyncForm('添加', this.onlineList, {}, data => {
  228. return Promise.all(data.userIds.map(userId => Course.addStudentOnline({ courseId: id, timeId: data.timeId, userId }))).then(() => {
  229. asyncSMessage('添加成功!');
  230. this.refresh();
  231. });
  232. }).catch(err => {
  233. console.log(err);
  234. });
  235. }
  236. deleteOnlineStudent(row) {
  237. Course.delStudentOnline({ id: row.id }).then(() => {
  238. asyncSMessage('删除成功!');
  239. this.refresh();
  240. });
  241. }
  242. changeVs(record) {
  243. const { id } = this.params;
  244. record.time = [moment(record.useStartTime), moment(record.useEndTime)];
  245. asyncForm('修改', this.vsList, record, data => {
  246. return Course.editStudentVs(Object.assign({ courseId: id }, data)).then(() => {
  247. asyncSMessage('添加成功!');
  248. this.refresh();
  249. });
  250. }).then(component => {
  251. bindSearch(this.vsList, 'userId', component, (search) => {
  252. return User.list(search);
  253. }, (row) => {
  254. return {
  255. title: `${row.nickname}(${row.mobile})`,
  256. value: row.id,
  257. };
  258. }, record.userId, null);
  259. bindSearch(this.vsList, 'teacherId', component, (search) => {
  260. return Course.listTeacher(Object.assign({ courseId: id }, search));
  261. }, (row) => {
  262. return {
  263. title: row.realname,
  264. value: row.id,
  265. };
  266. }, record.teacherId, null);
  267. });
  268. }
  269. addVsStudentAction() {
  270. const { id } = this.params;
  271. asyncForm('添加', this.vsList, {}, data => {
  272. ([data.useStartTime, data.useEndTime] = data.time);
  273. return Course.addStudentVs(Object.assign({ courseId: id }, data)).then(() => {
  274. asyncSMessage('添加成功!');
  275. this.refresh();
  276. });
  277. }).then(component => {
  278. bindSearch(this.vsList, 'userId', component, (search) => {
  279. return User.list(search);
  280. }, (row) => {
  281. return {
  282. title: `${row.nickname}(${row.mobile})`,
  283. value: row.id,
  284. };
  285. }, null, null);
  286. bindSearch(this.vsList, 'teacherId', component, (search) => {
  287. return Course.listTeacher(Object.assign({ courseId: id }, search));
  288. }, (row) => {
  289. return {
  290. title: row.realname,
  291. value: row.id,
  292. };
  293. }, null, null);
  294. });
  295. }
  296. renderOnline() {
  297. return <Block flex>
  298. {<FilterLayout
  299. show
  300. itemList={this.onlineFilter}
  301. data={this.state.search}
  302. onChange={data => {
  303. this.search(data);
  304. }} />}
  305. <ActionLayout
  306. itemList={this.onlineAction}
  307. selectedKeys={this.state.selectedKeys}
  308. onAction={key => this.onAction(key)}
  309. />
  310. <TableLayout
  311. columns={this.onlineColumns}
  312. list={this.state.list}
  313. pagination={this.state.page}
  314. loading={this.props.core.loading}
  315. onChange={(pagination, filters, sorter) => this.tableChange(pagination, filters, sorter)}
  316. onSelect={(keys, rows) => this.tableSelect(keys, rows)}
  317. selectedKeys={this.state.selectedKeys}
  318. />
  319. </Block>;
  320. }
  321. renderVs() {
  322. const { data } = this.state;
  323. return <Block flex>
  324. {data.vsType === 'novice' && <ActionLayout
  325. itemList={this.vsAction}
  326. selectedKeys={this.state.selectedKeys}
  327. onAction={key => this.onAction(key)}
  328. />}
  329. <TableLayout
  330. columns={this.vsColumns}
  331. list={this.state.list}
  332. pagination={this.state.page}
  333. loading={this.props.core.loading}
  334. onChange={(pagination, filters, sorter) => this.tableChange(pagination, filters, sorter)}
  335. onSelect={(keys, rows) => this.tableSelect(keys, rows)}
  336. selectedKeys={this.state.selectedKeys}
  337. />
  338. </Block>;
  339. }
  340. renderView() {
  341. switch (this.state.module) {
  342. case 'online':
  343. return [this.renderOnline()];
  344. case 'vs':
  345. return [this.renderVs()];
  346. case 'video':
  347. return [];
  348. default:
  349. return <div />;
  350. }
  351. }
  352. }