Index.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace app\api\controller;
  3. use app\admin\model\DayRecord;
  4. use app\admin\model\Record;
  5. use app\common\controller\Api;
  6. use think\Db;
  7. /**
  8. * 首页接口
  9. */
  10. class Index extends Api
  11. {
  12. protected $noNeedLogin = ['*'];
  13. protected $noNeedRight = ['*'];
  14. public function _initialize()
  15. {
  16. $this->recordModel = new \app\admin\model\greenroom\Record;
  17. $this->adminModel = new \app\admin\model\Admin;
  18. $this->userlinkModel = new \app\admin\model\greenroom\Userlink;
  19. $this->moneyLogModel = new \app\common\model\MoneyLog;
  20. }
  21. /**
  22. * 首页
  23. *
  24. */
  25. public function index()
  26. {
  27. $this->success('请求成功');
  28. }
  29. //跟新收益
  30. public function upincome()
  31. {
  32. $maxId = $this->recordModel->max('id');
  33. $recorddata = $this->recordModel
  34. ->field('sum(income) as income ,uid as id')
  35. ->where("id", "<=", $maxId)
  36. ->where(['status' => 0])
  37. ->group('uid')
  38. ->select();
  39. $temp = $userids = [];
  40. $returndata = '1';
  41. Db::startTrans();
  42. if ($recorddata) {
  43. $returndata = '12';
  44. foreach ($recorddata as $key => $val) {
  45. $temp[$val['id']] = $val['income'];
  46. $userids[] = $val['id'];
  47. }
  48. $admindata = db('admin')->where(['id' => ['in', $userids]])->select();
  49. $upincome =$moenylog= [];
  50. foreach ($admindata as $k => $vl) {
  51. if (isset($temp[$vl['id']])) {
  52. db("admin")->where('id', $vl['id'])->setInc("money",$temp[$vl['id']]);
  53. $moenylog[]=['user_id'=>$vl['id'],'money' =>$temp[$vl['id']],'before' => $vl['money'], 'after' => $vl['money'] + $temp[$vl['id']], 'memo' => 'upincome'];
  54. }
  55. }
  56. if ($upincome) {
  57. $returndata = '123';
  58. // $this->adminModel->saveAll($upincome);
  59. $this->moneyLogModel->saveAll($moenylog);
  60. $this->recordModel->where("id", "<=", $maxId)->where(['status' => 0])->update(['status' => 1]);
  61. }
  62. }
  63. Db::commit();
  64. return $returndata;
  65. }
  66. /**
  67. * 跑链接收益
  68. *
  69. */
  70. public function runrecord()
  71. {
  72. $nowtime = time();
  73. $spacetime = 10 * 60;
  74. $returndata = '';
  75. $userdata = db('admin')->field("id")
  76. // ->where(['livetime' => ['gt', $nowtime - $spacetime]])
  77. ->where(['incomeendtime' => ['gt', $nowtime]])
  78. ->select();
  79. Db::startTrans();
  80. if (!$userdata) {
  81. $returndata = 'no data';
  82. } else {
  83. $userids = array_column($userdata, 'id');
  84. $userlink = db('userlink')
  85. ->join(['fa_link'], 'fa_userlink.link_id = fa_link.id')
  86. ->field("fa_link.link as link,
  87. fa_link.id as link_id,
  88. fa_userlink.uid as uid,
  89. fa_userlink.id as userlinkid,
  90. fa_userlink.income as income,
  91. fa_link.minincome as minincome,
  92. fa_link.maxincome as maxincome
  93. ")->where(['uid' => ['in', $userids]])->select();
  94. if (!$userlink) {
  95. $returndata = 'no data1';
  96. } else {
  97. $savedata = $userlinkdata = [];
  98. foreach ($userlink as $key => $val) {
  99. $unit = bcpow(10, 10);
  100. // $income = mt_rand( * $unit, $val['maxincome'] * $unit) / $unit;
  101. $income = bcdiv(mt_rand(bcmul($val['minincome'], $unit, 10), bcmul($val['maxincome'], $unit, 10)), $unit, 10);
  102. $savedata[] = ['income' => $income,
  103. 'time' => $nowtime,
  104. 'link' => $val['link'],
  105. 'uid' => $val['uid'],
  106. 'status' => 0
  107. ];
  108. $userlinkdata[] = ['id' => $val['userlinkid'], 'income' => $val['income'] + $income];
  109. }
  110. $this->userlinkModel->saveAll($userlinkdata);
  111. $this->recordModel->saveAll($savedata);
  112. $returndata = 'insert ' . count($savedata) . ' record';
  113. }
  114. Db::commit();
  115. }
  116. return $returndata;
  117. }
  118. public function oldrecord()
  119. {
  120. $beginYesterday = mktime(0, 0, 0, date('m'), date('d') - 1, date('Y'));
  121. $endYesterday = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
  122. // 统计前面的收益
  123. $total = Record::where('time', ">=", $beginYesterday)
  124. ->where("time", '<', $endYesterday)
  125. ->field('sum(income) as income, count(id) as num,uid as id')
  126. ->group("uid")
  127. ->select();
  128. $date = date("Y-m-d", $beginYesterday);
  129. Db::startTrans();
  130. foreach ($total as $item) {
  131. DayRecord::create([
  132. "uid" => $item->id,
  133. "addtime" => $date,
  134. "link_num" => $item->num,
  135. "income" => bcmul('1', $item->income, 4)
  136. ]);
  137. }
  138. Record::where('time', ">=", $beginYesterday)
  139. ->where("time", '<', $endYesterday)
  140. ->where('status', 1)
  141. ->delete();
  142. Db::commit();
  143. // $beginYesterday=mktime(0,0,0,date('m'),date('d')-1,date('Y'));
  144. // $userlink = db('userlink')->select();
  145. // $uplink=[];
  146. // foreach ($userlink as $key=>$val){
  147. // if($val['dayincome']>0){
  148. // $uplinktemp=[
  149. // 'id'=>$val['id'],
  150. // 'dayincome'=>0,
  151. // 'oldincome'=>$val['dayincome'],
  152. // ];
  153. // if($val['endtime']<$beginYesterday){
  154. // $uplinktemp['oldincome']=0;
  155. // }
  156. // $uplink[]= $uplinktemp;
  157. // }
  158. // }
  159. // $this->userlinkModel->saveAll($uplink);
  160. }
  161. }