platformAct.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. getActText(data) {
  30. const { type } = data;
  31. let tag = '';
  32. let text = '';
  33. if (type === '2') {
  34. tag = '赠品';
  35. } else if (type === '3') {
  36. tag = '特价';
  37. } else if (type === '4') {
  38. tag = '加价购';
  39. } else if (type === '5') {
  40. tag = '满减';
  41. const discount = _.get(data, 'config.discount', []);
  42. for (const i of discount) {
  43. const { limit, number } = i;
  44. text = `${text}满${limit}减${number};`;
  45. }
  46. } else if (type === '6') {
  47. tag = '满折';
  48. const discount = _.get(data, 'config.discount', []);
  49. for (const i of discount) {
  50. const { limit, number, max } = i;
  51. text = `满${limit}打${number}折`;
  52. if (max) text = `${text}(最多减免${max}元)`;
  53. text = `${text};`;
  54. }
  55. } else if (type === '7') {
  56. tag = '套装';
  57. }
  58. return { tag, text };
  59. }
  60. async beforeUpdate(filter, update) {
  61. const id = _.get(filter, 'id');
  62. const num = await this.gjaModel.count({ platform_act: id });
  63. // 如果活动已经有商品关联了.那就不能修改活动类型了
  64. if (num > 0) delete update.type;
  65. return { filter, update };
  66. }
  67. }
  68. module.exports = PlatformActService;