123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- import { Controller, Query, Body, Get, Post, Inject } from '@midwayjs/core';
- import { HttpService } from '@midwayjs/axios';
- import { Context } from '@midwayjs/koa';
- import { ApiTags } from '@midwayjs/swagger';
- import { UtilService } from '../service/util.service';
- import { ServiceError } from '../error/service.error';
- const Excel = require('exceljs');
- @ApiTags(['工具'])
- @Controller('/util')
- export class UtilController {
- @Inject()
- service: UtilService;
- @Inject()
- ctx: Context;
- @Inject()
- httpService: HttpService;
- @Post('/toTotal')
- async toTotal() {
- const result = await this.service.toTotal();
- return result;
- }
- @Get('/oneStatistics')
- async oneStatistics(@Query('type') type: string) {
- const result = await this.service.oneStatistics(type);
- return result;
- }
- @Get('/twoStatistics')
- async twoStatistics(@Query('type') type: string) {
- const result = await this.service.twoStatistics(type);
- return result;
- }
- @Get('/thrStatistics')
- async thrStatistics(@Query('type') type: string) {
- const result = await this.service.thrStatistics(type);
- return result;
- }
- @Get('/fourStatistics')
- async fourStatistics(@Query('type') type: string) {
- const result = await this.service.fourStatistics(type);
- return result;
- }
- @Get('/fiveStatistics')
- async fiveStatistics(@Query('type') type: string) {
- const result = await this.service.fiveStatistics(type);
- return result;
- }
- @Get('/sixStatistics')
- async sixStatistics(@Query('type') type: string) {
- const result = await this.service.sixStatistics(type);
- return result;
- }
- @Get('/cstatistics')
- async Companystatistics() {
- const result = await this.service.Companystatistics();
- return result;
- }
- @Get('/sstatistics')
- async Supplystatistics() {
- const result = await this.service.Supplystatistics();
- return result;
- }
- @Get('/dstatistics')
- async Demandstatistics() {
- const result = await this.service.Demandstatistics();
- return result;
- }
- @Get('/pstatistics')
- async Projectstatistics() {
- const result = await this.service.Projectstatistics();
- return result;
- }
- @Get('/astatistics')
- async Achievementstatistics() {
- const result = await this.service.Achievementstatistics();
- return result;
- }
- @Post('/toExport')
- async toExport(@Body() data: object) {
- const result = await this.service.toExport(data);
- return result;
- }
- @Post('/toImport')
- async upload() {
- const body: any = this.ctx.request.body;
- const fData = await this.getFile(body.url);
- const workbook = new Excel.Workbook();
- await workbook.xlsx.load(fData);
- const result = [];
- const sheetList = [];
- workbook.eachSheet(sheet => {
- // 整理出的数据
- const rows = [];
- sheet.eachRow((row, ri) => {
- if (ri !== 1) {
- const rd = [null];
- row.eachCell({ includeEmpty: true }, c => {
- const value = c.value;
- rd.push(value);
- });
- rows.push(rd);
- }
- });
- sheetList.push({ sheet: sheet.name, rows });
- });
- for (const s of sheetList) {
- const { sheet, rows } = s;
- const func = this.service.nameToFunction(sheet);
- if (!func) continue;
- try {
- const num = await this.service[func](rows);
- result[sheet] = num.result;
- result.push({ key: sheet, num: num.result, errorList: num.errorList.join(',') });
- } catch (error) {
- console.log(error);
- result.push({ key: sheet, num: 'error' });
- }
- }
- return result;
- }
- /**
- * 根据短地址获取文件
- * @param {String} uri 短地址
- */
- async getFile(uri) {
- try {
- const result = await this.httpService.request({
- baseURL: 'http://127.0.0.1:19700',
- method: 'get',
- url: uri,
- responseType: 'arraybuffer',
- headers: {
- 'Content-Type': 'application/octet-stream',
- Accept: 'application/octet-stream',
- },
- });
- if (!(result && result.data)) {
- throw new ServiceError('未找到指定文件');
- }
- return result.data;
- } catch (error) {
- console.log(`error: ${uri}`);
- console.log(Object.keys(error));
- for (const key in error) {
- console.log(key);
- console.log(error[key]);
- }
- }
- }
- }
|