'use strict'; const BaseService = require('./base.js'); // 餐饮币变动服务类 class DiningCoinService extends BaseService { /** * 餐饮币变化 * @return {Promise} */ async addDiningCoinChangeLog(params = {}, transaction = null) { const that = this; let action_user = ''; // -1提现失败,0提现成功;1发放餐饮币;2提现中;3 // eslint-disable-next-line default-case switch (params.type) { case -1: case 0: case 2: action_user = '店铺提现'; break; case 1: action_user = '购买餐币'; break; case 3: action_user = '餐饮消费'; break; case 4: action_user = '核销餐币'; break; } const createBean = await that.app.comoBean.instance({ user_id: params.user_id, order_id: params.order_id, type: params.type, account: params.account, out_batch_no: params.out_batch_no, out_detail_no: params.out_detail_no, partner_id: params.partner_id, action_user, log_desc: params.log_desc, create_time: that.app.szjcomo.date('Y-m-d H:i:s'), ori_partner: params.ori_partner, customer_id: params.customer_id, customer_name: params.customer_name, customer_img: params.customer_img, }, { transaction }); const result = await that.create(createBean, that.app.model.DinnerCoinLogs, '记录用户餐饮币变动失败'); return result; } /** * 餐饮币发放到用户账户 并 记录 * @param orderInfo */ async dispatchDiningCoin(orderInfo = null) { const that = this; const transaction = await that.app.model.transaction(); try { if (orderInfo && !orderInfo.dining_coin_send) { // 2023/2/28 客源来源鉴别 let ori_partner_id = -1; const inviterInfo = await that.app.model.Users.findOne({ where: { user_id: orderInfo.inviter_id }, attributes: [ 'partner_id' ], }); if (inviterInfo) { ori_partner_id = inviterInfo.partner_id; } // 2023/2/28 订单餐饮币发放状态 let dining_coin_send = false; // 2023/2/27 订单商品匹配发放餐饮币 if (orderInfo.orders_products.length > 0) { for (const ordersProduct of orderInfo.orders_products) { if (ordersProduct.dining_partner_id > 0 && ordersProduct.dinning_coin_amount > 0) { // 2022/11/18 记录餐饮币发放记录 await that.addDiningCoinChangeLog({ user_id: orderInfo.user_id, order_id: orderInfo.order_id, partner_id: ordersProduct.dining_partner_id, type: 1, account: ordersProduct.dinning_coin_amount * ordersProduct.product_count, log_desc: '购酒获得餐饮币', }, transaction); // 2023/2/28 查询用户餐饮币账户列表 添加 或 更新账户余额 const info = await that.app.model.DinnerCoins.findOne({ where: { user_id: orderInfo.user_id, ori_partner: ori_partner_id === ordersProduct.dining_partner_id, partner_id: ordersProduct.dining_partner_id, expired: false, }, transaction, raw: true, }); if (!info) { // 2023/2/27 没有对应类型的餐饮币 则插入该类型的餐饮币账户 const data = { user_id: orderInfo.user_id, account: ordersProduct.dinning_coin_amount * ordersProduct.product_count, ori_partner: ori_partner_id === ordersProduct.dining_partner_id, partner_id: ordersProduct.dining_partner_id, create_time: that.app.szjcomo.date('Y-m-d H:i:s'), expired: false, expired_time: that.app.szjcomo.date('Y-m-d H:i:s', parseInt(+new Date() + '') / 1000 + 90 * 24 * 60 * 60), }; const res = await that.app.model.PartnerInfo.findOne({ where: { id: ordersProduct.dining_partner_id }, transaction, raw: true, }); data.partner_name = res.name; data.partner_address = res.address; data.partner_tel = res.tel_num; data.partner_opening_time = res.opening_time; const createBean = await that.app.comoBean.instance(data, { transaction }); await that.service.base.create(createBean, that.app.model.DinnerCoins, '餐饮币发放失败,请重试'); } else { const updateBean = await that.app.comoBean.instance({ account: info.account + ordersProduct.dinning_coin_amount * ordersProduct.product_count, update_time: that.app.szjcomo.date('Y-m-d H:i:s'), expired: false, expired_time: that.app.szjcomo.date('Y-m-d H:i:s', parseInt(+new Date() + '') / 1000 + 90 * 24 * 60 * 60), }, { where: { user_id: orderInfo.user_id, ori_partner: ori_partner_id === ordersProduct.dining_partner_id, partner_id: ordersProduct.dining_partner_id, }, transaction, }); await that.service.base.update(updateBean, that.app.model.DinnerCoins, '餐饮币余额更新失败,请重试'); } dining_coin_send = true; // 2023/3/28 更新餐店订单量+1 const updatePartnerBean = await that.app.comoBean.instance({ order_amount: that.app.Sequelize.literal('order_amount + ' + 1), update_time: that.app.szjcomo.date('Y-m-d H:i:s'), }, { where: { id: ordersProduct.dining_partner_id }, transaction }); await that.update(updatePartnerBean, that.app.model.PartnerInfo, '合作餐店订单量更新失败,请稍候重试'); } } // 2023/2/28 更新订单餐饮币发放状态 if (dining_coin_send) { const updateBean = that.app.comoBean.instance({ dining_coin_send }, { where: { order_id: orderInfo.order_id }, transaction, }); await that.service.base.update(updateBean, that.app.model.Orders, '更新订单餐饮币发放状态失败,请稍候重试'); } await transaction.commit(); } } } catch (e) { if (transaction) await transaction.rollback(); throw new Error(e.toString()); } } /** * 付款后操作检查订单及餐饮币发放 * @return {Promise} */ async orderDiningCoinDispatchAfterPay(order_id = -1) { const that = this; // 2022/12/24 查询订单包含商品列表详情 const order = await that.selectOrderInfo(order_id); if (!order) throw new Error('订单不存在'); await that.dispatchDiningCoin(order); } /** * 查询订单详情 * @param order_id * @return {Promise} */ async selectOrderInfo(order_id) { const that = this; const options = { where: { order_id }, include: [ { model: that.app.model.OrdersProducts, as: 'orders_products', attributes: { exclude: [ 'product_id', 'create_time', 'order_id', 'activity_name', 'activity_id', 'activity_desc' ], }, }, ], 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); try { return await that.service.base.select(selectBean, that.app.model.Orders, '查询订单详情失败,请稍候重试', false, false); } catch (e) { that.logger.error('订单id:%s ;分佣查询订单详情失败', order_id); return null; } } } module.exports = DiningCoinService;