'use strict'; const BaseService = require('./base.js'); // 分佣变动服务类 class CommissionService extends BaseService { /** * 佣金变化 * @param order_id (64字符 为提现out_detail_no) * @return {Promise} */ async addCommissionChangeLog(params) { const that = this; let action_user = ''; // -1提现失败,0提现成功;1分佣;2提现中; // eslint-disable-next-line default-case switch (params.type) { case -1: case 0: case 2: action_user = '用户提现'; break; case 1: action_user = '订单分佣'; break; } const createBean = await that.app.comoBean.instance({ user_id: params.user_id, order_id: params.order_id, type: params.type, commission: params.commission, out_batch_no: params.out_batch_no, out_detail_no: params.out_detail_no, action_user, log_desc: params.log_desc, create_time: that.app.szjcomo.date('Y-m-d H:i:s'), }, {}); const result = await that.create(createBean, that.app.model.UsersCommissionLogs, '记录邀请用户分佣变动失败'); return result; } /** * 记录邀请用户分佣变动 * log_id int unsigned auto_increment comment '记录ID' primary key, * log_desc varchar(255) not null comment '变动说明', * user_id int unsigned not null comment '用户ID', * order_id int default -1 not null comment '订单ID,提现类型订单id为-1', * commission float(10, 2) not null comment '变动金额', * action_user varchar(255) not null comment '操作对象', * type int default 0 not null comment '0提现;1分佣', * create_time timestamp default CURRENT_TIMESTAMP not null comment '创建时间' * @return {Promise} */ async inviterCommissionLog(orderUserInfo = {}, inviter_id, order_id, type, commission, log_desc) { const that = this; let action_user = ''; if (type === 0) { action_user = '提现'; } else if (type === 1) { action_user = '分佣'; } const createBean = await that.app.comoBean.instance({ user_id: inviter_id, order_id, type, commission, action_user, inviter_id: orderUserInfo.user_id ? orderUserInfo.user_id : -1, inviter_name: orderUserInfo.nickname ? orderUserInfo.nickname : '', inviter_img: orderUserInfo.headimgurl ? orderUserInfo.headimgurl : '', log_desc, create_time: that.app.szjcomo.date('Y-m-d H:i:s'), }, {}); const result = await that.create(createBean, that.app.model.UsersCommissionLogs, '记录邀请用户分佣变动失败'); return result; } /** * 分佣发放到用户账户 并 记录 * @param order * @return {Promise} commission_log_id */ async dispatchCommission(order = null) { const that = this; let commission_log_id = -1; try { if (order && order.inviter_id > 0) { // 2022/11/18 获取配置分佣比例 const keys = [ 'shop_commission_ratio', 'partner_commission_ratio', 'not_commission_category', 'special_sale_category', 'sale_commission_ratio' ]; const config = await that.service.configs.getConfigMoreValue(keys); let ratioDefault = 0; let ratioPartner = 0; let ratioSpecialSale = 0; let saleCategoryId = 15; // 2023/4/18 邀请人餐店合伙信息 const partnerInfo = await that.app.model.PartnerInfo.findOne({ where: { user_id: order.inviter_id }, }); // 2023/4/18 : 餐店 套餐推广 category_id 分佣比例特殊处理 partner_commission_ratio ratioPartner = Number(config.partner_commission_ratio); ratioDefault = Number(config.shop_commission_ratio); ratioSpecialSale = Number(config.sale_commission_ratio); saleCategoryId = Number(config.special_sale_category); const temA = config.not_commission_category.replace('[', ''); const temB = temA.replace(']', ''); let notCommissionCategory = temB.split(','); notCommissionCategory = notCommissionCategory.map(Number); // 2022/11/18 计算分佣金额 排除无分佣的分类 let commission = 0; if (order.orders_products.length > 0) { for (let i = 0; i < order.orders_products.length; i++) { if (!notCommissionCategory.includes(order.orders_products[i].category_id)) { if (saleCategoryId === order.orders_products[i].category_id) { // 2023/5/24 特销产品分佣 commission += order.orders_products[i].total_price * ratioSpecialSale; } else if (partnerInfo && partnerInfo.category_id === order.orders_products[i].category_id) { // 2023/4/18 餐店推广自己的分类套餐 分佣比例有区别 commission += order.orders_products[i].total_price * ratioPartner; } else { commission += order.orders_products[i].total_price * ratioDefault; } } } } else { commission = ratioDefault * order.order_amount; } commission = commission.toFixed(2); if (commission > 0) { // 2022/11/18 记录 并 发放到用户账户 const updateBean = await that.app.comoBean.instance({ commission: that.app.Sequelize.literal('commission + ' + commission), update_time: that.app.szjcomo.date('Y-m-d H:i:s'), }, { where: { user_id: order.inviter_id } }); // 2022/9/27 用户分佣余额更新 用户表szj_users await that.update(updateBean, that.app.model.Users, '用户分佣金额更新失败,请稍候重试'); // 2022/12/13 当前订单的用户信息 const orderUserInfo = await that.app.model.Users.findOne({ where: { user_id: order.user_id }, attributes: [ 'user_id', 'nickname', 'headimgurl' ], raw: true, }); // 2022/11/18 记录分佣发放 const logResult = await that.inviterCommissionLog(orderUserInfo, order.inviter_id, order.order_id, 1, commission, '邀请用户下单分佣'); commission_log_id = logResult.log_id; } } } catch (e) { return commission_log_id; } return commission_log_id; } /** * 付款后操作检查订单及其分佣处理 * @return {Promise} */ async orderCommissionDispatchAfterPay(order_id = -1) { const that = this; /* const order = await that.app.model.Orders.findOne({ where: { order_id }, raw: true, });*/ // 2022/12/24 查询订单包含商品列表详情 const order = await that.selectOrderInfo(order_id); if (!order) throw new Error('订单不存在'); if (order.inviter_id > 0 && order.commission_log_id === -1 && order.order_status === 1) { // 2022/11/17 发放分佣并记录 const dispatchResult = that.dispatchCommission(order); let commission_log_id = -1; dispatchResult.then(log_id => { commission_log_id = log_id; if (commission_log_id >= 0) { // 2022/11/17 更新订单信息 const updateBean = that.app.comoBean.instance({ commission_log_id }, { where: { order_id: order.order_id }, }); that.service.base.update(updateBean, that.app.model.Orders, '发放分佣后更新订单状态失败,请稍候重试'); } else { that.logger.error('发放分佣记录失败,对应订单order_id: %s , 订单金额为:%s', order.order_id, order.order_amount); } }); } } /** * 查询订单详情 * @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 = CommissionService;