goods.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. pipline.push({ $project: { name: 1, view_num: 1, sell_num: 1, file: 1, sell_money: { $toDouble: '$specs.sell_money' }, createdAt: '$meta.createdAt' } });
  60. pipline.push({
  61. $group: {
  62. _id: '$_id',
  63. name: { $first: '$name' },
  64. view_num: { $first: '$view_num' },
  65. sell_num: { $first: '$sell_num' },
  66. sell_money: { $min: '$sell_money' },
  67. file: { $first: '$file' },
  68. createdAt: { $first: '$createdAt' },
  69. },
  70. });
  71. // 排序处理
  72. if (view_num) pipline.push({ $sort: { view_num } });
  73. if (sell_num) pipline.push({ $sort: { sell_num } });
  74. if (sell_money) pipline.push({ $sort: { sell_money } });
  75. // 分页处理
  76. const qPipline = _.cloneDeep(pipline);
  77. qPipline.push({ $sort: { createdAt: -1 } });
  78. if (parseInt(skip)) qPipline.push({ $skip: parseInt(skip) });
  79. if (parseInt(limit)) qPipline.push({ $limit: parseInt(limit) });
  80. const list = await this.goodsModel.aggregate(qPipline);
  81. const tPipline = _.cloneDeep(pipline);
  82. tPipline.push({ $count: 'total' });
  83. const total = await this.goodsModel.aggregate(tPipline);
  84. return { list, total: _.get(_.head(total), 'total', 0) };
  85. }
  86. async indexActTagsGoods() {
  87. const dictData = await this.ctx.model.Dev.DictData.find({ code: 'act_tags' });
  88. const result = [];
  89. for (const t of dictData) {
  90. const { label, value } = t;
  91. const list = await this.searchActTagsGoods(value);
  92. const arr = [];
  93. for (const g of list) {
  94. const obj = {
  95. url: _.get(g, 'file'),
  96. name: _.get(g, 'name'),
  97. id: _.get(g, '_id'),
  98. };
  99. if (arr.length === 0) {
  100. obj.title = label;
  101. }
  102. arr.push(obj);
  103. }
  104. result.push({ list: arr });
  105. }
  106. return result;
  107. }
  108. async searchActTagsGoods(act_tags, limit = 2) {
  109. const pipline = [{ $sort: { 'meta.createdAt': -1 } }, { $match: { status: { $ne: '0' }, act_tags } }];
  110. pipline.push({ $project: { name: 1, file: 1 } });
  111. if (parseInt(limit)) pipline.push({ $limit: parseInt(limit) });
  112. const list = await this.goodsModel.aggregate(pipline);
  113. return list;
  114. }
  115. }
  116. module.exports = GoodsService;