1
0

2 Incheckningar 814066c836 ... 49c23cfbba

Upphovsman SHA1 Meddelande Datum
  kirin 49c23cfbba Merge remote-tracking branch 'origin/master' 5 månader sedan
  kirin 473099a726 feat(admin): 添加分红积分基准价格设置并进行有效性验证 5 månader sedan

+ 27 - 0
app/common.php

@@ -1384,3 +1384,30 @@ if(!function_exists('generateTimeRanges')) {
         return $timeRanges;
     }
 }
+
+
+if (!function_exists('get_group_user')) {
+    //所有下级
+    function get_group_user($id, $init = true, $members = null)
+    {
+        if ($init) {
+            $us = \app\common\model\user\User::column('spread_uid', 'uid');
+            $members = [];
+            foreach ($us as $k => $v) {
+                if ($v > 0)
+                    $members[$v][] = $k;
+            }
+            $id = [$id];
+        }
+        $arr = array();
+        foreach ($id as $v) {
+            $child = $members[$v] ?? [];
+            $arr = array_merge($arr, $child);
+        }
+        if (count($arr)) {
+            return array_merge($arr, get_group_user($arr, false, $members));
+        } else {
+            return $arr;
+        }
+    }
+}

+ 119 - 0
app/common/model/user/AwardLake.php

@@ -0,0 +1,119 @@
+<?php
+
+namespace app\common\model\user;
+
+use app\common\model\BaseModel;
+use app\common\repositories\user\AwardIntegralPriceRepository;
+use think\db\exception\DataNotFoundException;
+use think\db\exception\DbException;
+use think\db\exception\ModelNotFoundException;
+use think\Exception;
+
+class Awardlake extends BaseModel
+{
+    public static function tablePk(): ?string
+    {
+        return 'id';
+    }
+
+    public static function tableName(): string
+    {
+        return 'award_integral_price';
+    }
+
+    /**
+     * @param int $lake_type 奖池类型 1:节能油,2:礼包
+     * @param float $order_price 订单价格
+     * @param int $order_id 订单ID
+     * @return true
+     * @throws Exception
+     * @throws DataNotFoundException
+     * @throws DbException
+     * @throws ModelNotFoundException
+     */
+    public function addOrderLakes($lake_type, $order_price, $order_id)
+    {
+        switch ($lake_type) {
+            case 1:
+                $model = new OilLevel();
+                break;
+            case 2:
+                $model = new GiftLevel();
+                break;
+            default :
+                throw new Exception('奖池类型错误');
+        }
+        $levels = $model->select();
+        foreach ($levels as $level) {
+            $lake = self::where('level', $level['id'])->where('type', $lake_type)
+                ->find();
+            if (!$lake) $lake = self::create([
+                'level' => $level['id'],
+                'type' => $lake_type,
+                'award_name' => $level['name'] . '分红',
+                'num' => 0,
+                'send_time' => time()
+            ]);
+            $award = bcdiv(bcmul((string)$order_price, (string)$level['award_ratio']), '100', 2);
+            AwardLakeLog::income($lake['id'], 'order_add', $award, $order_id, '用户购买商品添加分红池');
+        }
+        return true;
+    }
+
+    public function autoSend()
+    {
+        $time = strtotime(date('Y-m-d 01:00:00'));
+        if (time() < $time) {
+            return true;
+        }
+        $lakes = self::where('send_time', '<', $time)->select();
+        foreach ($lakes as $lake) {
+            $this->sendAward($lake['id']);
+        }
+    }
+
+    public function sendAward($lake_id)
+    {
+        $lake = self::where('id', $lake_id)->find();
+        if (!$lake) return true;
+        if ($lake['num'] <= 0) {
+            self::where('id', $lake['id'])->update(['send_time' => time()]);
+            return true;
+        }
+        switch ($lake['type']) {
+            case 1:
+                $model = new OilLevel();
+                $field = 'oil_level';
+                break;
+            case 2:
+                $model = new GiftLevel();
+                $field = 'gift_level';
+                break;
+            default :
+                $model = new OilLevel();
+                $field = 'oil_level';
+        }
+        $level = $model->where('id', $lake['level'])->find();
+        $levels = $model->where('grade', '>=', $level['grade'])->column('id');
+        $users = User::where($field, 'in', $levels)->where('status', 1)->select();
+        $every = bcdiv($lake['num'], count($users), 2);
+        if ($every <= 0) {
+            self::where('id', $lake['id'])->update(['send_time' => time()]);
+            return true;
+        }
+        $real_send = 0;
+        $price = app()->make(AwardIntegralPriceRepository::class)->awardIntegralPrice();
+        foreach ($users as $user) {
+            //todo 添加用户积分
+
+
+
+
+
+            //todo end
+            $real_send = bcadd($real_send, $every, 2);
+        }
+        AwardLakeLog::expend($lake['id'], 'send', $real_send, 0, '奖池分红');
+        self::where('id', $lake['id'])->update(['send_time' => time()]);
+    }
+}

+ 45 - 0
app/common/model/user/AwardLakeLog.php

@@ -0,0 +1,45 @@
+<?php
+
+namespace app\common\model\user;
+
+use app\common\model\BaseModel;
+use think\Exception;
+
+class AwardLakeLog extends BaseModel
+{
+    public static function tablePk(): ?string
+    {
+        return 'id';
+    }
+
+    public static function tableName(): string
+    {
+        return 'award_integral_price';
+    }
+
+
+    public static function income($lake_id, $type, $num, $link_id = 0, $mark = '')
+    {
+        $lake = AwardLake::find($lake_id);
+        if (!$lake) throw new Exception('invalid lake');
+        $pm = 1;
+        $add_time = time();
+        $res = self::create(compact('lake_id', 'type', 'num', 'link_id', 'mark', 'pm', 'add_time'));
+        $res = $res && AwardLake::where('id', $lake_id)->inc('num', $num)->update();
+        return $res;
+    }
+
+    public static function expend($lake_id, $type, $num, $link_id = 0, $mark = '')
+    {
+        $lake = AwardLake::find($lake_id);
+        if (!$lake) throw new Exception('invalid lake');
+        if ($lake->num < $num) {
+            throw new Exception('lake num not enough');
+        }
+        $pm = 0;
+        $add_time = time();
+        $res = self::create(compact('lake_id', 'type', 'num', 'link_id', 'mark', 'pm', 'add_time'));
+        $res = $res && AwardLake::where('id', $lake_id)->dec('num', $num)->update();
+        return $res;
+    }
+}

+ 46 - 0
app/common/model/user/GiftLevel.php

@@ -0,0 +1,46 @@
+<?php
+
+namespace app\common\model\user;
+
+use app\common\model\BaseModel;
+
+class GiftLevel extends BaseModel
+{
+    public static function tablePk(): ?string
+    {
+        return 'id';
+    }
+
+    public static function tableName(): string
+    {
+        return 'award_integral_price';
+    }
+
+
+    public function checkLevel($uid, $pass = [])
+    {
+        if (in_array($uid, $pass)) return true;
+        $user = User::find($uid);
+        $group_users = get_group_user($user);
+        //Todo 获取在$group_users内所有用户的消费情况
+        $achievement = 0;
+
+
+        $old_level = self::where('id', $user['level'])->find();
+        if (!$old_level) $old_level = [
+            'grade' => 0,
+            'name' => '普通会员'
+        ];
+        $level = self::where('grade', '>', $old_level['grade'])->where('achievement', '<=', $achievement)
+            ->order('grade', 'desc')->find();
+        if ($level) {
+            $user->level = $level['id'];
+            $user->save();
+        }
+
+        $pass[] = $uid;
+        $spread = User::where('uid', $user['spread_uid'])->find();
+        if ($spread) return $this->checkLevel($spread['id'], $pass);
+        return true;
+    }
+}

+ 45 - 0
app/common/model/user/OilLevel.php

@@ -0,0 +1,45 @@
+<?php
+
+namespace app\common\model\user;
+
+use app\common\model\BaseModel;
+
+class OilLevel extends BaseModel
+{
+    public static function tablePk(): ?string
+    {
+        return 'id';
+    }
+
+    public static function tableName(): string
+    {
+        return 'award_integral_price';
+    }
+
+    public function checkLevel($uid, $pass = [])
+    {
+        if (in_array($uid, $pass)) return true;
+        $user = User::find($uid);
+        $group_users = get_group_user($user);
+        //Todo 获取在$group_users内所有用户的消费情况
+        $achievement = 0;
+
+
+        $old_level = self::where('id', $user['level'])->find();
+        if (!$old_level) $old_level = [
+            'grade' => 0,
+            'name' => '普通会员'
+        ];
+        $level = self::where('grade', '>', $old_level['grade'])->where('achievement', '<=', $achievement)
+            ->order('grade', 'desc')->find();
+        if ($level) {
+            $user->level = $level['id'];
+            $user->save();
+        }
+
+        $pass[] = $uid;
+        $spread = User::where('uid', $user['spread_uid'])->find();
+        if ($spread) return $this->checkLevel($spread['id'], $pass);
+        return true;
+    }
+}