123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- '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(),
- };
- }
- /**
- * 确认收货验证器
- * @date:2024/7/8
- */
- get receiveValidate() {
- const that = this;
- return {
- order_status: that.ctx.rules.default(3)
- .number(),
- };
- }
- /**
- * [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));
- }
- }
- /**
- * [orderDeliver 订单确认收货操作]
- * @return {[type]} [description]
- */
- async orderReceive() {
- const that = this;
- let transaction;
- try {
- const data = await that.ctx.validate(that.receiveValidate, await that.ctx.anyParse());
- transaction = await that.app.model.transaction();
- const updateBean = await that.app.comoBean.instance({
- order_status: 3,
- }, { where: { order_id: data.order_id }, transaction });
- const result = await that.service.base.update(updateBean, that.useModel, '订单确认收货失败,请稍候重试');
- // 记录订单操作日志
- await that.service.order.orderAction({
- admin_id: data.admin_id,
- order_id: data.order_id,
- action_desc: '订单确认收货',
- });
- 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));
- }
- }
- };
|