GameActiveDayLogic.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace app\v1\logic\tool;
  3. use support\think\Db;
  4. class GameActiveDayLogic
  5. {
  6. protected string $table = "base_total_day";
  7. protected string $group = 'game_id, agent_id, site_id';
  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. $hour = date('H');
  35. $i = date('i');
  36. // 重跑前3天
  37. if($hour==4 && $i<40){
  38. for ($d=1; $d<=3; $d++){
  39. $this->date = date('Y-m-d', strtotime("-{$d} day"));
  40. $this->initStart();
  41. }
  42. }
  43. }
  44. protected function initStart(): void
  45. {
  46. // 重置数据
  47. $this->data = [];
  48. $this->activeTotalData();
  49. $this->siteAuth();
  50. $this->replaceData();
  51. }
  52. protected function activeTotalData()
  53. {
  54. $tb = "basic_login_total_game_" . date('Y', strtotime($this->date));
  55. $filed = "{$this->group}, active as days, sum(login_count) as active_total";
  56. $where = [
  57. "tdate" => $this->date,
  58. ];
  59. $result = Db::connect('db_game_log')->table($tb)->field($filed)->where($where)->group("$this->group, days")->select();
  60. $this->pushData($result);
  61. }
  62. protected function siteAuth(): void
  63. {
  64. $agentList = Db::connect('db_advert')->table("agent")->column("id, media_id, auth_id");
  65. $agentListMap = array_column($agentList, null, "id");
  66. if($this->data){
  67. foreach ($this->data as &$item){
  68. $item['tdate'] = $this->date;
  69. $item['auth_id'] = $agentListMap[$item['agent_id']]['auth_id'] ?? 0; // 负责人
  70. $item['media_id'] = $agentListMap[$item['agent_id']]['media_id'] ?? 0; // 媒体
  71. }
  72. }
  73. }
  74. protected function replaceData(): bool
  75. {
  76. // 先删除,再写入
  77. Db::connect('db_data_report')->table($this->table)->where(['tdate' => $this->date])->delete();
  78. return Db::connect('db_data_report')->table($this->table)->insertAll($this->data) !== false;
  79. }
  80. protected function pushData($rows): void
  81. {
  82. if ($rows) {
  83. foreach ($rows as $row) {
  84. $this->group = $this->group . ", days";
  85. $groupArr = explode(',', $this->group);
  86. $uniqueKey = "";
  87. foreach ($groupArr as $groupKey){
  88. $uniqueKey .= $row[$groupKey] . "-";
  89. }
  90. foreach ($row as $k => $v) {
  91. $this->data[$uniqueKey][$k] = !empty($this->data[$uniqueKey][$k]) ? $this->data[$uniqueKey][$k] + $v :$v;
  92. }
  93. }
  94. }
  95. }
  96. }