123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- 'use strict';
- const { CrudService } = require('naf-framework-mongoose/lib/service');
- const { BusinessError, ErrorCode } = require('naf-core').Error;
- const { ObjectId } = require('mongoose').Types;
- const _ = require('lodash');
- const assert = require('assert');
- // 展会-用户
- class Dock_userService extends CrudService {
- constructor(ctx) {
- super(ctx, 'dock_user');
- this.model = this.ctx.model.DockUser;
- }
- async create(data) {
- const { goodsList = [] } = data;
- if (goodsList.length > 0) {
- data.goodsList = goodsList.map(i => {
- if (!i.status) i.status = '0';
- i._id = ObjectId();
- return i;
- });
- }
- return await this.model.create(data);
- }
- async update({ id }, data) {
- const { goodsList = [] } = data;
- if (goodsList.length > 0) {
- data.goodsList = goodsList.map(i => {
- if (!i.status) i.status = '0';
- if (!i._id)i._id = ObjectId();
- else i._id = ObjectId(i._id);
- return i;
- });
- }
- return this.model.updateOne({ _id: ObjectId(id) }, data);
- }
- /**
- * 申请参展的产品审核
- * @param {Object} {id} 申请参展的产品的_id
- * @param {Object} {good_id, status} 要改变的状态
- * 只要有一个产品通过审核,该用户就更变为可通过状态=>need_pass_user
- */
- async goodsCheck({ id }, { good_id, status }) {
- const object = await this.model.findOne({ _id: ObjectId(id), goodsList: { $elemMatch: { id: good_id } } });
- if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到需要审核的产品信息');
- const product = object.goodsList.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), goodsList: { $elemMatch: { id: good_id } } }, object);
- const need_pass_user = object.goodsList.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();
- }
- async findUser({ dock_id, user_id }) {
- const res = await this.model.findOne({ dock_id: ObjectId(dock_id), user_id: ObjectId(user_id) });
- return res;
- }
- }
- module.exports = Dock_userService;
|