UpdateTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * Created by: PhpStorm
  4. * User: lytian
  5. * Date: 2019/12/27
  6. * Time: 16:54
  7. */
  8. namespace app\admin\command;
  9. use think\Config;
  10. use think\console\Command;
  11. use think\console\Input;
  12. use think\console\input\Argument;
  13. use think\console\Output;
  14. use think\Db;
  15. use think\Request;
  16. class UpdateTest extends Command
  17. {
  18. public function Configure()
  19. {
  20. $this->setName('UpdateTest')
  21. ->setDescription('清空consume表consume_info字段')
  22. ->addArgument('beginNum', Argument::REQUIRED, '数据库得开始编号')
  23. ->addArgument('endNum', Argument::REQUIRED, '数据库结束编号');
  24. }
  25. private $beginNum;
  26. private $endNum;
  27. public function execute(Input $input, Output $output)
  28. {
  29. Request::instance()->module('admin');
  30. $this->beginNum = $input->getArgument('beginNum');
  31. $this->endNum = $input->getArgument('endNum');
  32. $db = Config::get('db');
  33. $mod = $db['shard_num'];
  34. while ($this->beginNum <= $this->endNum) {
  35. //连接shard库
  36. $shardDbConfig = $this->getDbDeploy($this->beginNum, 'shard');
  37. $consumeTable = $shardDbConfig['table'].'.consume';
  38. unset($shardDbConfig['table']);
  39. //查询最小的id
  40. $sql = "SELECT min(id) as first_id, max(id) as last_id FROM {$consumeTable}";
  41. $row = Db::connect($shardDbConfig)->query($sql);
  42. $start = $row[0]['first_id'];
  43. $last = $row[0]['last_id'];
  44. $do = true;
  45. $userId = $this->beginNum + (int)$mod;
  46. model("Consume")
  47. ->setConnect($userId)
  48. ->update(['consume_info' => null], ['id' => $start]);
  49. while ($do) {
  50. $offset = $start + 10;
  51. $where = [
  52. 'id' => ['between', [$start+1, $offset]],
  53. 'consume_info' => ['exp', 'is not null'],
  54. ];
  55. model("Consume")
  56. ->setConnect($userId)
  57. ->update(['consume_info' => null], $where);
  58. if ($start >= $last) {
  59. $do = false;
  60. }
  61. $start += 10;
  62. }
  63. $this->beginNum++;
  64. }
  65. }
  66. /**
  67. * 从库配置
  68. *
  69. * @param $param
  70. * @param string $deploy
  71. * @return array|mixed
  72. */
  73. private function getDbDeploy($param, $deploy = 'shard')
  74. {
  75. $db = Config::get('db');
  76. $mod = $param % $db[$deploy . '_num'];
  77. $mod = abs($mod);
  78. $list = explode(';', $db[$deploy . '_list']);
  79. foreach ($list as $item) {
  80. $con = explode(':', $item); // 0=0-191库编号 1=192.168.1.149主IP 2=3306主端口 3=192.168.1.150从IP 4=3306从端口
  81. if (count($con) >= 3) {
  82. $c = explode('-', $con[0]); //库编号 0开始 1结束
  83. if (count($c) >= 2) {
  84. if ($c[0] <= $mod && $mod <= $c[1]) {
  85. $database = Config::get('database');
  86. if (count($con) >= 5) { //开启主从 & 带主从配置
  87. $database['deploy'] = 1;
  88. $database['rw_separate'] = true;
  89. $database['hostname'] = $con[1] . ',' . $con[3]; //192.168.1.149,192.168.1.150
  90. $database['hostport'] = $con[2] . ',' . $con[4]; //3306,3306
  91. } else { //只有主库
  92. $database['hostname'] = $con[1];
  93. $database['hostport'] = $con[2];
  94. }
  95. $database['database'] = 'mysql';
  96. $database['table'] = str_replace('$mod', $mod, $db[$deploy . '_database']);
  97. return $database;
  98. }
  99. }
  100. }
  101. }
  102. return [];
  103. }
  104. }