goods.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. this.gjaModel = this.ctx.model.Shop.GoodsJoinAct;
  14. this.goodsTagsModel = this.ctx.model.System.GoodsTags;
  15. }
  16. /**
  17. *
  18. * @param {Object} query 查询条件
  19. * @param query.id 商品数据id
  20. */
  21. async goodsDetail({ id }) {
  22. assert(id, '缺少商品信息');
  23. const pipeline = [{ $match: { _id: ObjectId(id) } }];
  24. const goodsProject = { file: 1, tags: 1, name: 1, shot_brief: 1, brief: 1, send_time: 1, shop: 1, view_num: 1, act_tags: 1 };
  25. // 将 活动标签都进行数据替换
  26. pipeline.push({
  27. $lookup: {
  28. from: 'actTags',
  29. localField: 'act_tags',
  30. foreignField: 'value',
  31. pipeline: [{ $project: { label: 1 } }],
  32. as: 'act_tags',
  33. },
  34. });
  35. // 找店铺信息
  36. pipeline.push({ $addFields: { shop_id: { $toObjectId: '$shop' } } });
  37. pipeline.push({
  38. $lookup: {
  39. from: 'shop',
  40. localField: 'shop_id',
  41. foreignField: '_id',
  42. pipeline: [
  43. { $addFields: { shop_id: { $toString: '$_id' } } },
  44. // 计算店铺商品总数
  45. {
  46. $lookup: {
  47. from: 'goods',
  48. localField: 'shop_id',
  49. foreignField: 'shop',
  50. pipeline: [{ $match: { status: '1' } }, { $count: 'gn' }],
  51. as: 'gn',
  52. },
  53. },
  54. { $project: { logo: 1, name: 1, person: 1, phone: 1, _id: 1, goods_score: 1, send_score: 1, service_score: 1, goods_num: { $first: '$gn.gn' } } },
  55. ],
  56. as: 'shopInfo',
  57. },
  58. });
  59. pipeline.push({ $project: { ...goodsProject, shopInfo: { $first: '$shopInfo' } } });
  60. // 找规格
  61. pipeline.push({ $addFields: { goods_id: { $toString: '$_id' } } });
  62. pipeline.push({
  63. $lookup: {
  64. from: 'goodsSpec',
  65. localField: 'goods_id',
  66. foreignField: 'goods',
  67. pipeline: [
  68. {
  69. $project: {
  70. sell_money: { $toDouble: '$sell_money' },
  71. flow_money: { $toDouble: '$flow_money' },
  72. freight: { $toDouble: '$freight' },
  73. name: 1,
  74. num: 1,
  75. can_group: 1,
  76. group_config: 1,
  77. file: 1,
  78. },
  79. },
  80. ],
  81. as: 'specs',
  82. },
  83. });
  84. pipeline.push({ $project: { ...goodsProject, goods_id: 1, specs: 1, shopInfo: 1 } });
  85. // 找到商品是否参与活动,且该活动是否正在进行
  86. pipeline.push({
  87. $lookup: {
  88. from: 'goodsJoinAct',
  89. localField: 'goods_id',
  90. foreignField: 'goods._id',
  91. pipeline: [
  92. { $addFields: { pa: { $toObjectId: '$platform_act' } } },
  93. {
  94. $lookup: {
  95. from: 'platformAct',
  96. localField: 'pa',
  97. foreignField: '_id',
  98. pipeline: [{ $match: { is_use: '0' } }],
  99. as: 'act',
  100. },
  101. },
  102. { $unwind: '$act' },
  103. { $replaceRoot: { newRoot: '$act' } },
  104. { $project: { act_time: 1, config: 1, type: 1 } },
  105. ],
  106. as: 'act',
  107. },
  108. });
  109. // 整理数据
  110. pipeline.push({
  111. $project: {
  112. _id: 0,
  113. goods: {
  114. _id: '$$CURRENT._id',
  115. brief: '$$CURRENT.brief',
  116. file: '$$CURRENT.file',
  117. name: '$$CURRENT.name',
  118. send_time: '$$CURRENT.send_time',
  119. tags: '$$CURRENT.tags',
  120. act_tags: '$$CURRENT.act_tags',
  121. shot_brief: '$$CURRENT.shot_brief',
  122. view_num: '$$CURRENT.view_num',
  123. },
  124. shop: '$shopInfo',
  125. specs: '$specs',
  126. act: '$act',
  127. },
  128. });
  129. const res = await this.goodsModel.aggregate(pipeline);
  130. let data = _.head(res);
  131. if (data) data = JSON.parse(JSON.stringify(data));
  132. if (data.goods.tags.length > 0) {
  133. const nt = [];
  134. for (const t of data.goods.tags) {
  135. const code = _.last(t);
  136. const data = await this.goodsTagsModel.findOne({ code }, 'label');
  137. nt.push(data);
  138. }
  139. data.goods.tags = nt;
  140. }
  141. for (const d of data.act) {
  142. const { tag, text } = this.ctx.service.system.platformAct.getActText(d);
  143. d.text = text;
  144. d.tag = tag;
  145. }
  146. data.act = _.uniqBy(data.act, '_id');
  147. return data;
  148. }
  149. async indexGoodsList(condition, { skip = 0, limit = 20 } = {}) {
  150. condition = this.dealFilter(condition);
  151. const pipeline = [{ $match: { status: { $ne: '0' } } }]; // { $sort: { sort: 1 } },
  152. const { view_num, sell_num, sell_money, name, shop, tags, act_tags } = condition;
  153. let sort = {};
  154. if (view_num) sort.view_num = parseInt(view_num);
  155. if (sell_num) sort.sell_num = parseInt(sell_num);
  156. if (sell_money) sort.sell_money = parseInt(sell_money);
  157. if (name) pipeline.push({ $match: { name: new RegExp(name) } });
  158. if (shop) pipeline.push({ $match: { shop } });
  159. if (tags) pipeline.push({ $match: { tags: { $elemMatch: { $elemMatch: { $eq: tags } } } } });
  160. if (act_tags) pipeline.push({ $match: { act_tags: { $elemMatch: { $eq: act_tags } } } });
  161. pipeline.push({
  162. $lookup: {
  163. from: 'actTags',
  164. localField: 'act_tags',
  165. foreignField: 'value',
  166. pipeline: [
  167. { $match: { status: '0', show_goods: '0' } },
  168. { $sort: { sort: 1 } },
  169. { $project: { label: 1, _id: 0 } },
  170. ],
  171. as: 'actTagsShow',
  172. },
  173. });
  174. pipeline.push({ $addFields: { goods_id: { $toString: '$_id' }, create_time: { $dateToString: { date: '$meta.createdAt', format: '%Y-%m-%d %H:%M:%S', timezone: '+08:00' } } } });
  175. // 表关联
  176. pipeline.push({
  177. $lookup: {
  178. from: 'goodsSpec',
  179. localField: 'goods_id',
  180. foreignField: 'goods',
  181. as: 'specs',
  182. },
  183. });
  184. // 按照规格平铺数据
  185. pipeline.push({ $unwind: '$specs' });
  186. // 格式化平铺后的数据
  187. pipeline.push({
  188. $project: {
  189. name: 1,
  190. view_num: 1,
  191. sell_num: 1,
  192. file: 1,
  193. sort: 1,
  194. create_time: 1,
  195. sell_money: { $toDouble: '$specs.sell_money' },
  196. flow_money: { $toDouble: '$specs.flow_money' },
  197. num: '$specs.num',
  198. actTagsShow: 1,
  199. },
  200. });
  201. pipeline.push({
  202. $group: {
  203. _id: '$_id',
  204. data: { $min: '$$CURRENT' },
  205. },
  206. });
  207. pipeline.push({
  208. $project: {
  209. name: '$data.name',
  210. view_num: '$data.view_num',
  211. sell_num: '$data.sell_num',
  212. file: '$data.file',
  213. sell_money: '$data.sell_money',
  214. flow_money: '$data.flow_money',
  215. num: '$data.num',
  216. create_time: '$data.create_time',
  217. sort: '$data.sort',
  218. actTagsShow: '$data.actTagsShow',
  219. },
  220. });
  221. if (Object.keys(sort).length <= 0) sort = { sort: -1, create_time: -1 };
  222. pipeline.push({ $sort: { ...sort, sort: -1, create_time: -1 } });
  223. // 分页处理
  224. const qpipeline = _.cloneDeep(pipeline);
  225. if (parseInt(skip)) qpipeline.push({ $skip: parseInt(skip) });
  226. if (parseInt(limit)) qpipeline.push({ $limit: parseInt(limit) });
  227. let list = await this.goodsModel.aggregate(qpipeline);
  228. list = list.map(i => {
  229. const obj = _.pick(i, [ 'name', 'file', 'num', 'flow_money', 'sell_money', 'view_num', '_id', 'actTagsShow' ]);
  230. return obj;
  231. });
  232. const tpipeline = _.cloneDeep(pipeline);
  233. tpipeline.push({ $count: 'total' });
  234. const total = await this.goodsModel.aggregate(tpipeline);
  235. return { list, total: _.get(_.head(total), 'total', 0) };
  236. }
  237. async indexActTagsGoods() {
  238. // 将使用中且展示在首页的查出来排序
  239. const list = await this.ctx.model.System.ActTags.find({ status: '0', show_index: '0' }).sort({ sort: 1 });
  240. const result = [];
  241. for (const t of list) {
  242. const { label, value } = t;
  243. const list = await this.searchActTagsGoods(value);
  244. const arr = [];
  245. for (const g of list) {
  246. const obj = {
  247. url: _.get(g, 'file'),
  248. name: _.get(g, 'name'),
  249. id: _.get(g, '_id'),
  250. value,
  251. };
  252. if (arr.length === 0) {
  253. obj.title = label;
  254. }
  255. arr.push(obj);
  256. }
  257. result.push({ list: arr });
  258. }
  259. return result;
  260. }
  261. async searchActTagsGoods(act_tags, limit = 2) {
  262. const pipeline = [{ $sort: { 'meta.createdAt': -1 } }, { $match: { status: { $ne: '0' }, act_tags } }];
  263. pipeline.push({ $project: { name: 1, file: 1 } });
  264. if (parseInt(limit)) pipeline.push({ $limit: parseInt(limit) });
  265. const list = await this.goodsModel.aggregate(pipeline);
  266. return list;
  267. }
  268. }
  269. module.exports = GoodsService;