| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 | 
							- 'use strict';
 
- /**
 
-  *
 
-  *
 
-  * @author Mai
 
-  * @date 2018/5/7
 
-  * @version
 
-  */
 
- const fs = require('fs');
 
- const path = require('path');
 
- const excel = require('node-xlsx');
 
- const xlsx = require('js-xlsx');
 
- const sendToWormhole = require('stream-wormhole');
 
- const loadExcelType = {
 
-     display: 1,
 
-     actual: 2,
 
- };
 
- const loadType = loadExcelType.display;
 
- module.exports = app => {
 
-     class DealBillsController extends app.BaseController {
 
-         /**
 
-          * 获取标段的签约清单数据
 
-          * @param ctx
 
-          * @return {Promise<void>}
 
-          */
 
-         async getData(ctx) {
 
-             const responseData = {
 
-                 err: 0,
 
-                 msg: '',
 
-                 data: [],
 
-             };
 
-             try {
 
-                 responseData.data = await ctx.service.dealBills.getAllDataByCondition({ where: { tender_id: ctx.tender.id } });
 
-             } catch (error) {
 
-                 this.log(error);
 
-                 responseData.err = 1;
 
-                 responseData.msg = error.toString();
 
-             }
 
-             ctx.body = responseData;
 
-         }
 
-         /**
 
-          * 导入Excel数据
 
-          * @param ctx
 
-          * @return {Promise<void>}
 
-          */
 
-         async loadExcel(ctx) {
 
-             let stream;
 
-             try {
 
-                 stream = await ctx.getFileStream();
 
-                 const create_time = Date.parse(new Date()) / 1000;
 
-                 const fileInfo = path.parse(stream.filename);
 
-                 const fileName = this.app.config.filePath + '/cache/deal_bills/uploads/' + 'deal_bills' + create_time + fileInfo.ext;
 
-                 // 保存文件
 
-                 await ctx.helper.saveStreamFile(stream, fileName);
 
-                 if (loadType === loadExcelType.display) {
 
-                     const wb = xlsx.readFile(fileName);
 
-                     const name = wb.SheetNames[0];
 
-                     const sheetData = {
 
-                         rows: xlsx.utils.sheet_to_json(wb.Sheets[name], {header: 1}),
 
-                         merge: wb.Sheets[name]["!merges"],
 
-                     };
 
-                     const result = await ctx.service.dealBills.importDataJsXlsx(sheetData, ctx.tender.id);
 
-                     if (!result) {
 
-                         throw '导入数据失败';
 
-                     }
 
-                 } else {
 
-                     // 读取文件
 
-                     const sheet = excel.parse(fileName);
 
-                     if (!sheet || sheet.length === 0 || sheet[0] === undefined || sheet[0].data === undefined) {
 
-                         throw 'excel没有对应数据';
 
-                     }
 
-                     const result = await ctx.service.dealBills.importData(sheet[0], ctx.tender.id);
 
-                     if (!result) {
 
-                         throw '导入数据失败';
 
-                     }
 
-                 }
 
-                 const dealBills = await this.ctx.service.dealBills.getAllDataByCondition({ where: { tender_id: ctx.tender.id } });
 
-                 ctx.body = {err: 0, msg: '', data: dealBills};
 
-             } catch (err) {
 
-                 this.log(err);
 
-                 // 失败需要消耗掉stream 以防卡死
 
-                 if (stream) {
 
-                     await sendToWormhole(stream);
 
-                 }
 
-                 ctx.body = {err: 1, msg: err.toString(), data: null};
 
-             }
 
-         }
 
-         /**
 
-          * 下载模板文件
 
-          * @param ctx
 
-          * @return {Promise<void>}
 
-          */
 
-         async download(ctx) {
 
-             const file = ctx.params.file;
 
-             if (file) {
 
-                 try {
 
-                     let fileName;
 
-                     if (file === '签约清单导入格式.xls') {
 
-                         fileName = this.app.baseDir + '/app/public/deal_bills/签约清单导入格式.xls';
 
-                         ctx.body = await fs.readFileSync(fileName);
 
-                     } else if (file === '签约清单.xlsx') {
 
-                         const create_time = Date.parse(new Date()) / 1000;
 
-                         fileName = this.app.baseDir + '/app/public/deal_bills/downloads/' + ctx.tender.id + '-' + create_time + '.xlsx';
 
-                         await this.ctx.helper.recursiveMkdirSync(this.app.baseDir + '/app/public/deal_bills/downloads');
 
-                         // todo 导出签约清单Excel
 
-                         const setting = {
 
-                             header: ['清单编号', '名称', '单位', '数量', '单价', '金额'],
 
-                             width: [80, 150, 60, 80, 80, 80],
 
-                             hAlign: ['left', 'left', 'center', 'right', 'right', 'right'],
 
-                         };
 
-                         const dealBills = await ctx.service.dealBills.getAllDataByCondition({ where: { tender_id: ctx.tender.id } });
 
-                         const data = [];
 
-                         for (const db of dealBills) {
 
-                             data.push([db.code, db.name, db.unit, db.quantity, db.unit_price, db.total_price]);
 
-                         }
 
-                         const arraySheetData = this.ctx.helper.simpleXlsxSheetData(setting, data);
 
-                         xlsx.writeFile({
 
-                             SheetNames: ['Sheet1'],
 
-                             Sheets: {
 
-                                 'Sheet1': arraySheetData,
 
-                             },
 
-                         }, fileName);
 
-                         ctx.body = await fs.readFileSync(fileName);
 
-                         // 输出文件后删除
 
-                         fs.unlinkSync(fileName);
 
-                     }
 
-                 } catch (err) {
 
-                     this.log(err);
 
-                 }
 
-             }
 
-         }
 
-     }
 
-     return DealBillsController;
 
- };
 
 
  |