UserDdFlushRecharge.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * Created by: PhpStorm
  4. * User: lytian
  5. * Date: 2019/9/23
  6. * Time: 16:43
  7. */
  8. namespace app\admin\command;
  9. use app\common\library\Redis;
  10. use app\main\service\UserDdFlushService;
  11. use think\Config;
  12. use think\console\Command;
  13. use think\console\Input;
  14. use think\console\input\Option;
  15. use think\console\Output;
  16. use think\Exception;
  17. use think\Log;
  18. use think\Request;
  19. /**
  20. * 刷新用户的剩余永久书币 刷扣量的历史数据
  21. * Class UserDdFlushRecharge
  22. * @package app\admin\command
  23. */
  24. class UserDdFlushRecharge extends Command
  25. {
  26. public function configure()
  27. {
  28. $this->setName('UserDdFlushRecharge')
  29. ->addOption('db_index', 'd', Option::VALUE_REQUIRED, 'user库索引 0-511', null)
  30. ->setDescription('刷新用户充值记录剩余永久书币');
  31. }
  32. public function execute(Input $input, Output $output)
  33. {
  34. Request::instance()->module('admin');
  35. $dbIndex = $input->getOption('db_index');
  36. $num = Config::get('db.user_num');
  37. $redis = Redis::instance();
  38. try {
  39. if ($dbIndex == null) {
  40. //没传即全部
  41. $end = $num - 1;
  42. $dbIndex = "0,{$end}";
  43. }
  44. $dbIndexs = explode(',', $dbIndex);
  45. if (count($dbIndexs) !=2) {
  46. //只传开始值
  47. $begin = $end = $dbIndexs[0];
  48. } else {
  49. list($begin, $end) = $dbIndexs;
  50. }
  51. $begin += $num;
  52. $end += $num;
  53. while ($begin <= $end) {
  54. $total = model("User")->setConnect($begin)->where('flush_state', 'eq', 0)->count();
  55. $gone = 0;
  56. model("User")->setConnect($begin)->where('flush_state', 'eq', 0)->chunk(100, function ($rows) use ($redis, $begin, $total, &$gone) {
  57. if (count($rows) > 0) {
  58. $gone += count($rows);
  59. foreach ($rows as $row) {
  60. //需要刷充值记录表剩余多少永久书币
  61. if ($row['kandian'] > 0) {
  62. UserDdFlushService::instance()->flushUserRechargeRemainKandian($row['id'], $row['kandian']);
  63. Log::info("刷新永久书币 用户{$row['id']} 刷新脚本状态成功");
  64. }
  65. $row->save(['flush_state' => 1]);
  66. //清理缓存
  67. $redis->del("UN:".$row['id']);
  68. }
  69. $perent = round($gone / $total, 4) * 100;
  70. $index = $begin - 512;
  71. echo "刷新永久书币 用户库ID:{$index}, 运行了{$perent}%, " . time() . PHP_EOL;
  72. unset($rows);
  73. } else {
  74. return false;
  75. }
  76. }, 'id', 'asc');
  77. $begin++;
  78. }
  79. } catch (Exception $e) {
  80. Log::info("刷新永久书币 异常:". $e->getMessage());
  81. $output->write("刷新永久书币 异常:". $e->getMessage());
  82. }
  83. $output->write("刷新永久书币 结束");
  84. }
  85. }