WIN-2308041133\Administrator 8 months ago
parent
commit
504c51f742
1 changed files with 18 additions and 8 deletions
  1. 18 8
      application/api/controller/Platform.php

+ 18 - 8
application/api/controller/Platform.php

@@ -3,6 +3,7 @@
 
 namespace app\api\controller;
 
+use app\common\model\MoneyLog;
 use think\Db;
 use fast\Tree;
 use app\common\controller\Api;
@@ -167,8 +168,9 @@ class Platform extends Api
     }
 //    每日分红
     public function dividend_day(){
+                 Db::startTrans();
+         try {
 //        获取所有用户贡献值列表
-//        $list = model('video_user')->select();
         $vid_list = model('video_user')->where('contribution','>',0)->column('vid');
         $vid_list = array_unique($vid_list);  //需要分红的短剧id
 // 获取当前时间
@@ -180,22 +182,30 @@ class Platform extends Api
         $yesterdayEnd = strtotime("12:59:59", $yesterday);
         if($vid_list) {
             foreach ($vid_list as $k => $v) {
-                $user_list = model('video_user')->where('vid', $v)->where('contribution','>',0)->select();
                 $count = model('video_user')->where('vid',$v)->field('sum(contribution) as count')->find();
                 $count = $count['count'];  //该短剧的总贡献值
                 $income = model('video_platform_record')->where('vid', $v)->where('createtime', 'between', [$yesterdayStart, $yesterdayEnd])->sum('num');  //该短剧昨日收益
-                var_dump($income);die();
+                $each_income = $income / $count;  //每点贡献值的收益
+                $user_list = model('video_user')->where('vid', $v)->where('contribution','>',0)->select();
+                $name = model('Videolist')->where('id', $v)->value('name');  //该短剧名称
                 if($user_list) {
                     foreach ($user_list as $key => $value) {
+                        $user_income = $value['contribution'] * $each_income;  //该用户该短剧的收益
+                        $money = model('User')->where('uid',$value['uid'])->value('money');  //该用户当前余额
+                        $after = $money + $user_income;  //该用户分红后余额
+                        MoneyLog::create(['user_id' => $value['uid'], 'money' =>$money  , 'before' => $money, 'after' => $after, 'memo' => $name.'每日收益分红']);
+                        $res = model('User')->where('uid', $value['uid'])->update(['money' => $after]);  //更新用户余额
                     }
                 }
             }
         }
-//        统计该短剧的总版版权数量以及持有版权的用户列表
-
-//        获取昨天该短剧的收益数据
-
-//        给各个用户分红
+                     // 提交事务
+             Db::commit();
+         } catch (\Exception $e) {
+             // 回滚事务
+             var_dump($e);
+             Db::rollback();
+         }
     }
 
 }