1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- 'use strict';
- const { CrudService } = require('naf-framework-mongoose/lib/service');
- const { BusinessError, ErrorCode } = require('naf-core').Error;
- const _ = require('lodash');
- const { ObjectId } = require('mongoose').Types;
- const assert = require('assert');
- // 展会用户
- class Dock_userService extends CrudService {
- constructor(ctx) {
- super(ctx, 'dock_user');
- this.model = this.ctx.model.Dock.DockUser;
- }
- 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 = Dock_userService;
|