handle.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. 'use strict';
  2. const Service = require('egg').Service;
  3. const xlsx = require('xlsx');
  4. const fs = require('fs');
  5. class HandleService extends Service {
  6. constructor(ctx) {
  7. super(ctx);
  8. this.progressModel = this.ctx.model.Progress;
  9. this.config = this.app.config;
  10. }
  11. // 导入处理
  12. async import({ type, taskId }) {
  13. const files = this.ctx.request.files[0];
  14. const workbook = xlsx.readFile(files.filepath);
  15. const name = workbook.SheetNames[0];
  16. const sheet = workbook.Sheets[name];
  17. const data = xlsx.utils.sheet_to_json(sheet);
  18. const total = data.length;
  19. let progress = 0;
  20. for (let i = 0; i < data.length; i++) {
  21. // 此处根据类型调用不同的数据存储服务
  22. const item = data[i];
  23. const fun = this.ctx.service.imperts[`import${type}`];
  24. await fun({ item, _this: this });
  25. // 此处调用进度存储
  26. progress = (i + 1) / total * 100;
  27. await this.ctx.service.progress.updateProgress({ taskId, progress, type });
  28. }
  29. }
  30. // 导出处理
  31. async export({ type, taskId, filters, ids, fileType }) {
  32. // 创建过滤条件
  33. const filter = {};
  34. let res;
  35. // 默认总数
  36. let total = (ids && ids.length) || 0;
  37. // 如果条件存在
  38. if (filters) {
  39. filter.$or = [];
  40. for (const i in filters) {
  41. const data = `{ "${i}": { "$regex": "${filters[i]}" } }`;
  42. filter.$or.push(JSON.parse(data));
  43. }
  44. // 获取查询总数函数
  45. const totalfun = this.ctx.service.total[`total${type}`];
  46. // 赋值总数
  47. total = await totalfun({ ...filter, _this: this });
  48. }
  49. // 获取查询函数
  50. const fun = this.ctx.service.exports[`exports${type}`];
  51. // 计算页数
  52. const pages = Math.ceil(total / 10);
  53. // 此处调用创建文件
  54. const file = await this.establishFile({ taskId, type, fileType });
  55. if (file === 'success') {
  56. for (let i = 0; i < pages; i++) {
  57. // 进度
  58. const progress = (i + 1) / pages * 100;
  59. // 更新进度
  60. await this.ctx.service.progress.updateProgress({ taskId, progress, type });
  61. // 调用查询数据函数
  62. res = await fun({ skip: i, filter, ids, _this: this });
  63. // 此处调用写入文件
  64. if (res.length <= 0) return;
  65. await this.insertFile({ type, data: res, taskId, fileType });
  66. }
  67. }
  68. }
  69. // 创建文件
  70. async establishFile({ taskId, type, fileType }) {
  71. const fun = this.ctx.service[fileType].establish;
  72. return await fun({ fileName: taskId, type, _this: this });
  73. }
  74. // 持续写入文件
  75. async insertFile({ type, data, taskId, fileType }) {
  76. const fun = this.ctx.service[fileType].insertFile;
  77. await fun({ type, fileName: taskId, data, _this: this });
  78. }
  79. // 模板下载
  80. async template({ type, fileType, fileName }) {
  81. const fun = this.ctx.service.template[fileType];
  82. const url = await fun({ fileName, type, _this: this });
  83. const postfix = url.split('.')[1];
  84. this.ctx.attachment(`${fileName}.${postfix}`);
  85. return fs.createReadStream(url);
  86. }
  87. // 文件下载
  88. async exportsDownload({ taskId, fileName }) {
  89. const url = `${this.app.config.exportsPath}/${taskId}.xlsx`;
  90. const postfix = url.split('.')[1];
  91. this.ctx.attachment(`${fileName}.${postfix}`);
  92. return fs.createReadStream(url);
  93. }
  94. }
  95. module.exports = HandleService;