123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- 'use strict';
- const BaseService = require('./base.js');
- class CommissionService extends BaseService {
-
- async addCommissionChangeLog(params) {
- const that = this;
- let action_user = '';
-
-
- 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;
- }
-
- 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;
- }
-
- async dispatchCommission(order = null) {
- const that = this;
- let commission_log_id = -1;
- try {
- if (order && order.inviter_id > 0) {
-
- 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;
-
- const partnerInfo = await that.app.model.PartnerInfo.findOne({
- where: { user_id: order.inviter_id },
- });
-
- 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);
-
- 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) {
-
- commission += order.orders_products[i].total_price * ratioSpecialSale;
- } else if (partnerInfo && partnerInfo.category_id === order.orders_products[i].category_id) {
-
- 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) {
-
- 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 } });
-
- await that.update(updateBean, that.app.model.Users, '用户分佣金额更新失败,请稍候重试');
-
- const orderUserInfo = await that.app.model.Users.findOne({
- where: { user_id: order.user_id },
- attributes: [ 'user_id', 'nickname', 'headimgurl' ],
- raw: true,
- });
-
- 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;
- }
-
- async orderCommissionDispatchAfterPay(order_id = -1) {
- const that = this;
-
-
- 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) {
-
- const dispatchResult = that.dispatchCommission(order);
- let commission_log_id = -1;
- dispatchResult.then(log_id => {
- commission_log_id = log_id;
- if (commission_log_id >= 0) {
-
- 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);
- }
- });
- }
- }
-
- 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;
|