util.controller.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import { Controller, Query, Body, Get, Post, Inject } from '@midwayjs/core';
  2. import { HttpService } from '@midwayjs/axios';
  3. import { Context } from '@midwayjs/koa';
  4. import { ApiTags } from '@midwayjs/swagger';
  5. import { UtilService } from '../service/util.service';
  6. import { ServiceError } from '../error/service.error';
  7. const Excel = require('exceljs');
  8. @ApiTags(['工具'])
  9. @Controller('/util')
  10. export class UtilController {
  11. @Inject()
  12. service: UtilService;
  13. @Inject()
  14. ctx: Context;
  15. @Inject()
  16. httpService: HttpService;
  17. @Post('/toTotal')
  18. async toTotal() {
  19. const result = await this.service.toTotal();
  20. return result;
  21. }
  22. @Get('/oneStatistics')
  23. async oneStatistics(@Query('type') type: string) {
  24. const result = await this.service.oneStatistics(type);
  25. return result;
  26. }
  27. @Get('/twoStatistics')
  28. async twoStatistics(@Query('type') type: string) {
  29. const result = await this.service.twoStatistics(type);
  30. return result;
  31. }
  32. @Get('/thrStatistics')
  33. async thrStatistics(@Query('type') type: string) {
  34. const result = await this.service.thrStatistics(type);
  35. return result;
  36. }
  37. @Get('/fourStatistics')
  38. async fourStatistics(@Query('type') type: string) {
  39. const result = await this.service.fourStatistics(type);
  40. return result;
  41. }
  42. @Get('/fiveStatistics')
  43. async fiveStatistics(@Query('type') type: string) {
  44. const result = await this.service.fiveStatistics(type);
  45. return result;
  46. }
  47. @Get('/sixStatistics')
  48. async sixStatistics(@Query('type') type: string) {
  49. const result = await this.service.sixStatistics(type);
  50. return result;
  51. }
  52. @Get('/cstatistics')
  53. async Companystatistics() {
  54. const result = await this.service.Companystatistics();
  55. return result;
  56. }
  57. @Get('/sstatistics')
  58. async Supplystatistics() {
  59. const result = await this.service.Supplystatistics();
  60. return result;
  61. }
  62. @Get('/dstatistics')
  63. async Demandstatistics() {
  64. const result = await this.service.Demandstatistics();
  65. return result;
  66. }
  67. @Get('/pstatistics')
  68. async Projectstatistics() {
  69. const result = await this.service.Projectstatistics();
  70. return result;
  71. }
  72. @Get('/astatistics')
  73. async Achievementstatistics() {
  74. const result = await this.service.Achievementstatistics();
  75. return result;
  76. }
  77. @Post('/toExport')
  78. async toExport(@Body() data: object) {
  79. const result = await this.service.toExport(data);
  80. return result;
  81. }
  82. @Post('/toImport')
  83. async upload() {
  84. const body: any = this.ctx.request.body;
  85. const fData = await this.getFile(body.url);
  86. const workbook = new Excel.Workbook();
  87. await workbook.xlsx.load(fData);
  88. const result = [];
  89. const sheetList = [];
  90. workbook.eachSheet(sheet => {
  91. // 整理出的数据
  92. const rows = [];
  93. sheet.eachRow((row, ri) => {
  94. if (ri !== 1) {
  95. const rd = [null];
  96. row.eachCell({ includeEmpty: true }, c => {
  97. const value = c.value;
  98. rd.push(value);
  99. });
  100. rows.push(rd);
  101. }
  102. });
  103. sheetList.push({ sheet: sheet.name, rows });
  104. });
  105. for (const s of sheetList) {
  106. const { sheet, rows } = s;
  107. const func = this.service.nameToFunction(sheet);
  108. if (!func) continue;
  109. try {
  110. const num = await this.service[func](rows);
  111. result[sheet] = num.result;
  112. result.push({ key: sheet, num: num.result, errorList: num.errorList.join(',') });
  113. } catch (error) {
  114. console.log(error);
  115. result.push({ key: sheet, num: 'error' });
  116. }
  117. }
  118. return result;
  119. }
  120. /**
  121. * 根据短地址获取文件
  122. * @param {String} uri 短地址
  123. */
  124. async getFile(uri) {
  125. try {
  126. const result = await this.httpService.request({
  127. baseURL: 'http://127.0.0.1:19700',
  128. method: 'get',
  129. url: uri,
  130. responseType: 'arraybuffer',
  131. headers: {
  132. 'Content-Type': 'application/octet-stream',
  133. Accept: 'application/octet-stream',
  134. },
  135. });
  136. if (!(result && result.data)) {
  137. throw new ServiceError('未找到指定文件');
  138. }
  139. return result.data;
  140. } catch (error) {
  141. console.log(`error: ${uri}`);
  142. console.log(Object.keys(error));
  143. for (const key in error) {
  144. console.log(key);
  145. console.log(error[key]);
  146. }
  147. }
  148. }
  149. }