BasicActiveDayDataReportLogic.php 2.9 KB

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