123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- <?php
- // +----------------------------------------------------------------------
- // | likeshop开源商城系统
- // +----------------------------------------------------------------------
- // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
- // | gitee下载:https://gitee.com/likeshop_gitee
- // | github下载:https://github.com/likeshop-github
- // | 访问官网:https://www.likeshop.cn
- // | 访问社区:https://home.likeshop.cn
- // | 访问手册:http://doc.likeshop.cn
- // | 微信公众号:likeshop技术社区
- // | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用,未经许可不能去除前后端官方版权标识
- // | likeshop系列产品收费版本务必购买商业授权,购买去版权授权后,方可去除前后端官方版权标识
- // | 禁止对系统程序代码以任何目的,任何形式的再发布
- // | likeshop团队版权所有并拥有最终解释权
- // +----------------------------------------------------------------------
- // | author: likeshop.cn.team
- // +----------------------------------------------------------------------
-
-
- namespace app\common\server;
-
-
- use app\common\enum\NoticeEnum;
- use think\facade\Db;
-
- class DistributionServer
- {
- public static function commission($order_id)
- {
- $field = [
- 'o.user_id' => 'user_id',
- 'og.id' => 'order_goods_id',
- 'og.goods_num' => 'goods_num',
- 'og.total_pay_price' => 'total_price',
- 'g.first_ratio' => 'first_ratio',
- 'g.second_ratio' => 'second_ratio',
- 'g.third_ratio' => 'third_ratio',
- 'u.first_leader' => 'first_leader',
- 'u.second_leader' => 'second_leader',
- 'u.third_leader' => 'third_leader',
- ];
- $goods_lists = Db::name('order_goods')
- ->alias('og')
- ->join('goods g', 'og.goods_id=g.id')
- ->join('order o', 'og.order_id=o.id')
- ->join('user u', 'o.user_id=u.id')
- ->field($field)
- ->where('o.id', $order_id)
- ->where('g.is_distribution', 1)
- ->select();
- if (empty($goods_lists)) {
- //无商品需要分佣
- return;
- }
-
- //按商品分佣
- $time = time();
- foreach ($goods_lists as $goods) {
-
- //一级分佣
- if ($goods['first_ratio'] <= 0 || $goods['first_ratio'] >= 100) {
- //设置为0,不分佣
- continue;
- }
- if (empty($goods['first_leader'])) {
- //无上级,无需分佣
- continue;
- }
- $money = $goods['total_price'] * $goods['first_ratio'] / 100;
- if ($money < 0.01) {
- //金额小于0.01分佣
- continue;
- }
- self::createOrder($goods['first_leader'], $goods['order_goods_id'], $goods['goods_num'], $money, $time);
- }
-
- foreach ($goods_lists as $goods) {
- //二级分佣
- if ($goods['second_ratio'] <= 0 || $goods['second_ratio'] >= 100) {
- //设置为0,不分佣
- continue;
- }
- $money = $goods['total_price'] * $goods['second_ratio'] / 100;
- if ($money < 0.01) {
- //金额小于0.01分佣
- continue;
- }
- if (empty($goods['second_leader'])) {
- //无上级,无需分佣
- continue;
- }
- self::createOrder($goods['second_leader'], $goods['order_goods_id'], $goods['goods_num'], $money, $time);
-
- }
-
- foreach ($goods_lists as $goods) {
- //三级分佣
- if ($goods['three_ratio'] <= 0) {
- //设置为0,不分佣
- continue;
- }
- $money = $goods['total_price'] * $goods['three_ratio'] / 100;
- if ($money < 0.01) {
- //金额小于0.01分佣
- continue;
- }
- if (empty($goods['third_leader'])) {
- //无上级,无需分佣
- continue;
- }
- self::createOrder($goods['third_leader'], $goods['order_goods_id'], $goods['goods_num'], $money, $time);
- }
-
-
- }
-
- /**
- * 创建分佣订单
- * @param $leader_id
- * @param $source_id
- * @param $goods_num
- * @param $money
- * @param $create_time
- */
- private static function createOrder($leader_id, $source_id, $goods_num, $money, $create_time)
- {
- $data = [
- 'sn' => createSn('distribution_order_goods','sn'),
- 'user_id' => $leader_id,
- 'order_goods_id' => $source_id,
- 'goods_num' => $goods_num,
- 'money' => $money,
- 'status' => 1,
- 'create_time' => $create_time,
- ];
- Db::name('distribution_order_goods')
- ->insert($data);
-
- event('Notice', [
- 'scene' => NoticeEnum::GET_FUTURE_EARNINGS_NOTICE,
- 'params' => [
- 'user_id' => $leader_id,
- 'future_earnings' => $money,
- 'time' => date('Y-m-d H:i:s', time())
- ]
- ]);
-
- }
- }
|