Index.php 6.1 KB

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