Platform.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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("23: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 = model('video_platform')->where('vid', $v)->sum('dividend');
  184. if ($each_income<=0) continue;
  185. // $each_income = $income / $count; //每点贡献值的收益
  186. $user_list = model('video_user')->where('vid', $v)->where('contribution','>',0)->select();
  187. $name = model('Videolist')->where('id', $v)->value('name'); //该短剧名称
  188. if (empty($name)){
  189. continue;
  190. }
  191. if($user_list) {
  192. foreach ($user_list as $key => $value) {
  193. // $user_income = $value['contribution'] * $each_income; //该用户该短剧的收益
  194. $user_income = bcmul($value['contribution'],$each_income,8); //该用户该短剧的收益
  195. $money = model('User')->where('id',$value['uid'])->value('money'); //该用户当前余额
  196. $after = bcadd($money,$user_income,8); //该用户分红后余额
  197. MoneyLog::create(['user_id' => $value['uid'], 'money' =>$user_income , 'before' => $money, 'after' => $after, 'memo' => $name.'每日收益分红']);
  198. $res = model('User')->where('id', $value['uid'])->update(['money' => $after]); //更新用户余额
  199. $income_after = bcadd($value['income'],$user_income,8); //该用户总收益
  200. $res2=model('video_user')->where('id',$value['id'])->update(['income' => $income_after]); //更新用户收益
  201. }
  202. }
  203. }
  204. }
  205. // // 提交事务
  206. Db::commit();
  207. } catch (\Exception $e) {
  208. // 回滚事务
  209. var_dump($e);
  210. Db::rollback();
  211. }
  212. $this->success('分红完成');
  213. }
  214. // 用户贡献值变动记录
  215. public function user_contribution(){
  216. $map = []; // 初始化$map数组,避免未定义时的错误
  217. if(input('uid', 0) > 0){
  218. // 明确指定uid所属的表(假设主表为video_share)
  219. $map['video_contribution_record.uid'] = input('uid');
  220. }
  221. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  222. $config = $this->config();
  223. $list = model('VideoContributionRecord')
  224. ->with('videolist')
  225. ->where($map)
  226. ->limit($offset, $limit)
  227. ->select();
  228. $total = model('VideoContributionRecord')
  229. ->with('videolist')
  230. ->where($map)
  231. ->count();
  232. // var_dump(model('VideoShare')->getLastSql());die();
  233. $data = ['config' => $config, 'doctor' => $list,'total'=>$total];
  234. $this->success('请求成功', $data);
  235. }
  236. // 用户贡献值列表
  237. public function user_contribution_list(){
  238. $map = []; // 初始化$map数组,避免未定义时的错误
  239. if(input('uid', 0) > 0){
  240. // 明确指定uid所属的表(假设主表为video_share)
  241. $map['video_user.uid'] = input('uid');
  242. }
  243. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  244. $config = $this->config();
  245. $list = model('VideoUser')
  246. ->with('videolist')
  247. ->where($map)
  248. ->limit($offset, $limit)
  249. ->select();
  250. $total = model('VideoUser')
  251. ->with('videolist')
  252. ->where($map)
  253. ->count();
  254. // var_dump(model('VideoShare')->getLastSql());die();
  255. $data = ['config' => $config, 'doctor' => $list,'total'=>$total];
  256. $this->success('请求成功', $data);
  257. }
  258. }