Subject.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. /**
  3. * 持仓到期自动结算
  4. * User: qissen
  5. * Date: 2017/11/26
  6. * Time: 11:26a
  7. */
  8. namespace app\crontab\command;
  9. use app\core\model\Subject as Model;
  10. use app\core\model\SubjectStat;
  11. use app\core\model\User;
  12. use think\console\Command;
  13. use think\console\Input;
  14. use think\console\Output;
  15. use think\Log;
  16. use tool\Common;
  17. class Subject extends Command
  18. {
  19. protected function configure()
  20. {
  21. $this->setName('Subject')->setDescription('Subject开始');
  22. }
  23. protected function execute(Input $input, Output $output)
  24. {
  25. //在这里我们进行结算
  26. Log::info('Subject:'.Common::timetodate(THINK_START_TIME));
  27. $this->setStatusOnline();
  28. $this->setStatusOverTime();
  29. $this->setRandCreateCang();
  30. $output->writeln("Subject开始");
  31. }
  32. /**
  33. * 设置上线
  34. */
  35. private function setStatusOnline() {
  36. Model::where([
  37. 'releaseTime'=>['lt', THINK_START_TIME],
  38. 'status'=>Model::STATUS_ONLINE_CHECK
  39. ])->chunk(100, function($list) {
  40. foreach ($list as $item) {
  41. $item->status = Model::STATUS_ONLINE;
  42. $item->listOrder = THINK_START_TIME;
  43. $item->save();
  44. //将临近的app去掉首页
  45. if($item['isIndexApp']) {
  46. $model = Model::where([
  47. 'subjectTypeID'=>$item['subjectTypeID'],
  48. 'BACKUPID'=>0,
  49. 'status'=>Model::STATUS_ONLINE,
  50. 'subjectID'=>['neq', $item['subjectID']]
  51. ])->order('addTime desc')->find();
  52. if(!$model) {
  53. continue;
  54. }
  55. $model['isIndexPc'] = 0;
  56. $model['isIndexApp'] = 0;
  57. $model->save();
  58. }
  59. /********************* 如果进行只能上线一个标请将我打开 ***********************
  60. //将同类型最近一个产品设置为满标
  61. $model = Model::where([
  62. 'subjectTypeID'=>$item['subjectTypeID'],
  63. 'BACKUPID'=>0,
  64. 'status'=>Model::STATUS_ONLINE,
  65. 'subjectID'=>['neq', $item['subjectID']]
  66. ])->order('addTime desc')->find();
  67. if(!$model) {
  68. continue;
  69. }
  70. //第0步,得到money
  71. $money = $model['price'] - $model->subjectStat['moneyTotalInvest'];
  72. if($money <= 0) {
  73. continue;
  74. }
  75. //第一步,得到一个虚拟账户
  76. $userList = User::where([
  77. 'isForged'=>1
  78. ])->field(['userID'])->select();
  79. if($userList->isEmpty()) {
  80. continue;
  81. }
  82. $userIDS = array_column($userList->toArray(), 'userID');
  83. $userIDIndex = mt_rand(0, count($userIDS) - 1);
  84. $userID = $userIDS[$userIDIndex];
  85. $user = User::get($userID);
  86. //第二步,保存一个仓
  87. $cang = \app\core\model\Cang::create([
  88. 'subjectID' => $model['subjectID'],
  89. 'userID' => $user['userID'],
  90. 'moneySubject' => $money,
  91. 'ben' => $money,
  92. 'status'=>\app\core\model\Cang::STATUS_PAY,
  93. 'isForged'=>1
  94. ]);
  95. $cang['alias'] = \app\core\model\Cang::createAlias($cang['cangID']);
  96. $cang->save();
  97. //第三步,对产品进行统计
  98. SubjectStat::where([
  99. 'subjectID'=>$model['subjectID']
  100. ])->setInc('moneyTotalInvest', $money * 100);
  101. SubjectStat::where([
  102. 'subjectID'=>$model['subjectID']
  103. ])->setInc('timesInvest');
  104. //第四步,将该标去掉首页符号
  105. $model['isIndexPc'] = 0;
  106. $model['isIndexApp'] = 0;
  107. $model->save();
  108. //第四步,设置为满标
  109. Model::setSubjectFull($model);
  110. ********************************** end *************************************/
  111. }
  112. });
  113. }
  114. private function setStatusOverTime() {
  115. Model::where([
  116. 'overTime'=>['lt', THINK_START_TIME],
  117. 'status'=>Model::STATUS_FULL
  118. ])->chunk(100, function($list) {
  119. foreach ($list as $item) {
  120. $item->status = Model::STATUS_OVERTIME;
  121. $item->save();
  122. }
  123. });
  124. }
  125. //随机虚拟认购
  126. private function setRandCreateCang() {
  127. Model::where([
  128. 'status'=>Model::STATUS_ONLINE,
  129. 'BACKUPID'=>0
  130. ])->chunk(100, function($list) {
  131. foreach ($list as $model) {
  132. //第0步,得到money
  133. $moneyStill = $model['price'] - $model->subjectStat['moneyTotalInvest'];
  134. if($moneyStill <= 0) {
  135. continue;
  136. }
  137. //$hour = date("H",time());
  138. $cang = \app\core\model\Cang::where([
  139. 'subjectID'=>$model['subjectID']
  140. ])->order('addTime desc')->find();
  141. if($cang && $cang->getData('addTime') + mt_rand(8000,12000) > THINK_START_TIME) {
  142. continue;
  143. }
  144. $moneyS = [1000, 1000, 1000, 2000, 5000, 10000];
  145. $index = mt_rand(0,5);
  146. $money = $moneyS[$index];//一次投2000
  147. if($moneyStill < $money) {
  148. $money = $moneyStill;
  149. }
  150. //第一步,得到一个虚拟账户
  151. $userList = User::where([
  152. 'isForged'=>1
  153. ])->field(['userID'])->select();
  154. if($userList->isEmpty()) {
  155. continue;
  156. }
  157. $userIDS = array_column($userList->toArray(), 'userID');
  158. $userIDIndex = mt_rand(0, count($userIDS) - 1);
  159. $userID = $userIDS[$userIDIndex];
  160. $user = User::get($userID);
  161. //第二步,保存一个仓
  162. $cang = \app\core\model\Cang::create([
  163. 'subjectID' => $model['subjectID'],
  164. 'userID' => $user['userID'],
  165. 'moneySubject' => $money,
  166. 'ben' => $money,
  167. 'status'=>\app\core\model\Cang::STATUS_PAY,
  168. 'isForged'=>1
  169. ]);
  170. $cang['alias'] = \app\core\model\Cang::createAlias($cang['cangID']);
  171. $cang->save();
  172. //第三步,对产品进行统计
  173. SubjectStat::where([
  174. 'subjectID'=>$model['subjectID']
  175. ])->setInc('moneyTotalInvest', $money * 100);
  176. SubjectStat::where([
  177. 'subjectID'=>$model['subjectID']
  178. ])->setInc('timesInvest');
  179. //第四步,判断是否满标
  180. if($model->subjectStat['moneyTotalInvest'] + $money == $model['price']) {
  181. //如果满标了,设置满标
  182. Model::setSubjectFull($model);
  183. }
  184. }
  185. });
  186. }
  187. }