commission.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. 'use strict';
  2. const BaseService = require('./base.js');
  3. // 分佣变动服务类
  4. class CommissionService extends BaseService {
  5. /**
  6. * 佣金变化
  7. * @param order_id (64字符 为提现out_detail_no)
  8. * @return {Promise<type[]>}
  9. */
  10. async addCommissionChangeLog(params) {
  11. const that = this;
  12. let action_user = '';
  13. // -1提现失败,0提现成功;1分佣;2提现中;
  14. // eslint-disable-next-line default-case
  15. switch (params.type) {
  16. case -1:
  17. case 0:
  18. case 2:
  19. action_user = '用户提现';
  20. break;
  21. case 1:
  22. action_user = '订单分佣';
  23. break;
  24. }
  25. const createBean = await that.app.comoBean.instance({
  26. user_id: params.user_id, order_id: params.order_id, type: params.type, commission: params.commission,
  27. out_batch_no: params.out_batch_no, out_detail_no: params.out_detail_no,
  28. action_user, log_desc: params.log_desc, create_time: that.app.szjcomo.date('Y-m-d H:i:s'),
  29. }, {});
  30. const result = await that.create(createBean, that.app.model.UsersCommissionLogs, '记录邀请用户分佣变动失败');
  31. return result;
  32. }
  33. /**
  34. * 记录邀请用户分佣变动
  35. * log_id int unsigned auto_increment comment '记录ID' primary key,
  36. * log_desc varchar(255) not null comment '变动说明',
  37. * user_id int unsigned not null comment '用户ID',
  38. * order_id int default -1 not null comment '订单ID,提现类型订单id为-1',
  39. * commission float(10, 2) not null comment '变动金额',
  40. * action_user varchar(255) not null comment '操作对象',
  41. * type int default 0 not null comment '0提现;1分佣',
  42. * create_time timestamp default CURRENT_TIMESTAMP not null comment '创建时间'
  43. * @return {Promise<type[]>}
  44. */
  45. async inviterCommissionLog(orderUserInfo = {}, inviter_id, order_id, type, commission, log_desc) {
  46. const that = this;
  47. let action_user = '';
  48. if (type === 0) {
  49. action_user = '提现';
  50. } else if (type === 1) {
  51. action_user = '分佣';
  52. }
  53. const createBean = await that.app.comoBean.instance({
  54. user_id: inviter_id,
  55. order_id,
  56. type,
  57. commission,
  58. action_user,
  59. inviter_id: orderUserInfo.user_id ? orderUserInfo.user_id : -1,
  60. inviter_name: orderUserInfo.nickname ? orderUserInfo.nickname : '',
  61. inviter_img: orderUserInfo.headimgurl ? orderUserInfo.headimgurl : '',
  62. log_desc,
  63. create_time: that.app.szjcomo.date('Y-m-d H:i:s'),
  64. }, {});
  65. const result = await that.create(createBean, that.app.model.UsersCommissionLogs, '记录邀请用户分佣变动失败');
  66. return result;
  67. }
  68. /**
  69. * 分佣发放到用户账户 并 记录
  70. * @param order
  71. * @return {Promise<number>} commission_log_id
  72. */
  73. async dispatchCommission(order = null) {
  74. const that = this;
  75. let commission_log_id = -1;
  76. try {
  77. if (order && order.inviter_id > 0) {
  78. // 2022/11/18 获取配置分佣比例
  79. const keys = [ 'shop_commission_ratio', 'employee_commission_ratio', 'not_commission_category',
  80. 'special_sale_category', 'sale_commission_ratio' ];
  81. const config = await that.service.configs.getConfigMoreValue(keys);
  82. let ratioEmployee = 0.01;
  83. let ratioDefault = 0.05;
  84. let ratioSpecialSale = 0;
  85. let saleCategoryId = 8;
  86. // 2023/4/18 邀请人个人信息
  87. const inviterInfo = await that.app.model.Users.findOne({
  88. where: { user_id: order.inviter_id },
  89. });
  90. ratioEmployee = Number(config.employee_commission_ratio) ? Number(config.employee_commission_ratio) : 0.01;
  91. ratioDefault = Number(config.shop_commission_ratio) ? Number(config.shop_commission_ratio) : 0.05;
  92. ratioSpecialSale = Number(config.sale_commission_ratio) ? Number(config.sale_commission_ratio) : 0;
  93. saleCategoryId = Number(config.special_sale_category) ? Number(config.special_sale_category) : 8;
  94. const temA = config.not_commission_category.replace('[', '');
  95. const temB = temA.replace(']', '');
  96. let notCommissionCategory = temB.split(',');
  97. notCommissionCategory = notCommissionCategory.map(Number);
  98. // 2022/11/18 计算分佣金额 排除无分佣的分类
  99. let commission = 0;
  100. if (order.orders_products.length > 0) {
  101. for (let i = 0; i < order.orders_products.length; i++) {
  102. if (!notCommissionCategory.includes(order.orders_products[i].category_id)) {
  103. if (saleCategoryId === order.orders_products[i].category_id) {
  104. // 2023/5/24 特销产品分佣
  105. commission += order.orders_products[i].total_price * ratioSpecialSale;
  106. } else if (inviterInfo && inviterInfo.is_employee == true) {
  107. // 2023/4/18 店员分佣比例有区别
  108. commission += order.orders_products[i].total_price * ratioEmployee;
  109. } else {
  110. commission += order.orders_products[i].total_price * ratioDefault;
  111. }
  112. }
  113. }
  114. } else {
  115. commission = ratioDefault * order.order_amount;
  116. }
  117. commission = commission.toFixed(2);
  118. if (commission > 0) {
  119. // 2022/11/18 记录 并 发放到用户账户
  120. const updateBean = await that.app.comoBean.instance({
  121. commission: that.app.Sequelize.literal('commission + ' + commission),
  122. update_time: that.app.szjcomo.date('Y-m-d H:i:s'),
  123. }, { where: { user_id: order.inviter_id } });
  124. // 2022/9/27 用户分佣余额更新 用户表szj_users
  125. await that.update(updateBean, that.app.model.Users, '用户分佣金额更新失败,请稍候重试');
  126. // 2022/12/13 当前订单的用户信息
  127. const orderUserInfo = await that.app.model.Users.findOne({
  128. where: { user_id: order.user_id },
  129. attributes: [ 'user_id', 'nickname', 'headimgurl' ],
  130. raw: true,
  131. });
  132. // 2022/11/18 记录分佣发放
  133. const logResult = await that.inviterCommissionLog(orderUserInfo, order.inviter_id, order.order_id,
  134. 1, commission, '邀请用户下单分佣');
  135. commission_log_id = logResult.log_id;
  136. }
  137. }
  138. } catch (e) {
  139. return commission_log_id;
  140. }
  141. return commission_log_id;
  142. }
  143. /**
  144. * 付款后操作检查订单及其分佣处理
  145. * @return {Promise<void>}
  146. */
  147. async orderCommissionDispatchAfterPay(order_id = -1) {
  148. const that = this;
  149. /* const order = await that.app.model.Orders.findOne({
  150. where: { order_id }, raw: true,
  151. });*/
  152. // 2022/12/24 查询订单包含商品列表详情
  153. const order = await that.selectOrderInfo(order_id);
  154. if (!order) throw new Error('订单不存在');
  155. if (order.inviter_id > 0 && order.commission_log_id === -1 && order.order_status === 1) {
  156. // 2022/11/17 发放分佣并记录
  157. const dispatchResult = that.dispatchCommission(order);
  158. let commission_log_id = -1;
  159. dispatchResult.then(log_id => {
  160. commission_log_id = log_id;
  161. if (commission_log_id >= 0) {
  162. // 2022/11/17 更新订单信息
  163. const updateBean = that.app.comoBean.instance({ commission_log_id }, {
  164. where: { order_id: order.order_id },
  165. });
  166. that.service.base.update(updateBean, that.app.model.Orders, '发放分佣后更新订单状态失败,请稍候重试');
  167. } else {
  168. that.logger.error('发放分佣记录失败,对应订单order_id: %s , 订单金额为:%s', order.order_id, order.order_amount);
  169. }
  170. });
  171. }
  172. }
  173. /**
  174. * 查询订单详情
  175. * @param order_id
  176. * @return {Promise<null|*>}
  177. */
  178. async selectOrderInfo(order_id) {
  179. const that = this;
  180. const options = {
  181. where: { order_id }, include: [
  182. {
  183. model: that.app.model.OrdersProducts, as: 'orders_products', attributes: {
  184. exclude: [ 'product_id', 'create_time', 'order_id', 'activity_name', 'activity_id', 'activity_desc' ],
  185. },
  186. },
  187. ], attributes: {
  188. exclude: [ 'seller_remarks', 'province_id', 'city_id', 'county_id', 'shipping_id', 'shipping_fee', 'activity_id', 'activity_name', 'activity_desc' ],
  189. },
  190. };
  191. const selectBean = await that.app.comoBean.instance({}, options);
  192. try {
  193. return await that.service.base.select(selectBean, that.app.model.Orders, '查询订单详情失败,请稍候重试', false, false);
  194. } catch (e) {
  195. that.logger.error('订单id:%s ;分佣查询订单详情失败', order_id);
  196. return null;
  197. }
  198. }
  199. }
  200. module.exports = CommissionService;