goods.js 11 KB

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