transaction.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. 'use strict';
  2. const assert = require('assert');
  3. const _ = require('lodash');
  4. const { ObjectId } = require('mongoose').Types;
  5. const { CrudService } = require('naf-framework-mongoose/lib/service');
  6. const { BusinessError, ErrorCode } = require('naf-core').Error;
  7. class TransactionService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'transaction');
  10. this.model = this.ctx.model.Transaction;
  11. this.umodel = this.ctx.model.User;
  12. this.pmodel = this.ctx.model.Product;
  13. this.omodel = this.ctx.model.Operationlog;
  14. }
  15. async create(data) {
  16. console.log(data);
  17. const { dockid, userid, product_id, market_userid, description, status, username, product_name, market_username } = data;
  18. if (!username) {
  19. const user = await this.umodel.findById(userid);
  20. username = user.name;
  21. }
  22. if (!product_name) {
  23. const product = await this.pmodel.findById(product_id);
  24. product_name = product.name;
  25. }
  26. if (!market_username) {
  27. const market_user = await this.umodel.findById(market_userid);
  28. market_username = market_user.name;
  29. }
  30. const newdata = { ...data, username, product_name, market_username };
  31. console.log(data);
  32. const operationlogdata = { dockid, userid, product_id, market_userid, description, status, username, product_name, market_username, type: '1' };
  33. await this.omodel.create(operationlogdata);
  34. return await this.model.create(newdata);
  35. }
  36. async update({ id }, data) {
  37. const { dockid, userid, product_id, market_userid, description, status, username, product_name, market_username } = data;
  38. const transaction = await this.model.findById(id);
  39. if (!username) {
  40. const user = await this.umodel.findById(userid);
  41. username = user.name;
  42. }
  43. if (!product_name) {
  44. const product = await this.pmodel.findById(product_id);
  45. product_name = product.name;
  46. }
  47. if (!market_username) {
  48. const market_user = await this.umodel.findById(market_userid);
  49. market_username = market_user.name;
  50. }
  51. transaction.status = status;
  52. const operationlogdata = { dockid, userid, product_id, market_userid, description, status, username, product_name, market_username, type: '1' };
  53. await this.omodel.create(operationlogdata);
  54. return await transaction.save();
  55. }
  56. async findTransactionList({ product_id }) {
  57. const product = await this.model.find({ product_id });
  58. return product;
  59. }
  60. }
  61. module.exports = TransactionService;