transport.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. 'use strict';
  2. const _ = require('lodash');
  3. const { ObjectId } = require('mongoose').Types;
  4. const { CrudService } = require('naf-framework-mongoose/lib/service');
  5. const { BusinessError, ErrorCode } = require('naf-core').Error;
  6. class TransportService extends CrudService {
  7. constructor(ctx) {
  8. super(ctx, 'transport');
  9. this.model = this.ctx.model.Transport;
  10. this.os = this.ctx.service.order.order;
  11. }
  12. async create(data) {
  13. const { goods, no } = data;
  14. // 需要将发走的货物找到其对应的订单,然后添加订单的发货数据记录
  15. await this.os.sendGoods(goods, no);
  16. const res = await this.model.create(data);
  17. return res;
  18. }
  19. /**
  20. * 签收
  21. * @param {Object} data 运输单数据
  22. */
  23. async sign(data) {
  24. const { id, sign_time } = data;
  25. const transport = await this.model.find({ _id: ObjectId(id) });
  26. if (!transport) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到运输单信息');
  27. const { split, no } = data;
  28. // 修改订单部分
  29. await this.os.arriveGoods(split, no);
  30. // 修改运输单部分
  31. transport.status = '1';
  32. if (sign_time) transport.sign_time = sign_time;
  33. return await transport.save();
  34. }
  35. }
  36. module.exports = TransportService;