|
@@ -5,7 +5,8 @@ const assert = require('assert');
|
|
|
const _ = require('lodash');
|
|
|
const { CrudService } = require('naf-framework-mongoose/lib/service');
|
|
|
const { BusinessError, ErrorCode } = require('naf-core').Error;
|
|
|
-
|
|
|
+const XLSX = require('xlsx');
|
|
|
+var utils = require('../utils/utils.js');
|
|
|
|
|
|
class ProductService extends CrudService {
|
|
|
constructor(ctx) {
|
|
@@ -79,6 +80,77 @@ class ProductService extends CrudService {
|
|
|
}
|
|
|
return data;
|
|
|
}
|
|
|
+ // 导出测试
|
|
|
+ async exportexcel({totaltype}){
|
|
|
+ let productList = await this.model.find({ totaltype });
|
|
|
+ for(let product of productList){
|
|
|
+ product.totaltype = utils.getName("totaltype",product.totaltype);
|
|
|
+ product.phase = utils.getName("phase",product.phase);
|
|
|
+ product.priceunit = utils.getName("priceunit",product.priceunit);
|
|
|
+ product.field = utils.getName("field",product.field);
|
|
|
+ product.coopermode = utils.getName("coopermode",product.coopermode);
|
|
|
+ product.business = utils.getName("business",product.business);
|
|
|
+ product.mature = utils.getName("mature",product.mature);
|
|
|
+ }
|
|
|
+ // 需要打出的列表
|
|
|
+ const _headers = [
|
|
|
+ { key: 'totaltype', title: '分类' },
|
|
|
+ { key: 'name', title: '名称' },
|
|
|
+ { key: 'product_type_name', title: '类型名称' },
|
|
|
+ { key: 'introduction', title: '简介' },
|
|
|
+ { key: 'phase', title: '研发阶段' },
|
|
|
+ { key: 'price', title: '单价' },
|
|
|
+ { key: 'priceunit', title: '单位' },
|
|
|
+ { key: 'field', title: '所属领域' },
|
|
|
+ { key: 'scope', title: '服务范围' },
|
|
|
+ { key: 'coopermode', title: '合作方式' },
|
|
|
+ { key: 'business', title: '交易方式' },
|
|
|
+ { key: 'budget', title: '投入预算' },
|
|
|
+ { key: 'end_date', title: '需求截止日期' },
|
|
|
+ { key: 'difficult_problem', title: '难题及瓶颈问题' },
|
|
|
+ { key: 'demand', title: '企业解决需求' },
|
|
|
+ { key: 'company', title: '单位名称' },
|
|
|
+ { key: 'address', title: '单位地址' },
|
|
|
+ { key: 'team', title: '技术团队情况' },
|
|
|
+ { key: 'property', title: '知识产权情况' },
|
|
|
+ { key: 'mature', title: '技术成熟度' },
|
|
|
+ { key: 'coopercompany', title: '合作企业' },
|
|
|
+ { key: 'other', title: '其他需求' },
|
|
|
+ { key: 'contact_user', title: '联系人' },
|
|
|
+ { key: 'contact_tel', title: '联系电话' }
|
|
|
+ ];
|
|
|
+
|
|
|
+ const _data = productList;
|
|
|
+
|
|
|
+ const headers = _headers.map(({ title }) => title)
|
|
|
+ .map((v, i) => Object.assign({}, { v: v, position: String.fromCharCode(65 + i) + 1 }))
|
|
|
+ .reduce((prev, next) => Object.assign({}, prev, { [next.position]: { v: next.v } }), {});
|
|
|
+
|
|
|
+ const data = _data
|
|
|
+ .map((v, i) => _headers.map(({ key }, j) => Object.assign({}, { v: v[key], position: String.fromCharCode(65 + j) + (i + 2) })))
|
|
|
+ .reduce((prev, next) => prev.concat(next))
|
|
|
+ .reduce((prev, next) => Object.assign({}, prev, { [next.position]: { v: next.v } }), {});
|
|
|
+
|
|
|
+ // 合并 headers 和 data
|
|
|
+ const output = Object.assign({}, headers, data);
|
|
|
+
|
|
|
+ // 获取所有单元格的位置
|
|
|
+ const outputPos = Object.keys(output);
|
|
|
+
|
|
|
+ // 计算出范围
|
|
|
+ const ref = outputPos[0] + ':' + outputPos[outputPos.length - 1];
|
|
|
+
|
|
|
+ // 构建 workbook 对象
|
|
|
+ const wb = {
|
|
|
+ SheetNames: ['sheet1'],
|
|
|
+ Sheets: {
|
|
|
+ 'sheet1': Object.assign({}, output, { '!ref': ref })
|
|
|
+ }
|
|
|
+ };
|
|
|
+ console.log(new Date().getTime());
|
|
|
+ // 导出 Excel
|
|
|
+ XLSX.writeFile(wb, 'D:/'+new Date().getTime()+'.xlsx');
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
module.exports = ProductService;
|