Platform.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. // 用户版权购买订单和版权统计
  36. public function user_share_order(){
  37. $map = []; // 初始化$map数组,避免未定义时的错误
  38. if(input('uid', 0) > 0){
  39. // 明确指定uid所属的表(假设主表为video_share)
  40. $map['video_share.uid'] = input('uid');
  41. $count = $this->user_share_count($map['video_share.uid']);
  42. }
  43. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  44. $config = $this->config();
  45. $list = model('VideoShare')
  46. ->with('videolist,user')
  47. ->where($map)
  48. ->limit($offset, $limit)
  49. ->select();
  50. $total = model('VideoShare')
  51. ->with('users')
  52. ->where($map)
  53. ->count();
  54. // var_dump(model('VideoShare')->getLastSql());die();
  55. $data = ['config' => $config, 'doctor' => $list,'total'=>$total,'count' => $count];
  56. $this->success('请求成功', $data);
  57. }
  58. public function user_share_count($uid){ // 统计用户各个短剧的版权数量
  59. $map = []; // 初始化$map数组,避免未定义时的错误
  60. $map['uid'] = $uid;
  61. $timestamp = strtotime("-3 years");
  62. $count = model('VideoShare')
  63. ->where($map)
  64. ->where('createtime','>',$timestamp)
  65. ->group('vid')
  66. ->field('vid,sum(num) as count')
  67. ->select();
  68. return $count;
  69. }
  70. // 查看各平台的收益情况
  71. public function platform_income(){
  72. $map = []; // 初始化查询条件数组
  73. // 修正条件:使用正确的输入参数名 platform_id
  74. if(input('platform_id', 0) > 0){
  75. $map['video_platform_record.platform_id'] = input('platform_id', 0);
  76. }
  77. // vid 条件正确,无需修改
  78. if(input('vid', 0) > 0){
  79. $map['video_platform_record.vid'] = input('vid', 0);
  80. }
  81. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  82. $config = $this->config();
  83. $list = model('VideoPlatformRecord')
  84. ->with('platform,videolist')
  85. ->where($map)
  86. ->limit($offset, $limit)
  87. ->select();
  88. $total = model('VideoPlatformRecord')
  89. ->with('platform,videolist')
  90. ->where($map)
  91. ->count();
  92. $data = ['config' => $config, 'doctor' => $list, 'total' => $total];
  93. $this->success('请求成功', $data);
  94. }
  95. // 每日分配各个短剧在不同平台的收益
  96. public function platform_income_day(){
  97. // 先获取所有短剧在不同平台的收益上下限
  98. $list = model('VideoPlatform')->select();
  99. if($list) {
  100. foreach ($list as $k => $v) {
  101. $randomNumber = mt_rand($v['min'] * 100, $v['max'] * 100) / 100;
  102. $up = [
  103. 'vid' => $v['vid'],
  104. 'vname' => $v['vname'],
  105. 'platform_id' => $v['platform_id'],
  106. 'pname' => $v['pname'],
  107. 'num' => $randomNumber,
  108. 'createtime' => time(),
  109. ];
  110. $id = Db::name('VideoPlatformRecord')->insertGetId($up);
  111. }
  112. }
  113. $this->success('分配完成');
  114. }
  115. // 检查过期版权,并扣除贡献值
  116. public function past_copyright(){
  117. // Db::startTrans();
  118. // try {
  119. $list = model('VideoShare')->where('endtime','<',time())->where('type',2)->where('expire',0)->select();
  120. if($list) {
  121. foreach ($list as $k => $v) {
  122. @file_put_contents("quanju.txt", json_encode($v) . "-要扣除的版权\r\n", 8);
  123. $video_user = model('VideoUser')->where('uid',$v['uid'])->where('vid',$v['vid'])->find();
  124. if ($video_user){
  125. // 要过期的版权数量也就是贡献值数量
  126. $share = $video_user['share']-$v['num']; // 扣除版权
  127. $contribution = $video_user['contribution']-$v['num']; //扣除贡献值
  128. if ($share <= 0&&$contribution<=0){
  129. $res1 = model('VideoUser')->where('uid',$v['uid'])->where('vid',$v['vid'])->delete();
  130. }else{
  131. $res1 = model('VideoUser')->where('uid',$v['uid'])->where('vid',$v['vid'])->update(['share'=>$share,'contribution'=>$contribution]);
  132. }
  133. @file_put_contents("quanju.txt", $res1 . "-修改状态\r\n", 8);
  134. // 保存扣除记录
  135. $up = [
  136. 'vid' => $v['vid'],
  137. 'uid' => $v['uid'],
  138. 'link_id' => $v['id'],
  139. 'num' => $v['num'],
  140. 'after' => $contribution,
  141. 'content' => '版权过期,扣除对应贡献值',
  142. 'createtime' => time(),
  143. 'pm' => 1,
  144. ];
  145. $id = Db::name('VideoContributionRecord')->insertGetId($up);
  146. @file_put_contents("quanju.txt", $id . "-记录id\r\n", 8);
  147. $user = model('Videolist')->where('id',$v['vid'])->find();
  148. $user_contribution = $user['share']+$v['num'];
  149. @file_put_contents("quanju.txt", $user_contribution . "-修改后版权\r\n", 8);
  150. $res2 = model('Videolist')->where('id',$v['vid'])->update(['share'=>$user_contribution]);
  151. $res3 = model('VideoShare')->where('id',$v['id'])->update(['expire'=>1]); //修改为已过期
  152. }
  153. }
  154. }
  155. // // 提交事务
  156. // Db::commit();
  157. // } catch (\Exception $e) {
  158. // // 回滚事务
  159. // var_dump($e);
  160. // Db::rollback();
  161. // }
  162. $this->success('检查完成');
  163. }
  164. // 每日分红
  165. public function dividend_day(){
  166. // Db::startTrans();
  167. // try {
  168. // 获取所有用户贡献值列表
  169. $vid_list = model('video_user')->where('contribution','>',0)->column('vid');
  170. $vid_list = array_unique($vid_list); //需要分红的短剧id
  171. // 获取当前时间
  172. $now = time();
  173. // 获取昨天的日期
  174. $yesterday = strtotime("-1 day", $now);
  175. // 昨天的0点时间戳
  176. $yesterdayStart = strtotime("00:00:00", $yesterday);
  177. $yesterdayEnd = strtotime("12:59:59", $yesterday);
  178. if($vid_list) {
  179. foreach ($vid_list as $k => $v) {
  180. $count = model('video_user')->where('vid',$v)->field('sum(contribution) as count')->find();
  181. $count = $count['count']; //该短剧的总贡献值
  182. $income = model('video_platform_record')->where('vid', $v)->where('createtime', 'between', [$yesterdayStart, $yesterdayEnd])->sum('num'); //该短剧昨日收益
  183. $each_income = $income / $count; //每点贡献值的收益
  184. $user_list = model('video_user')->where('vid', $v)->where('contribution','>',0)->select();
  185. $name =model('Videolist')->where('id',$v)->find(); //该短剧名称
  186. if($user_list) {
  187. foreach ($user_list as $key => $value) {
  188. $user_income = $value['contribution'] * $each_income; //该用户该短剧的收益
  189. $money = model('User')->where('id',$value['uid'])->value('money'); //该用户当前余额
  190. $after = bcadd($money,$user_income,2); //该用户分红后余额
  191. @file_put_contents("usdt.txt", $v."-我id呢\r\n", 8);
  192. @file_put_contents("usdt.txt", json_encode($name)."-我名字呢\r\n", 8);
  193. MoneyLog::create(['user_id' => $value['uid'], 'money' =>$user_income , 'before' => $money, 'after' => $after, 'memo' => $name.'每日收益分红']);
  194. $res = model('User')->where('id', $value['uid'])->update(['money' => $after]); //更新用户余额
  195. $income_after = bcadd($value['income'],$value['income'],2); //该用户总收益
  196. $res2=model('video_user')->where('id',$value['id'])->update(['income' => $income_after]); //更新用户收益
  197. }
  198. }
  199. }
  200. }
  201. // // 提交事务
  202. // Db::commit();
  203. // } catch (\Exception $e) {
  204. // // 回滚事务
  205. // var_dump($e);
  206. // Db::rollback();
  207. // }
  208. $this->success('分红完成');
  209. }
  210. // 用户贡献值变动记录
  211. public function user_contribution(){
  212. $map = []; // 初始化$map数组,避免未定义时的错误
  213. if(input('uid', 0) > 0){
  214. // 明确指定uid所属的表(假设主表为video_share)
  215. $map['video_contribution_record.uid'] = input('uid');
  216. }
  217. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  218. $config = $this->config();
  219. $list = model('VideoContributionRecord')
  220. ->with('videolist')
  221. ->where($map)
  222. ->limit($offset, $limit)
  223. ->select();
  224. $total = model('VideoContributionRecord')
  225. ->with('videolist')
  226. ->where($map)
  227. ->count();
  228. // var_dump(model('VideoShare')->getLastSql());die();
  229. $data = ['config' => $config, 'doctor' => $list,'total'=>$total];
  230. $this->success('请求成功', $data);
  231. }
  232. // 用户贡献值列表
  233. public function user_contribution_list(){
  234. $map = []; // 初始化$map数组,避免未定义时的错误
  235. if(input('uid', 0) > 0){
  236. // 明确指定uid所属的表(假设主表为video_share)
  237. $map['video_user.uid'] = input('uid');
  238. }
  239. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  240. $config = $this->config();
  241. $list = model('VideoUser')
  242. ->with('videolist')
  243. ->where($map)
  244. ->limit($offset, $limit)
  245. ->select();
  246. $total = model('VideoUser')
  247. ->with('videolist')
  248. ->where($map)
  249. ->count();
  250. // var_dump(model('VideoShare')->getLastSql());die();
  251. $data = ['config' => $config, 'doctor' => $list,'total'=>$total];
  252. $this->success('请求成功', $data);
  253. }
  254. }