123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- /* eslint-disable eqeqeq */
- 'use strict';
- const ShopService = require('./shop.js');
- // 订单服务类
- class OrderService extends ShopService {
- /**
- * [statusNames 订单状态说明]
- * @return {[type]} [description]
- */
- get statusNames() {
- return [ '待付款', '待发货', '待收货', '待评价', '交易完成', '备货中(无效)', '订单已取消' ];
- }
- /**
- * [orderAction 订单操作记录]
- * @param {Object} data [description]
- * @return {[type]} [description]
- */
- async orderAction(data = {}, transaction = null) {
- const that = this;
- const createData = Object.assign({ create_time: that.app.szjcomo.date('Y-m-d H:i:s') }, data);
- const createBean = await that.app.comoBean.instance(createData, { transaction });
- const result = await that.service.base.create(createBean, that.app.model.OrdersAction, '订单操作记录失败,请重试');
- return result;
- }
- /**
- * [orderLogs 查询订单操作日志]
- * @param {[type]} order_id [description]
- * @return {[type]} [description]
- */
- async orderLogs(order_id) {
- const that = this;
- const seq = that.app.Sequelize;
- const selectBean = await that.app.comoBean.instance({}, {
- where: { order_id }, include: { model: that.app.model.AdminUser, as: 'admin_user', attributes: [] },
- attributes: {
- include: [[ seq.col('admin_user.username'), 'username' ]],
- exclude: [ 'admin_id', 'order_id' ],
- }, order: [[ 'action_id', 'asc' ]],
- });
- const result = await that.service.base.select(selectBean, that.app.model.OrdersAction, '操作记录查询失败,请稍候重试', false, true);
- return result;
- }
- /**
- * [caclPaymentMoney 计算用户需要支付的金额]
- * @param {[type]} data [description]
- * @param {[type]} orderAmount [description]
- * @param {[type]} transaction [description]
- * @return {[type]} [description]
- */
- async caclPaymentMoney(data, orderAmount, transaction = null) {
- const that = this;
- const userMoney = await that.getUserMoney(data.user_id, transaction);
- let payment_money = 0;
- const surplus_amout = userMoney >= orderAmount ? orderAmount : userMoney;
- if (surplus_amout < orderAmount) payment_money = (orderAmount - surplus_amout);
- return { surplus_amout, payment_money };
- }
- /**
- * [caclPaymentMoneyV2 计算用户需要支付的金额]
- * @param {[type]} data [description]
- * @param orderAmount
- * @param {[type]} transaction [description]
- * @param isUseMoney 是否使用余额抵扣
- * @return {[type]} [description]
- */
- async caclPaymentMoneyV2(data, orderAmount = {
- orderPriceCanDiscount: 0,
- orderPriceNotDiscount: 0,
- }, transaction = null, isUseMoney = false) {
- const that = this;
- let userMoney = 0;
- if (isUseMoney) {
- userMoney = await that.getUserMoney(data.user_id, transaction);
- } else {
- userMoney = 0;
- }
- let payment_money = 0;
- const surplus_amout = userMoney >= orderAmount.orderPriceCanDiscount ? orderAmount.orderPriceCanDiscount : userMoney;
- const totalAmount = orderAmount.orderPriceCanDiscount + orderAmount.orderPriceNotDiscount;
- if (surplus_amout < totalAmount) payment_money = (totalAmount - surplus_amout);
- return { surplus_amout, payment_money };
- }
- /**
- * [getSurplusPayParams 用户余额支付操作]
- * @param {Number} order_id [description]
- * @param {Number} user_id [description]
- * @return {[type]} [description]
- */
- async getSurplusPayParams(order_id, user_id = 0, surplus_amout = 0, transaction = null) {
- const that = this;
- const user = await that.app.model.Users.findOne({
- where: { user_id },
- attributes: [ 'openid' ],
- transaction,
- });
- const tmp = {
- openid: user.openid || '',
- total_fee: surplus_amout * 100,
- user_id,
- pay_id: 0,
- cash_fee: surplus_amout * 100,
- };
- const result = await that.service.shop.createPaymentLog(tmp, transaction);
- const updateBean = await that.app.comoBean.instance({ order_status: 1, pay_sn: result.out_trade_no }, {
- transaction, where: { order_id, user_id },
- });
- const res = await that.service.base.update(updateBean, that.app.model.Orders, '订单状态更新失败,请稍候重试');
- await that.service.shop.orderPrinter(order_id);
- return { payment_money: 0, params: {}, paymoney_type: 0 };
- }
- /**
- * [getOrederPrinterInfo 获取打印订单的详情]
- * @param {[type]} order_id [description]
- * @return {[type]} [description]
- */
- async getOrederPrinterInfo(order_id) {
- const that = this;
- const seq = that.app.Sequelize;
- const selectBean = await that.app.comoBean.instance({}, {
- where: { order_id },
- include: [
- { model: that.app.model.PaysConfig, attributes: [], as: 'pays_config' },
- { model: that.app.model.Users, as: 'users', attributes: [] },
- { model: that.app.model.Users, as: 'inviter', attributes: [] },
- { model: that.app.model.Payments, as: 'payments', attributes: [] },
- {
- model: that.app.model.OrdersProducts, as: 'orders_products', attributes: {
- exclude: [ 'product_id', 'create_time', 'order_id', 'activity_name', 'activity_id', 'activity_desc' ],
- },
- },
- ],
- attributes: [
- [ seq.col('pays_config.pay_name'), 'pay_name' ],
- [ seq.col('users.nickname'), 'userName' ],
- [ seq.col('inviter.nickname'), 'inviterName' ],
- [ seq.col('payments.total_fee'), 'total_fee' ],
- 'order_status', 'pay_sn', 'consignee', 'mobile', 'address',
- 'create_time', 'order_sn', 'order_amount', 'surplus_amout', 'user_remarks',
- ],
- });
- const result = await that.service.base.select(selectBean, that.app.model.Orders, '查询订单详情失败,请稍候重试', false, false);
- result.order_status = that.statusNames[result.order_status];
- result.order_amount = Number(result.order_amount)
- .toFixed(2);
- result.surplus_amout = Number(result.surplus_amout)
- .toFixed(2);
- const resultTemp1 = JSON.stringify(result);
- const result2 = JSON.parse(resultTemp1);
- result2.total_fee = result2.total_fee ? (Number(result2.total_fee) / 100).toFixed(2)
- .toString() : '0.00';
- const goods = [];
- result2.orders_products.forEach(item => {
- goods.push(`${item.product_name}${'【' + item.product_sn + '】'}${item.volume > 0 ? item.volume + 'ml' : ''}\tx\t${item.product_count}`);
- });
- result2.goods = goods.join('\n');
- result2.nickname = result2.inviterName ? result2.userName + '【邀请人:' + result2.inviterName + '】' : result2.userName;
- return result2;
- }
- /**
- * [orderDataCount 订单数据统计]
- * @return {[type]} [description]
- */
- async orderDataCount() {
- const that = this;
- const seq = that.app.Sequelize;
- const orderCount = await that.app.model.Orders.count({
- group: [ 'order_status' ],
- });
- const saleCount = await that.app.model.Orders.sum('order_amount', { where: { order_status: { [seq.Op.in]: [ 1, 2, 3, 4 ] } } });
- const surplusCount = await that.app.model.Orders.sum('surplus_amout', { where: { order_status: { [seq.Op.in]: [ 1, 2, 3, 4 ] } } });
- const result = { data_count: orderCount, sale_money: saleCount, surplus_money: surplusCount };
- return result;
- }
- /**
- * [saleDataCount 销售数量统计]
- * @return {[type]} [description]
- */
- async saleDataCount(day = 20) {
- const that = this;
- const curTime = that.app.szjcomo.time();
- const seq = that.app.Sequelize;
- const times = [];
- for (let i = 0; i < day; i++) {
- const tmpTime = curTime - i * 24 * 60 * 60;
- times.push([ `${that.app.szjcomo.date('Y-m-d', tmpTime)} 00:00:00`, `${that.app.szjcomo.date('Y-m-d', tmpTime)} 23:59:59` ]);
- }
- const orders = await that.app.model.Orders.findAll({
- where: {
- create_time: { [seq.Op.between]: [ times[day - 1][0], times[0][1] ] },
- order_status: { [seq.Op.gt]: 0 },
- },
- attributes: [ 'order_amount', 'create_time' ], raw: true,
- });
- const result = [];
- times.forEach(item => {
- const startTime = that.app.szjcomo.strToTime(item[0]);
- const endTime = that.app.szjcomo.strToTime(item[1]);
- const tmpData = { date: that.app.szjcomo.date('m-d', startTime), money: 0 };
- orders.forEach(childItem => {
- const tmpTime = that.app.szjcomo.strToTime(childItem.create_time);
- if (tmpTime >= startTime && tmpTime <= endTime) {
- tmpData.money += childItem.order_amount;
- }
- });
- result.push(tmpData);
- });
- return result;
- }
- /**
- * [productStockSub 发货后扣除库存]
- * @param {Array} products [description]
- * @param {[type]} transaction [description]
- * @return {[type]} [description]
- */
- async productStockSub(order_id, transaction = null, index = 2) {
- const that = this;
- const product_stock_subtime = await that.service.configs.getConfigValue('product_stock_subtime');
- if (Number(product_stock_subtime) != index) return false;
- const seq = that.app.Sequelize;
- const products = await that.app.model.OrdersProducts.findAll({
- where: { order_id }, attributes: [ 'product_id', 'deliver_count' ], raw: true,
- transaction,
- });
- if (!products.length) return false;
- for (let i = 0; i < products.length; i++) {
- const item = products[i];
- const updateData = {
- product_stock: seq.literal(`product_stock - ${item.deliver_count}`),
- sale_count: seq.literal(`sale_count + ${item.deliver_count}`),
- update_time: that.app.szjcomo.date('Y-m-d H:i:s'),
- };
- const res = await that.app.model.Products.update(updateData, {
- where: { product_id: item.product_id },
- transaction,
- });
- if (!res) throw new Error('库存扣除失败,请稍候重试');
- }
- return true;
- }
- }
- module.exports = OrderService;
|