goods.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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 moment = require('moment');
  7. const { ObjectId } = require('mongoose').Types;
  8. //
  9. class GoodsService extends CrudService {
  10. constructor(ctx) {
  11. super(ctx, 'goods');
  12. this.goodsModel = this.ctx.model.Shop.Goods;
  13. this.goodsSpecModel = this.ctx.model.Shop.GoodsSpec;
  14. this.gjaModel = this.ctx.model.Shop.GoodsJoinAct;
  15. this.goodsTagsModel = this.ctx.model.System.GoodsTags;
  16. this.platformActModel = this.ctx.model.System.PlatformAct;
  17. this.gjaModel = this.ctx.model.Shop.GoodsJoinAct;
  18. this.actTagsModel = this.ctx.model.System.ActTags;
  19. this.goodsConfigModel = this.ctx.model.Shop.GoodsConfig;
  20. this.userModel = this.ctx.model.User.User;
  21. this.setModel = this.ctx.model.Shop.GoodsSet;
  22. }
  23. /**
  24. *
  25. * @param {Object} query 查询条件
  26. * @param query.id 商品数据id
  27. */
  28. async goodsDetail({ id }) {
  29. assert(id, '缺少商品信息');
  30. const pipeline = [{ $match: { _id: ObjectId(id) } }];
  31. 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 };
  32. // 将 活动标签都进行数据替换
  33. pipeline.push({
  34. $lookup: {
  35. from: 'actTags',
  36. localField: 'act_tags',
  37. foreignField: 'value',
  38. pipeline: [{ $match: { show_goods: '0' } }, { $project: { label: 1 } }],
  39. as: 'act_tags',
  40. },
  41. });
  42. // 找店铺信息
  43. pipeline.push({ $addFields: { shop_id: { $toObjectId: '$shop' } } });
  44. pipeline.push({
  45. $lookup: {
  46. from: 'shop',
  47. localField: 'shop_id',
  48. foreignField: '_id',
  49. pipeline: [
  50. { $addFields: { shop_id: { $toString: '$_id' } } },
  51. // 计算店铺商品总数
  52. {
  53. $lookup: {
  54. from: 'goods',
  55. localField: 'shop_id',
  56. foreignField: 'shop',
  57. pipeline: [{ $match: { status: '1' } }, { $count: 'gn' }],
  58. as: 'gn',
  59. },
  60. },
  61. { $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' } } },
  62. ],
  63. as: 'shopInfo',
  64. },
  65. });
  66. pipeline.push({ $project: { ...goodsProject, shopInfo: { $first: '$shopInfo' } } });
  67. // 找规格
  68. pipeline.push({ $addFields: { goods_id: { $toString: '$_id' } } });
  69. pipeline.push({
  70. $lookup: {
  71. from: 'goodsSpec',
  72. localField: 'goods_id',
  73. foreignField: 'goods',
  74. pipeline: [
  75. {
  76. $project: {
  77. sell_money: { $toDouble: '$sell_money' },
  78. flow_money: { $toDouble: '$flow_money' },
  79. freight: { $toDouble: '$freight' },
  80. name: 1,
  81. num: 1,
  82. buy_limit: 1,
  83. limit_num: 1,
  84. file: 1,
  85. leader_price: 1,
  86. status: 1,
  87. sort: 1,
  88. },
  89. },
  90. ],
  91. as: 'specs',
  92. },
  93. });
  94. pipeline.push({ $project: { ...goodsProject, goods_id: 1, specs: 1, shopInfo: 1 } });
  95. // 找到商品是否参与活动,且该活动是否正在进行
  96. pipeline.push({
  97. $lookup: {
  98. from: 'goodsJoinAct',
  99. localField: 'goods_id',
  100. foreignField: 'goods._id',
  101. pipeline: [
  102. { $addFields: { pa: { $toObjectId: '$platform_act' } } },
  103. {
  104. $lookup: {
  105. from: 'platformAct',
  106. localField: 'pa',
  107. foreignField: '_id',
  108. pipeline: [{ $match: { is_use: '0' } }],
  109. as: 'act',
  110. },
  111. },
  112. { $unwind: '$act' },
  113. { $replaceRoot: { newRoot: '$act' } },
  114. { $project: { act_time: 1, config: 1, type: 1 } },
  115. ],
  116. as: 'act',
  117. },
  118. });
  119. // 整理数据
  120. pipeline.push({
  121. $project: {
  122. _id: 0,
  123. goods: {
  124. _id: '$$CURRENT._id',
  125. brief: '$$CURRENT.brief',
  126. file: '$$CURRENT.file',
  127. name: '$$CURRENT.name',
  128. send_time: '$$CURRENT.send_time',
  129. tags: '$$CURRENT.tags',
  130. act_tags: '$$CURRENT.act_tags',
  131. shot_brief: '$$CURRENT.shot_brief',
  132. view_num: '$$CURRENT.view_num',
  133. sell_num: '$$CURRENT.sell_num',
  134. },
  135. shop: '$shopInfo',
  136. specs: '$specs',
  137. act: '$act',
  138. },
  139. });
  140. const res = await this.goodsModel.aggregate(pipeline);
  141. let data = _.head(res);
  142. if (data) data = JSON.parse(JSON.stringify(data));
  143. if (data.goods.tags.length > 0) {
  144. const nt = [];
  145. for (const t of data.goods.tags) {
  146. const code = _.last(t);
  147. const data = await this.goodsTagsModel.findOne({ code }, 'label');
  148. nt.push(data);
  149. }
  150. data.goods.tags = nt;
  151. }
  152. data.act = data.act.filter(f => {
  153. const start_time = _.get(f, 'config.time_start');
  154. const end_time = _.get(f, 'config.time_end');
  155. if (!(start_time && end_time)) return false;
  156. return moment().isBetween(start_time, end_time, null, '[]');
  157. });
  158. for (const d of data.act) {
  159. const { tag, text, aboutList } = await this.ctx.service.system.platformAct.getActText(d, data.goods);
  160. d.text = text;
  161. d.tag = tag;
  162. d.list = aboutList;
  163. if (d.type === '2') {
  164. // 处理赠品与规格的显示问题
  165. for (const i of d.list) {
  166. const spec = data.specs.find(f => f._id === i.spec);
  167. if (spec) i.spec_name = _.get(spec, 'name');
  168. }
  169. }
  170. }
  171. data.act = _.uniqBy(data.act, '_id');
  172. const user_id = _.get(this.ctx, 'user._id');
  173. if (user_id) {
  174. // 检查团长价格
  175. const is_leader = await this.ctx.service.user.user.getUserIsLeader(user_id);
  176. if (is_leader) {
  177. data.specs = data.specs.map(i => {
  178. const { sell_money, leader_price } = i;
  179. const sm = this.ctx.toNumber(sell_money);
  180. const obj = { o_sell_money: sm };
  181. if (leader_price) {
  182. const lp = this.ctx.toNumber(leader_price);
  183. obj.leader_price = lp;
  184. obj.sell_money = lp;
  185. }
  186. return { ...i, ...obj };
  187. });
  188. } else {
  189. data.specs = data.specs.map(i => _.omit(i, [ 'leader_price' ]));
  190. }
  191. } else {
  192. data.specs = data.specs.map(i => _.omit(i, [ 'leader_price' ]));
  193. }
  194. const sets = await this.getGoodsSetList(data);
  195. data.sets = sets;
  196. return data;
  197. }
  198. /**
  199. * 套装查询
  200. * @param data
  201. */
  202. async getGoodsSetList(data) {
  203. const goods = _.get(data, 'goods');
  204. if (!goods) return [];
  205. const goodsSetList = await this.setModel.find({ 'set.goods': goods._id, is_use: '0' }).lean();
  206. const arr = [];
  207. for (const sd of goodsSetList) {
  208. const { _id, set = [], sell_money, name } = sd;
  209. const obj = { _id, name, sell_money, goods_total: set.length };
  210. const newSet = [];
  211. for (const s of set) {
  212. const { goods, goods_name, spec, spec_name } = s;
  213. const goodsData = await this.goodsModel.findById(goods, { file: 1 }).lean();
  214. const specData = await this.goodsSpecModel.findById(spec, { file: 1 }).lean();
  215. const file = [ ..._.get(specData, 'file', []), ..._.get(goodsData, 'file', []) ];
  216. const newSetData = { goods, goods_name, spec_name, file };
  217. newSet.push(newSetData);
  218. }
  219. obj.set = newSet;
  220. arr.push(obj);
  221. }
  222. return arr;
  223. }
  224. async indexGoodsList(condition, { skip = 0, limit = 20 } = {}) {
  225. condition = this.dealFilter(condition);
  226. const pipeline = [{ $match: { status: { $ne: '0' } } }]; // { $sort: { sort: 1 } },
  227. const { view_num, sell_num, sell_money, name, shop, tags, act_tags } = condition;
  228. let sort = {};
  229. if (view_num) sort.view_num = parseInt(view_num);
  230. if (sell_num) sort.sell_num = parseInt(sell_num);
  231. if (sell_money) sort.sell_money = parseInt(sell_money);
  232. if (name) pipeline.push({ $match: { name: new RegExp(name) } });
  233. if (shop) pipeline.push({ $match: { shop } });
  234. if (tags) pipeline.push({ $match: { tags: { $elemMatch: { $elemMatch: { $eq: tags } } } } });
  235. if (act_tags) pipeline.push({ $match: { act_tags: { $elemMatch: { $eq: act_tags } } } });
  236. pipeline.push({
  237. $addFields: { goods_id: { $toString: '$_id' }, create_time: { $dateToString: { date: '$meta.createdAt', format: '%Y-%m-%d %H:%M:%S', timezone: '+08:00' } } },
  238. });
  239. // 表关联
  240. pipeline.push({
  241. $lookup: {
  242. from: 'goodsSpec',
  243. localField: 'goods_id',
  244. foreignField: 'goods',
  245. as: 'specs',
  246. },
  247. });
  248. // 按照规格平铺数据
  249. pipeline.push({ $unwind: '$specs' });
  250. // 格式化平铺后的数据
  251. pipeline.push({
  252. $project: {
  253. name: 1,
  254. view_num: 1,
  255. sell_num: 1,
  256. file: 1,
  257. sort: 1,
  258. create_time: 1,
  259. sell_money: { $toDouble: '$specs.sell_money' },
  260. flow_money: { $toDouble: '$specs.flow_money' },
  261. leader_price: { $toDouble: '$specs.leader_price' },
  262. num: '$specs.num',
  263. act_tags: 1,
  264. },
  265. });
  266. pipeline.push({
  267. $group: {
  268. _id: '$_id',
  269. data: { $min: '$$CURRENT' },
  270. },
  271. });
  272. pipeline.push({
  273. $project: {
  274. name: '$data.name',
  275. view_num: '$data.view_num',
  276. sell_num: '$data.sell_num',
  277. file: '$data.file',
  278. sell_money: '$data.sell_money',
  279. flow_money: '$data.flow_money',
  280. leader_price: '$data.leader_price',
  281. num: '$data.num',
  282. create_time: '$data.create_time',
  283. sort: '$data.sort',
  284. act_tags: '$data.act_tags',
  285. },
  286. });
  287. if (Object.keys(sort).length <= 0) sort = { sort: -1, create_time: -1 };
  288. pipeline.push({ $sort: { ...sort, sort: -1, create_time: -1 } });
  289. // 分页处理
  290. const qpipeline = _.cloneDeep(pipeline);
  291. if (parseInt(skip)) qpipeline.push({ $skip: parseInt(skip) });
  292. if (parseInt(limit)) qpipeline.push({ $limit: parseInt(limit) });
  293. let list = await this.goodsModel.aggregate(qpipeline);
  294. list = list.map(i => {
  295. const obj = _.pick(i, [ 'name', 'file', 'num', 'flow_money', 'sell_money', 'view_num', '_id', 'actTagsShow', 'act_tags', 'leader_price' ]);
  296. if (obj.leader_price) obj.leader_price = this.ctx.toNumber(obj.leader_price);
  297. return obj;
  298. });
  299. // 处理活动标签
  300. await this.getActTags(list);
  301. // 实时匹配活动
  302. const nowTime = moment().format('YYYY-MM-DD HH:mm:ss');
  303. // 只找: 买赠;特价;满减/折; 特价需要修改价格; 剩下的打标签
  304. const platformActList = await this.platformActModel
  305. .find({ is_use: '0', type: [ '2', '3', '5', '6' ], 'config.time_start': { $lte: nowTime }, 'config.time_end': { $gte: nowTime } })
  306. .lean(); //
  307. // platformActList = platformActList.filter(f => {
  308. // const start_time = _.get(f, 'config.time_start');
  309. // const end_time = _.get(f, 'config.time_end');
  310. // if (!(start_time && end_time)) return false;
  311. // return moment().isBetween(start_time, end_time, null, '[]');
  312. // });
  313. await this.getAboutAct(platformActList, list);
  314. // 检查团长价格
  315. const user_id = _.get(this.ctx, 'user._id');
  316. const is_leader = await this.ctx.service.user.user.getUserIsLeader(user_id);
  317. if (!is_leader) {
  318. list = list.map(i => _.omit(i, [ 'leader_price' ]));
  319. } else {
  320. for (const i of list) {
  321. const { leader_price, _id: goods } = i;
  322. if (leader_price) continue;
  323. let specs = await this.goodsSpecModel.find({ goods, leader_price: { $gt: 0 } }, { leader_price: 1 }).lean();
  324. specs = _.orderBy(specs, [ 'leader_price' ], [ 'asc' ]);
  325. const head = _.head(specs);
  326. i.leader_price = this.ctx.toNumber(_.get(head, 'leader_price', 0));
  327. }
  328. }
  329. const tpipeline = _.cloneDeep(pipeline);
  330. tpipeline.push({ $count: 'total' });
  331. const total = await this.goodsModel.aggregate(tpipeline);
  332. return { list, total: _.get(_.head(total), 'total', 0) };
  333. }
  334. async getActTags(list) {
  335. let tags = list.map(i => i.act_tags);
  336. tags = _.flattenDeep(tags);
  337. tags = _.uniq(tags);
  338. tags = _.compact(tags);
  339. const tagsData = await this.actTagsModel.find({ value: tags, show_goods: '0' });
  340. for (const i of list) {
  341. const { act_tags = [] } = i;
  342. const actTagsShow = [];
  343. if (act_tags.length > 0) {
  344. for (const t of act_tags) {
  345. const r = tagsData.find(f => f.value === t);
  346. if (r) actTagsShow.push({ label: r.label });
  347. }
  348. }
  349. i.actTagsShow = actTagsShow;
  350. delete i.act_tags;
  351. }
  352. }
  353. /**
  354. * 处理商品有关活动部分
  355. * @param {Array} platformActList 正在进行中的活动
  356. * @param {Array} list 查询商品
  357. */
  358. async getAboutAct(platformActList, list) {
  359. const platform_act = platformActList.map(i => ObjectId(i._id).toString());
  360. for (const goods of list) {
  361. const { _id: goods_id, p_act = [], sell_money } = goods;
  362. const gjaList = await this.gjaModel.find({ platform_act, 'goods._id': ObjectId(goods_id).toString() });
  363. if (gjaList.length <= 0) continue;
  364. for (const gja of gjaList) {
  365. const { platform_act_type: type } = gja;
  366. if (type === '2' && gjaList.length > 0) p_act.push('买赠');
  367. else if (type === '3' && gjaList.length > 0) {
  368. p_act.push('特价');
  369. let sortList = JSON.parse(JSON.stringify(gjaList));
  370. sortList = sortList.map(i => ({ ...i, sp_price: _.get(i, 'config.sp_price', 0) }));
  371. sortList = _.orderBy(sortList, [ 'sp_price' ], [ 'asc' ]);
  372. sortList = sortList.filter(f => this.ctx.minus(0, f.sp_price) < 0);
  373. const lp = _.get(_.head(sortList), 'sp_price');
  374. if (lp && this.ctx.minus(lp, sell_money) < 0) goods.sell_money = lp;
  375. } else if (type === '5' && gjaList.length > 0) p_act.push('满减');
  376. else if (type === '6' && gjaList.length > 0) p_act.push('满折');
  377. goods.p_act = _.uniq(p_act);
  378. }
  379. }
  380. }
  381. async indexActTagsGoods() {
  382. // 将使用中且展示在首页的查出来排序
  383. const list = await this.ctx.model.System.ActTags.find({ status: '0', show_index: '0' }).sort({ sort: 1 });
  384. const result = [];
  385. for (const t of list) {
  386. const { label, value } = t;
  387. const list = await this.searchActTagsGoods(value);
  388. const arr = [];
  389. for (const g of list) {
  390. const obj = {
  391. url: _.get(g, 'file'),
  392. name: _.get(g, 'name'),
  393. id: _.get(g, '_id'),
  394. value,
  395. };
  396. if (arr.length === 0) {
  397. obj.title = label;
  398. }
  399. arr.push(obj);
  400. }
  401. result.push({ list: arr });
  402. }
  403. return result;
  404. }
  405. async searchActTagsGoods(act_tags, limit = 2) {
  406. const pipeline = [{ $sort: { 'meta.createdAt': -1 } }, { $match: { status: { $ne: '0' }, act_tags } }];
  407. pipeline.push({ $project: { name: 1, file: 1 } });
  408. if (parseInt(limit)) pipeline.push({ $limit: parseInt(limit) });
  409. const list = await this.goodsModel.aggregate(pipeline);
  410. return list;
  411. }
  412. // 弃用
  413. async searchLeaderPrice(list) {
  414. for (const i of list) {
  415. const { _id: goods } = i;
  416. let gcl = await this.goodsConfigModel.find({ goods }).lean();
  417. gcl = _.orderBy(gcl, [ 'leader_price' ], [ 'asc' ]);
  418. i.leader_price = _.get(_.head(gcl), 'leader_price');
  419. }
  420. return list;
  421. }
  422. }
  423. module.exports = GoodsService;