hrjy 2 роки тому
батько
коміт
ce8de49888

+ 0 - 1
app/api/controller/auction/AuctionProductController.php

@@ -7,7 +7,6 @@ use app\models\auction\Auction;
 use app\models\auction\AuctionBooking;
 use app\models\auction\AuctionOrder;
 use app\models\auction\AuctionProduct;
-use app\models\auction\AuctionTime;
 use app\models\user\User;
 use app\models\user\UserBill;
 use app\Request;

+ 40 - 0
app/common.php

@@ -622,3 +622,43 @@ if (!function_exists('getNewOrderId')) {
         return $orderId;
     }
 }
+
+if (!function_exists('getDataFind')){
+    function getDataFind($table,$where=[]){
+        $data = \think\facade\Db::name($table)->where($where)->find();
+        return $data;
+    }
+}
+if (!function_exists('getParent')){
+    //查找所有上级
+    function getParent($uid){
+        //当前用户
+        static $arr=[];
+        //当前用户
+        $member = getDataFind('user',array('uid'=>$uid));
+        if ($member['spread_uid'] > 0){
+            $parent = getDataFind('user',array('uid'=>$member['spread_uid']));
+
+            $arr[]= $parent['uid'];
+
+            getParent($parent['uid']);
+        }
+        return $arr;
+
+    }
+}
+
+function get_downline($members,$id){
+    $arr=array();
+        foreach ($members as $key => $v) {
+
+            if($v['spread_uid']==$id){  //pid为0的是顶级分类
+
+                $arr[]=$v['uid'];
+                $arr = array_merge($arr,get_downline($members,$v['uid']));
+            }
+        }
+     return $arr;
+
+}
+

+ 34 - 2
app/http/middleware/AllowOriginMiddleware.php

@@ -7,6 +7,7 @@ namespace app\http\middleware;
 use app\models\auction\Auction;
 use app\models\auction\AuctionOrder;
 use app\models\store\StoreBargainUser;
+use app\models\user\User;
 use app\Request;
 use crmeb\interfaces\MiddlewareInterface;
 use think\facade\Config;
@@ -58,16 +59,47 @@ class AllowOriginMiddleware implements MiddlewareInterface
         }
         $request->filter(['htmlspecialchars', 'strip_tags', 'addslashes', 'trim']);
         Auction::frequency(); // 更新场次
+
+        try {
+            Db::startTrans();
+            User::kpi();//KPi奖励
+            Db::commit();
+        } catch (\Exception $e) {
+            Db::rollback();
+        }
+        try {
+            Db::startTrans();
+            User::bonus();//分红奖励
+            Db::commit();
+        } catch (\Exception $e) {
+            Db::rollback();
+        }
+
+
+        try {
+            Db::startTrans();
+            User::direct_push();//直推奖励
+            Db::commit();
+        } catch (\Exception $e) {
+            Db::rollback();
+        }
+        try {
+            Db::startTrans();
+            User::up_time();// 时间更新
+            Db::commit();
+        } catch (\Exception $e) {
+            Db::rollback();
+        }
+
         try {
-            // 更新订单日期
             Db::startTrans();
             AuctionOrder::deduction();// 更新订单
             Db::commit();
         } catch (\Exception $e) {
             Db::rollback();
         }
+
         try {
-            // 更新订单日期
             Db::startTrans();
             AuctionOrder::th();// 退回广告值
             Db::commit();

+ 20 - 24
app/models/auction/AuctionOrder.php

@@ -51,13 +51,15 @@ class AuctionOrder extends BaseModel
                 $list = self::where([['uid', '=', $uid], ['status', '=', 1]])->page($data['page'], $data['limit'])->select(); //待支付
 
             }else if($data['type'] == 2){
-                $list = self::where([['uid', '=', $uid], ['status', '=', 2]])->page($data['page'], $data['limit'])->select(); //完成订单
+                $list = self::where([['uid', '=', $uid],['status', '=', 2]])->page($data['page'], $data['limit'])->select(); //完成订单
+
+            }else if($data['type'] == 3){
+                $list = AuctionProduct::where([['uid', '=', $uid], ['is_show', '=', 0]])->page($data['page'], $data['limit'])->select(); //未上架订单
 
             }else{
                 $list = self::where([['uid', '=', $uid], ['status', '<', 1]])->page($data['page'], $data['limit'])->select(); //过期订单
             }
         }
-
         $list = !empty($list)? $list->toArray(): [];
         return $list;
     }
@@ -79,30 +81,25 @@ class AuctionOrder extends BaseModel
                 ->leftJoin('user u', 'a.uid = u.uid')
                 ->where('a.order_id', $data['order_id'])->find(); //详细订单
         }else{
-            if ($data['type'] == 1){
-                $list = self::alias('a')
-                    ->field('a.*,u.nickname,u.avatar,u.uid as user_id')
-                    ->leftJoin('user u', 'a.uid = u.uid')
-                    ->where([['a.collection_id', '=', $uid], ['a.status', '=', 1]])
-                    ->page($data['page'], $data['limit'])
-                    ->select(); //待支付订单
-
-            }else if($data['type'] == 2){
-                $list = self::alias('a')
-                    ->field('a.*,u.nickname,u.avatar,u.uid as user_id')
-                    ->leftJoin('user u', 'a.uid = u.uid')
-                    ->where([['a.collection_id', '=', $uid], ['a.status', '=', 2]])
-                    ->page($data['page'], $data['limit'])
-                    ->select(); //完成订单
-
+            if ($data['type'] == 3){
+                $list = AuctionProduct::where('uid', $uid)
+                    ->where('is_show', 1)->select();
             }else{
+                if ($data['type'] ==  1){
+                    $status = 1;
+                }elseif ($data['type'] == 2){
+                    $status = 2;
+                }else{
+                    $status = 0;
+                }
                 $list = self::alias('a')
                     ->field('a.*,u.nickname,u.avatar,u.uid as user_id')
                     ->leftJoin('user u', 'a.uid = u.uid')
-                    ->where([['a.collection_id', '=', $uid], ['a.status', '=', 0]])
+                    ->where([['a.collection_id', '=', $uid], ['a.status', '=', $status]])
                     ->page($data['page'], $data['limit'])
-                    ->select(); //过期订单
+                    ->select(); //待支付订单
             }
+
         }
         $list = !empty($list)? $list->toArray(): [];
 
@@ -118,7 +115,7 @@ class AuctionOrder extends BaseModel
      */
     public static function deduction()
     {
-        $hour = strtotime(date('Y-m-d H:i:s', strtotime('-30 minute'))); // 一小时以前
+        $hour = strtotime(date('Y-m-d H:i:s', strtotime('-30 minute'))); // 30分钟以前
 
         $order = AuctionOrder::where('create_time', '<', $hour)->where('status', '=', 1)->select(); // 查询不在当前30分钟前的订单
         if ($order){
@@ -153,10 +150,9 @@ class AuctionOrder extends BaseModel
                 $booking = AuctionBooking::where([['auction_id', '=', $v['id']], ['status','=' , 1]])->select();
 
                 if ($booking){
-                    $bf = 0.1; // 广告值扣除倍率
                     foreach ($booking as $key => $value){
-                        $userBok = AuctionBooking::where('uid', '=',$value['uid'])->where('deduct_advert', '>', 0)->count();
-                        $bf =  (0.1*$userBok >= 1)? 1: 0.1*$userBok;
+                        $userBok = AuctionBooking::where('uid', '=',$value['uid'])->where('deduct_advert', '>', 0)->count(); //预约未购买退回的次数
+                        $bf =  (0.1*$userBok >= 1)? 1: 0.1*$userBok; // 0.1 * 退回次数
                         $find = AuctionBooking::find($value['id']);
                         $find['status'] = 0;
                         $find['deduct_advert'] = $value['advert']*$bf; // 扣除广告书

+ 2 - 1
app/models/auction/AuctionProduct.php

@@ -55,7 +55,8 @@ class AuctionProduct extends BaseModel
         if (empty($product)){
             $config = SystemConfig::where('config_tab_id', 24)->select();
             $data = [
-                'auction_id' => $auction['id']
+                'auction_id' => $auction['id'],
+                'create_time' => time()
             ];
             foreach ($config as $v)
             {

+ 33 - 0
app/models/auction/AuctionReward.php

@@ -0,0 +1,33 @@
+<?php
+
+namespace app\models\auction;
+
+use app\models\store\StoreProduct;
+use crmeb\services\SystemConfigService;
+use think\facade\Db;
+use crmeb\traits\ModelTrait;
+use crmeb\basic\BaseModel;
+
+/**
+ * TODO 场馆model
+ * Class Article
+ * @package app\models\article
+ */
+class AuctionReward extends BaseModel
+{
+    /**
+     * 数据表主键
+     * @var string
+     */
+    protected $pk = 'id';
+
+    /**
+     * 模型名称
+     * @var string
+     */
+    protected $name = 'auction_reward';
+
+    use ModelTrait;
+
+
+}

+ 227 - 0
app/models/user/User.php

@@ -3,6 +3,9 @@
 
 namespace app\models\user;
 
+use app\models\auction\AuctionBooking;
+use app\models\auction\AuctionOrder;
+use app\models\auction\AuctionReward;
 use app\models\store\StoreOrder;
 use app\models\store\StoreProduct;
 use crmeb\services\SystemConfigService;
@@ -732,4 +735,228 @@ class User extends BaseModel
         }
         return $model->where('brokerage_price', '>', $brokerage_price)->count('uid');
     }
+
+    /**
+     * 直推奖励
+     * @return void
+     */
+    public static function direct_push()
+    {
+        if (!AuctionReward::where('create_time', strtotime(date('Y-m-d', time())))->find()){
+            // 查找到今天没有分发奖励
+            $time = strtotime(date('Y-m-d', strtotime('-1 day'))); // 昨天的时间
+            $timet = strtotime(date('Y-m-d', time())); // 今天时间
+            $order = AuctionOrder::where('status', '=',2)->where('create_time', '>=', $time)->where('create_time', '<=', $timet)->select();
+            if ($order){
+                foreach ($order as $k => $v) {
+                    $user = User::where('uid', $v['uid'])->find();
+                    if ($user['spread_uid']){
+                        $spread = User::where('uid', $user['spread_uid'])->find();
+                        $bo = AuctionBooking::where('uid', '=', $spread['uid'])->where('create_time', '>=', $time)->where('create_time', '<=', $timet)->find();
+                        if ($bo){
+                            $money =  round($v['price'] * 0.03, 2);
+                            $spread['sp_final'] += $money;
+                            $spread->save();
+                            UserBill::income('奖励购物积分', $spread['uid'], 'sp_final', 'add_sp_final', $money, $user['uid'], $spread['sp_final'], '奖励购物积分');
+                        }
+                    }
+                }
+            }
+        }
+    }
+
+    /**
+     * 分红奖励
+     * @return void
+     * @throws DataNotFoundException
+     * @throws DbException
+     * @throws ModelNotFoundException
+     */
+    public static function bonus(){
+        if (!AuctionReward::where('create_time', strtotime(date('Y-m-d', time())))->find()){
+            $config = SystemConfigService::more(['bonus', 'bonus_standard']);
+            $user = self::field('uid,spread_uid, sp_final')->select();
+            $user = empty($user) ? [] : $user->toArray();
+            $time = strtotime(date('Y-m-d', strtotime('-1 day'))); // 昨天的时间
+            $timet = strtotime(date('Y-m-d', time())); // 今天时间
+            $orderMoney = AuctionOrder::where('status', '=',2)->where('create_time', '>=', $time)->where('create_time', '<=', $timet)->sum('price');// 找到昨天的总流水金额
+            $moneys = [];
+            foreach ($user as $k => $v){
+                $bo = AuctionBooking::where('uid', '=', $v['uid'])->where('create_time', '>=', $time)->where('create_time', '<=', $timet)->find(); // 查看昨天有预约记录
+                if ($bo){
+                    $dow = User::where('spread_uid', $v['uid'])->select()->toArray(); // 是否有下级
+                    if (!empty($dow)){
+                        foreach ($dow as $key => $value){
+                            $userDow[$value['uid']] = get_downline($user,$value['uid']);
+                        }
+                        foreach ($userDow as $dk => $dv){
+                            //查询出下级每个分支昨天流水多少
+                            $money = AuctionOrder::where('uid', $dk)->where('status',2)->sum('price');
+                            if ($dv){
+                                $money += AuctionOrder::where('uid', 'in', $dv)->where('status', '=', 2)->sum('price');
+                            }
+                            $moneys[] = $money;
+                        }
+                        sort($moneys); // 排序
+                        array_pop($moneys); // 删除最大的流水
+                        $sum  = array_sum($moneys);
+                        if ($sum > (float)$config['bonus_standard']){
+                            $reward = round($orderMoney * ($config['bonus']/100), 2); // 奖励分红额度
+                            $v['sp_final'] += $reward;
+                            User::where('uid', $v['uid'])
+                                ->update(['sp_final' => $v['sp_final']]);
+                            UserBill::income('分红奖励', $v['uid'], 'sp_final', 'add_sp_final', $reward, 0, $v['sp_final'], '分红奖励');
+
+                        }
+                        unset($moneys);
+                        unset($userDow);
+                    }
+                }
+            }
+        }
+    }
+
+    /**
+     * KPI奖励
+     * @return void
+     * @throws DataNotFoundException
+     * @throws DbException
+     * @throws ModelNotFoundException
+     */
+    public static function kpi()
+    {
+        if (!AuctionReward::where('create_time', strtotime(date('Y-m-d', time())))->find()){
+            $user = self::field('uid,spread_uid, sp_final')->select();
+            $user = empty($user) ? [] : $user->toArray();
+            $moneys = [];
+            $time = strtotime(date('Y-m-d', strtotime('-1 day'))); // 昨天的时间
+            $timet = strtotime(date('Y-m-d', time())); // 今天时间
+            $userl = [];
+            foreach ($user as $k => $v){
+                $bo = AuctionBooking::where('uid', '=', $v['uid'])->where('create_time', '>=', $time)->where('create_time', '<=', $timet)->find(); // 查看昨天有预约记录
+                if ($bo){
+                    $dow = User::where('spread_uid', $v['uid'])->select()->toArray(); // 是否有下级
+                    if (!empty($dow)){
+                        foreach ($dow as $key => $value){
+                            $userDow[$value['uid']] = get_downline($user,$value['uid']); // 获取到下级
+                        }
+                        foreach ($userDow as $dk => $dv){
+                            //查询出下级每个分支昨天流水多少
+                            $money = AuctionOrder::where('uid', $dk)->where('status',2)->sum('price');
+                            if ($dv){
+                                $money += AuctionOrder::where('uid', 'in', $dv)->where('status', '=', 2)->sum('price');
+                            }
+                            $moneys[$dk] = $money; // 查询到支线下级总额度
+                        }
+                        $userl = $moneys;
+                        sort($moneys); // 排序
+                        array_pop($moneys); // 删除最大的流水
+                        $sum  = array_sum($moneys);
+                        $level = self::con($sum);
+                        if ($level > 0){
+                            if (empty($v['spread_uid'])){
+                                // 没有上级
+                                foreach ($userl as $uk => $uv){
+                                    $towdow = self::dow($uk);
+                                    if ($level > $towdow){
+                                        $reward = ($level - $towdow)/100; // 减掉下级获得的倍率
+                                        $userl[$uk] = $uv * $reward;
+                                    }elseif ($level == $towdow){
+                                        $reward = $level/100; // 如果等级相同就只拿百分之十
+                                        $userl[$uk] = ($uv * $reward) * 0.1;
+                                    }
+
+                                }
+                                $userMoney = array_sum($userl);
+                                $v['sp_final'] += $userMoney;
+                                User::where('uid', $v['uid'])
+                                    ->update(['sp_final' => $v['sp_final']]);
+                                UserBill::income('KPI奖励', $v['uid'], 'sp_final', 'add_sp_final', $userMoney, 0, $v['sp_final'], 'KPI奖励');
+                            }else{
+                                //有上级
+                                foreach ($userl as $uk => $uv){
+                                    $towdow = self::dow($uk);
+                                    if ($level > $towdow){
+                                        $reward = $level/100; // 减掉下级获得的倍率
+                                    }elseif ($level == $towdow){
+                                        $reward = ($level* 0.1)/100; // 如果下级等级相同就只拿百分之十
+                                    }
+                                    $userl[$uk] = $uv * $reward;
+                                }
+                                $plevel  = self::dow($v['spread_uid']);
+                                $userMoney = array_sum($userl);
+                                if ($plevel == $level) {
+                                    $userMoney = $userMoney * 0.9; // 如果和上级相同等级只拿百分之九十
+                                }
+                                $v['sp_final'] += $userMoney;
+                                User::where('uid', $v['uid'])
+                                    ->update(['sp_final' => $v['sp_final']]);
+                                UserBill::income('KPI奖励', $v['uid'], 'sp_final', 'add_sp_final', $userMoney, 0, $v['sp_final'], 'KPI奖励');
+                            }
+
+                        }
+                        unset($userl);
+                        unset($moneys);
+                        unset($userDow);
+                    }
+                }
+            }
+        }
+
+    }
+
+
+
+
+    public static function dow($id)
+    {
+        $user = self::field('uid,spread_uid, sp_final')->select();
+        $dow = User::where('spread_uid', $id)->select()->toArray(); // 是否有下级
+        if (!empty($dow)) {
+            foreach ($dow as $key => $value) {
+                $userDow[$value['uid']] = get_downline($user, $value['uid']);
+            }
+            foreach ($userDow as $dk => $dv) {
+                //查询出下级每个分支昨天流水多少
+                $money = AuctionOrder::where('uid', $dk)->where('status', 2)->sum('price');
+                if ($dv) {
+                    $money += AuctionOrder::where('uid', 'in', $dv)->where('status', '=', 2)->sum('price');
+                }
+                $moneys[$dk] = $money;
+            }
+            sort($moneys); // 排序
+            array_pop($moneys); // 删除最大的流水
+            $sum = array_sum($moneys);
+            $level = self::con($sum);
+            return $level;
+        }
+        return 0;
+    }
+
+    public static function con($sum){
+        $config = SystemConfigService::more([
+            'level_one','one_standard','level_two','two_standard','level_three','three_standard','level_four','four_standard','level_five','five_standard'
+        ]);
+        $status = '';
+        if ($sum < $config['one_standard']) $status = 0;
+        if ($sum >= $config['one_standard'] and $sum < $config['two_standard']) $status = $config['level_one']; // 达标1级
+        if ($sum >= $config['two_standard'] and $sum < $config['three_standard']) $status = $config['level_two'];// 达标2级
+        if ($sum >= $config['three_standard'] and $sum < $config['four_standard']) $status = $config['level_three'];// 达标3级
+        if ($sum >= $config['four_standard'] and $sum < $config['five_standard']) $status = $config['level_four'];// 达标4级
+        if ($sum >= $config['five_standard']) $status = $config['level_five'];// 达标5级
+
+        return $status;
+    }
+
+
+    public static function up_time()
+    {
+        if (!AuctionReward::where('create_time', strtotime(date('Y-m-d', time())))->find()){
+            AuctionReward::create([
+                'create_time' => strtotime(date('Y-m-d', time()))
+            ]);
+        }
+    }
+
+
 }

+ 34 - 0
crmeb/subscribes/TaskSubscribe.php

@@ -7,6 +7,7 @@ use app\models\auction\AuctionOrder;
 use app\models\store\StoreBargainUser;
 use app\models\store\StoreOrder;
 use app\models\store\StorePink;
+use app\models\user\User;
 use app\models\user\UserToken;
 use think\facade\Db;
 
@@ -90,6 +91,7 @@ class TaskSubscribe
         } catch (\Exception $e) {
             Db::rollback();
         }
+
     }
 
     /**
@@ -97,6 +99,38 @@ class TaskSubscribe
      */
     public function onTask_60()
     {
+
+        try {
+            Db::startTrans();
+            User::kpi();//KPi奖励
+            Db::commit();
+        } catch (\Exception $e) {
+            Db::rollback();
+        }
+        try {
+            Db::startTrans();
+            User::bonus();//分红奖励
+            Db::commit();
+        } catch (\Exception $e) {
+            Db::rollback();
+        }
+
+
+        try {
+            Db::startTrans();
+            User::direct_push();//直推奖励
+            Db::commit();
+        } catch (\Exception $e) {
+            Db::rollback();
+        }
+        try {
+            Db::startTrans();
+            User::up_time();// 时间更新
+            Db::commit();
+        } catch (\Exception $e) {
+            Db::rollback();
+        }
+
     }
 
     /**