截流自动化的商城平台
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

DistributionServer.php 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop开源商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | gitee下载:https://gitee.com/likeshop_gitee
  7. // | github下载:https://github.com/likeshop-github
  8. // | 访问官网:https://www.likeshop.cn
  9. // | 访问社区:https://home.likeshop.cn
  10. // | 访问手册:http://doc.likeshop.cn
  11. // | 微信公众号:likeshop技术社区
  12. // | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用,未经许可不能去除前后端官方版权标识
  13. // | likeshop系列产品收费版本务必购买商业授权,购买去版权授权后,方可去除前后端官方版权标识
  14. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  15. // | likeshop团队版权所有并拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshop.cn.team
  18. // +----------------------------------------------------------------------
  19. namespace app\common\server;
  20. use app\common\enum\NoticeEnum;
  21. use think\facade\Db;
  22. class DistributionServer
  23. {
  24. public static function commission($order_id)
  25. {
  26. $field = [
  27. 'o.user_id' => 'user_id',
  28. 'og.id' => 'order_goods_id',
  29. 'og.goods_num' => 'goods_num',
  30. 'og.total_pay_price' => 'total_price',
  31. 'g.first_ratio' => 'first_ratio',
  32. 'g.second_ratio' => 'second_ratio',
  33. 'g.third_ratio' => 'third_ratio',
  34. 'u.first_leader' => 'first_leader',
  35. 'u.second_leader' => 'second_leader',
  36. 'u.third_leader' => 'third_leader',
  37. ];
  38. $goods_lists = Db::name('order_goods')
  39. ->alias('og')
  40. ->join('goods g', 'og.goods_id=g.id')
  41. ->join('order o', 'og.order_id=o.id')
  42. ->join('user u', 'o.user_id=u.id')
  43. ->field($field)
  44. ->where('o.id', $order_id)
  45. ->where('g.is_distribution', 1)
  46. ->select();
  47. if (empty($goods_lists)) {
  48. //无商品需要分佣
  49. return;
  50. }
  51. //按商品分佣
  52. $time = time();
  53. foreach ($goods_lists as $goods) {
  54. //一级分佣
  55. if ($goods['first_ratio'] <= 0 || $goods['first_ratio'] >= 100) {
  56. //设置为0,不分佣
  57. continue;
  58. }
  59. if (empty($goods['first_leader'])) {
  60. //无上级,无需分佣
  61. continue;
  62. }
  63. $money = $goods['total_price'] * $goods['first_ratio'] / 100;
  64. if ($money < 0.01) {
  65. //金额小于0.01分佣
  66. continue;
  67. }
  68. self::createOrder($goods['first_leader'], $goods['order_goods_id'], $goods['goods_num'], $money, $time);
  69. }
  70. foreach ($goods_lists as $goods) {
  71. //二级分佣
  72. if ($goods['second_ratio'] <= 0 || $goods['second_ratio'] >= 100) {
  73. //设置为0,不分佣
  74. continue;
  75. }
  76. $money = $goods['total_price'] * $goods['second_ratio'] / 100;
  77. if ($money < 0.01) {
  78. //金额小于0.01分佣
  79. continue;
  80. }
  81. if (empty($goods['second_leader'])) {
  82. //无上级,无需分佣
  83. continue;
  84. }
  85. self::createOrder($goods['second_leader'], $goods['order_goods_id'], $goods['goods_num'], $money, $time);
  86. }
  87. foreach ($goods_lists as $goods) {
  88. //三级分佣
  89. if ($goods['three_ratio'] <= 0) {
  90. //设置为0,不分佣
  91. continue;
  92. }
  93. $money = $goods['total_price'] * $goods['three_ratio'] / 100;
  94. if ($money < 0.01) {
  95. //金额小于0.01分佣
  96. continue;
  97. }
  98. if (empty($goods['third_leader'])) {
  99. //无上级,无需分佣
  100. continue;
  101. }
  102. self::createOrder($goods['third_leader'], $goods['order_goods_id'], $goods['goods_num'], $money, $time);
  103. }
  104. }
  105. /**
  106. * 创建分佣订单
  107. * @param $leader_id
  108. * @param $source_id
  109. * @param $goods_num
  110. * @param $money
  111. * @param $create_time
  112. */
  113. private static function createOrder($leader_id, $source_id, $goods_num, $money, $create_time)
  114. {
  115. $data = [
  116. 'sn' => createSn('distribution_order_goods','sn'),
  117. 'user_id' => $leader_id,
  118. 'order_goods_id' => $source_id,
  119. 'goods_num' => $goods_num,
  120. 'money' => $money,
  121. 'status' => 1,
  122. 'create_time' => $create_time,
  123. ];
  124. Db::name('distribution_order_goods')
  125. ->insert($data);
  126. event('Notice', [
  127. 'scene' => NoticeEnum::GET_FUTURE_EARNINGS_NOTICE,
  128. 'params' => [
  129. 'user_id' => $leader_id,
  130. 'future_earnings' => $money,
  131. 'time' => date('Y-m-d H:i:s', time())
  132. ]
  133. ]);
  134. }
  135. }