goods.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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, aboutList } = await this.ctx.service.system.platformAct.getActText(d, data.goods);
  143. d.text = text;
  144. d.tag = tag;
  145. d.list = aboutList;
  146. if (d.type === '2') {
  147. // 处理赠品与规格的显示问题
  148. for (const i of d.list) {
  149. const spec = data.specs.find(f => f._id === i.spec);
  150. if (spec) i.spec_name = _.get(spec, 'name');
  151. }
  152. }
  153. }
  154. data.act = _.uniqBy(data.act, '_id');
  155. return data;
  156. }
  157. async indexGoodsList(condition, { skip = 0, limit = 20 } = {}) {
  158. condition = this.dealFilter(condition);
  159. const pipeline = [{ $match: { status: { $ne: '0' } } }]; // { $sort: { sort: 1 } },
  160. const { view_num, sell_num, sell_money, name, shop, tags, act_tags } = condition;
  161. let sort = {};
  162. if (view_num) sort.view_num = parseInt(view_num);
  163. if (sell_num) sort.sell_num = parseInt(sell_num);
  164. if (sell_money) sort.sell_money = parseInt(sell_money);
  165. if (name) pipeline.push({ $match: { name: new RegExp(name) } });
  166. if (shop) pipeline.push({ $match: { shop } });
  167. if (tags) pipeline.push({ $match: { tags: { $elemMatch: { $elemMatch: { $eq: tags } } } } });
  168. if (act_tags) pipeline.push({ $match: { act_tags: { $elemMatch: { $eq: act_tags } } } });
  169. pipeline.push({
  170. $lookup: {
  171. from: 'actTags',
  172. localField: 'act_tags',
  173. foreignField: 'value',
  174. pipeline: [
  175. { $match: { status: '0', show_goods: '0' } },
  176. { $sort: { sort: 1 } },
  177. { $project: { label: 1, _id: 0 } },
  178. ],
  179. as: 'actTagsShow',
  180. },
  181. });
  182. pipeline.push({ $addFields: { goods_id: { $toString: '$_id' }, create_time: { $dateToString: { date: '$meta.createdAt', format: '%Y-%m-%d %H:%M:%S', timezone: '+08:00' } } } });
  183. // 表关联
  184. pipeline.push({
  185. $lookup: {
  186. from: 'goodsSpec',
  187. localField: 'goods_id',
  188. foreignField: 'goods',
  189. as: 'specs',
  190. },
  191. });
  192. // 按照规格平铺数据
  193. pipeline.push({ $unwind: '$specs' });
  194. // 格式化平铺后的数据
  195. pipeline.push({
  196. $project: {
  197. name: 1,
  198. view_num: 1,
  199. sell_num: 1,
  200. file: 1,
  201. sort: 1,
  202. create_time: 1,
  203. sell_money: { $toDouble: '$specs.sell_money' },
  204. flow_money: { $toDouble: '$specs.flow_money' },
  205. num: '$specs.num',
  206. actTagsShow: 1,
  207. },
  208. });
  209. pipeline.push({
  210. $group: {
  211. _id: '$_id',
  212. data: { $min: '$$CURRENT' },
  213. },
  214. });
  215. pipeline.push({
  216. $project: {
  217. name: '$data.name',
  218. view_num: '$data.view_num',
  219. sell_num: '$data.sell_num',
  220. file: '$data.file',
  221. sell_money: '$data.sell_money',
  222. flow_money: '$data.flow_money',
  223. num: '$data.num',
  224. create_time: '$data.create_time',
  225. sort: '$data.sort',
  226. actTagsShow: '$data.actTagsShow',
  227. },
  228. });
  229. if (Object.keys(sort).length <= 0) sort = { sort: -1, create_time: -1 };
  230. pipeline.push({ $sort: { ...sort, sort: -1, create_time: -1 } });
  231. // 分页处理
  232. const qpipeline = _.cloneDeep(pipeline);
  233. if (parseInt(skip)) qpipeline.push({ $skip: parseInt(skip) });
  234. if (parseInt(limit)) qpipeline.push({ $limit: parseInt(limit) });
  235. let list = await this.goodsModel.aggregate(qpipeline);
  236. list = list.map(i => {
  237. const obj = _.pick(i, [ 'name', 'file', 'num', 'flow_money', 'sell_money', 'view_num', '_id', 'actTagsShow' ]);
  238. return obj;
  239. });
  240. const tpipeline = _.cloneDeep(pipeline);
  241. tpipeline.push({ $count: 'total' });
  242. const total = await this.goodsModel.aggregate(tpipeline);
  243. return { list, total: _.get(_.head(total), 'total', 0) };
  244. }
  245. async indexActTagsGoods() {
  246. // 将使用中且展示在首页的查出来排序
  247. const list = await this.ctx.model.System.ActTags.find({ status: '0', show_index: '0' }).sort({ sort: 1 });
  248. const result = [];
  249. for (const t of list) {
  250. const { label, value } = t;
  251. const list = await this.searchActTagsGoods(value);
  252. const arr = [];
  253. for (const g of list) {
  254. const obj = {
  255. url: _.get(g, 'file'),
  256. name: _.get(g, 'name'),
  257. id: _.get(g, '_id'),
  258. value,
  259. };
  260. if (arr.length === 0) {
  261. obj.title = label;
  262. }
  263. arr.push(obj);
  264. }
  265. result.push({ list: arr });
  266. }
  267. return result;
  268. }
  269. async searchActTagsGoods(act_tags, limit = 2) {
  270. const pipeline = [{ $sort: { 'meta.createdAt': -1 } }, { $match: { status: { $ne: '0' }, act_tags } }];
  271. pipeline.push({ $project: { name: 1, file: 1 } });
  272. if (parseInt(limit)) pipeline.push({ $limit: parseInt(limit) });
  273. const list = await this.goodsModel.aggregate(pipeline);
  274. return list;
  275. }
  276. }
  277. module.exports = GoodsService;