1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- 'use strict';
- const assert = require('assert');
- const _ = require('lodash');
- const { ObjectId } = require('mongoose').Types;
- const { CrudService } = require('naf-framework-mongoose/lib/service');
- const { BusinessError, ErrorCode } = require('naf-core').Error;
- class TransactionService extends CrudService {
- constructor(ctx) {
- super(ctx, 'transaction');
- this.model = this.ctx.model.Transaction;
- this.umodel = this.ctx.model.User;
- this.pmodel = this.ctx.model.Product;
- this.omodel = this.ctx.model.Operationlog;
- }
- async create(data) {
- console.log(data);
- const { dockid, userid, product_id, market_userid, description, status, username, product_name, market_username } = data;
- if (!username) {
- const user = await this.umodel.findById(userid);
- username = user.name;
- }
- if (!product_name) {
- const product = await this.pmodel.findById(product_id);
- product_name = product.name;
- }
- if (!market_username) {
- const market_user = await this.umodel.findById(market_userid);
- market_username = market_user.name;
- }
- const newdata = { ...data, username, product_name, market_username };
- console.log(data);
- const operationlogdata = { dockid, userid, product_id, market_userid, description, status, username, product_name, market_username, type: '1' };
- await this.omodel.create(operationlogdata);
- return await this.model.create(newdata);
- }
- async update({ id }, data) {
- const { dockid, userid, product_id, market_userid, description, status, username, product_name, market_username } = data;
- const transaction = await this.model.findById(id);
- if (!username) {
- const user = await this.umodel.findById(userid);
- username = user.name;
- }
- if (!product_name) {
- const product = await this.pmodel.findById(product_id);
- product_name = product.name;
- }
- if (!market_username) {
- const market_user = await this.umodel.findById(market_userid);
- market_username = market_user.name;
- }
- transaction.status = status;
- const operationlogdata = { dockid, userid, product_id, market_userid, description, status, username, product_name, market_username, type: '1' };
- await this.omodel.create(operationlogdata);
- return await transaction.save();
- }
- async findTransactionList({ product_id }) {
- const product = await this.model.find({ product_id });
- return product;
- }
- }
- module.exports = TransactionService;
|