Platform.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\model\MoneyLog;
  4. use think\Db;
  5. use fast\Tree;
  6. use app\common\controller\Api;
  7. /**
  8. * 股份 收益 分红
  9. */
  10. class Platform extends Api
  11. {
  12. protected $noNeedLogin = ['*'];
  13. protected $noNeedRight = ['*'];
  14. public function index()
  15. {
  16. $config=$this->config();
  17. $configs=config('site');
  18. $map=[];
  19. // $map['ishot']=2;
  20. // $map['endtimesjc']=['>',time()];
  21. // if(input('uid',0)>0){
  22. // $map['uid']=input('uid');
  23. // }
  24. $list = model('Platform')
  25. // ->with('user')
  26. // ->where($map)
  27. ->limit(10)
  28. ->select();
  29. $data=['config'=>$config,'doctor'=>$list];
  30. $this->success('请求成功',$data);
  31. }
  32. public function test(){
  33. $this->success('请求成功');
  34. }
  35. public function user_share_order(){
  36. $map = []; // 初始化$map数组,避免未定义时的错误
  37. if(input('uid', 0) > 0){
  38. // 明确指定uid所属的表(假设主表为video_share)
  39. $map['video_share.uid'] = input('uid');
  40. $count = $this->user_share_count($map['video_share.uid']);
  41. }
  42. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  43. $config = $this->config();
  44. $list = model('VideoShare')
  45. ->with('videolist,user')
  46. ->where($map)
  47. ->limit($offset, $limit)
  48. ->select();
  49. $total = model('VideoShare')
  50. ->with('users')
  51. ->where($map)
  52. ->count();
  53. // var_dump(model('VideoShare')->getLastSql());die();
  54. $data = ['config' => $config, 'doctor' => $list,'total'=>$total,'count' => $count];
  55. $this->success('请求成功', $data);
  56. }
  57. public function user_share_count($uid){ // 统计用户各个短剧的版权数量
  58. $map = []; // 初始化$map数组,避免未定义时的错误
  59. $map['uid'] = $uid;
  60. $timestamp = strtotime("-3 years");
  61. $count = model('VideoShare')
  62. ->where($map)
  63. ->where('createtime','>',$timestamp)
  64. ->group('vid')
  65. ->field('vid,sum(num) as count')
  66. ->select();
  67. return $count;
  68. }
  69. // 查看各平台的收益情况
  70. public function platform_income(){
  71. $map = []; // 初始化查询条件数组
  72. // 修正条件:使用正确的输入参数名 platform_id
  73. if(input('platform_id', 0) > 0){
  74. $map['video_platform_record.platform_id'] = input('platform_id', 0);
  75. }
  76. // vid 条件正确,无需修改
  77. if(input('vid', 0) > 0){
  78. $map['video_platform_record.vid'] = input('vid', 0);
  79. }
  80. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  81. $config = $this->config();
  82. $list = model('VideoPlatformRecord')
  83. ->with('platform,videolist')
  84. ->where($map)
  85. ->limit($offset, $limit)
  86. ->select();
  87. $total = model('VideoPlatformRecord')
  88. ->with('platform,videolist')
  89. ->where($map)
  90. ->count();
  91. $data = ['config' => $config, 'doctor' => $list, 'total' => $total];
  92. $this->success('请求成功', $data);
  93. }
  94. // 每日分配各个短剧在不同平台的收益
  95. public function platform_income_day(){
  96. // 先获取所有短剧在不同平台的收益上下限
  97. $list = model('VideoPlatform')->select();
  98. if($list) {
  99. foreach ($list as $k => $v) {
  100. $randomNumber = mt_rand($v['min'] * 100, $v['max'] * 100) / 100;
  101. $up = [
  102. 'vid' => $v['vid'],
  103. 'platform_id' => $v['platform_id'],
  104. 'num' => $randomNumber,
  105. 'createtime' => time(),
  106. ];
  107. $id = Db::name('VideoPlatformRecord')->insertGetId($up);
  108. }
  109. }
  110. $this->success('分配完成');
  111. }
  112. // 检查过期版权,并扣除贡献值
  113. public function past_copyright(){
  114. // Db::startTrans();
  115. // try {
  116. $list = model('VideoShare')->where('endtime','<',time())->where('type',2)->where('expire',0)->select();
  117. if($list) {
  118. foreach ($list as $k => $v) {
  119. @file_put_contents("quanju.txt", json_encode($v) . "-要扣除的版权\r\n", 8);
  120. $video_user = model('VideoUser')->where('uid',$v['uid'])->where('vid',$v['vid'])->find();
  121. if ($video_user){
  122. // 要过期的版权数量也就是贡献值数量
  123. $share = $video_user['share']-$v['num']; // 扣除版权
  124. $contribution = $video_user['contribution']-$v['num']; //扣除贡献值
  125. if ($share <= 0&&$contribution<=0){
  126. $res1 = model('VideoUser')->where('uid',$v['uid'])->where('vid',$v['vid'])->delete();
  127. }else{
  128. $res1 = model('VideoUser')->where('uid',$v['uid'])->where('vid',$v['vid'])->update(['share'=>$share,'contribution'=>$contribution]);
  129. }
  130. @file_put_contents("quanju.txt", $res1 . "-修改状态\r\n", 8);
  131. // 保存扣除记录
  132. $up = [
  133. 'vid' => $v['vid'],
  134. 'uid' => $v['uid'],
  135. 'link_id' => $v['id'],
  136. 'num' => $v['num'],
  137. 'after' => $contribution,
  138. 'content' => '版权过期,扣除对应贡献值',
  139. 'createtime' => time(),
  140. 'pm' => 1,
  141. ];
  142. $id = Db::name('VideoContributionRecord')->insertGetId($up);
  143. @file_put_contents("quanju.txt", $id . "-记录id\r\n", 8);
  144. $user = model('Videolist')->where('id',$v['vid'])->find();
  145. $user_contribution = $user['share']+$v['num'];
  146. @file_put_contents("quanju.txt", $user_contribution . "-修改后版权\r\n", 8);
  147. $res2 = model('Videolist')->where('id',$v['vid'])->update(['share'=>$user_contribution]);
  148. $res3 = model('VideoShare')->where('id',$v['id'])->update(['expire'=>1]); //修改为已过期
  149. }
  150. }
  151. }
  152. // // 提交事务
  153. // Db::commit();
  154. // } catch (\Exception $e) {
  155. // // 回滚事务
  156. // var_dump($e);
  157. // Db::rollback();
  158. // }
  159. $this->success('检查完成');
  160. }
  161. // 每日分红
  162. public function dividend_day(){
  163. // Db::startTrans();
  164. // try {
  165. // 获取所有用户贡献值列表
  166. $vid_list = model('video_user')->where('contribution','>',0)->column('vid');
  167. $vid_list = array_unique($vid_list); //需要分红的短剧id
  168. // 获取当前时间
  169. $now = time();
  170. // 获取昨天的日期
  171. $yesterday = strtotime("-1 day", $now);
  172. // 昨天的0点时间戳
  173. $yesterdayStart = strtotime("00:00:00", $yesterday);
  174. $yesterdayEnd = strtotime("12:59:59", $yesterday);
  175. if($vid_list) {
  176. foreach ($vid_list as $k => $v) {
  177. $count = model('video_user')->where('vid',$v)->field('sum(contribution) as count')->find();
  178. $count = $count['count']; //该短剧的总贡献值
  179. $income = model('video_platform_record')->where('vid', $v)->where('createtime', 'between', [$yesterdayStart, $yesterdayEnd])->sum('num'); //该短剧昨日收益
  180. $each_income = $income / $count; //每点贡献值的收益
  181. $user_list = model('video_user')->where('vid', $v)->where('contribution','>',0)->select();
  182. $name = model('Videolist')->where('id', $v)->value('name'); //该短剧名称
  183. if($user_list) {
  184. foreach ($user_list as $key => $value) {
  185. $user_income = $value['contribution'] * $each_income; //该用户该短剧的收益
  186. $money = model('User')->where('id',$value['uid'])->value('money'); //该用户当前余额
  187. $after = $money + $user_income; //该用户分红后余额
  188. MoneyLog::create(['user_id' => $value['uid'], 'money' =>$user_income , 'before' => $money, 'after' => $after, 'memo' => $name.'每日收益分红']);
  189. $res = model('User')->where('id', $value['uid'])->update(['money' => $after]); //更新用户余额
  190. }
  191. }
  192. }
  193. }
  194. // // 提交事务
  195. // Db::commit();
  196. // } catch (\Exception $e) {
  197. // // 回滚事务
  198. // var_dump($e);
  199. // Db::rollback();
  200. // }
  201. $this->success('分红完成');
  202. }
  203. }