goods.js 8.7 KB

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