'use strict'; 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) { super(ctx, 'product'); this.model = this.ctx.model.Product; } async newfetch({ id }) { let product = await this.model.findById(id); if (product.is_display) { product = JSON.parse(JSON.stringify(product)); const _newproduct = _.omit( product, product.is_display ); return _newproduct; } return product; } async newquery({ skip, limit, ...info }) { const products = await this.model.find(info); const _products = await this.model.find(info).skip(Number(skip)).limit(Number(limit)); const data = []; for (const _product of _products) { console.log(_product); const newproduct = await this.newfetch({ id: _product._id }); data.push(newproduct); } return { data, total: products.length }; } // 根据uid查询所有子id下的所有产品 async allquery(info) { const { code, status, pid } = info; assert(code, '缺少参数'); if (code === "JLCJGLY") { const data = await this.model.find({ status }); return data; } const url = this.ctx.app.config.axios.auth.baseUrl + '?code=' + code + '&skip=0&limit=9999'; const res = await this.ctx.curl(url, { method: 'get', headers: { 'content-type': 'application/json', }, dataType: 'json', }); const data = []; for (const elm of res.data.data) { const datas = await this.model.find({ userid: elm.id, status }); for (const el of datas) { data.push(el); } } const url1 = this.ctx.app.config.axios.auth.baseUrl + '?pid=' + pid + '&skip=0&limit=9999'; const res1 = await this.ctx.curl(url1, { method: 'get', headers: { 'content-type': 'application/json', }, dataType: 'json', }); for (const elm of res1.data.data) { const datas = await this.model.find({ userid: elm.id, status }); for (const el of datas) { data.push(el); } } 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); } let _headers = [] if(totaltype === '0'){ // 技术 _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: 'coopermode', title: '合作方式' }, { key: 'business', title: '交易方式' }, { key: 'budget', title: '投入预算' }, { key: 'end_date', title: '需求截止日期' }, { key: 'difficult_problem', title: '难题及瓶颈问题' }, { key: 'demand', title: '企业解决需求' }, { key: 'contact_user', title: '联系人' }, { key: 'contact_tel', title: '联系电话' } ]; }else{ // 产品 _headers = [ { key: 'totaltype', title: '分类' }, { key: 'name', title: '名称' }, { key: 'product_type_name', title: '类型名称' }, { key: 'introduction', title: '简介' }, { key: 'price', title: '单价' }, { key: 'priceunit', title: '单位' }, { key: 'field', title: '所属领域' }, { key: 'scope', title: '服务范围' }, { key: 'coopermode', title: '合作方式' }, { key: 'business', 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 }) } }; const nowDate = new Date().getTime() const path = 'D:\\wwwroot\\service\\service-file\\upload\\platform\\' + nowDate + '.xlsx' const respath = 'http://free.liaoningdoupo.com:80/files/platform/'+ nowDate + '.xlsx' // 导出 Excel XLSX.writeFile(wb, path); return respath } } module.exports = ProductService;