| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- <?php
- namespace app\api\controller;
- use app\common\model\MoneyLog;
- use think\Db;
- use fast\Tree;
- use app\common\controller\Api;
- /**
- * 股份 收益 分红
- */
- class Platform extends Api
- {
- protected $noNeedLogin = ['*'];
- protected $noNeedRight = ['*'];
- public function index()
- {
- $config=$this->config();
- $configs=config('site');
- $map=[];
- // $map['ishot']=2;
- // $map['endtimesjc']=['>',time()];
- // if(input('uid',0)>0){
- // $map['uid']=input('uid');
- // }
- $list = model('Platform')
- // ->with('user')
- // ->where($map)
- ->limit(10)
- ->select();
- $data=['config'=>$config,'doctor'=>$list];
- $this->success('请求成功',$data);
- }
- public function test(){
- $this->success('请求成功');
- }
- public function user_share_order(){
- $map = []; // 初始化$map数组,避免未定义时的错误
- if(input('uid', 0) > 0){
- // 明确指定uid所属的表(假设主表为video_share)
- $map['video_share.uid'] = input('uid');
- $count = $this->user_share_count($map['video_share.uid']);
- }
- list($where, $sort, $order, $offset, $limit) = $this->buildparams();
- $config = $this->config();
- $list = model('VideoShare')
- ->with('videolist,user')
- ->where($map)
- ->limit($offset, $limit)
- ->select();
- $total = model('VideoShare')
- ->with('users')
- ->where($map)
- ->count();
- // var_dump(model('VideoShare')->getLastSql());die();
- $data = ['config' => $config, 'doctor' => $list,'total'=>$total,'count' => $count];
- $this->success('请求成功', $data);
- }
- public function user_share_count($uid){ // 统计用户各个短剧的版权数量
- $map = []; // 初始化$map数组,避免未定义时的错误
- $map['uid'] = $uid;
- $timestamp = strtotime("-3 years");
- $count = model('VideoShare')
- ->where($map)
- ->where('createtime','>',$timestamp)
- ->group('vid')
- ->field('vid,sum(num) as count')
- ->select();
- return $count;
- }
- // 查看各平台的收益情况
- public function platform_income(){
- $map = []; // 初始化查询条件数组
- // 修正条件:使用正确的输入参数名 platform_id
- if(input('platform_id', 0) > 0){
- $map['video_platform_record.platform_id'] = input('platform_id', 0);
- }
- // vid 条件正确,无需修改
- if(input('vid', 0) > 0){
- $map['video_platform_record.vid'] = input('vid', 0);
- }
- list($where, $sort, $order, $offset, $limit) = $this->buildparams();
- $config = $this->config();
- $list = model('VideoPlatformRecord')
- ->with('platform,videolist')
- ->where($map)
- ->limit($offset, $limit)
- ->select();
- $total = model('VideoPlatformRecord')
- ->with('platform,videolist')
- ->where($map)
- ->count();
- $data = ['config' => $config, 'doctor' => $list, 'total' => $total];
- $this->success('请求成功', $data);
- }
- // 每日分配各个短剧在不同平台的收益
- public function platform_income_day(){
- // 先获取所有短剧在不同平台的收益上下限
- $list = model('VideoPlatform')->select();
- if($list) {
- foreach ($list as $k => $v) {
- $randomNumber = mt_rand($v['min'] * 100, $v['max'] * 100) / 100;
- $up = [
- 'vid' => $v['vid'],
- 'platform_id' => $v['platform_id'],
- 'num' => $randomNumber,
- 'createtime' => time(),
- ];
- $id = Db::name('VideoPlatformRecord')->insertGetId($up);
- }
- }
- $this->success('分配完成');
- }
- // 检查过期版权,并扣除贡献值
- public function past_copyright(){
- // Db::startTrans();
- // try {
- $list = model('VideoShare')->where('endtime','<',time())->where('type',2)->where('expire',0)->select();
- if($list) {
- foreach ($list as $k => $v) {
- @file_put_contents("quanju.txt", json_encode($v) . "-要扣除的版权\r\n", 8);
- $video_user = model('VideoUser')->where('uid',$v['uid'])->where('vid',$v['vid'])->find();
- if ($video_user){
- // 要过期的版权数量也就是贡献值数量
- $share = $video_user['share']-$v['num']; // 扣除版权
- $contribution = $video_user['contribution']-$v['num']; //扣除贡献值
- if ($share <= 0&&$contribution<=0){
- $res1 = model('VideoUser')->where('uid',$v['uid'])->where('vid',$v['vid'])->delete();
- }else{
- $res1 = model('VideoUser')->where('uid',$v['uid'])->where('vid',$v['vid'])->update(['share'=>$share,'contribution'=>$contribution]);
- }
- @file_put_contents("quanju.txt", $res1 . "-修改状态\r\n", 8);
- // 保存扣除记录
- $up = [
- 'vid' => $v['vid'],
- 'uid' => $v['uid'],
- 'link_id' => $v['id'],
- 'num' => $v['num'],
- 'after' => $contribution,
- 'content' => '版权过期,扣除对应贡献值',
- 'createtime' => time(),
- 'pm' => 1,
- ];
- $id = Db::name('VideoContributionRecord')->insertGetId($up);
- @file_put_contents("quanju.txt", $id . "-记录id\r\n", 8);
- $user = model('Videolist')->where('id',$v['vid'])->find();
- $user_contribution = $user['share']+$v['num'];
- @file_put_contents("quanju.txt", $user_contribution . "-修改后版权\r\n", 8);
- $res2 = model('Videolist')->where('id',$v['vid'])->update(['share'=>$user_contribution]);
- $res3 = model('VideoShare')->where('id',$v['id'])->update(['expire'=>1]); //修改为已过期
- }
- }
- }
- // // 提交事务
- // Db::commit();
- // } catch (\Exception $e) {
- // // 回滚事务
- // var_dump($e);
- // Db::rollback();
- // }
- $this->success('检查完成');
- }
- // 每日分红
- public function dividend_day(){
- // Db::startTrans();
- // try {
- // 获取所有用户贡献值列表
- $vid_list = model('video_user')->where('contribution','>',0)->column('vid');
- $vid_list = array_unique($vid_list); //需要分红的短剧id
- // 获取当前时间
- $now = time();
- // 获取昨天的日期
- $yesterday = strtotime("-1 day", $now);
- // 昨天的0点时间戳
- $yesterdayStart = strtotime("00:00:00", $yesterday);
- $yesterdayEnd = strtotime("12:59:59", $yesterday);
- if($vid_list) {
- foreach ($vid_list as $k => $v) {
- $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'); //该短剧昨日收益
- $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('id',$value['uid'])->value('money'); //该用户当前余额
- $after = $money + $user_income; //该用户分红后余额
- MoneyLog::create(['user_id' => $value['uid'], 'money' =>$user_income , 'before' => $money, 'after' => $after, 'memo' => $name.'每日收益分红']);
- $res = model('User')->where('id', $value['uid'])->update(['money' => $after]); //更新用户余额
- }
- }
- }
- }
- // // 提交事务
- // Db::commit();
- // } catch (\Exception $e) {
- // // 回滚事务
- // var_dump($e);
- // Db::rollback();
- // }
- $this->success('分红完成');
- }
- }
|