Platform.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. 'platform_id' => $v['platform_id'],
  105. 'num' => $randomNumber,
  106. 'createtime' => time(),
  107. ];
  108. $id = Db::name('VideoPlatformRecord')->insertGetId($up);
  109. }
  110. }
  111. $this->success('分配完成');
  112. }
  113. // 检查过期版权,并扣除贡献值
  114. public function past_copyright(){
  115. // Db::startTrans();
  116. // try {
  117. $list = model('VideoShare')->where('endtime','<',time())->where('type',2)->where('expire',0)->select();
  118. if($list) {
  119. foreach ($list as $k => $v) {
  120. @file_put_contents("quanju.txt", json_encode($v) . "-要扣除的版权\r\n", 8);
  121. $video_user = model('VideoUser')->where('uid',$v['uid'])->where('vid',$v['vid'])->find();
  122. if ($video_user){
  123. // 要过期的版权数量也就是贡献值数量
  124. $share = $video_user['share']-$v['num']; // 扣除版权
  125. $contribution = $video_user['contribution']-$v['num']; //扣除贡献值
  126. if ($share <= 0&&$contribution<=0){
  127. $res1 = model('VideoUser')->where('uid',$v['uid'])->where('vid',$v['vid'])->delete();
  128. }else{
  129. $res1 = model('VideoUser')->where('uid',$v['uid'])->where('vid',$v['vid'])->update(['share'=>$share,'contribution'=>$contribution]);
  130. }
  131. @file_put_contents("quanju.txt", $res1 . "-修改状态\r\n", 8);
  132. // 保存扣除记录
  133. $up = [
  134. 'vid' => $v['vid'],
  135. 'uid' => $v['uid'],
  136. 'link_id' => $v['id'],
  137. 'num' => $v['num'],
  138. 'after' => $contribution,
  139. 'content' => '版权过期,扣除对应贡献值',
  140. 'createtime' => time(),
  141. 'pm' => 1,
  142. ];
  143. $id = Db::name('VideoContributionRecord')->insertGetId($up);
  144. @file_put_contents("quanju.txt", $id . "-记录id\r\n", 8);
  145. $user = model('Videolist')->where('id',$v['vid'])->find();
  146. $user_contribution = $user['share']+$v['num'];
  147. @file_put_contents("quanju.txt", $user_contribution . "-修改后版权\r\n", 8);
  148. $res2 = model('Videolist')->where('id',$v['vid'])->update(['share'=>$user_contribution]);
  149. $res3 = model('VideoShare')->where('id',$v['id'])->update(['expire'=>1]); //修改为已过期
  150. }
  151. }
  152. }
  153. // // 提交事务
  154. // Db::commit();
  155. // } catch (\Exception $e) {
  156. // // 回滚事务
  157. // var_dump($e);
  158. // Db::rollback();
  159. // }
  160. $this->success('检查完成');
  161. }
  162. // 每日分红
  163. public function dividend_day(){
  164. // Db::startTrans();
  165. // try {
  166. // 获取所有用户贡献值列表
  167. $vid_list = model('video_user')->where('contribution','>',0)->column('vid');
  168. $vid_list = array_unique($vid_list); //需要分红的短剧id
  169. // 获取当前时间
  170. $now = time();
  171. // 获取昨天的日期
  172. $yesterday = strtotime("-1 day", $now);
  173. // 昨天的0点时间戳
  174. $yesterdayStart = strtotime("00:00:00", $yesterday);
  175. $yesterdayEnd = strtotime("12:59:59", $yesterday);
  176. if($vid_list) {
  177. foreach ($vid_list as $k => $v) {
  178. $count = model('video_user')->where('vid',$v)->field('sum(contribution) as count')->find();
  179. $count = $count['count']; //该短剧的总贡献值
  180. $income = model('video_platform_record')->where('vid', $v)->where('createtime', 'between', [$yesterdayStart, $yesterdayEnd])->sum('num'); //该短剧昨日收益
  181. $each_income = $income / $count; //每点贡献值的收益
  182. $user_list = model('video_user')->where('vid', $v)->where('contribution','>',0)->select();
  183. $name = model('Videolist')->where('id', $v)->value('name'); //该短剧名称
  184. if($user_list) {
  185. foreach ($user_list as $key => $value) {
  186. $user_income = $value['contribution'] * $each_income; //该用户该短剧的收益
  187. $money = model('User')->where('id',$value['uid'])->value('money'); //该用户当前余额
  188. $after = $money + $user_income; //该用户分红后余额
  189. MoneyLog::create(['user_id' => $value['uid'], 'money' =>$user_income , 'before' => $money, 'after' => $after, 'memo' => $name.'每日收益分红']);
  190. $res = model('User')->where('id', $value['uid'])->update(['money' => $after]); //更新用户余额
  191. }
  192. }
  193. }
  194. }
  195. // // 提交事务
  196. // Db::commit();
  197. // } catch (\Exception $e) {
  198. // // 回滚事务
  199. // var_dump($e);
  200. // Db::rollback();
  201. // }
  202. $this->success('分红完成');
  203. }
  204. // 用户贡献值变动记录
  205. public function user_contribution(){
  206. $map = []; // 初始化$map数组,避免未定义时的错误
  207. if(input('uid', 0) > 0){
  208. // 明确指定uid所属的表(假设主表为video_share)
  209. $map['video_contribution_record.uid'] = input('uid');
  210. }
  211. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  212. $config = $this->config();
  213. $list = model('VideoContributionRecord')
  214. ->with('videolist')
  215. ->where($map)
  216. ->limit($offset, $limit)
  217. ->select();
  218. $total = model('VideoContributionRecord')
  219. ->with('videolist')
  220. ->where($map)
  221. ->count();
  222. // var_dump(model('VideoShare')->getLastSql());die();
  223. $data = ['config' => $config, 'doctor' => $list,'total'=>$total];
  224. $this->success('请求成功', $data);
  225. }
  226. // 用户贡献值列表
  227. public function user_contribution_list(){
  228. $map = []; // 初始化$map数组,避免未定义时的错误
  229. if(input('uid', 0) > 0){
  230. // 明确指定uid所属的表(假设主表为video_share)
  231. $map['video_user.uid'] = input('uid');
  232. }
  233. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  234. $config = $this->config();
  235. $list = model('VideoUser')
  236. ->with('videolist')
  237. ->where($map)
  238. ->limit($offset, $limit)
  239. ->select();
  240. $total = model('VideoUser')
  241. ->with('videolist')
  242. ->where($map)
  243. ->count();
  244. // var_dump(model('VideoShare')->getLastSql());die();
  245. $data = ['config' => $config, 'doctor' => $list,'total'=>$total];
  246. $this->success('请求成功', $data);
  247. }
  248. }