|
@@ -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();
|
|
|
}
|