'use strict'; const assert = require('assert'); const _ = require('lodash'); const { ObjectId } = require('mongoose').Types; const { CrudService } = require('naf-framework-mongoose/lib/service'); const { BusinessError } = require('naf-core').Error; const moment = require('moment'); const jwt = require('jsonwebtoken'); class ChatService extends CrudService { constructor(ctx) { super(ctx, 'dock'); this.model = this.ctx.model.Dock; } // 创建登录Token async createJwtPwd(password) { const { secret } = this.config.jwt; const token = await jwt.sign(password, secret); return token; } async create(body) { // roomid与密码每次加一 const findroom = await this.model.find().sort({ room_id: -1 }); if (findroom.length > 0) { const room = parseInt(findroom[0].room_id) + 1 + ''; // 将用户输入的密码进行加密并与查询到的用户数据密码相比对 const pas = await this.createJwtPwd(room); body.room_id = room; body.password = { secret: pas }; } else { // 将用户输入的密码进行加密并与查询到的用户数据密码相比对 const room = '1001'; const pas = await this.createJwtPwd(room); body.room_id = room; body.password = { secret: pas }; } const res = await this.model.create(body); return { ...JSON.parse(JSON.stringify(res)), password: res.password.secret, room_id: res.room_id, }; } // 产品 async goods({ id }, body) { // console.log(body); const dock = await this.model.findOne({ _id: ObjectId(id) }); if (!dock) { throw new BusinessError('没有查询到该对接会'); } if (body._id) { let { apply } = dock; apply = apply.map(a => { a.goodsList = a.goodsList.map(g => { if (g.id === body._id) { g.dockStatus = body.dockStatus; } return g; }); // goodsList中有一个是审核通过的,就把这个人通过 dockStatus === 1 const r = a.goodsList.find(f => f.dockStatus === '1'); if (r) a.status = '1'; return a; }); dock.apply = apply; } const res = await dock.save(); const info = _.last(res.apply.goodsList); return info; } async apply({ id }, body) { const dock = await this.model.findOne({ _id: ObjectId(id) }); if (!dock) { throw new BusinessError('没有查询到该对接会'); } if (body._id) { // 修改 const apply = dock.apply.find(item => item._id === body._id); if (body.user_name) { apply.user_name = body.user_name; } if (body.goodsList) { apply.goodsList = body.goodsList; } if (body.contact_tel) { apply.contact_tel = body.contact_tel; } if (body.apply_time) { apply.apply_time = body.apply_time; } if (body.role) { apply.role = body.role; } if (body.status) { apply.status = body.status; } } else { // 如果是新添,查找之前该userid是否申请了,如果申请了,就把商品列表合并,如果没申请,就保存 const { apply } = dock; const ri = apply.findIndex(f => f.user_id === body.user_id); if (ri < 0) { // 保存 dock.apply.push({ ...body, apply_time: moment().format('YYYY-MM-DD HH:mm:ss'), }); } else { // 之前申请过,合并goodsList apply[ri].goodsList = [ ...apply[ri].goodsList, ...body.goodsList ]; dock.apply = apply; console.log(apply); } } const res = await dock.save(); const info = _.last(res.apply); return info; } async check({ id, dock_id }, { status }) { assert(status, '请审核是否允许参加对接会'); const dock = await this.model.findOne({ _id: ObjectId(dock_id) }); if (!dock) { throw new BusinessError('没有查询到该对接会'); } const user = dock.apply.id(id); if (!user) { throw new BusinessError('没有查询到用户的申请'); } user.status = status; const res = dock.save(); return res; } // async dockCheck({ id }, { is_allowed, reason = '' }) { // const dock = await this.model.findOne({ _id: ObjectId(id) }); // if (!dock) { // throw new BusinessError('没有查询到该对接会'); // } // assert(is_allowed, '请选择审核结果'); // dock.is_allowed = is_allowed; // dock.reason = reason; // const res = await dock.save(); // return res; // } // 根据申请人id查询所有申请的对接会 async myapply({ user_id, status, skip, limit }) { const total = await this.model.count({ status, 'apply.user_id': user_id }); const data = await this.model .find({ status, 'apply.user_id': user_id }) .skip(Number(skip)) .limit(Number(limit)); const result = { total, data }; return result; } async updatevipuser({ id }, info) { const dock = await this.model.findById(id); if (!dock) { throw new BusinessError('没有查询到该对接会'); } const vipuser = await dock.vipuser.id(info.vipid); if (!vipuser) { throw new BusinessError('没有查询到vip用户'); } if (info.vipname) { vipuser.vipname = info.vipname; } if (info.vipphone) { vipuser.vipphone = info.vipphone; } if (info.role) { vipuser.role = info.role; } if (info.content) { vipuser.content = info.content; } return await dock.save(); } async createvipuser({ id }, info) { const dock = await this.model.findById(id); if (!dock) { throw new BusinessError('没有查询到该对接会'); } const vipuser = {}; vipuser.uid = info.uid; vipuser.vipname = info.vipname; vipuser.vipphone = info.vipphone; vipuser.role = info.role; vipuser.company = info.company; vipuser.email = info.email; vipuser.content = info.content; dock.vipuser.push(vipuser); return await dock.save(); } async dockfetch({ id }) { const dock = await this.model.findById(id); const data = JSON.parse(JSON.stringify(dock)); if (dock) { const applydata = []; for (const elm of dock.apply) { const url = 'http://localhost:9004/api/market/transaction?userid=' + elm.user_id; const res = await this.ctx.curl(url, { method: 'get', headers: { 'content-type': 'application/json', }, dataType: 'json', }); const newdata = { ...JSON.parse(JSON.stringify(elm)), transdata: res.data.data, }; applydata.push(newdata); } data.apply = applydata; } return data; } async getdock({ id }) { const res = await this.model.find({ vipuser: { $elemMatch: { uid: id } } }); return res; } async getdockByopenid({ openid }) { const res = await this.model.findOne({ openid }); return res; } async getapply({ user_id }) { const res = await this.model.find({ 'apply.user_id': { $regex: user_id } }); return _.flatten(res); } async getgoodslist({ name }) { assert(name, '请输入产品名称'); const res = await this.model.find({ 'apply.goodsList.name': { $regex: name }, }); const data = []; for (const elm of res) { const applys = elm.apply; for (const el of applys) { const goodlists = el.goodsList; const goodlist = _.filter(goodlists, function(o) { const oo = _.includes(o.name, name); console.log(oo); return oo; }); data.push(goodlist); } } return _.flatten(data); } async dockVipDelete(data) { const { id } = data; assert(id, '缺少vip用户的uid'); console.log(id); const res = await this.model.update( { vipuser: { $elemMatch: { uid: id } } }, { $pull: { vipuser: { uid: id } } } ); return res; } async update({ id }, data) { const arr = await this.model.findById(id); const bool = _.isEqual(data.videodata, arr.videodata); if (!bool) { // TODO MQ const { mq } = this.ctx; if (mq) { const exchange = 'dock_video'; const parm = { durable: true, }; await mq.fanout(exchange, id, JSON.stringify(data.videodata), parm); } } const res = await this.model.update({ _id: ObjectId(id) }, data); return res; } } module.exports = ChatService;