Index.php 6.1 KB

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