Index.php 6.0 KB

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