BasicActiveDayDataReportLogic.php 2.9 KB

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