'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 OrderService extends CrudService { constructor(ctx) { super(ctx, 'order'); this.model = this.ctx.model.Order; } /** * 新添订单 * @param {Object} data 订单数据 */ async create(data) { const res = await this.model.create(data); // 复制进split中 if (!res) throw new BusinessError(ErrorCode.SERVICE_FAULT, '订单创建失败'); await this.copyToSplit(res._id); try { this.record(res._id, { method: 'create' }); } catch (error) { this.logger.error(`订单id:${res.id}记录创建失败:${error.toString()}`); } return res; } /** * 修改订单 * @param {Object} { id, ...data } 要修改的订单数据 */ async update({ id, ...data }) { const res = await this.model.findById(id); const { goods: newGoods, ...info } = data; let { goods: oldGoods, split } = res; if (oldGoods) oldGoods = JSON.parse(JSON.stringify(oldGoods)); if (split) split = JSON.parse(JSON.stringify(split)); // 找到删除项 // oldGoods中有,但是newGoods中却没有的数据,就是要删除的;且需要检验被删除的数据有没有被拆分 oldGoods = oldGoods.map(og => { const need_delete = newGoods.find(f => ObjectId(f._id).equals(og._id)); if (!need_delete) { const sobj = split.find(f => ObjectId(f.pid).equals(og._id)); if (sobj) { // 查找其有没有被拆分 const r = split.find(f => ObjectId(f.pid).equals(sobj._id)); if (r) throw new BusinessError(ErrorCode.DATA_INVALID, '无法删除已被拆分过的货物'); const s_index = split.findIndex(f => ObjectId(f.pid).equals(og._id)); split.splice(s_index, 1); } return undefined; } return og; }); oldGoods = _.compact(oldGoods); // 判断是否有修改项 const updateRes = await this.goodsUpdate(oldGoods, newGoods, split); if (updateRes) { const { oldGoods: ogs, split: splist } = updateRes; if (ogs) oldGoods = ogs; if (splist) split = splist; } // 判断有没有新添的数据 const addGoods = newGoods.filter(f => !f._id); if (addGoods.length > 0) { // 有新增货物项 // TODO,复制到split中和oldGoods中 for (const ng of addGoods) { oldGoods.push(ng); } } res.goods = oldGoods; res.split = split; // console.group('oldGoods'); // console.log(oldGoods); // console.groupEnd(); // console.group('split'); // console.log(split); // console.groupEnd(); await this.model.update({ _id: ObjectId(id) }, info); await res.save(); this.copyToSplit(id); try { this.record(res._id, { method: 'update' }); } catch (error) { this.logger.error(`订单id:${res.id}记录创建失败:${error.toString()}`); } return; } /** * 检查并处理有修改的货物:拆分就不允许修改了 * @param {Array} oldGoods 查出修改前的货物列表 * @param {Array} newGoods 修改后的货物列表 * @param {Array} split 原拆分货物列表,用来检查是否可以修改 */ async goodsUpdate(oldGoods, newGoods, split) { oldGoods = oldGoods.map(og => { const is_split = split.find(f => ObjectId(f.pid).equals(og._id) && f.type === '1'); if (is_split) return og; // 没有拆分,可以直接更换 const ng = newGoods.find(f => ObjectId(f._id).equals(og._id)); if (ng) return { ...ng, update: true }; return og; }); // 修改拆分货物列表 const toUpdateSplit = oldGoods.filter(f => f.update); split = split.map(i => { const res = toUpdateSplit.find(f => ObjectId(f._id).equals(i.pid)); if (res) { const obj = this.goodsCopy(res); return obj; } return i; }); // 将oldGoods的update摘出去 oldGoods = oldGoods.map(i => _.omit(i, [ 'update' ])); return { oldGoods, split }; } /** * 将货物复制成拆分货物的数据并返回 * @param {Object} data 货物列表的每项 * @return split */ goodsCopy(data) { const split = _.pick(data, [ 'name', 'number', 'weight', 'volume', 'transport_type', 'remark', ]); split.pid = data._id; return split; } /** * 根据订单id,复制货物 到 拆分货物,只是复制,其他操作在各自的方法中,这里只是复制 * @param {String} id 订单id */ async copyToSplit(id) { const order = await this.model.findById(id); if (!order) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到订单'); const { goods, split } = order; for (const g of goods) { const has = split.find(f => ObjectId(f.pid).equals(g._id)); if (!has) { order.split.push(this.goodsCopy(g)); } } await order.save(); } /** * 订单操作记录 * @param {String} id 订单id * @param {Object} {method:方法} 参数 */ async record(id, { method }) { const order = await this.model.findById(id); if (!order) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到订单'); const { authorization } = this.ctx.request.header; let user = decodeURI(authorization); if (!user) { throw new BusinessError(ErrorCode.USER_NOT_EXIST, '未找到操作人'); } user = JSON.parse(user); const { id: userid, name: username } = user; const record = { opera: username, operaid: userid }; // 创建记录 if (method === 'create') record.message = `${username}创建订单`; else if (method === 'update') record.message = `${username}修改订单`; else if (method === 'in') record.message = `${username}修改收入`; else if (method === 'out') record.message = `${username}修改支出`; else if (method === 'split') record.message = `${username}拆分货物`; order.record.push(record); await order.save(); } } module.exports = OrderService;