'use strict'; const ManagerController = require('../manager.js'); /** * [exports 订单控制器] * @type {[type]} */ module.exports = class OrdersController extends ManagerController { /** * [useModel 使用模型] * @return {[type]} [description] */ get useModel() { const that = this; return that.app.model.Orders; } /** * [selectValidate 订单查询验证器] * @return {[type]} [description] */ get selectValidate() { const that = this; return { page: that.ctx.rules.default(1) .number(), limit: that.ctx.rules.default(20) .number(), product_name: that.ctx.rules.default('') .required(), uname: that.ctx.rules.default('') .required(), order_status: that.ctx.rules.default('') .required(), order_time: that.ctx.rules.default('') .required(), order_id: that.ctx.rules.default(0) .number(), }; } /** * [deliverValidate 订单发货验证器] * @return {[type]} [description] */ get deliverValidate() { const that = this; return { goods: that.ctx.rules.name('商品发货信息') .required() .isArray(), is_express: that.ctx.rules.name('是否需要物流') .required() .notEmpty() .number(), express_sn: that.ctx.rules.default('') .required(), order_id: that.ctx.rules.name('订单ID') .required() .notEmpty() .number(), express_id: that.ctx.rules.default(0) .number(), deliver_desc: that.ctx.rules.default('') .required(), admin_id: that.ctx.rules.default(that.service.manager.ActionAdminUserId()) .number(), deliver_time: that.ctx.rules.default(that.app.szjcomo.date('Y-m-d H:i:s')) .required(), }; } /** * [logsValidate 订单操作日志验证器] * @return {[type]} [description] */ get logsValidate() { const that = this; return { order_id: that.ctx.rules.name('订单ID') .required() .notEmpty() .number(), }; } /** * [select 查询订单列表] * @return {[type]} [description] */ async select() { const that = this; try { const data = await that.ctx.validate(that.selectValidate, await that.ctx.getParse()); let result; if (data.order_id) { result = await that.selectInfo(data.order_id); } else { result = await that.selectList(data); } return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false)); } catch (err) { return that.ctx.appJson(that.app.szjcomo.appResult(err.message)); } } /** * [selectInfo 订单详情] * @param {[type]} order_id [description] * @return {[type]} [description] */ async selectInfo(order_id) { const that = this; const options = { where: { order_id }, include: [ { model: that.app.model.PaysConfig, attributes: [ 'pay_name' ], as: 'pays_config' }, { model: that.app.model.Users, as: 'users', attributes: [ 'account_name', 'nickname' ] }, { model: that.app.model.Users, as: 'inviter', attributes: [ 'account_name', 'nickname' ] }, { model: that.app.model.OrdersProducts, as: 'orders_products', attributes: { exclude: [ 'product_id', 'create_time', 'order_id', 'activity_name', 'activity_id', 'activity_desc' ], }, }, { model: that.app.model.Payments, as: 'payments', attributes: [ 'create_time' ], required: false }, ], attributes: { exclude: [ 'seller_remarks', 'province_id', 'city_id', 'county_id', 'shipping_id', 'shipping_fee', 'activity_id', 'activity_name', 'activity_desc' ], }, }; const selectBean = await that.app.comoBean.instance({}, options); return await that.service.base.select(selectBean, that.useModel, '查询订单详情失败,请稍候重试', false, false); } /** * [selectList 订单列表] * @param {Object} data [description] * @return {[type]} [description] */ async selectList(data = {}) { const that = this; const seq = that.app.Sequelize; const productWhere = {}; let userWhere = {}; if (data.product_name) productWhere.product_name = { [seq.Op.regexp]: data.product_name }; if (data.uname) userWhere = { [seq.Op.or]: [{ nickname: { [seq.Op.regexp]: data.uname } }, { account_name: { [seq.Op.regexp]: data.uname } }] }; const opts = { offset: (data.page - 1) * data.limit, limit: data.limit, where: {}, include: [ { model: that.app.model.PaysConfig, attributes: [ 'pay_name' ], as: 'pays_config' }, { model: that.app.model.Users, as: 'users', attributes: [ 'account_name', 'nickname' ], where: userWhere }, { model: that.app.model.Users, as: 'inviter', attributes: [ 'account_name', 'nickname' ] }, { model: that.app.model.OrdersProducts, as: 'orders_products', attributes: { exclude: [ 'product_id', 'create_time', 'order_id', 'activity_name', 'activity_id', 'activity_desc' ], }, where: productWhere, order: [[ 'rec_id', 'asc' ]], }, ], attributes: { exclude: [ 'seller_remarks', 'province_id', 'city_id', 'county_id', 'shipping_id', 'shipping_fee', 'activity_id', 'activity_name', 'activity_desc' ], }, order: [[ 'order_id', 'desc' ]], }; if (data.order_status !== '') opts.where.order_status = Number(data.order_status); if (data.order_time) opts.where.create_time = { [seq.Op.between]: data.order_time.split(',') }; const selectBean = await that.app.comoBean.instance(data, opts); const result = await that.service.base.select(selectBean, that.useModel, '订单列表查询失败,请稍候重试', true, true); return result; } /** * [orderDeliver 订单发货操作] * @return {[type]} [description] */ async orderDeliver() { const that = this; let transaction; try { const data = await that.ctx.validate(that.deliverValidate, await that.ctx.anyParse()); transaction = await that.app.model.transaction(); const updateBean = await that.app.comoBean.instance({ is_express: data.is_express, express_sn: data.express_sn, express_id: data.express_id, deliver_desc: data.deliver_desc, deliver_time: data.deliver_time, order_status: 2, }, { where: { order_id: data.order_id }, transaction }); const result = await that.service.base.update(updateBean, that.useModel, '订单发货失败,请稍候重试'); for (let i = 0; i < data.goods.length; i++) { const item = data.goods[i]; const tmp = await that.app.model.OrdersProducts.update({ deliver_count: Number(item.deliver_count), is_deliver: 1, }, { where: { rec_id: item.rec_id, order_id: data.order_id }, transaction }); if (!tmp) throw new Error('订单发货失败,请稍候重试'); } // 记录订单操作日志 await that.service.order.orderAction({ admin_id: data.admin_id, order_id: data.order_id, action_desc: '订单发货', }); // 订单发货时扣除库存,受控于后台配置的扣除时机 await that.service.order.productStockSub(data.order_id, transaction, 2); await transaction.commit(); return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false)); } catch (err) { if (transaction) await transaction.rollback(); return that.ctx.appJson(that.app.szjcomo.appResult(err.message)); } } /** * [orderLogs 查看订单操作日志] * @return {[type]} [description] */ async orderLogs() { const that = this; try { const data = await that.ctx.validate(that.loginValidate, await that.ctx.getParse()); const result = await that.service.order.orderLogs(data.order_id); return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false)); } catch (err) { return that.ctx.appJson(that.app.szjcomo.appResult(err.message)); } } /** * [orderPrinter 订单打印] * @return {[type]} [description] */ async orderPrinter() { const that = this; try { const data = await that.ctx.validate(that.loginValidate, await that.ctx.getParse()); const result = await that.service.shop.orderPrinter(data.order_id, true); return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false)); } catch (err) { return that.ctx.appJson(that.app.szjcomo.appResult(err.message)); } } };