platformAct.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. //
  7. class PlatformActService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'platformact');
  10. this.model = this.ctx.model.System.PlatformAct;
  11. this.gjaModel = this.ctx.model.Shop.GoodsJoinAct;
  12. }
  13. async query(filter, { skip = 0, limit, projection } = {}) {
  14. let condition = _.cloneDeep(filter);
  15. condition = await this.beforeQuery(condition);
  16. condition = this.dealFilter(condition);
  17. // 过滤出ref字段
  18. const { populate } = this.getRefMods();
  19. // 带ref查询
  20. let rs = await this.model
  21. .find(condition, projection, { skip, limit, sort: { sort: -1, 'meta.createdAt': -1 } })
  22. .populate(populate)
  23. .exec();
  24. rs = JSON.parse(JSON.stringify(rs));
  25. // 整理ref数据
  26. rs = await this.afterQuery(filter, rs);
  27. return rs;
  28. }
  29. /**
  30. * 获取设置
  31. * @param {PlatformAct} data 活动数据
  32. * @param {Goods} goods 商品数据
  33. */
  34. async getActText(data, goods) {
  35. const { type } = data;
  36. let tag = '';
  37. let text = '';
  38. const aboutList = [];
  39. if (type === '2') {
  40. tag = '赠品';
  41. // 寻找赠品信息
  42. const list = await this.gjaModel.find({ platform_act: data._id, 'goods._id': goods._id });
  43. for (const i of list) {
  44. const gift = _.get(i, 'config.gift', []);
  45. const goods = _.get(i, 'goods._id');
  46. const spec = _.get(i, 'spec._id');
  47. aboutList.push({ goods, spec, gift });
  48. }
  49. } else if (type === '3') {
  50. tag = '特价';
  51. // 寻找商品的特价
  52. const list = await this.gjaModel.find({ platform_act: data._id, 'goods._id': goods._id });
  53. for (const i of list) {
  54. const goods = _.get(i, 'goods._id');
  55. const spec = _.get(i, 'spec._id');
  56. const price = _.get(i, 'config.sp_price');
  57. aboutList.push({ goods, spec, price });
  58. }
  59. } else if (type === '4') {
  60. tag = '加价购';
  61. } else if (type === '5') {
  62. tag = '满减';
  63. const discount = _.get(data, 'config.discount', []);
  64. for (const i of discount) {
  65. const { limit, number } = i;
  66. text = `${text}满${limit}减${number};`;
  67. }
  68. text = _.trimEnd(text, ';');
  69. } else if (type === '6') {
  70. tag = '满折';
  71. const discount = _.get(data, 'config.discount', []);
  72. for (const i of discount) {
  73. const { limit, number, max } = i;
  74. text = `${text}满${limit}打${number}折`;
  75. if (max) text = `${text}(最多减免${max}元)`;
  76. text = `${text};`;
  77. }
  78. text = _.trimEnd(text, ';');
  79. } else if (type === '7') {
  80. tag = '套装';
  81. }
  82. return { tag, text, aboutList };
  83. }
  84. async beforeUpdate(filter, update) {
  85. const id = _.get(filter, 'id');
  86. const num = await this.gjaModel.count({ platform_act: id });
  87. // 如果活动已经有商品关联了.那就不能修改活动类型了
  88. if (num > 0) delete update.type;
  89. return { filter, update };
  90. }
  91. }
  92. module.exports = PlatformActService;