Index.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 * 90;
  78. $minTime = $nowtime - $spacetime;
  79. // 1711172121
  80. // 1711166775
  81. $redis = new \Redis();
  82. $redis->connect('127.0.0.1','6379');
  83. $redis->zRemRangeByScore("live", 0, $minTime);
  84. $uid = $redis->zRangeByScore("live", $minTime, $nowtime);
  85. $returndata = '';
  86. $userdata = db('admin')->field("id")
  87. // ->whereIn('id', $uid)
  88. // ->where(['livetime' => ['gt', $nowtime - $spacetime]])
  89. ->where(['incomeendtime' => ['gt', $nowtime]])
  90. ->select();
  91. Db::startTrans();
  92. if (!$userdata) {
  93. $returndata = 'no data';
  94. } else {
  95. $userids = array_column($userdata, 'id');
  96. $userlink = db('userlink')
  97. ->join(['fa_link'], 'fa_userlink.link_id = fa_link.id')
  98. ->field("fa_link.link as link,
  99. fa_link.id as link_id,
  100. fa_userlink.uid as uid,
  101. fa_userlink.id as userlinkid,
  102. fa_userlink.income as income,
  103. fa_link.minincome as minincome,
  104. fa_link.maxincome as maxincome
  105. ")->where(['uid' => ['in', $userids]])->select();
  106. if (!$userlink) {
  107. $returndata = 'no data1';
  108. } else {
  109. $savedata = $userlinkdata = [];
  110. foreach ($userlink as $key => $val) {
  111. $unit = bcpow(10, 10);
  112. // $income = mt_rand( * $unit, $val['maxincome'] * $unit) / $unit;
  113. $income = bcdiv(mt_rand(bcmul($val['minincome'], $unit, 10), bcmul($val['maxincome'], $unit, 10)), $unit, 10);
  114. $savedata[] = ['income' => $income,
  115. 'time' => $nowtime,
  116. 'link' => $val['link'],
  117. 'uid' => $val['uid'],
  118. 'status' => 0
  119. ];
  120. $userlinkdata[] = ['id' => $val['userlinkid'], 'income' => $val['income'] + $income];
  121. if ($key != 0 && $key % 100 == 0) {
  122. echo $key."\n";
  123. $this->userlinkModel->saveAll($userlinkdata);
  124. $this->recordModel->saveAll($savedata);
  125. $userlinkdata = [];
  126. $savedata = [];
  127. Db::commit();
  128. Db::startTrans();
  129. }
  130. }
  131. $this->userlinkModel->saveAll($userlinkdata);
  132. $this->recordModel->saveAll($savedata);
  133. $returndata = 'insert ' . count($savedata) . ' record';
  134. }
  135. Db::commit();
  136. }
  137. return $returndata;
  138. }
  139. public function oldrecord()
  140. {
  141. $beginYesterday = mktime(0, 0, 0, date('m'), date('d') - 1, date('Y'));
  142. $endYesterday = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
  143. // 统计前面的收益
  144. $total = Record::where('time', ">=", $beginYesterday)
  145. ->where("time", '<', $endYesterday)
  146. ->field('sum(income) as income, count(id) as num,uid as id')
  147. ->group("uid")
  148. ->select();
  149. $date = date("Y-m-d", $beginYesterday);
  150. Db::startTrans();
  151. $totalRecord = count($total);
  152. echo "总共{$totalRecord}条记录\n";
  153. foreach ($total as $key=>$item) {
  154. echo "正在处理第{$key}条\n";
  155. DayRecord::create([
  156. "uid" => $item->id,
  157. "addtime" => $date,
  158. "link_num" => $item->num,
  159. "income" => bcmul('1', $item->income, 4)
  160. ]);
  161. }
  162. Record::where('time', ">=", $beginYesterday)
  163. ->where("time", '<', $endYesterday)
  164. ->where('status', 1)
  165. ->delete();
  166. Db::commit();
  167. // $beginYesterday=mktime(0,0,0,date('m'),date('d')-1,date('Y'));
  168. // $userlink = db('userlink')->select();
  169. // $uplink=[];
  170. // foreach ($userlink as $key=>$val){
  171. // if($val['dayincome']>0){
  172. // $uplinktemp=[
  173. // 'id'=>$val['id'],
  174. // 'dayincome'=>0,
  175. // 'oldincome'=>$val['dayincome'],
  176. // ];
  177. // if($val['endtime']<$beginYesterday){
  178. // $uplinktemp['oldincome']=0;
  179. // }
  180. // $uplink[]= $uplinktemp;
  181. // }
  182. // }
  183. // $this->userlinkModel->saveAll($uplink);
  184. }
  185. }