product.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. 'use strict';
  2. const assert = require('assert');
  3. const _ = require('lodash');
  4. const { CrudService } = require('naf-framework-mongoose/lib/service');
  5. const { BusinessError, ErrorCode } = require('naf-core').Error;
  6. const XLSX = require('xlsx');
  7. var utils = require('../utils/utils.js');
  8. class ProductService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'product');
  11. this.model = this.ctx.model.Product;
  12. }
  13. async newfetch({ id }) {
  14. let product = await this.model.findById(id);
  15. if (product.is_display) {
  16. product = JSON.parse(JSON.stringify(product));
  17. const _newproduct = _.omit(
  18. product,
  19. product.is_display
  20. );
  21. return _newproduct;
  22. }
  23. return product;
  24. }
  25. async newquery({ skip, limit, ...info }) {
  26. const products = await this.model.find(info);
  27. const _products = await this.model.find(info).skip(Number(skip)).limit(Number(limit));
  28. const data = [];
  29. for (const _product of _products) {
  30. console.log(_product);
  31. const newproduct = await this.newfetch({ id: _product._id });
  32. data.push(newproduct);
  33. }
  34. return { data, total: products.length };
  35. }
  36. // 根据uid查询所有子id下的所有产品
  37. async allquery(info) {
  38. const { code, status, pid } = info;
  39. assert(code, '缺少参数');
  40. if (code === "JLCJGLY") {
  41. const data = await this.model.find({ status });
  42. return data;
  43. }
  44. const url = this.ctx.app.config.axios.auth.baseUrl + '?code=' + code + '&skip=0&limit=9999';
  45. const res = await this.ctx.curl(url, {
  46. method: 'get',
  47. headers: {
  48. 'content-type': 'application/json',
  49. },
  50. dataType: 'json',
  51. });
  52. const data = [];
  53. for (const elm of res.data.data) {
  54. const datas = await this.model.find({ userid: elm.id, status });
  55. for (const el of datas) {
  56. data.push(el);
  57. }
  58. }
  59. const url1 = this.ctx.app.config.axios.auth.baseUrl + '?pid=' + pid + '&skip=0&limit=9999';
  60. const res1 = await this.ctx.curl(url1, {
  61. method: 'get',
  62. headers: {
  63. 'content-type': 'application/json',
  64. },
  65. dataType: 'json',
  66. });
  67. for (const elm of res1.data.data) {
  68. const datas = await this.model.find({ userid: elm.id, status });
  69. for (const el of datas) {
  70. data.push(el);
  71. }
  72. }
  73. return data;
  74. }
  75. // 导出测试
  76. async exportexcel({totaltype}){
  77. let productList = await this.model.find({ totaltype });
  78. for(let product of productList){
  79. product.totaltype = utils.getName("totaltype",product.totaltype);
  80. product.phase = utils.getName("phase",product.phase);
  81. product.priceunit = utils.getName("priceunit",product.priceunit);
  82. product.field = utils.getName("field",product.field);
  83. product.coopermode = utils.getName("coopermode",product.coopermode);
  84. product.business = utils.getName("business",product.business);
  85. product.mature = utils.getName("mature",product.mature);
  86. }
  87. let _headers = []
  88. if(totaltype === '0'){
  89. // 技术
  90. _headers = [
  91. { key: 'totaltype', title: '分类' },
  92. { key: 'name', title: '名称' },
  93. { key: 'product_type_name', title: '类型名称' },
  94. { key: 'introduction', title: '简介' },
  95. { key: 'phase', title: '研发阶段' },
  96. { key: 'price', title: '单价' },
  97. { key: 'priceunit', title: '单位' },
  98. { key: 'field', title: '所属领域' },
  99. { key: 'coopermode', title: '合作方式' },
  100. { key: 'business', title: '交易方式' },
  101. { key: 'budget', title: '投入预算' },
  102. { key: 'end_date', title: '需求截止日期' },
  103. { key: 'difficult_problem', title: '难题及瓶颈问题' },
  104. { key: 'demand', title: '企业解决需求' },
  105. { key: 'contact_user', title: '联系人' },
  106. { key: 'contact_tel', title: '联系电话' }
  107. ];
  108. }else{
  109. // 产品
  110. _headers = [
  111. { key: 'totaltype', title: '分类' },
  112. { key: 'name', title: '名称' },
  113. { key: 'product_type_name', title: '类型名称' },
  114. { key: 'introduction', title: '简介' },
  115. { key: 'price', title: '单价' },
  116. { key: 'priceunit', title: '单位' },
  117. { key: 'field', title: '所属领域' },
  118. { key: 'scope', title: '服务范围' },
  119. { key: 'coopermode', title: '合作方式' },
  120. { key: 'business', title: '交易方式' },
  121. { key: 'company', title: '单位名称' },
  122. { key: 'address', title: '单位地址' },
  123. { key: 'team', title: '技术团队情况' },
  124. { key: 'property', title: '知识产权情况' },
  125. { key: 'mature', title: '技术成熟度' },
  126. { key: 'coopercompany', title: '合作企业' },
  127. { key: 'other', title: '其他需求' },
  128. { key: 'contact_user', title: '联系人' },
  129. { key: 'contact_tel', title: '联系电话' }
  130. ];
  131. }
  132. // 需要打出的列表
  133. const _data = productList;
  134. const headers = _headers.map(({ title }) => title)
  135. .map((v, i) => Object.assign({}, { v: v, position: String.fromCharCode(65 + i) + 1 }))
  136. .reduce((prev, next) => Object.assign({}, prev, { [next.position]: { v: next.v } }), {});
  137. const data = _data
  138. .map((v, i) => _headers.map(({ key }, j) => Object.assign({}, { v: v[key], position: String.fromCharCode(65 + j) + (i + 2) })))
  139. .reduce((prev, next) => prev.concat(next))
  140. .reduce((prev, next) => Object.assign({}, prev, { [next.position]: { v: next.v } }), {});
  141. // 合并 headers 和 data
  142. const output = Object.assign({}, headers, data);
  143. // 获取所有单元格的位置
  144. const outputPos = Object.keys(output);
  145. // 计算出范围
  146. const ref = outputPos[0] + ':' + outputPos[outputPos.length - 1];
  147. // 构建 workbook 对象
  148. const wb = {
  149. SheetNames: ['sheet1'],
  150. Sheets: {
  151. 'sheet1': Object.assign({}, output, { '!ref': ref })
  152. }
  153. };
  154. const nowDate = new Date().getTime()
  155. const path = 'D:\\wwwroot\\service\\service-file\\upload\\platform\\' + nowDate + '.xlsx'
  156. const respath = 'http://free.liaoningdoupo.com:80/files/platform/'+ nowDate + '.xlsx'
  157. // 导出 Excel
  158. XLSX.writeFile(wb, path);
  159. return respath
  160. }
  161. }
  162. module.exports = ProductService;