goods.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 });
  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. // goods: 商品信息; specs:商品规格信息; shop:店铺信息
  30. const returnData = { goods, specs, shop };
  31. // 添加浏览次数
  32. await this.goodsModel.updateOne({ _id: id }, { view_num: (goods.view_num || 0) + 1 });
  33. return returnData;
  34. }
  35. async indexGoodsList(condition, { skip = 0, limit = 20 } = {}) {
  36. condition = this.dealFilter(condition);
  37. const pipline = [{ $sort: { sort: 1 } }, { $match: { status: { $ne: '0' } } }];
  38. const { view_num, sell_num, sell_money, name, shop, tags } = condition;
  39. const sort = {};
  40. if (view_num) sort.view_num = view_num;
  41. if (sell_num) sort.sell_num = sell_num;
  42. if (sell_money) sort.sell_money = sell_money;
  43. if (name) pipline.push({ $match: { name: new RegExp(name) } });
  44. if (shop) pipline.push({ $match: { shop } });
  45. if (tags) pipline.push({ $match: { tags: { $elemMatch: { $elemMatch: { $eq: tags } } } } });
  46. pipline.push({ $addFields: { goods_id: { $toString: '$_id' } } });
  47. // 表关联
  48. pipline.push({
  49. $lookup: {
  50. from: 'goodsSpec',
  51. localField: 'goods_id',
  52. foreignField: 'goods',
  53. as: 'specs',
  54. },
  55. });
  56. // 按照规格平铺数据
  57. pipline.push({ $unwind: '$specs' });
  58. // 格式化平铺后的数据
  59. // 2022-10-17反馈-问题6:将规格图片也拿出来
  60. // TODO: 整理规格图片与正常的商品图片
  61. pipline.push({
  62. $project: { name: 1, view_num: 1, sell_num: 1, file: 1, sell_money: { $toDouble: '$specs.sell_money' }, createdAt: '$meta.createdAt', spec_file: '$specs.file' },
  63. });
  64. pipline.push({
  65. $group: {
  66. _id: '$_id',
  67. name: { $first: '$name' },
  68. view_num: { $first: '$view_num' },
  69. sell_num: { $first: '$sell_num' },
  70. sell_money: { $min: '$sell_money' },
  71. file: { $first: '$file' },
  72. spec_file: { $first: '$spec_file' },
  73. createdAt: { $first: '$createdAt' },
  74. },
  75. });
  76. // 排序处理
  77. if (view_num) pipline.push({ $sort: { view_num } });
  78. if (sell_num) pipline.push({ $sort: { sell_num } });
  79. if (sell_money) pipline.push({ $sort: { sell_money } });
  80. // 分页处理
  81. const qPipline = _.cloneDeep(pipline);
  82. qPipline.push({ $sort: { createdAt: -1 } });
  83. if (parseInt(skip)) qPipline.push({ $skip: parseInt(skip) });
  84. if (parseInt(limit)) qPipline.push({ $limit: parseInt(limit) });
  85. let list = await this.goodsModel.aggregate(qPipline);
  86. // 处理合并图片
  87. list = list.map(i => {
  88. if (_.isArray(i.spec_file)) {
  89. i.file = [ ...i.spec_file, ...i.file ];
  90. }
  91. return i;
  92. });
  93. const tPipline = _.cloneDeep(pipline);
  94. tPipline.push({ $count: 'total' });
  95. const total = await this.goodsModel.aggregate(tPipline);
  96. return { list, total: _.get(_.head(total), 'total', 0) };
  97. }
  98. async indexActTagsGoods() {
  99. const dictData = await this.ctx.model.Dev.DictData.find({ code: 'act_tags' });
  100. const result = [];
  101. for (const t of dictData) {
  102. const { label, value } = t;
  103. const list = await this.searchActTagsGoods(value);
  104. const arr = [];
  105. for (const g of list) {
  106. const obj = {
  107. url: _.get(g, 'file'),
  108. name: _.get(g, 'name'),
  109. id: _.get(g, '_id'),
  110. };
  111. if (arr.length === 0) {
  112. obj.title = label;
  113. }
  114. arr.push(obj);
  115. }
  116. result.push({ list: arr });
  117. }
  118. return result;
  119. }
  120. async searchActTagsGoods(act_tags, limit = 2) {
  121. const pipline = [{ $sort: { 'meta.createdAt': -1 } }, { $match: { status: { $ne: '0' }, act_tags } }];
  122. pipline.push({ $project: { name: 1, file: 1 } });
  123. if (parseInt(limit)) pipline.push({ $limit: parseInt(limit) });
  124. const list = await this.goodsModel.aggregate(pipline);
  125. return list;
  126. }
  127. }
  128. module.exports = GoodsService;