responsibilityCenterConnect.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * @Author: your name
  3. * @Date: 2021-07-27 19:58:13
  4. * @LastEditTime: 2021-07-27 20:25:41
  5. * @LastEditors: Please set LastEditors
  6. * @Description: In User Settings Edit
  7. * @FilePath: /TracerMethodology_PC/mock/responsibilityCenterConnect.js
  8. */
  9. import moment from 'moment';
  10. import { parse } from 'url'; // mock tableListDataSource
  11. const genList = (current, pageSize) => {
  12. const tableListDataSource = [];
  13. for (let i = 0; i < pageSize; i += 1) {
  14. const index = (current - 1) * 10 + i;
  15. tableListDataSource.push({
  16. key: index,
  17. id: index,
  18. responsibilityId: `责任中心id ${index}`,
  19. responsibilityName: `责任中心名 ${index}`,
  20. child:[
  21. {
  22. key: `one${index}`,
  23. id: `one${index}`,
  24. responsibilityName: `责任中心名 ${index}-0`,
  25. responsibilityId: `责任中心id ${index}-0`,
  26. departments:[
  27. {
  28. departmentId:`two${index}`,
  29. departmentName:`科室名 ${index}`
  30. },
  31. {
  32. departmentId:`two${index}`,
  33. departmentName:`科室名 ${index}`
  34. }
  35. ]
  36. },
  37. {
  38. key: index,
  39. id: index,
  40. responsibilityName: `责任中心名 ${index}-1`,
  41. responsibilityId: `责任中心id ${index}-1`,
  42. departments:[
  43. {
  44. departmentId:index,
  45. departmentName:`科室名 ${index}`
  46. },
  47. {
  48. departmentId:`three${index}`,
  49. departmentName:`科室名 ${index}`
  50. }
  51. ]
  52. }
  53. ],
  54. // owner: '曲丽丽',
  55. // desc: '这是一段描述',
  56. // updatedAt: moment().format('YYYY-MM-DD'),
  57. createTime: moment().format('YYYY-MM-DD'),
  58. });
  59. }
  60. // console.log({tableListDataSource});
  61. tableListDataSource.reverse();
  62. return tableListDataSource;
  63. };
  64. let tableListDataSource = genList(1, 100);
  65. function getList(req, res, u) {
  66. let realUrl = u;
  67. if (!realUrl || Object.prototype.toString.call(realUrl) !== '[object String]') {
  68. realUrl = req.url;
  69. }
  70. const { current = 1, pageSize = 10 } = req.query;
  71. const params = parse(realUrl, true).query;
  72. let dataSource = [...tableListDataSource].slice((current - 1) * pageSize, current * pageSize);
  73. if (params.sorter) {
  74. const sorter = JSON.parse(params.sorter);
  75. dataSource = dataSource.sort((prev, next) => {
  76. let sortNumber = 0;
  77. Object.keys(sorter).forEach((key) => {
  78. if (sorter[key] === 'descend') {
  79. if (prev[key] - next[key] > 0) {
  80. sortNumber += -1;
  81. } else {
  82. sortNumber += 1;
  83. }
  84. return;
  85. }
  86. if (prev[key] - next[key] > 0) {
  87. sortNumber += 1;
  88. } else {
  89. sortNumber += -1;
  90. }
  91. });
  92. return sortNumber;
  93. });
  94. }
  95. if (params.filter) {
  96. const filter = JSON.parse(params.filter);
  97. if (Object.keys(filter).length > 0) {
  98. dataSource = dataSource.filter((item) => {
  99. return Object.keys(filter).some((key) => {
  100. if (!filter[key]) {
  101. return true;
  102. }
  103. if (filter[key].includes(`${item[key]}`)) {
  104. return true;
  105. }
  106. return false;
  107. });
  108. });
  109. }
  110. }
  111. if (params.name) {
  112. dataSource = dataSource.filter((data) => data?.name?.includes(params.name || ''));
  113. }
  114. const result = {
  115. data: {
  116. list: dataSource,
  117. totalCount: tableListDataSource.length,
  118. pageSize: pageSize,
  119. },
  120. status: 200,
  121. success: true,
  122. };
  123. return res.json(result);
  124. }
  125. function updateList(req, res, u, b) {
  126. let realUrl = u;
  127. if (!realUrl || Object.prototype.toString.call(realUrl) !== '[object String]') {
  128. realUrl = req.url;
  129. }
  130. // const body = (b && b.body) || req.body;
  131. const { method, body } = req;
  132. const { name,isHospital,parentId,parentName} = body;
  133. switch (method) {
  134. /* eslint no-case-declarations:0 */
  135. case 'DELETE':
  136. const {ids} = body;
  137. // console.log({realUrl});
  138. tableListDataSource = tableListDataSource.filter((item) => !ids.includes(item.id));
  139. break;
  140. case 'POST':
  141. (() => {
  142. const i = Math.ceil(Math.random() * 10000);
  143. const newRule = {
  144. key: tableListDataSource.length,
  145. id:tableListDataSource.length,
  146. avatar: [
  147. 'https://gw.alipayobjects.com/zos/rmsportal/eeHMaZBwmTvLdIwMfBpg.png',
  148. 'https://gw.alipayobjects.com/zos/rmsportal/udxAbMEhpwthVVcjLXik.png',
  149. ][i % 2],
  150. name,isHospital,parentId,parentName,
  151. createdAt: moment().format('YYYY-MM-DD'),
  152. };
  153. tableListDataSource.unshift(newRule);
  154. return res.json(newRule);
  155. })();
  156. return;
  157. case 'update':
  158. (() => {
  159. let newRule = {};
  160. tableListDataSource = tableListDataSource.map((item) => {
  161. if (item.key === key) {
  162. newRule = { ...item, desc, name };
  163. return { ...item, desc, name };
  164. }
  165. return item;
  166. });
  167. return res.json(newRule);
  168. })();
  169. return;
  170. default:
  171. break;
  172. }
  173. const result = {
  174. list: tableListDataSource,
  175. pagination: {
  176. total: tableListDataSource.length,
  177. },
  178. };
  179. res.json(result);
  180. }
  181. export default {
  182. // GET 科室列表
  183. 'GET /api/centerDepartment/list':getList,
  184. 'POST /api/centerDepartment/save': updateList,
  185. };