lrf402788946 vor 4 Jahren
Ursprung
Commit
2fbec9b65d

+ 65 - 0
app/controller/order/.transport.js

@@ -0,0 +1,65 @@
+module.exports = {
+  create: {
+    requestBody: [
+      "no",
+      "!owner",
+      "supply_type",
+      "supplier",
+      "goods",
+      "send_time",
+      "out_bill",
+      "remark",
+      "is_js",
+      "status",
+      "remark",
+    ],
+  },
+  destroy: {
+    params: ["!id"],
+    service: "delete",
+  },
+  update: {
+    params: ["!id"],
+    requestBody: [
+      "no",
+      "!owner",
+      "supply_type",
+      "supplier",
+      "goods",
+      "send_time",
+      "out_bill",
+      "remark",
+      "is_js",
+      "status",
+      "remark",
+    ],
+  },
+  show: {
+    parameters: {
+      params: ["!id"],
+    },
+    service: "fetch",
+  },
+  index: {
+    parameters: {
+      query: {
+        no: "%no%",
+        owner: "owner",
+        supply_type: "supply_type",
+        client: "client",
+        "send_time@start": "send_time@start",
+        "send_time@end": "send_time@end",
+        "rq_arrive_time@start": "rq_arrive_time@start",
+        is_js: "is_js",
+        status: "status",
+      },
+    },
+    service: "query",
+    options: {
+      query: ["skip", "limit"],
+      sort: ["meta.createdAt"],
+      desc: true,
+      count: true,
+    },
+  },
+};

+ 17 - 0
app/controller/order/transport.js

@@ -0,0 +1,17 @@
+'use strict';
+
+// const _ = require('lodash');
+const meta = require('./.transport.js');
+const Controller = require('egg').Controller;
+const { CrudController } = require('naf-framework-mongoose/lib/controller');
+
+// 订单
+class TransportController extends Controller {
+  constructor(ctx) {
+    super(ctx);
+    this.service = this.ctx.service.order.transport;
+  }
+
+}
+
+module.exports = CrudController(TransportController, meta);

+ 2 - 0
app/model/order.js

@@ -5,6 +5,7 @@ const metaPlugin = require('naf-framework-mongoose/lib/model/meta-plugin');
 // 发货数据
 const sendList = new Schema({
   split_id: { type: String, maxLength: 200 }, // 拆分的货物id
+  no: { type: String, maxLength: 200 }, // 运输单号
   name: { type: String, maxLength: 200 }, // 货物名
   number: { type: Number, maxLength: 200 }, // 数量
   weight: { type: Number, maxLength: 200 }, // 重量
@@ -15,6 +16,7 @@ const sendList = new Schema({
 // 签收数据
 const arriveList = new Schema({
   split_id: { type: String, maxLength: 200 }, // 拆分的货物id
+  no: { type: String, maxLength: 200 }, // 运输单号
   name: { type: String, maxLength: 200 }, // 货物名
   number: { type: Number, maxLength: 200 }, // 数量
   weight: { type: Number, maxLength: 200 }, // 重量

+ 10 - 7
app/model/transport.js

@@ -13,9 +13,9 @@ const goodsList = new Schema({
   sq_ss: { type: Number, maxLength: 200 }, // 税前实收
   sh_ys: { type: Number, maxLength: 200 }, // 税后应收
   sh_ss: { type: Number, maxLength: 200 }, // 税后实收
-  split_id: { type: String, maxLength: 200 }, // 拆分的货物id
+  split_id: { type: String, maxLength: 200, required: true }, // 拆分的货物id
 });
-// 运输支出
+// 运输支出(单车,单趟)
 const bill = new Schema({
   item: { type: String, maxlength: 200 }, // 支出项
   taxes: { type: String, maxLength: 200 }, // 税率
@@ -34,7 +34,7 @@ const supplier = new Schema({
   mode: { type: String, maxlength: 200 }, // 计费方式
   taxes: { type: String, maxLength: 200 }, // 税率
 });
-// 车辆日常维护
+// 运输信息
 const transport = {
   no: {
     type: String,
@@ -128,10 +128,13 @@ const transport = {
 
 const schema = new Schema(transport, { toJSON: { virtuals: true } });
 schema.index({ id: 1 });
-schema.index({ car_no: 1 });
-schema.index({ money: 1 });
-schema.index({ item: 1 });
-schema.index({ date: 1 });
+schema.index({ no: 1 });
+schema.index({ supply_type: 1 });
+schema.index({ supplier: 1 });
+schema.index({ goods: 1 });
+schema.index({ sign_time: 1 });
+schema.index({ out_bill: 1 });
+schema.index({ remark: 1 });
 schema.index({ status: 1 });
 
 schema.plugin(metaPlugin);

+ 4 - 1
app/router/order.js

@@ -12,7 +12,10 @@ module.exports = app => {
   router.post('order', `${prefix}/order/split`, controller[index].order.splitGoods);
   // 支出
   router.post('order', `${prefix}/order/outBill`, controller[index].order.outBill);
-
+  // 订单
   router.resources(`${prefix}/order`, controller[index].order); // index、create、show、destroy
   router.post('order', `${prefix}/order/update/:id`, controller[index].order.update);
+  // 运输
+  router.resources(`${prefix}/transport`, controller[index].transport); // index、create、show、destroy
+  router.post('transport', `${prefix}/transport/update/:id`, controller[index].transport.update);
 };

+ 2 - 2
app/service/order/bill.js

@@ -5,9 +5,9 @@ const { CrudService } = require('naf-framework-mongoose/lib/service');
 const { BusinessError, ErrorCode } = require('naf-core').Error;
 class BillService extends CrudService {
   constructor(ctx) {
-    super(ctx, 'order');
+    super(ctx, 'in');
     this.model = this.ctx.model.Order;
-    this.os = this.ctx.service.order;
+    this.os = this.ctx.service.order.order;
   }
 
   /**

+ 93 - 2
app/service/order/order.js

@@ -156,13 +156,103 @@ class OrderService extends CrudService {
 
   }
 
+  /**
+   * 发货,添加记录及修改状态
+   * @param {Array} goods 发货的货物列表
+   * @param {String} no 运输单号
+   */
+  async sendGoods(goods, no) {
+    for (const g of goods) {
+      const { split_id, name, number, weight, volume } = g;
+      const order = await this.model.findOne({ 'split._id': ObjectId(split_id) });
+      if (!order) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, `未找到该${name}所在的订单信息`);
+      const obj = { split_id, name, number, weight, volume, no };
+      // 添加该货物的发货记录
+      order.send_time.push(obj);
+      // 修改该货物的状态
+      const good = order.split.id(split_id);
+      good.status = '1';
+      order.goods_status = await this.checkGoodsStatus(order.split);
+      await order.save();
+      // 更新记录
+      let message = `${name}已发货(数量:${number};重量:${weight}吨;体积:${volume}m³)`;
+      const all_send = order.split.every(e => e.status === '0');
+      if (all_send) message = `${message}; 所有货物已发出`;
+      try {
+        this.record(order._id, { method: 'send', message });
+      } catch (error) {
+        this.logger.error(`订单id:${order.id}记录创建失败:${error.toString()}`);
+      }
+    }
+  }
+
+  /**
+   * 签收,添加记录及修改状态
+   * @param {Array} goods 签收的货物列表
+   * @param {String} no 运输单号
+   * @param {String} time 签收时间
+   */
+  async arriveGoods(goods, no, time) {
+    for (const g of goods) {
+      const { split_id, name, number, weight, volume } = g;
+      const order = await this.model.findOne({ 'split._id': ObjectId(split_id) });
+      if (!order) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, `未找到该${name}所在的订单信息`);
+      const obj = { split_id, name, number, weight, volume, no };
+      if (time) obj.time = time;
+      // 添加收货记录
+      order.arrive_time.push(obj);
+      // 修改该货物的状态
+      const good = order.split.id(split_id);
+      good.status = '-1';
+      order.goods_status = await this.checkGoodsStatus(order.split);
+      await order.save();
+      // 更新记录
+      let message = `${name}已签收(数量:${number};重量:${weight}吨;体积:${volume}m³)`;
+      const all_arrive = order.split.every(e => e.status === '-1');
+      if (all_arrive) message = `${message}; 所有货物已签收`;
+      try {
+        this.record(order._id, { method: 'arrive', message });
+      } catch (error) {
+        this.logger.error(`订单id:${order.id}记录创建失败:${error.toString()}`);
+      }
+    }
+  }
+
+  /**
+   * 检查拆分货物列表,更新goods_status
+   * @param {Array} splitList 拆分货物列表
+   */
+  async checkGoodsStatus(splitList) {
+    // 未发车
+    const res = splitList.every(e => e.status === '0');
+    if (res) return '未发货';
+    // 检查是否全发车的货物
+    const all_send = splitList.every(e => e.status === '1');
+    if (all_send) return '所有货物已发出';
+    // 检查是否全到达了
+    const all_arrive = splitList.every(e => e.status === '-1');
+    if (all_arrive) return '所有货物全部到达';
+    // 检查是否有未发货
+    const has_not_send = splitList.some(e => e.status === '0');
+    // 检查是否有发货的
+    const is_send = splitList.some(e => e.status === '1');
+    // 检查是否有到达的
+    const is_arrive = splitList.some(e => e.status === '-1');
+    const word = [];
+    if (has_not_send) word.push('有未发出的货物');
+    if (is_send)word.push('部分货物已发出');
+    if (is_arrive)word.push('部分货物已到达');
+    if (word.length > 0) return word.join(';');
+    return '状态错误';
+  }
+
 
   /**
    * 订单操作记录
    * @param {String} id 订单id
-   * @param {Object} {method:方法} 参数
+   * @param {Object} {method:方法, message:自定义文字} 参数
    */
-  async record(id, { method }) {
+  async record(id, { method, message }) {
     const order = await this.model.findById(id);
     if (!order) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到订单');
     const { authorization } = this.ctx.request.header;
@@ -177,6 +267,7 @@ class OrderService extends CrudService {
     else if (method === 'in') record.message = `${username}修改收入`;
     else if (method === 'out') record.message = `${username}修改支出`;
     else if (method === 'split') record.message = `${username}拆分货物`;
+    else if (method === 'send' || method === 'arrive') record.message = `${message}`;
     order.record.push(record);
     await order.save();
   }

+ 2 - 2
app/service/order/split.js

@@ -5,9 +5,9 @@ const { CrudService } = require('naf-framework-mongoose/lib/service');
 const { BusinessError, ErrorCode } = require('naf-core').Error;
 class SplitService extends CrudService {
   constructor(ctx) {
-    super(ctx, 'order');
+    super(ctx, 'split');
     this.model = this.ctx.model.Order;
-    this.os = this.ctx.service.order;
+    this.os = this.ctx.service.order.order;
   }
   /**
    * 拆分货物

+ 40 - 0
app/service/order/transport.js

@@ -0,0 +1,40 @@
+'use strict';
+const _ = require('lodash');
+const { ObjectId } = require('mongoose').Types;
+const { CrudService } = require('naf-framework-mongoose/lib/service');
+const { BusinessError, ErrorCode } = require('naf-core').Error;
+class TransportService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'transport');
+    this.model = this.ctx.model.Transport;
+    this.os = this.ctx.service.order.order;
+  }
+
+  async create(data) {
+    const { goods, no } = data;
+    // 需要将发走的货物找到其对应的订单,然后添加订单的发货数据记录
+    await this.os.sendGoods(goods, no);
+    const res = await this.model.create(data);
+    return res;
+  }
+
+
+  /**
+   * 签收
+   * @param {Object} data 运输单数据
+   */
+  async sign(data) {
+    const { id, sign_time } = data;
+    const transport = await this.model.find({ _id: ObjectId(id) });
+    if (!transport) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到运输单信息');
+    const { split, no } = data;
+    // 修改订单部分
+    await this.os.arriveGoods(split, no);
+    // 修改运输单部分
+    transport.status = '1';
+    if (sign_time) transport.sign_time = sign_time;
+    return await transport.save();
+  }
+
+}
+module.exports = TransportService;

+ 1 - 2
app/service/util.js

@@ -26,8 +26,7 @@ class UtilService extends CrudService {
     return res;
   }
 
-  async utilMethod() {
-    console.log('in function:');
+  async utilMethod(query, body) {
   }
 }
 module.exports = UtilService;