Browse Source

新手福利

afa 8 tháng trước cách đây
mục cha
commit
7302ea0df1

+ 31 - 11
application/api/controller/Airdrop.php

@@ -2,14 +2,19 @@
 
 namespace app\api\controller;
 
+use think\Exception;
+use app\api\logic\WelfareLoginc;
+use app\common\model\UserModel;
 use app\common\controller\Api;
-use app\common\model\AnnouncementModel;
-
+use app\common\model\UserWelfare;
+use think\Db;
+use think\exception\DbException;
+use app\common\model\ProductOrder;
 //空投
 class Airdrop extends Api
 {
 
-    protected array $noNeedLogin = ['list','show'];
+    protected array $noNeedLogin = [''];
     protected string $lan = '';
 
     public function _initialize()
@@ -19,17 +24,32 @@ class Airdrop extends Api
     }
 
     //新人福利
-    public function newbie()
+    public function newbie(UserWelfare $userWelfare, ProductOrder $productOrder, UserModel $userModel)
     {
-        $paginator = (new AnnouncementModel)
-            ->where('type_id', 1)
-            ->where('status', 1)
-            ->order('id DESC,weigh desc')
-            ->field('id,img_url,createtime,' . $this->lan.'_title as title')
-            ->paginate($this->pageSize);
-        $this->success('', $this->buildResp($paginator->total(), $paginator->currentPage(), $paginator->items()));
+
+        $row = $userWelfare::getIsWelfare();
+        if(empty($row->is_newbie)) $this->error(__('暂未开启'));
+
+        $info = $productOrder::getUserWelfare($this->auth->id, $productOrder::Newbie);
+        if(!empty($info)) $this->error(__('您已领取'));
+        
+        Db::startTrans();
+        try {
+            //添加Rwa茶记录
+            $result = WelfareLoginc::setUserWelfareLos($this->auth->id, $row->product_id, $row->num, $this->lan);
+            //添加茶数量
+            $userModel::updateForRwaNum($this->auth->id, $this->auth->parent_id, $row->num, '+');
+            Db::commit();
+        } catch (\Exception $e) {
+            Db::rollback();
+            $this->error($e->getMessage());
+        }
+        $this->success('', $result);
     }
 
+
+
+
     public function show(int $id)
     {
         $info = (new AnnouncementModel)

+ 2 - 1
application/api/lang/en.php

@@ -33,5 +33,6 @@ return [
     '已转让'                                                                      => 'Consigned',
     '已提货'                                                                      => 'Gifted',
     '已取消'                                                                      => 'Cancelled',
-    '物流运费'                                                                    => 'Logistics freight'
+    '物流运费'                                                                    => 'Logistics freight',
+    '暂未开启'                                                                    => 'Not yet enabled'   
 ]; 

+ 1 - 0
application/api/lang/zh-cn.php

@@ -126,6 +126,7 @@ return [
     '不能购买自己寄售的商品'                                                       => '不能购买自己寄售的商品',
     '转让支付'                                                                    => '寄售支付',
     '转让收款'                                                                    => '寄售收款',
+    '暂未开启'                                                                    => '暂未开启'                  
 
 
 ];

+ 326 - 0
application/api/logic/WelfareLoginc.php

@@ -0,0 +1,326 @@
+<?php
+
+
+namespace app\api\logic;
+
+use app\api\controller\Product;
+use app\common\model\ProductArea;
+use app\common\controller\Api;
+use app\common\model\UserWelfare;
+use app\common\model\ProductPopular;
+use app\common\model\ProductOrder;
+use fast\Action;
+use fast\Asset;
+use fast\MembershipLevel;
+use think\Db;
+use think\Log;
+use think\Exception;
+use think\console\Output;
+use fast\Http;
+
+/**
+ *  空投福利
+ */
+class WelfareLoginc
+{
+ 
+    /** 添加新人福利
+     * @param int   $orderId 订单id
+     * @param array $orderInfo 订单详情
+     * @param int   $typeId    订单类型
+     * @param int   $userId    用户id
+     * @param int   $fromUser  来源id
+     * @param float $fees      手续费
+     * @param float $fees      抢购价
+     */
+    public static function setUserWelfareLos($uid, int $productId, $num, $lan)
+    {
+        $tim = time();
+        $result = ProductPopular::alias('a')->where('a.start_time', '<=', $tim)->where('a.end_time', '>=', $tim)
+                ->join('product_list b', "a.product_id = b.id", "left")
+                ->field('a.*,b.is_area,'.$lan.'_name')
+                ->where('a.product_id', $productId)->find();
+        if($num > $result->stock) throw new Exception(__('库存不足'));       
+        if(empty($result->is_area)){
+            ProductOrder::setPopularNoAreaOrder($num, $result->id, $result->price, $result->product_id, $uid);
+        }else{
+            $areaArr = ProductArea::where('product_id', $productId)->where('status', ProductArea::NORMAL)->orderRaw('RAND()')->limit($num)->column('id');
+            ProductOrder::setPopularAreaOrder($areaArr, $result->id, $result->price, $result->product_id, $uid);
+        }
+        return ['start_time'=>$tim, 'name'=>$result->$lan.'_name'];
+    }
+
+
+
+    /**
+     * 计算理论收益
+     * @var array
+     */
+    protected function calculateRewards($info, $team_level_config)
+    {
+        $user = DB::table('user')
+            ->where('id', $info['user_id'])
+            ->find();
+        $level_info = [];
+        foreach ($team_level_config as $level) {
+            if ($user['team_power'] >= $level['compute_require']) {
+                $level_info['id'] = $level['level_id'];
+                $level_info['rate'] = $level['compute_incentive'];
+            }
+        }
+        if(isset($level_info['id']) && $level_info['id'] > 0){
+            //团队等级大于0,才发放收益
+            $amount = $info['today_power'] * $level_info['rate'];
+
+            DB::table('team_rewards')
+                ->where('id', $info['id'])
+                ->update([
+                    'team_power'    => $user['team_power'],
+                    'level_id'      => $level_info['id'],
+                    'rate'          => $level_info['rate'],
+                    'commission'    => $amount,
+                    'update_time'   => date('Y-m-d H:i:s')
+                ]);
+            //累计上级的团队收益
+            DB::table('team_rewards')
+                ->where('user_id', $user['parent_id'])
+                ->where('date_time', $info['date_time'])
+                ->update([
+                    'team_commission' => DB::raw("team_commission + " . $amount),
+                    'update_time'   => date('Y-m-d H:i:s')
+                ]);
+
+        }
+    }
+
+
+
+
+    /**
+     * 发放团队加权分红
+     * 1.提取各个等级的会员列表
+     * 2.再计算当日新增业绩
+     * 3.根据每个等级现存的人数,按比例平分佣金
+     */
+    public function usdtRewards()
+    {
+        $date = $this->getExeDate($this->rewards_type['usdt']['type_id']);
+
+        if(empty($date)){
+            return "本次没有可用时间";
+        }
+
+        //创建定时任务执行日志
+        $TimedTaskLog = new TimedTaskLogModel();
+        $log_id = $TimedTaskLog->insertGetId([
+            'type_id'       => $this->rewards_type['usdt']['type_id'],
+            'type_name'     => $this->rewards_type['usdt']['type_name'],
+            'date_time'     => $date,
+            'create_time'   => date('Y-m-d H:i:s'),
+        ]);
+
+        $between_time = [strtotime($date), strtotime($date) + 86400];
+
+        //读取当日新增数据,按用户分组统计
+        $recharge_amount = DB::table('offline_recharge_record')
+            ->whereBetween('create_time', $between_time)
+            ->where('order_type', 1)
+            ->where('status', 1)
+            ->sum('amount');
+
+        if($recharge_amount < 1){
+            (new Output())->writeln("本次没有可用新增数据:" . $date);
+            return $log_id;
+        }
+
+        //1.查找有等级的会员
+        $level_user_list = DB::table('user')
+            ->field('id,team_level_id')
+            ->where('team_level_id', '>', 0)
+            ->select();
+        $levels_info = [];
+        //整理数据格式,以team_level_id为键名,存放该等级下的会员id数组
+        foreach ($level_user_list as $info) {
+            $levels_info[$info['team_level_id']][] = $info['id'];
+        }
+        //读取团队等级配置
+        $team_level_config = DB::table('team_level')
+            ->field('level_id, weight_dividend')
+            ->select();
+        foreach ($team_level_config as $level) {
+            if(isset($levels_info[$level['level_id']])){//存在当前等级的会员
+                //计算当前等级平分佣金金额:当日新增业绩 * 分红比例 / 当前等级人数
+                $amount = $recharge_amount * $level['weight_dividend'] / count($levels_info[$level['level_id']]);
+                if($amount > 0){
+                    foreach ($levels_info[$level['level_id']] as $user_id){
+                        //发放佣金
+                        (new LedgerWalletModel)->changeWalletAccount($user_id, Asset::USDT, $amount, Action::UsdtWeightDividend);
+                    }
+                }else{
+                    Log::info("分红等级" . $level['level_id'] . "在" . $date . "分红金额为0;当日新增业绩:" . $amount . ",分红比例:" . $level['weight_dividend']. ",分红人数" . dump($levels_info[$level['level_id']]));
+                }
+            }
+        }
+        return $log_id;
+    }
+
+    /**
+     * 发放社区长、系统领导人加权分红
+     * 1.提取社区长等级的会员列表
+     * 2.再计算当日新增业绩
+     * 3.根据每个等级现存的人数,按比例平分佣金
+     * 5.提取系统领导人等级的会员列表
+     * 6.再计算当日新增业绩
+     * 7.根据每个等级现存的人数,按比例平分佣金
+     */
+    public function marketRewards()
+    {
+        $date = $this->getExeDate($this->rewards_type['market']['type_id']);
+
+        if(empty($date)){
+            return "本次没有可用时间";
+        }
+
+        //创建定时任务执行日志
+        $TimedTaskLog = new TimedTaskLogModel();
+        $log_id = $TimedTaskLog->insertGetId([
+            'type_id'       => $this->rewards_type['market']['type_id'],
+            'type_name'     => $this->rewards_type['market']['type_name'],
+            'date_time'     => $date,
+            'create_time'   => date('Y-m-d H:i:s'),
+        ]);
+
+        $between_time = [strtotime($date), strtotime($date) + 86400];
+
+        //读取当日新增数据
+        $recharge_power = DB::table('offline_recharge_record')
+            ->whereBetween('create_time', $between_time)
+            ->where('status', 1)
+            ->sum('power');
+
+        if($recharge_power < 1){
+            (new Output())->writeln("本次没有可用新增数据:" . $date);
+            return $log_id;
+        }
+
+        //团队长分红
+        //查找有团队长等级的会员
+        $community_user_list = DB::table('user')
+            ->field('id,market_level_id')
+            ->where('market_level_id', MembershipLevel::CommunityLeader)
+            ->select();
+
+        $community_rewards = (new ParametersModel())->getValue('communityRewards'); //社区长分红参数 数据格式为:0.02:0.5:0.5
+        $usdt_rate = (new ParametersModel())->getValue('usdtToPowerRate');
+        $community_rewards_arr = explode(':', $community_rewards);
+
+        $reward = $recharge_power * $community_rewards_arr[0] / count($community_user_list);//新增业绩 * 分红比例 / 人数
+        if($reward > 0) {
+            $reward_power = $reward * $community_rewards_arr[1];
+            $reward_usdt = $reward * $community_rewards_arr[2] / $usdt_rate;//算力折算成U
+
+            foreach ($community_user_list as $info) {
+                //发放分红
+                (new LedgerWalletModel)->changeWalletAccount($info['id'], Asset::POWER, $reward_power, Action::PowerBonusAward);
+
+                (new LedgerWalletModel)->changeWalletAccount($info['id'], Asset::USDT, $reward_usdt, Action::UsdtWeightDividend);
+            }
+        }else {
+            Log::info("社区长分红在" . $date . "分红金额为0;当日新增业绩:" . $recharge_power . ",分红比例:" . $community_rewards_arr[0] . ",分红人数" . count($community_user_list));
+        }
+
+        //领导人分红
+        //查找有团队长等级的会员
+        $sys_leader_user_list = DB::table('user')
+            ->field('id,market_level_id')
+            ->where('market_level_id', MembershipLevel::SystemLeader)
+            ->select();
+
+        $sys_leade_rewards = (new ParametersModel())->getValue('sysLeaderRewards'); //系统领导人分红参数 数据格式为:0.02:0.5:0.5
+        $sys_leade_rewards_arr = explode(':', $sys_leade_rewards);
+
+        $reward = $recharge_power * $sys_leade_rewards_arr[0] / count($sys_leader_user_list);//新增业绩 * 分红比例 / 人数
+        if($reward > 0) {
+            $reward_power = $reward * $sys_leade_rewards_arr[1];
+            $reward_usdt = $reward * $sys_leade_rewards_arr[2] * 8;//算力折算成U
+
+            foreach ($sys_leader_user_list as $info) {
+                //发放分红
+                (new LedgerWalletModel)->changeWalletAccount($info['id'], Asset::POWER, $reward_power, Action::PowerBonusAward);
+
+                (new LedgerWalletModel)->changeWalletAccount($info['id'], Asset::USDT, $reward_usdt, Action::UsdtWeightDividend);
+            }
+        }else {
+            Log::info("系统领导人分红在" . $date . "分红金额为0;当日新增业绩:" . $recharge_power . ",分红比例:" . $community_rewards_arr[0] . ",分红人数" . count($community_user_list));
+        }
+        return $log_id;
+    }
+
+
+    /**
+     * 获取当日收益 Daily earnings
+     */
+    private static function getDayEarnings(string $date)
+    {   
+        $between_time = [strtotime($date), strtotime($date) + 86400];
+        //读取当日新增数据
+        return ProductOrder::where('type_id', ProductOrder::Popular)->where('status', ProductOrder::Paid)->whereBetween('create_time', $between_time)->sum('price');
+    }
+
+  
+
+  
+
+    /**
+     * 获取可用日期
+     * @var array
+     */
+    protected function getExeDate($type_id = 1)
+    {
+        $today = date('Y-m-d',strtotime("-1day"));//当前时间减一天为最后一次可用时间
+        $dateInfo = DB::table('timed_task_log')
+            ->where('type_id' ,$type_id)
+            ->where('status', 1)
+            ->order('date_time', 'desc')
+            ->find();
+        if(empty($dateInfo)){//首次
+            return $today;
+        }
+        $new_date = date('Y-m-d',strtotime("+1day",strtotime($dateInfo['date_time'])));//最后一次发放日期+1天为本次执行时间
+        if($new_date > $today){//超过今天
+            return "";
+        }
+        return $new_date;
+    }
+
+    /**
+     * 插入或更新收益统计信息
+     * @var array
+     */
+    protected function createData($uids, $amount, $date)
+    {
+        foreach ($uids as $user_id) {
+            $check = DB::table('team_rewards')
+                ->field('id')
+                ->where('user_id', $user_id)
+                ->where('date_time', $date)
+                ->find();
+            if ($check) {//存在,则更新
+                DB::table('team_rewards')
+                    ->where('id', $check['id'])
+                    ->update([
+                        'today_power' => Db::raw('today_power+' . $amount),
+                        'update_time' => date('Y-m-d H:i:s')
+                    ]);
+            } else {//新增
+                DB::table('team_rewards')
+                    ->insert([
+                        'user_id' => $user_id,
+                        'date_time' => $date,
+                        'today_power' => Db::raw('today_power+' . $amount),
+                        'create_time' => date('Y-m-d H:i:s')
+                    ]);
+            }
+        }
+    }
+}

+ 9 - 1
application/common/model/ProductOrder.php

@@ -76,7 +76,7 @@ class ProductOrder extends Model
             $order_arr['area_id']   = $item;
             self::setCreateOrder($order_id, $order_arr, self::Popular, $uid, 0, getOrderSN('R'), 0, $price);
         }
-        //修改区域状态 areaArr
+        //修改区域状态
         $count = ProductArea::whereIn('id', $areaArr)->setField('status', ProductLists::STOP);
         if($count !=  count($areaArr)) $result = false;
         return $result;
@@ -110,6 +110,7 @@ class ProductOrder extends Model
         ]);
     }
 
+
     // 获取订单状态
     public static function getProductOrder($orderId, $status, string $field){
         return self::alias('a')->where('a.id', $orderId)
@@ -119,6 +120,13 @@ class ProductOrder extends Model
                 ->find();
     }
 
+    // 获取新人福利领取订单记录
+    public static function getUserWelfare($uid, $typeId){
+        return self::where('user_id', $uid)
+                ->where('type_id', $typeId)
+                ->select();
+    }
+
     public static function getStatusList()
     {
         return [self::Ordered => __('已下单'), self::Paid  => __('已付款'), self::Transferred => __('已转让'), 

+ 1 - 2
application/common/model/ProductPopular.php

@@ -85,8 +85,7 @@ class ProductPopular extends Model
         return $info;
     }
 
-
-
+ 
     //分类
     public function productsList()
     {

+ 3 - 1
application/common/model/UserAirdrop.php

@@ -26,7 +26,9 @@ class UserAirdrop extends Model
     ];
     
 
-    
+    public function getStatusList(){
+        
+    }
 
 
 

+ 5 - 1
application/common/model/UserWelfare.php

@@ -26,7 +26,11 @@ class UserWelfare extends Model
         'update_time_text'
     ];
     
-
+    //判断是否开启
+    public static function getIsWelfare(): object
+    {
+        return self::where('id', 1)->find();
+    }