GameRegPayMonthLogic.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace app\v1\logic\tool;
  3. use support\think\Db;
  4. class GameRegPayMonthLogic
  5. {
  6. protected string $table = "game_reg_pay_month";
  7. protected string $group = 'game_id, agent_id, site_id';
  8. protected array $data;
  9. protected string $month;
  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');
  17. $eDate = date('Y-m');
  18. }
  19. for ($date = $sDate; $date <= $eDate; $date = date('Y-m', strtotime($date . '+1 month'))){
  20. $this->month = $date;
  21. try {
  22. $this->initStart();
  23. }catch (\Exception $e){
  24. return json_encode(["status"=>"error", "msg"=>$e->getMessage()], 256);
  25. }
  26. }
  27. $this->reRun();
  28. return json_encode(["status"=>"success", "msg"=>""], 256);
  29. }
  30. // 重跑
  31. protected function reRun(): void
  32. {
  33. $day = date('d');
  34. $hour = date('H');
  35. $i = date('i');
  36. // 重跑上个整月
  37. if(($day==1 || $day==15) && $hour==4 && $i<10){
  38. $this->month = date('Y-m', strtotime("-1 month"));
  39. $this->initStart();
  40. }
  41. }
  42. protected function initStart(): void
  43. {
  44. // 重置数据
  45. $this->data = [];
  46. $this->regPay();
  47. $this->siteAuth();
  48. $this->replaceData();
  49. }
  50. protected function regPay(): void
  51. {
  52. $tb = "sdk_order_success";
  53. $sDate = "{$this->month}-01 00:00:00";
  54. $eDate = date('Y-m-t 23:59:59', strtotime($this->month));
  55. $filed = "{$this->group}, DATE_FORMAT(reg_date,'%Y-%m') as reg_month, sum(money) as pay_total, sum(paid_amount) as pay_amount, count(distinct uid) as pay_num";
  56. $where = [
  57. ['pay_date', 'between', [$sDate, $eDate]],
  58. ];
  59. $result = Db::connect('db_game_log')->table($tb)->field($filed)->where($where)->group("{$this->group}, DATE_FORMAT(reg_date,'%Y-%m')")->select()->toArray();
  60. $this->pushData($result);
  61. $regMonthList = array_column($result, 'reg_month');
  62. foreach ($regMonthList as $regMonth){
  63. $res = $this->addupTotalMonth($regMonth);
  64. if($res) $this->pushData($res);
  65. }
  66. }
  67. protected function addupTotalMonth($regMonth)
  68. {
  69. $tb = "sdk_order_success";
  70. $filed = "{$this->group},$regMonth as reg_month, sum(money) as addup_pay_total, sum(paid_amount) as addup_pay_amount, count(distinct uid) as addup_pay_num";
  71. $srData = $regMonth."-01 00:00:00";
  72. $erDate = date('Y-m-t 23:59:59', strtotime($srData));
  73. $epDate = date('Y-m-t 23:59:59', strtotime($this->month));
  74. $where = array(
  75. 'reg_date' => ['between', [$srData, $erDate]],
  76. 'pay_date' => ['between', [$srData, $epDate]],
  77. );
  78. return Db::connect('db_game_log')->table($tb)->field($filed)->where($where)->group_by($this->group.", reg_month")->select();
  79. }
  80. protected function siteAuth(): void
  81. {
  82. $agentList = Db::connect('db_advert')->table("agent")->column("id, media_id, auth_id");
  83. $agentListMap = array_column($agentList, null, "id");
  84. if($this->data){
  85. foreach ($this->data as &$item){
  86. $item['tdate'] = $this->month;
  87. $item['auth_id'] = $agentListMap[$item['agent_id']]['auth_id'] ?? 0; // 负责人
  88. $item['media_id'] = $agentListMap[$item['agent_id']]['media_id'] ?? 0; // 媒体
  89. }
  90. }
  91. }
  92. protected function replaceData(): bool
  93. {
  94. // 先删除,再写入
  95. Db::connect('db_data_report')->table($this->table)->where(['reg_month' => $this->month, 'pay_month'=>$this->month])->delete();
  96. return Db::connect('db_data_report')->table($this->table)->insertAll($this->data) !== false;
  97. }
  98. protected function pushData($rows): void
  99. {
  100. if ($rows) {
  101. foreach ($rows as $row) {
  102. $groupArr = explode(',', $this->group);
  103. $uniqueKey = "";
  104. foreach ($groupArr as $groupKey){
  105. $uniqueKey .= $row[$groupKey] . "-";
  106. }
  107. foreach ($row as $k => $v) {
  108. $this->data[$uniqueKey][$k] = !empty($this->data[$uniqueKey][$k]) ? $this->data[$uniqueKey][$k] + $v :$v;
  109. }
  110. }
  111. }
  112. }
  113. }