storeGoods.js 3.8 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 moment = require('moment');
  7. const { ObjectId } = require('mongoose').Types;
  8. //
  9. class StoreGoodsService extends CrudService {
  10. constructor(ctx) {
  11. super(ctx, 'storegoods');
  12. this.model = this.ctx.model.User.StoreGoods;
  13. this.goodsModel = this.ctx.model.Shop.Goods;
  14. }
  15. async create({ goods }) {
  16. assert(goods, '缺少商品信息');
  17. const customer = _.get(this.ctx, 'user._id');
  18. assert(customer, '缺少用户信息');
  19. let data = await this.model.findOne({ customer, goods });
  20. if (data) {
  21. await this.model.deleteOne({ customer, goods });
  22. return { msg: '取消收藏', result: false };
  23. }
  24. data = await this.model.create({ customer, goods, time: moment().format('YYYY-MM-DD HH:mm:ss') });
  25. return { msg: '收藏成功', result: true };
  26. }
  27. // 检查是否收藏商品
  28. async check({ goods, customer }) {
  29. const num = await this.model.count({ goods, customer });
  30. return num > 0;
  31. }
  32. // 用户查看收藏商品
  33. async userView(query = {}, { skip, limit } = {}) {
  34. const customer = _.get(this.ctx, 'user._id');
  35. assert(customer, '缺少用户信息');
  36. const { view_num, sell_num, sell_money, name, shop, time } = query;
  37. const searchPips = [];
  38. if (name) searchPips.push({ $match: { name: new RegExp(name) } });
  39. if (shop) searchPips.push({ $match: { shop } });
  40. const pipline = [{ $match: { customer } }];
  41. // 联表-商品
  42. pipline.push({ $addFields: { goods_id: { $toObjectId: '$goods' } } });
  43. pipline.push({
  44. $lookup: {
  45. from: 'goods',
  46. localField: 'goods_id',
  47. foreignField: '_id',
  48. as: 'gi',
  49. },
  50. });
  51. // 平铺
  52. pipline.push({ $unwind: '$gi' });
  53. // 格式化数据
  54. pipline.push({
  55. $project: {
  56. _id: '$gi._id',
  57. shop: '$gi.shop',
  58. time: 1,
  59. name: '$gi.name',
  60. file: '$gi.file',
  61. view_num: '$gi.view_num',
  62. status: '$gi.status',
  63. },
  64. });
  65. // 商品条件查询
  66. if (searchPips.length > 0) pipline.push(...searchPips);
  67. // 联表-规格
  68. pipline.push({ $addFields: { goods_id: { $toString: '$_id' } } });
  69. pipline.push({
  70. $lookup: {
  71. from: 'goodsSpec',
  72. localField: 'goods_id',
  73. foreignField: 'goods',
  74. as: 'specs',
  75. },
  76. });
  77. // 平铺
  78. pipline.push({ $unwind: '$specs' });
  79. // 格式化
  80. pipline.push({
  81. $project: {
  82. time: 1,
  83. _id: 1,
  84. shop: 1,
  85. name: 1,
  86. file: 1,
  87. view_num: 1,
  88. status: 1,
  89. sell_money: '$specs.sell_money',
  90. },
  91. });
  92. // 分组取最低价
  93. pipline.push({
  94. $group: {
  95. _id: '$_id',
  96. name: { $first: '$name' },
  97. view_num: { $first: '$view_num' },
  98. sell_money: { $min: '$sell_money' },
  99. file: { $first: '$file' },
  100. time: { $first: '$time' },
  101. },
  102. });
  103. const qPipline = _.cloneDeep(pipline);
  104. // 排序处理
  105. const sort = { };
  106. if (view_num) sort.view_num = parseInt(view_num);
  107. if (sell_num) sort.sell_num = parseInt(sell_num);
  108. if (sell_money) sort.sell_money = parseInt(sell_money);
  109. sort.time = parseInt(time) || -1;
  110. qPipline.push({ $sort: sort });
  111. if (parseInt(skip)) qPipline.push({ $skip: parseInt(skip) });
  112. if (parseInt(limit)) qPipline.push({ $limit: parseInt(limit) });
  113. const list = await this.model.aggregate(qPipline);
  114. const tPipline = _.cloneDeep(pipline);
  115. tPipline.push({ $count: 'total' });
  116. const total = await this.model.aggregate(tPipline);
  117. return { list, total: _.get(_.head(total), 'total', 0) };
  118. }
  119. }
  120. module.exports = StoreGoodsService;