BasicActiveDayLogic.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace app\v1\logic\tool;
  3. use support\think\Db;
  4. class BasicActiveDayLogic extends BaseLogic
  5. {
  6. protected string $table = "basic_active_day";
  7. protected string $group = 'game_id, agent_id, site_id, tdate, active';
  8. protected array $data;
  9. protected string $date;
  10. public function run($params=[])
  11. {
  12. if(!empty($params['date'])){
  13. $sDate = is_array($params['date']) ? $params['date'][0] : $params['date'];
  14. $eDate = is_array($params['date']) ? $params['date'][1] : $params['date'];
  15. }else{
  16. $sDate = date('Y-m-d');
  17. $eDate = date('Y-m-d');
  18. }
  19. for ($date = $sDate; $date <= $eDate; $date = date('Y-m-d', strtotime($date . '+1 day'))){
  20. $this->date = $date;
  21. $this->table = $this->table . "_" . date('Y', strtotime($this->date));
  22. try {
  23. $this->initStart();
  24. }catch (\Exception $e){
  25. return json_encode(["status"=>"error", "msg"=>$e->getMessage()], 256);
  26. }
  27. }
  28. $this->reRun();
  29. return json_encode(["status"=>"success", "msg"=>""], 256);
  30. }
  31. // 重跑
  32. protected function reRun(): void
  33. {
  34. $day = date('d');
  35. $hour = date('H');
  36. $i = date('i');
  37. $t = date('t');
  38. // 月末,重跑整月
  39. if($day==$t){
  40. if($hour==1 && $i<10){
  41. for ($d=1; $d<=$t; $d++){
  42. $this->date = date('Y-m-d', strtotime("-{$d} day"));
  43. $this->initStart();
  44. }
  45. }
  46. }else{
  47. // 重跑前3天
  48. if($hour==8 && $i<10){
  49. for ($d=1; $d<=3; $d++){
  50. $this->date = date('Y-m-d', strtotime("-{$d} day"));
  51. $this->initStart();
  52. }
  53. }
  54. }
  55. }
  56. protected function initStart(): void
  57. {
  58. // 重置数据
  59. $this->data = [];
  60. $this->loginTotalByGame();
  61. $this->replaceData(['tdate'=>$this->date]);
  62. }
  63. protected function loginTotalByGame()
  64. {
  65. $table = "sdk_active_log_" . date('Ym', strtotime($this->date));
  66. $filed = "game_id, agent_id, site_id, {$this->date} as tdate, active, count(distinct uid) as login_count";
  67. $where = [
  68. 'tdate' => $this->date,
  69. ];
  70. $result = Db::connect('db_game_log')->table($table)->field($filed)->where($where)->group($this->group)->select()->toArray();
  71. if($result) foreach ($result as &$vv){
  72. $vv['reg_date'] = date('Y-m-d', strtotime("{$this->date} -{$vv['days']}days"));
  73. }
  74. $this->pushData($result);
  75. }
  76. }