goods.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. //
  7. class GoodsService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'goods');
  10. this.goodsModel = this.ctx.model.Shop.Goods;
  11. this.goodsSpecModel = this.ctx.model.Shop.GoodsSpec;
  12. }
  13. /**
  14. *
  15. * @param {Object} query 查询条件
  16. * @param query.id 商品数据id
  17. */
  18. async goodsDetail({ id }) {
  19. const { populate } = this.ctx.service.shop.goods.getRefMods();
  20. 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);
  21. if (!goods) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到商品数据');
  22. goods = JSON.parse(JSON.stringify(goods));
  23. let specs = await this.goodsSpecModel.find({ goods: id, status: '0' }, { sell_money: 1, flow_money: 1, freight: 1, name: 1, num: 1 });
  24. specs = JSON.parse(JSON.stringify(specs));
  25. goods = _.omit(goods, [ 'meta', '__v' ]);
  26. const shop = _.pick(goods.shop, [ 'logo', 'name', 'person', 'phone', '_id' ]);
  27. delete goods.shop;
  28. // goods: 商品信息; specs:商品规格信息; shop:店铺信息
  29. const returnData = { goods, specs, shop };
  30. // 添加浏览次数
  31. await this.goodsModel.updateOne({ _id: id }, { view_num: (goods.view_num || 0) + 1 });
  32. return returnData;
  33. }
  34. async indexGoodsList(condition, { skip = 0, limit = 20 } = {}) {
  35. condition = this.dealFilter(condition);
  36. const pipline = [{ $match: { status: { $ne: '0' } } }];
  37. const { view_num, sell_num, sell_money, name } = condition;
  38. const sort = {};
  39. if (view_num) sort.view_num = view_num;
  40. if (sell_num) sort.sell_num = sell_num;
  41. if (sell_money) sort.sell_money = sell_money;
  42. if (name) pipline.push({ $match: { name } });
  43. pipline.push({ $addFields: { goods_id: { $toString: '$_id' } } });
  44. // 表关联
  45. pipline.push({
  46. $lookup: {
  47. from: 'goodsSpec',
  48. localField: 'goods_id',
  49. foreignField: 'goods',
  50. as: 'specs',
  51. },
  52. });
  53. // 按照规格平铺数据
  54. pipline.push({ $unwind: '$specs' });
  55. // 格式化平铺后的数据
  56. pipline.push({ $project: { name: 1, view_num: 1, sell_num: 1, file: 1, sell_money: { $toDouble: '$specs.sell_money' } } });
  57. pipline.push({
  58. $group: {
  59. _id: '$_id',
  60. name: { $first: '$name' },
  61. view_num: { $first: '$view_num' },
  62. sell_num: { $first: '$sell_num' },
  63. sell_money: { $min: '$sell_money' },
  64. file: { $first: '$file' },
  65. },
  66. });
  67. // 排序处理
  68. if (view_num) pipline.push({ $sort: { view_num } });
  69. if (sell_num) pipline.push({ $sort: { sell_num } });
  70. if (sell_money) pipline.push({ $sort: { sell_money } });
  71. // 分页处理
  72. if (parseInt(skip)) pipline.push({ $skip: parseInt(skip) });
  73. if (parseInt(limit)) pipline.push({ $limit: parseInt(limit) });
  74. const list = await this.goodsModel.aggregate(pipline);
  75. return list;
  76. }
  77. }
  78. module.exports = GoodsService;