dockUser.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 { ObjectId } = require('mongoose').Types;
  6. const assert = require('assert');
  7. // 展会用户
  8. class DockUserService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'dock_user');
  11. this.model = this.ctx.model.Dock.DockUser;
  12. }
  13. /**
  14. * 根据展会id查询参展产品
  15. * @param {Object} query 查询条件
  16. * @param {Object} options 其他条件
  17. * @property {String} dock_id 展会id
  18. * @property {Object} condition 其他查询条件
  19. */
  20. async getProductListByDockId({ dock_id, ...condition }, { skip = 0, limit = 0 }) {
  21. assert(dock_id, '缺少展会信息');
  22. condition = this.dealFilter(condition);
  23. // 先查展会下的所有产品
  24. const pipeline = [{ $match: { dock_id } }];
  25. // 只留产品列表
  26. pipeline.push({ $project: { productList: 1 } });
  27. // 将产品列表数组拆开成单项Object
  28. pipeline.push({ $unwind: '$productList' });
  29. // 其他针对产品的查询条件
  30. pipeline.push({ $match: condition });
  31. // 满足条件的总数
  32. const totalRes = await this.model.aggregate([ ...pipeline, { $group: { _id: null, total: { $sum: 1 } } }]);
  33. let total = 0;
  34. if (totalRes.length > 0) {
  35. const totalHead = _.head(totalRes);
  36. total = _.get(totalHead, 'total');
  37. }
  38. // 分页的是产品,不是展会
  39. if (limit) {
  40. pipeline.push({ $skip: parseInt(skip) });
  41. pipeline.push({ $limit: parseInt(limit) });
  42. }
  43. let data = await this.model.aggregate(pipeline);
  44. data = data.map(i => {
  45. const { productList } = i;
  46. return productList;
  47. });
  48. data = _.compact(data);
  49. return { data, total };
  50. }
  51. async create(data) {
  52. const { dock_id, user_id } = data;
  53. let { productList } = data;
  54. const user = await this.model.findOne({ dock_id, user_id });
  55. if (!user) {
  56. const { productList = [] } = data;
  57. if (productList.length > 0) {
  58. data.productList = productList.map(i => {
  59. i.status = '0';
  60. i._id = ObjectId();
  61. return i;
  62. });
  63. }
  64. return await this.model.create(data);
  65. }
  66. productList = _.differenceBy(productList, user.productList, 'id');
  67. if (productList.length > 0) {
  68. productList = productList.map(i => {
  69. i.status = '0';
  70. i._id = ObjectId();
  71. return i;
  72. });
  73. }
  74. user.productList = user.productList.concat(productList);
  75. return await user.save();
  76. }
  77. async update({ id }, data) {
  78. const { productList = [] } = data;
  79. if (productList.length > 0) {
  80. data.productList = productList.map(i => {
  81. console.log(i.status);
  82. if (i.status === '1') data.status = '1';
  83. if (!i._id) i._id = ObjectId();
  84. else i._id = ObjectId(i._id);
  85. return i;
  86. });
  87. }
  88. console.log(data);
  89. return this.model.updateOne({ _id: ObjectId(id) }, data);
  90. }
  91. /**
  92. * 申请参展的产品审核
  93. * @param {Object} {id} 申请参展的产品的_id
  94. * @param {Object} {good_id, status} 要改变的状态
  95. * 只要有一个产品通过审核,该用户就更变为可通过状态=>need_pass_user
  96. */
  97. async goodCheck({ id }, { good_id, status }) {
  98. const object = await this.model.findOne({ _id: ObjectId(id), productList: { $elemMatch: { id: good_id } } });
  99. if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到需要审核的产品信息');
  100. const product = object.productList.find(f => ObjectId(good_id).equals(f.id));
  101. if (!product) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未在需要审核的产品中找到指定的产品');
  102. product.status = status;
  103. await this.model.updateOne({ _id: ObjectId(id), productList: { $elemMatch: { id: good_id } } }, object);
  104. const need_pass_user = object.productList.some(e => e.status === '1');
  105. if (need_pass_user) this.userCheck({ id: object._id }, { status: '1' });
  106. }
  107. /**
  108. * 申请参展的用户审核
  109. * @param {Object} {id} 用户申请时生成的数据的id
  110. * @param {Object} {status} 要改变的状态
  111. */
  112. async userCheck({ id }, { status }) {
  113. const object = await this.model.findById(id);
  114. if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定的用户申请');
  115. object.status = status;
  116. await object.save();
  117. }
  118. }
  119. module.exports = DockUserService;