123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- 'use strict';
- const { CrudService } = require('naf-framework-mongoose-free/lib/service');
- const { BusinessError, ErrorCode } = require('naf-core').Error;
- const _ = require('lodash');
- const { ObjectId } = require('mongoose').Types;
- const assert = require('assert');
- // 展会用户
- class DockUserService extends CrudService {
- constructor(ctx) {
- super(ctx, 'dock_user');
- this.model = this.ctx.model.Dock.DockUser;
- }
- /**
- * 根据展会id查询参展产品
- * @param {Object} query 查询条件
- * @param {Object} options 其他条件
- * @property {String} dock_id 展会id
- * @property {Object} condition 其他查询条件
- */
- async getProductListByDockId({ dock_id, ...condition }, { skip = 0, limit = 0 }) {
- assert(dock_id, '缺少展会信息');
- condition = this.dealFilter(condition);
- // 先查展会下的所有产品
- const pipeline = [{ $match: { dock_id } }];
- // 只留产品列表
- pipeline.push({ $project: { productList: 1 } });
- // 将产品列表数组拆开成单项Object
- pipeline.push({ $unwind: '$productList' });
- // 其他针对产品的查询条件
- pipeline.push({ $match: condition });
- // 满足条件的总数
- const totalRes = await this.model.aggregate([ ...pipeline, { $group: { _id: null, total: { $sum: 1 } } }]);
- let total = 0;
- if (totalRes.length > 0) {
- const totalHead = _.head(totalRes);
- total = _.get(totalHead, 'total');
- }
- // 分页的是产品,不是展会
- if (limit) {
- pipeline.push({ $skip: parseInt(skip) });
- pipeline.push({ $limit: parseInt(limit) });
- }
- let data = await this.model.aggregate(pipeline);
- data = data.map(i => {
- const { productList } = i;
- return productList;
- });
- data = _.compact(data);
- return { data, total };
- }
- async create(data) {
- const { dock_id, user_id } = data;
- let { productList } = data;
- const user = await this.model.findOne({ dock_id, user_id });
- if (!user) {
- const { productList = [] } = data;
- if (productList.length > 0) {
- data.productList = productList.map(i => {
- i.status = '0';
- i._id = ObjectId();
- return i;
- });
- }
- return await this.model.create(data);
- }
- productList = _.differenceBy(productList, user.productList, 'id');
- if (productList.length > 0) {
- productList = productList.map(i => {
- i.status = '0';
- i._id = ObjectId();
- return i;
- });
- }
- user.productList = user.productList.concat(productList);
- return await user.save();
- }
- async update({ id }, data) {
- const { productList = [] } = data;
- if (productList.length > 0) {
- data.productList = productList.map(i => {
- console.log(i.status);
- if (i.status === '1') data.status = '1';
- if (!i._id) i._id = ObjectId();
- else i._id = ObjectId(i._id);
- return i;
- });
- }
- console.log(data);
- return this.model.updateOne({ _id: ObjectId(id) }, data);
- }
- /**
- * 申请参展的产品审核
- * @param {Object} {id} 申请参展的产品的_id
- * @param {Object} {good_id, status} 要改变的状态
- * 只要有一个产品通过审核,该用户就更变为可通过状态=>need_pass_user
- */
- async goodCheck({ id }, { good_id, status }) {
- const object = await this.model.findOne({ _id: ObjectId(id), productList: { $elemMatch: { id: good_id } } });
- if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到需要审核的产品信息');
- const product = object.productList.find(f => ObjectId(good_id).equals(f.id));
- if (!product) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未在需要审核的产品中找到指定的产品');
- product.status = status;
- await this.model.updateOne({ _id: ObjectId(id), productList: { $elemMatch: { id: good_id } } }, object);
- const need_pass_user = object.productList.some(e => e.status === '1');
- if (need_pass_user) this.userCheck({ id: object._id }, { status: '1' });
- }
- /**
- * 申请参展的用户审核
- * @param {Object} {id} 用户申请时生成的数据的id
- * @param {Object} {status} 要改变的状态
- */
- async userCheck({ id }, { status }) {
- const object = await this.model.findById(id);
- if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定的用户申请');
- object.status = status;
- await object.save();
- }
- }
- module.exports = DockUserService;
|