BasicLoginTotalLogic.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace app\v1\logic\tool;
  3. use support\think\Db;
  4. class BasicLoginTotalLogic
  5. {
  6. protected string $table = "basic_login_total";
  7. protected string $group = 'game_id, agent_id, site_id, 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->siteAuth();
  62. $this->replaceData();
  63. }
  64. protected function loginTotalByGame()
  65. {
  66. $table = "sdk_active_log_" . date('Ym', strtotime($this->date));
  67. $filed = "{$this->group}, count(distinct uid) as login_count";
  68. $where = [
  69. 'tdate' => $this->date,
  70. ];
  71. $result = Db::connect('db_game_log')->table($table)->field($filed)->where($where)->group($this->group)->select();
  72. $this->pushData($result);
  73. }
  74. protected function siteAuth(): void
  75. {
  76. $agentList = Db::connect('db_advert')->table("agent")->column("id, media_id, auth_id");
  77. $agentListMap = array_column($agentList, null, "id");
  78. if($this->data){
  79. foreach ($this->data as &$item){
  80. $item['tdate'] = $this->date;
  81. $item['auth_id'] = $agentListMap[$item['agent_id']]['auth_id'] ?? 0; // 负责人
  82. $item['media_id'] = $agentListMap[$item['agent_id']]['media_id'] ?? 0; // 媒体
  83. }
  84. }
  85. }
  86. protected function replaceData(): bool
  87. {
  88. // 先删除,再写入
  89. Db::connect('db_data_report')->table($this->table)->where(['tdate' => $this->date])->delete();
  90. return Db::connect('db_data_report')->table($this->table)->insertAll($this->data) !== false;
  91. }
  92. protected function pushData($rows): void
  93. {
  94. if ($rows) {
  95. foreach ($rows as $row) {
  96. $groupArr = explode(',', $this->group);
  97. $uniqueKey = "";
  98. foreach ($groupArr as $groupKey){
  99. $uniqueKey .= $row[$groupKey] . "-";
  100. }
  101. foreach ($row as $k => $v) {
  102. $this->data[$uniqueKey][$k] = !empty($this->data[$uniqueKey][$k]) ? $this->data[$uniqueKey][$k] + $v :$v;
  103. }
  104. }
  105. }
  106. }
  107. }