goods.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. this.platformActModel = this.ctx.model.System.PlatformAct;
  16. this.gjaModel = this.ctx.model.Shop.GoodsJoinAct;
  17. this.actTagsModel = this.ctx.model.System.ActTags;
  18. }
  19. /**
  20. *
  21. * @param {Object} query 查询条件
  22. * @param query.id 商品数据id
  23. */
  24. async goodsDetail({ id }) {
  25. assert(id, '缺少商品信息');
  26. const pipeline = [{ $match: { _id: ObjectId(id) } }];
  27. const goodsProject = { file: 1, tags: 1, name: 1, shot_brief: 1, brief: 1, send_time: 1, shop: 1, view_num: 1, act_tags: 1 };
  28. // 将 活动标签都进行数据替换
  29. pipeline.push({
  30. $lookup: {
  31. from: 'actTags',
  32. localField: 'act_tags',
  33. foreignField: 'value',
  34. pipeline: [{ $match: { show_goods: '0' } }, { $project: { label: 1 } }],
  35. as: 'act_tags',
  36. },
  37. });
  38. // 找店铺信息
  39. pipeline.push({ $addFields: { shop_id: { $toObjectId: '$shop' } } });
  40. pipeline.push({
  41. $lookup: {
  42. from: 'shop',
  43. localField: 'shop_id',
  44. foreignField: '_id',
  45. pipeline: [
  46. { $addFields: { shop_id: { $toString: '$_id' } } },
  47. // 计算店铺商品总数
  48. {
  49. $lookup: {
  50. from: 'goods',
  51. localField: 'shop_id',
  52. foreignField: 'shop',
  53. pipeline: [{ $match: { status: '1' } }, { $count: 'gn' }],
  54. as: 'gn',
  55. },
  56. },
  57. { $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' } } },
  58. ],
  59. as: 'shopInfo',
  60. },
  61. });
  62. pipeline.push({ $project: { ...goodsProject, shopInfo: { $first: '$shopInfo' } } });
  63. // 找规格
  64. pipeline.push({ $addFields: { goods_id: { $toString: '$_id' } } });
  65. pipeline.push({
  66. $lookup: {
  67. from: 'goodsSpec',
  68. localField: 'goods_id',
  69. foreignField: 'goods',
  70. pipeline: [
  71. {
  72. $project: {
  73. sell_money: { $toDouble: '$sell_money' },
  74. flow_money: { $toDouble: '$flow_money' },
  75. freight: { $toDouble: '$freight' },
  76. name: 1,
  77. num: 1,
  78. can_group: 1,
  79. group_config: 1,
  80. file: 1,
  81. },
  82. },
  83. ],
  84. as: 'specs',
  85. },
  86. });
  87. pipeline.push({ $project: { ...goodsProject, goods_id: 1, specs: 1, shopInfo: 1 } });
  88. // 找到商品是否参与活动,且该活动是否正在进行
  89. pipeline.push({
  90. $lookup: {
  91. from: 'goodsJoinAct',
  92. localField: 'goods_id',
  93. foreignField: 'goods._id',
  94. pipeline: [
  95. { $addFields: { pa: { $toObjectId: '$platform_act' } } },
  96. {
  97. $lookup: {
  98. from: 'platformAct',
  99. localField: 'pa',
  100. foreignField: '_id',
  101. pipeline: [{ $match: { is_use: '0' } }],
  102. as: 'act',
  103. },
  104. },
  105. { $unwind: '$act' },
  106. { $replaceRoot: { newRoot: '$act' } },
  107. { $project: { act_time: 1, config: 1, type: 1 } },
  108. ],
  109. as: 'act',
  110. },
  111. });
  112. // 整理数据
  113. pipeline.push({
  114. $project: {
  115. _id: 0,
  116. goods: {
  117. _id: '$$CURRENT._id',
  118. brief: '$$CURRENT.brief',
  119. file: '$$CURRENT.file',
  120. name: '$$CURRENT.name',
  121. send_time: '$$CURRENT.send_time',
  122. tags: '$$CURRENT.tags',
  123. act_tags: '$$CURRENT.act_tags',
  124. shot_brief: '$$CURRENT.shot_brief',
  125. view_num: '$$CURRENT.view_num',
  126. },
  127. shop: '$shopInfo',
  128. specs: '$specs',
  129. act: '$act',
  130. },
  131. });
  132. const res = await this.goodsModel.aggregate(pipeline);
  133. let data = _.head(res);
  134. if (data) data = JSON.parse(JSON.stringify(data));
  135. if (data.goods.tags.length > 0) {
  136. const nt = [];
  137. for (const t of data.goods.tags) {
  138. const code = _.last(t);
  139. const data = await this.goodsTagsModel.findOne({ code }, 'label');
  140. nt.push(data);
  141. }
  142. data.goods.tags = nt;
  143. }
  144. for (const d of data.act) {
  145. const { tag, text, aboutList } = await this.ctx.service.system.platformAct.getActText(d, data.goods);
  146. d.text = text;
  147. d.tag = tag;
  148. d.list = aboutList;
  149. if (d.type === '2') {
  150. // 处理赠品与规格的显示问题
  151. for (const i of d.list) {
  152. const spec = data.specs.find(f => f._id === i.spec);
  153. if (spec) i.spec_name = _.get(spec, 'name');
  154. }
  155. }
  156. }
  157. data.act = _.uniqBy(data.act, '_id');
  158. return data;
  159. }
  160. async indexGoodsList(condition, { skip = 0, limit = 20 } = {}) {
  161. condition = this.dealFilter(condition);
  162. const pipeline = [{ $match: { status: { $ne: '0' } } }]; // { $sort: { sort: 1 } },
  163. const { view_num, sell_num, sell_money, name, shop, tags, act_tags } = condition;
  164. let sort = {};
  165. if (view_num) sort.view_num = parseInt(view_num);
  166. if (sell_num) sort.sell_num = parseInt(sell_num);
  167. if (sell_money) sort.sell_money = parseInt(sell_money);
  168. if (name) pipeline.push({ $match: { name: new RegExp(name) } });
  169. if (shop) pipeline.push({ $match: { shop } });
  170. if (tags) pipeline.push({ $match: { tags: { $elemMatch: { $elemMatch: { $eq: tags } } } } });
  171. if (act_tags) pipeline.push({ $match: { act_tags: { $elemMatch: { $eq: act_tags } } } });
  172. pipeline.push({
  173. $addFields: { goods_id: { $toString: '$_id' }, create_time: { $dateToString: { date: '$meta.createdAt', format: '%Y-%m-%d %H:%M:%S', timezone: '+08:00' } } },
  174. });
  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. act_tags: 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. act_tags: '$data.act_tags',
  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', 'act_tags' ]);
  230. return obj;
  231. });
  232. // 处理活动标签
  233. await this.getActTags(list);
  234. // 实时匹配活动
  235. // 只找: 买赠;特价;满减/折; 特价需要修改价格; 剩下的打标签
  236. const platformActList = await this.platformActModel.find({ is_use: '0', type: [ '2', '3', '5', '6' ] });
  237. await this.getAboutAct(platformActList, list);
  238. const tpipeline = _.cloneDeep(pipeline);
  239. tpipeline.push({ $count: 'total' });
  240. const total = await this.goodsModel.aggregate(tpipeline);
  241. return { list, total: _.get(_.head(total), 'total', 0) };
  242. }
  243. async getActTags(list) {
  244. let tags = list.map(i => i.act_tags);
  245. tags = _.flattenDeep(tags);
  246. tags = _.uniq(tags);
  247. tags = _.compact(tags);
  248. const tagsData = await this.actTagsModel.find({ value: tags, show_goods: '0' });
  249. for (const i of list) {
  250. const { act_tags = [] } = i;
  251. const actTagsShow = [];
  252. if (act_tags.length > 0) {
  253. for (const t of act_tags) {
  254. const r = tagsData.find(f => f.value === t);
  255. if (r) actTagsShow.push({ label: r.label });
  256. }
  257. }
  258. i.actTagsShow = actTagsShow;
  259. delete i.act_tags;
  260. }
  261. }
  262. /**
  263. * 处理商品有关活动部分
  264. * @param {Array} platformActList 正在进行中的活动
  265. * @param {Array} list 查询商品
  266. */
  267. async getAboutAct(platformActList, list) {
  268. const platform_act = platformActList.map(i => ObjectId(i._id).toString());
  269. for (const goods of list) {
  270. const { _id: goods_id, p_act = [], sell_money } = goods;
  271. const gjaList = await this.gjaModel.find({ platform_act, 'goods._id': ObjectId(goods_id).toString() });
  272. if (gjaList.length <= 0) continue;
  273. for (const gja of gjaList) {
  274. const { platform_act_type: type } = gja;
  275. if (type === '2' && gjaList.length > 0) p_act.push('买赠');
  276. else if (type === '3' && gjaList.length > 0) {
  277. p_act.push('特价');
  278. let sortList = JSON.parse(JSON.stringify(gjaList));
  279. sortList = sortList.map(i => ({ ...i, sp_price: _.get(i, 'config.sp_price', 0) }));
  280. sortList = _.orderBy(sortList, [ 'sp_price' ], [ 'asc' ]);
  281. const lp = _.get(_.head(sortList), 'sp_price');
  282. if (lp && this.ctx.minus(lp, sell_money) < 0) goods.sell_money = lp;
  283. } else if (type === '5' && gjaList.length > 0) p_act.push('满减');
  284. else if (type === '6' && gjaList.length > 0) p_act.push('满折');
  285. goods.p_act = _.uniq(p_act);
  286. }
  287. }
  288. }
  289. async indexActTagsGoods() {
  290. // 将使用中且展示在首页的查出来排序
  291. const list = await this.ctx.model.System.ActTags.find({ status: '0', show_index: '0' }).sort({ sort: 1 });
  292. const result = [];
  293. for (const t of list) {
  294. const { label, value } = t;
  295. const list = await this.searchActTagsGoods(value);
  296. const arr = [];
  297. for (const g of list) {
  298. const obj = {
  299. url: _.get(g, 'file'),
  300. name: _.get(g, 'name'),
  301. id: _.get(g, '_id'),
  302. value,
  303. };
  304. if (arr.length === 0) {
  305. obj.title = label;
  306. }
  307. arr.push(obj);
  308. }
  309. result.push({ list: arr });
  310. }
  311. return result;
  312. }
  313. async searchActTagsGoods(act_tags, limit = 2) {
  314. const pipeline = [{ $sort: { 'meta.createdAt': -1 } }, { $match: { status: { $ne: '0' }, act_tags } }];
  315. pipeline.push({ $project: { name: 1, file: 1 } });
  316. if (parseInt(limit)) pipeline.push({ $limit: parseInt(limit) });
  317. const list = await this.goodsModel.aggregate(pipeline);
  318. return list;
  319. }
  320. }
  321. module.exports = GoodsService;