handle.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. // 获取查询总数函数
  46. const totalfun = this.ctx.service.total[`total${type}`];
  47. // 赋值总数
  48. total = await totalfun({ ...filter, _this: this });
  49. console.log(total);
  50. // 获取查询函数
  51. const fun = this.ctx.service.exports[`exports${type}`];
  52. // 计算页数
  53. const pages = Math.ceil(total / 10);
  54. console.log(pages);
  55. // 此处调用创建文件
  56. const file = await this.establishFile({ taskId, type, fileType });
  57. if (file === 'success') {
  58. for (let i = 0; i < pages; i++) {
  59. // 进度
  60. const progress = (i + 1) / pages * 100;
  61. console.log(taskId, ':', progress);
  62. // 更新进度
  63. await this.ctx.service.progress.updateProgress({ taskId, progress, type });
  64. // 调用查询数据函数
  65. res = await fun({ skip: i, filter, ids, _this: this });
  66. // 此处调用写入文件
  67. if (res.length <= 0) return;
  68. await this.insertFile({ type, data: res, taskId, fileType });
  69. }
  70. }
  71. }
  72. // 创建文件
  73. async establishFile({ taskId, type, fileType }) {
  74. const fun = this.ctx.service[fileType].establish;
  75. return await fun({ fileName: taskId, type, _this: this });
  76. }
  77. // 持续写入文件
  78. async insertFile({ type, data, taskId, fileType }) {
  79. const fun = this.ctx.service[fileType].insertFile;
  80. await fun({ type, fileName: taskId, data, _this: this });
  81. }
  82. // 模板下载
  83. async template({ type, fileType, fileName }) {
  84. const fun = this.ctx.service.template[fileType];
  85. const url = await fun({ fileName, type, _this: this });
  86. const postfix = url.split('.')[1];
  87. this.ctx.attachment(`${fileName}.${postfix}`);
  88. return fs.createReadStream(url);
  89. }
  90. // 文件下载
  91. async exportsDownload({ taskId, fileName }) {
  92. const url = `${this.app.config.exportsPath}/${taskId}.xlsx`;
  93. const postfix = url.split('.')[1];
  94. this.ctx.attachment(`${fileName}.${postfix}`);
  95. return fs.createReadStream(url);
  96. }
  97. }
  98. module.exports = HandleService;