goods.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 });
  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: { 'meta.createdAt': -1 } }, { $match: { status: { $ne: '0' } } }];
  38. const { view_num, sell_num, sell_money, name, shop } = 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. pipline.push({ $addFields: { goods_id: { $toString: '$_id' } } });
  46. // 表关联
  47. pipline.push({
  48. $lookup: {
  49. from: 'goodsSpec',
  50. localField: 'goods_id',
  51. foreignField: 'goods',
  52. as: 'specs',
  53. },
  54. });
  55. // 按照规格平铺数据
  56. pipline.push({ $unwind: '$specs' });
  57. // // 格式化平铺后的数据
  58. pipline.push({ $project: { name: 1, view_num: 1, sell_num: 1, file: 1, sell_money: { $toDouble: '$specs.sell_money' }, createdAt: '$meta.createdAt' } });
  59. pipline.push({
  60. $group: {
  61. _id: '$_id',
  62. name: { $first: '$name' },
  63. view_num: { $first: '$view_num' },
  64. sell_num: { $first: '$sell_num' },
  65. sell_money: { $min: '$sell_money' },
  66. file: { $first: '$file' },
  67. createdAt: { $first: '$createdAt' },
  68. },
  69. });
  70. // 排序处理
  71. if (view_num) pipline.push({ $sort: { view_num } });
  72. if (sell_num) pipline.push({ $sort: { sell_num } });
  73. if (sell_money) pipline.push({ $sort: { sell_money } });
  74. // 分页处理
  75. const qPipline = _.cloneDeep(pipline);
  76. qPipline.push({ $sort: { createdAt: -1 } });
  77. if (parseInt(skip)) qPipline.push({ $skip: parseInt(skip) });
  78. if (parseInt(limit)) qPipline.push({ $limit: parseInt(limit) });
  79. const list = await this.goodsModel.aggregate(qPipline);
  80. const tPipline = _.cloneDeep(pipline);
  81. tPipline.push({ $count: 'total' });
  82. const total = await this.goodsModel.aggregate(tPipline);
  83. return { list, total: _.get(_.head(total), 'total', 0) };
  84. }
  85. async indexActTagsGoods() {
  86. const dictData = await this.ctx.model.Dev.DictData.find({ code: 'act_tags' });
  87. const result = [];
  88. for (const t of dictData) {
  89. const { label, value } = t;
  90. const list = await this.searchActTagsGoods(value);
  91. const arr = [];
  92. for (const g of list) {
  93. const obj = {
  94. url: _.get(g, 'file'),
  95. name: _.get(g, 'name'),
  96. id: _.get(g, '_id'),
  97. };
  98. if (arr.length === 0) {
  99. obj.title = label;
  100. }
  101. arr.push(obj);
  102. }
  103. result.push({ list: arr });
  104. }
  105. return result;
  106. }
  107. async searchActTagsGoods(act_tags, limit = 2) {
  108. const pipline = [{ $sort: { 'meta.createdAt': -1 } }, { $match: { status: { $ne: '0' }, act_tags } }];
  109. pipline.push({ $project: { name: 1, file: 1 } });
  110. if (parseInt(limit)) pipline.push({ $limit: parseInt(limit) });
  111. const list = await this.goodsModel.aggregate(pipline);
  112. return list;
  113. }
  114. }
  115. module.exports = GoodsService;