goods.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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, sell_num: 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. sell_num: '$$CURRENT.sell_num',
  127. },
  128. shop: '$shopInfo',
  129. specs: '$specs',
  130. act: '$act',
  131. },
  132. });
  133. const res = await this.goodsModel.aggregate(pipeline);
  134. let data = _.head(res);
  135. if (data) data = JSON.parse(JSON.stringify(data));
  136. if (data.goods.tags.length > 0) {
  137. const nt = [];
  138. for (const t of data.goods.tags) {
  139. const code = _.last(t);
  140. const data = await this.goodsTagsModel.findOne({ code }, 'label');
  141. nt.push(data);
  142. }
  143. data.goods.tags = nt;
  144. }
  145. for (const d of data.act) {
  146. const { tag, text, aboutList } = await this.ctx.service.system.platformAct.getActText(d, data.goods);
  147. d.text = text;
  148. d.tag = tag;
  149. d.list = aboutList;
  150. if (d.type === '2') {
  151. // 处理赠品与规格的显示问题
  152. for (const i of d.list) {
  153. const spec = data.specs.find(f => f._id === i.spec);
  154. if (spec) i.spec_name = _.get(spec, 'name');
  155. }
  156. }
  157. }
  158. data.act = _.uniqBy(data.act, '_id');
  159. return data;
  160. }
  161. async indexGoodsList(condition, { skip = 0, limit = 20 } = {}) {
  162. condition = this.dealFilter(condition);
  163. const pipeline = [{ $match: { status: { $ne: '0' } } }]; // { $sort: { sort: 1 } },
  164. const { view_num, sell_num, sell_money, name, shop, tags, act_tags } = condition;
  165. let sort = {};
  166. if (view_num) sort.view_num = parseInt(view_num);
  167. if (sell_num) sort.sell_num = parseInt(sell_num);
  168. if (sell_money) sort.sell_money = parseInt(sell_money);
  169. if (name) pipeline.push({ $match: { name: new RegExp(name) } });
  170. if (shop) pipeline.push({ $match: { shop } });
  171. if (tags) pipeline.push({ $match: { tags: { $elemMatch: { $elemMatch: { $eq: tags } } } } });
  172. if (act_tags) pipeline.push({ $match: { act_tags: { $elemMatch: { $eq: act_tags } } } });
  173. pipeline.push({
  174. $addFields: { goods_id: { $toString: '$_id' }, create_time: { $dateToString: { date: '$meta.createdAt', format: '%Y-%m-%d %H:%M:%S', timezone: '+08:00' } } },
  175. });
  176. // 表关联
  177. pipeline.push({
  178. $lookup: {
  179. from: 'goodsSpec',
  180. localField: 'goods_id',
  181. foreignField: 'goods',
  182. as: 'specs',
  183. },
  184. });
  185. // 按照规格平铺数据
  186. pipeline.push({ $unwind: '$specs' });
  187. // 格式化平铺后的数据
  188. pipeline.push({
  189. $project: {
  190. name: 1,
  191. view_num: 1,
  192. sell_num: 1,
  193. file: 1,
  194. sort: 1,
  195. create_time: 1,
  196. sell_money: { $toDouble: '$specs.sell_money' },
  197. flow_money: { $toDouble: '$specs.flow_money' },
  198. num: '$specs.num',
  199. act_tags: 1,
  200. },
  201. });
  202. pipeline.push({
  203. $group: {
  204. _id: '$_id',
  205. data: { $min: '$$CURRENT' },
  206. },
  207. });
  208. pipeline.push({
  209. $project: {
  210. name: '$data.name',
  211. view_num: '$data.view_num',
  212. sell_num: '$data.sell_num',
  213. file: '$data.file',
  214. sell_money: '$data.sell_money',
  215. flow_money: '$data.flow_money',
  216. num: '$data.num',
  217. create_time: '$data.create_time',
  218. sort: '$data.sort',
  219. act_tags: '$data.act_tags',
  220. },
  221. });
  222. if (Object.keys(sort).length <= 0) sort = { sort: -1, create_time: -1 };
  223. pipeline.push({ $sort: { ...sort, sort: -1, create_time: -1 } });
  224. // 分页处理
  225. const qpipeline = _.cloneDeep(pipeline);
  226. if (parseInt(skip)) qpipeline.push({ $skip: parseInt(skip) });
  227. if (parseInt(limit)) qpipeline.push({ $limit: parseInt(limit) });
  228. let list = await this.goodsModel.aggregate(qpipeline);
  229. list = list.map(i => {
  230. const obj = _.pick(i, [ 'name', 'file', 'num', 'flow_money', 'sell_money', 'view_num', '_id', 'actTagsShow', 'act_tags' ]);
  231. return obj;
  232. });
  233. // 处理活动标签
  234. await this.getActTags(list);
  235. // 实时匹配活动
  236. // 只找: 买赠;特价;满减/折; 特价需要修改价格; 剩下的打标签
  237. const platformActList = await this.platformActModel.find({ is_use: '0', type: [ '2', '3', '5', '6' ] });
  238. await this.getAboutAct(platformActList, list);
  239. const tpipeline = _.cloneDeep(pipeline);
  240. tpipeline.push({ $count: 'total' });
  241. const total = await this.goodsModel.aggregate(tpipeline);
  242. return { list, total: _.get(_.head(total), 'total', 0) };
  243. }
  244. async getActTags(list) {
  245. let tags = list.map(i => i.act_tags);
  246. tags = _.flattenDeep(tags);
  247. tags = _.uniq(tags);
  248. tags = _.compact(tags);
  249. const tagsData = await this.actTagsModel.find({ value: tags, show_goods: '0' });
  250. for (const i of list) {
  251. const { act_tags = [] } = i;
  252. const actTagsShow = [];
  253. if (act_tags.length > 0) {
  254. for (const t of act_tags) {
  255. const r = tagsData.find(f => f.value === t);
  256. if (r) actTagsShow.push({ label: r.label });
  257. }
  258. }
  259. i.actTagsShow = actTagsShow;
  260. delete i.act_tags;
  261. }
  262. }
  263. /**
  264. * 处理商品有关活动部分
  265. * @param {Array} platformActList 正在进行中的活动
  266. * @param {Array} list 查询商品
  267. */
  268. async getAboutAct(platformActList, list) {
  269. const platform_act = platformActList.map(i => ObjectId(i._id).toString());
  270. for (const goods of list) {
  271. const { _id: goods_id, p_act = [], sell_money } = goods;
  272. const gjaList = await this.gjaModel.find({ platform_act, 'goods._id': ObjectId(goods_id).toString() });
  273. if (gjaList.length <= 0) continue;
  274. for (const gja of gjaList) {
  275. const { platform_act_type: type } = gja;
  276. if (type === '2' && gjaList.length > 0) p_act.push('买赠');
  277. else if (type === '3' && gjaList.length > 0) {
  278. p_act.push('特价');
  279. let sortList = JSON.parse(JSON.stringify(gjaList));
  280. sortList = sortList.map(i => ({ ...i, sp_price: _.get(i, 'config.sp_price', 0) }));
  281. sortList = _.orderBy(sortList, [ 'sp_price' ], [ 'asc' ]);
  282. sortList = sortList.filter(f => this.ctx.minus(0, f.sp_price) < 0);
  283. const lp = _.get(_.head(sortList), 'sp_price');
  284. if (lp && this.ctx.minus(lp, sell_money) < 0) goods.sell_money = lp;
  285. } else if (type === '5' && gjaList.length > 0) p_act.push('满减');
  286. else if (type === '6' && gjaList.length > 0) p_act.push('满折');
  287. goods.p_act = _.uniq(p_act);
  288. }
  289. }
  290. }
  291. async indexActTagsGoods() {
  292. // 将使用中且展示在首页的查出来排序
  293. const list = await this.ctx.model.System.ActTags.find({ status: '0', show_index: '0' }).sort({ sort: 1 });
  294. const result = [];
  295. for (const t of list) {
  296. const { label, value } = t;
  297. const list = await this.searchActTagsGoods(value);
  298. const arr = [];
  299. for (const g of list) {
  300. const obj = {
  301. url: _.get(g, 'file'),
  302. name: _.get(g, 'name'),
  303. id: _.get(g, '_id'),
  304. value,
  305. };
  306. if (arr.length === 0) {
  307. obj.title = label;
  308. }
  309. arr.push(obj);
  310. }
  311. result.push({ list: arr });
  312. }
  313. return result;
  314. }
  315. async searchActTagsGoods(act_tags, limit = 2) {
  316. const pipeline = [{ $sort: { 'meta.createdAt': -1 } }, { $match: { status: { $ne: '0' }, act_tags } }];
  317. pipeline.push({ $project: { name: 1, file: 1 } });
  318. if (parseInt(limit)) pipeline.push({ $limit: parseInt(limit) });
  319. const list = await this.goodsModel.aggregate(pipeline);
  320. return list;
  321. }
  322. }
  323. module.exports = GoodsService;