UpdateVipTimeChannel.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: wanggb
  5. * Date: 2019/9/24
  6. * Time: 14:13
  7. */
  8. namespace app\admin\command;
  9. use think\console\Command;
  10. use think\console\Input;
  11. use think\console\Output;
  12. use think\console\input\Argument;
  13. use think\Config;
  14. use think\Db;
  15. use think\Log;
  16. use think\Request;
  17. class UpdateVipTimeChannel extends Command
  18. {
  19. // 充值类型:1=看点充值,2=VIP充值,3=系统操作看点,4=系统操作vip,5=签到
  20. const VIP_RECHARGE = '2';
  21. const SYS_VIP_RECHARGE = '4';
  22. const PAGE_NUM = 2000; // 每次执行N条数据
  23. const DB_NUM = 512; //数据库个数
  24. protected function configure()
  25. {
  26. $this->setName('UpdateVipTimeChannel')
  27. ->addArgument('startDbNum', Argument::REQUIRED, "第几个数据库开始")
  28. ->addArgument('endDbNum', Argument::REQUIRED, "第几个数据库结束")
  29. ->setDescription('更新所有user库,Recharge表中的字段channel_vip_starttime,仅需要在项目上线时执行一次,此后不需要再执行,命令格式 php think UpdateVipTime startDbNum endDbNum');
  30. }
  31. protected function execute(Input $input, Output $output)
  32. {
  33. Request::instance()->module('admin');
  34. $startDbNum = $input->getArgument('startDbNum');
  35. $endDbNum = $input->getArgument('endDbNum');
  36. $output->writeln('开始数据库'.$startDbNum);
  37. $output->writeln('结束数据库'.$endDbNum);
  38. if($startDbNum >511 || $endDbNum > 511){
  39. $output->writeln('参数错误,数据库开始序号和结束序号不能大于511');
  40. die();
  41. }
  42. Log::info("开始刷channel_vip_starttime,开始时间:" . date("Y-m-d H:i:s", time()));
  43. for ($idx = $startDbNum; $idx <= $endDbNum; $idx++) {
  44. $output->writeln($idx);
  45. Log::info("***************************************************************************");
  46. $db = $this->dbConnect($idx);
  47. Log::info("数据库" . $db->getConfig('database') . "中的信息,开始执行");
  48. //$total 获取数据总个数
  49. $total = $this->getTotalNum($output, $db);
  50. Log::info("获取数据总个数:" . $total . "条数据");
  51. try {
  52. if ($total > 0) {
  53. $pagenum = self::PAGE_NUM;
  54. for ($i = 0; $i < $total / $pagenum; $i++) {
  55. $startNum = $i * $pagenum;
  56. $sql = "SELECT * FROM recharge WHERE `type` IN('" . self::VIP_RECHARGE . "' , '" . self::SYS_VIP_RECHARGE . "') AND dd = 0 ORDER BY id LIMIT " . $startNum . ", " . $pagenum;
  57. $result = $db->query($sql);
  58. if ($result) {
  59. foreach ($result as $key => $value) {
  60. $lastDataSql = "SELECT * FROM recharge WHERE user_id = " . $value['user_id'] . " AND `type` IN('" . self::VIP_RECHARGE . "' , '" . self::SYS_VIP_RECHARGE . "') AND id <" . $value['id'] . " AND dd = 0 ORDER BY id DESC LIMIT 1";
  61. $lastDataResult = $db->query($lastDataSql);
  62. if(!$lastDataResult){
  63. $update_sql = "UPDATE recharge SET channel_vip_starttime = createtime WHERE id = " . $value['id'];
  64. }else{
  65. $lastData = $lastDataResult[0];
  66. $days = intval($lastData['day']);
  67. $hours = intval($lastData['hour']);
  68. $target_vip_time = strtotime("+$days days +$hours hours", $lastData['channel_vip_starttime']);
  69. if($value['createtime'] > $target_vip_time){
  70. $update_sql = "UPDATE recharge SET channel_vip_starttime = createtime WHERE id = " . $value['id'];
  71. }else{
  72. $update_sql = "UPDATE recharge SET channel_vip_starttime = ".$target_vip_time." WHERE id = " . $value['id'];
  73. }
  74. }
  75. $db->query($update_sql);
  76. }
  77. }
  78. $output->writeln($db->getConfig('database') . "数据库,Recharge表中第" . $startNum . "到" . ($startNum + $pagenum) . "条数据更新完成");
  79. $output->writeln("======================");
  80. }
  81. $output->writeln($db->getConfig('database') . "数据库,Recharge表中数据更新完成");
  82. }
  83. $output->writeln($db->getConfig('database') . "数据库,Recharge表中没有数据,程序结束!");
  84. Log::info("数据库" . $db->getConfig('database') . "中的信息,顺利执行完成");
  85. } catch (\Exception $exception) {
  86. Log::error($exception->getMessage());
  87. Log::error("数据库" . $db->getConfig('database') . "中的信息,出错了,需要重新执行");
  88. }
  89. }
  90. Log::info("刷 channel_vip_starttime 完成,结束时间:" . date("Y-m-d H:i:s", time()));
  91. }
  92. // 获取recharge表中数据总条数
  93. private function getTotalNum(Output $output, $db)
  94. {
  95. $num_sql = "SELECT COUNT(*) as total FROM recharge WHERE type IN('".self::VIP_RECHARGE."', '".self::SYS_VIP_RECHARGE."') AND dd = 0 ";
  96. $num_result = $db->query($num_sql);
  97. return $num_result[0]['total'] ?? 0;
  98. }
  99. //链接数据库
  100. private function dbConnect($db_num)
  101. {
  102. Log::info("开始刷channel_vip_starttime,开始时间:".date("Y-m-d H:i:s", time()));
  103. $db_config = $this->charge_get_db_deploy($db_num);
  104. $db = Db::connect($db_config);
  105. return $db;
  106. }
  107. //分库 0-512
  108. private function charge_get_db_deploy($mod, $deploy = 'user')
  109. {
  110. $db = Config::get('db');
  111. $list = explode(';', $db[$deploy . '_list']);
  112. foreach ($list as $item) {
  113. $con = explode(':', $item); // 0=0-191库编号 1=192.168.1.149主IP 2=3306主端口 3=192.168.1.150从IP 4=3306从端口
  114. if (count($con) >= 3) {
  115. $c = explode('-', $con[0]); //库编号 0开始 1结束
  116. if (count($c) >= 2) {
  117. if ($c[0] <= $mod && $mod <= $c[1]) {
  118. $database = Config::get('database');
  119. if ($database['deploy'] == 1 && count($con) >= 5) { //开启主从 & 带主从配置
  120. $database['hostname'] = $con[1] . ',' . $con[3]; //192.168.1.149,192.168.1.150
  121. $database['hostport'] = $con[2] . ',' . $con[4]; //3306,3306
  122. } else { //只有主库
  123. $database['hostname'] = $con[1];
  124. $database['hostport'] = $con[2];
  125. }
  126. Log::info("分库获取成功 IP:{$database['hostname']} port: {$database['hostport']}");
  127. $database['database'] = str_replace('$mod', $mod, $db[$deploy . '_database']);
  128. return $database;
  129. }
  130. }
  131. }
  132. }
  133. Log::error("分库获取失败!");
  134. return [];
  135. }
  136. }