goods.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. 'use strict';
  2. const { CrudService } = require('naf-framework-mongoose-free/lib/service');
  3. const { BusinessError, ErrorCode } = require('naf-core').Error;
  4. const _ = require('lodash');
  5. const assert = require('assert');
  6. const { ObjectId } = require('mongoose').Types;
  7. //
  8. class GoodsService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'goods');
  11. this.goodsModel = this.ctx.model.Shop.Goods;
  12. this.goodsSpecModel = this.ctx.model.Shop.GoodsSpec;
  13. }
  14. /**
  15. *
  16. * @param {Object} query 查询条件
  17. * @param query.id 商品数据id
  18. */
  19. async goodsDetail({ id }) {
  20. const { populate } = this.ctx.service.shop.goods.getRefMods();
  21. let goods = await this.goodsModel.findById(id, { file: 1, tags: 1, name: 1, shot_brief: 1, brief: 1, send_time: 1, shop: 1, view_num: 1 }).populate(populate);
  22. if (!goods) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到商品数据');
  23. goods = JSON.parse(JSON.stringify(goods));
  24. let specs = await this.goodsSpecModel.find({ goods: id, status: '0' }, { sell_money: 1, flow_money: 1, freight: 1, name: 1, num: 1, can_group: 1, group_config: 1, file: 1 });
  25. specs = JSON.parse(JSON.stringify(specs));
  26. goods = _.omit(goods, [ 'meta', '__v' ]);
  27. const shop = _.pick(goods.shop, [ 'logo', 'name', 'person', 'phone', '_id' ]);
  28. delete goods.shop;
  29. // 2022-10-17反馈问题6:将规格图片和商品图片合并,规格图片在前
  30. for (const spec of specs) {
  31. if (_.isArray(spec.file)) {
  32. goods.file = [ ...spec.file, ..._.get(goods, 'file', []) ];
  33. }
  34. }
  35. // goods: 商品信息; specs:商品规格信息; shop:店铺信息
  36. const returnData = { goods, specs, shop };
  37. // 添加浏览次数
  38. await this.goodsModel.updateOne({ _id: id }, { view_num: (goods.view_num || 0) + 1 });
  39. return returnData;
  40. }
  41. async indexGoodsList(condition, { skip = 0, limit = 20 } = {}) {
  42. condition = this.dealFilter(condition);
  43. const pipline = [{ $match: { status: { $ne: '0' } } }]; // { $sort: { sort: 1 } },
  44. const { view_num, sell_num, sell_money, name, shop, tags } = condition;
  45. let sort = {};
  46. if (view_num) sort.view_num = parseInt(view_num);
  47. if (sell_num) sort.sell_num = parseInt(sell_num);
  48. if (sell_money) sort.sell_money = parseInt(sell_money);
  49. if (name) pipline.push({ $match: { name: new RegExp(name) } });
  50. if (shop) pipline.push({ $match: { shop } });
  51. if (tags) pipline.push({ $match: { tags: { $elemMatch: { $elemMatch: { $eq: tags } } } } });
  52. pipline.push({ $addFields: { goods_id: { $toString: '$_id' }, create_time: { $dateToString: { date: '$meta.createdAt', format: '%Y-%m-%d %H:%M:%S', timezone: '+08:00' } } } });
  53. // 表关联
  54. pipline.push({
  55. $lookup: {
  56. from: 'goodsSpec',
  57. localField: 'goods_id',
  58. foreignField: 'goods',
  59. as: 'specs',
  60. },
  61. });
  62. // 按照规格平铺数据
  63. pipline.push({ $unwind: '$specs' });
  64. // 格式化平铺后的数据
  65. // 2022-10-17反馈-问题6:将规格图片也拿出来
  66. // TODO: 整理规格图片与正常的商品图片
  67. pipline.push({
  68. $project: {
  69. name: 1,
  70. view_num: 1,
  71. sell_num: 1,
  72. file: 1,
  73. sort: 1,
  74. create_time: 1,
  75. sell_money: { $toDouble: '$specs.sell_money' },
  76. flow_money: { $toDouble: '$specs.flow_money' },
  77. createdAt: '$meta.createdAt',
  78. spec_file: '$specs.file',
  79. },
  80. });
  81. pipline.push({
  82. $group: {
  83. _id: '$_id',
  84. data: { $min: '$$CURRENT' },
  85. },
  86. });
  87. pipline.push({
  88. $project: {
  89. name: '$data.name',
  90. view_num: '$data.view_num',
  91. sell_num: '$data.sell_num',
  92. file: '$data.file',
  93. sell_money: '$data.sell_money',
  94. flow_money: '$data.flow_money',
  95. createdAt: '$data.createdAt',
  96. spec_file: '$data.spec_file',
  97. create_time: '$data.create_time',
  98. sort: '$data.sort',
  99. },
  100. });
  101. sort.sort = 1;
  102. sort = { ...sort, sort: 1, create_time: -1 };
  103. pipline.push({ $sort: sort });
  104. console.log(pipline);
  105. // 分页处理
  106. const qPipline = _.cloneDeep(pipline);
  107. if (parseInt(skip)) qPipline.push({ $skip: parseInt(skip) });
  108. if (parseInt(limit)) qPipline.push({ $limit: parseInt(limit) });
  109. let list = await this.goodsModel.aggregate(qPipline);
  110. // 处理合并图片
  111. list = list.map(i => {
  112. if (_.isArray(i.spec_file)) {
  113. i.file = [ ...i.spec_file, ...i.file ];
  114. delete i.spec_file;
  115. }
  116. return i;
  117. });
  118. const tPipline = _.cloneDeep(pipline);
  119. tPipline.push({ $count: 'total' });
  120. const total = await this.goodsModel.aggregate(tPipline);
  121. return { list, total: _.get(_.head(total), 'total', 0) };
  122. }
  123. async indexActTagsGoods() {
  124. // 将使用中且展示在首页的查出来排序
  125. const list = await this.ctx.model.System.ActTags.find({ status: '0', show_index: '0' }).sort({ sort: 1 });
  126. const result = [];
  127. for (const t of list) {
  128. const { label, value } = t;
  129. const list = await this.searchActTagsGoods(value);
  130. const arr = [];
  131. for (const g of list) {
  132. const obj = {
  133. url: _.get(g, 'file'),
  134. name: _.get(g, 'name'),
  135. id: _.get(g, '_id'),
  136. value,
  137. };
  138. if (arr.length === 0) {
  139. obj.title = label;
  140. }
  141. arr.push(obj);
  142. }
  143. result.push({ list: arr });
  144. }
  145. return result;
  146. }
  147. async searchActTagsGoods(act_tags, limit = 2) {
  148. const pipline = [{ $sort: { 'meta.createdAt': -1 } }, { $match: { status: { $ne: '0' }, act_tags } }];
  149. pipline.push({ $project: { name: 1, file: 1 } });
  150. if (parseInt(limit)) pipline.push({ $limit: parseInt(limit) });
  151. const list = await this.goodsModel.aggregate(pipline);
  152. return list;
  153. }
  154. }
  155. module.exports = GoodsService;