CampaignStatistics.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace app\admin\command;
  3. use think\Config;
  4. use think\console\Command;
  5. use think\console\Input;
  6. use think\console\input\Argument;
  7. use think\console\Output;
  8. use think\Db;
  9. use think\Log;
  10. use think\Request;
  11. /**
  12. * 消耗活动统计订单的脚本(废弃-订单表将迁移至polardb)
  13. * Class CampaignStatistics
  14. * @package app\admin\command
  15. */
  16. class CampaignStatistics extends Command
  17. {
  18. /**
  19. * 配置指令
  20. */
  21. protected function configure()
  22. {
  23. $this->setName('CampaignStatistics')
  24. ->addArgument('date', Argument::REQUIRED, "时间");
  25. }
  26. protected function execute(Input $input, Output $output)
  27. {
  28. $date = $input->getArgument('date');
  29. //cli模式下无法获取到当前的项目模块,手动指定一下
  30. Request::instance()->module('admin');
  31. if ( !$date ){
  32. $date = date( 'Ymd', time()-3600*24*8 );
  33. }
  34. $sql = "select user_id,money,FROM_UNIXTIME(createtime) as ordertime,createtime from orders_extend e JOIN orders o on e.order_id=o.id where finishtime>0 and FROM_UNIXTIME(createtime,'%Y%m%d')=".$date;
  35. $data = model('orders_extend')->query($sql);
  36. $title = 'user_id,money,ordertime,ordertime,isCheckIn,isCharge'.PHP_EOL;
  37. file_put_contents(LOG_PATH.'/order'.$date.'.csv',$title,FILE_APPEND);
  38. if ( !empty($data) ){
  39. foreach ($data as $k=>$v){
  40. $str = '';
  41. $userMatch = $this->dbConnect($v['user_id'],'user')->table('user_match')
  42. ->field('id,createtime')
  43. ->where('user_id','eq',$v['user_id'] )
  44. ->where('match_date','eq',$date )
  45. ->find();
  46. $recharge = $this->dbConnect($v['user_id'],'shard')->table('consume')->where('createtime','<',$v['createtime'])->find();
  47. $v['isCheckIn'] = empty($userMatch) ? 0 : $userMatch['createtime'];
  48. $v['isRcharge'] = empty($recharge) ? 0 : date('Y-m-d H:i:s',$recharge['createtime'] );
  49. foreach($v as $kk =>$vv ){
  50. $str .= $vv.',';
  51. }
  52. $str = trim($str,',').PHP_EOL;
  53. file_put_contents(LOG_PATH.'/order'.$date.'.csv',$str,FILE_APPEND);
  54. log::info('消耗活动订单和报名对比:'.json_encode($v));
  55. }
  56. echo 'success';
  57. }else{
  58. echo '没有消耗活动类型订单';
  59. }
  60. }
  61. /**
  62. * 获取数据库连接
  63. * @param $param 编号
  64. * @param string $deploy 业务
  65. * @return \think\db\Connection
  66. * @throws \think\Exception
  67. */
  68. private function dbConnect($param, $deploy)
  69. {
  70. $db_config = $this->get_db_deploy($param, $deploy);
  71. if (empty($this->dbConnects[$db_config['database']])) {
  72. Log::info(sprintf('打开数据库连接,database:%s', $db_config['database']));
  73. $this->dbConnects[$db_config['database']] = Db::connect($db_config);
  74. }
  75. return $this->dbConnects[$db_config['database']];
  76. }
  77. /**
  78. * 关闭数据库连接
  79. */
  80. private function closeDbConnect()
  81. {
  82. foreach ($this->dbConnects as $database => $dbConnect) {
  83. Log::info(sprintf('关闭数据库连接,database:%s', $database));
  84. $dbConnect->close();
  85. }
  86. $this->dbConnects = [];
  87. }
  88. /**
  89. * 获取db分库的配置参数
  90. *
  91. * @param string|int $param 取模值
  92. * @param string $deploy 分库前缀
  93. * @return array
  94. */
  95. function get_db_deploy($param, $deploy = 'shard')
  96. {
  97. $db = Config::get('db');
  98. $mod = $param % $db[$deploy . '_num'];
  99. $mod = abs($mod);
  100. $list = explode(';', $db[$deploy . '_list']);
  101. foreach ($list as $item) {
  102. $con = explode(':', $item); // 0=0-191库编号 1=192.168.1.149主IP 2=3306主端口 3=192.168.1.150从IP 4=3306从端口
  103. if (count($con) >= 3) {
  104. $c = explode('-', $con[0]); //库编号 0开始 1结束
  105. if (count($c) >= 2) {
  106. if ($c[0] <= $mod && $mod <= $c[1]) {
  107. $database = Config::get('database');
  108. if ($database['deploy'] == 1 && count($con) >= 5) { //开启主从 & 带主从配置
  109. $database['hostname'] = $con[1] . ',' . $con[3]; //192.168.1.149,192.168.1.150
  110. $database['hostport'] = $con[2] . ',' . $con[4]; //3306,3306
  111. } else { //只有主库
  112. $database['hostname'] = $con[1];
  113. $database['hostport'] = $con[2];
  114. }
  115. $database['database'] = str_replace('$mod', $mod, $db[$deploy . '_database']);
  116. return $database;
  117. }
  118. }
  119. }
  120. }
  121. Log::error("分库获取失败!");
  122. return [];
  123. }
  124. }