GameRegPayDayDataReportLogic.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace app\v1\logic\tool\dataReport;
  3. use support\think\Db;
  4. class GameRegPayDayDataReportLogic extends BaseDataReportLogic
  5. {
  6. protected string $table = "game_reg_pay_day";
  7. protected string $group = 'game_id, agent_id, site_id, reg_date, pay_date';
  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. // 重跑上个
  41. if(($day==1 || $day==15) && $hour==4 && $i<10){
  42. $this->date = date('Y-m-d', strtotime("-1 days"));
  43. $this->initStart();
  44. }
  45. }
  46. protected function initStart(): void
  47. {
  48. $this->table = $this->table . "_" . date('Y', strtotime($this->date));
  49. // 重置数据
  50. $this->data = [];
  51. $this->regPay();
  52. $this->replaceData(['reg_date'=>$this->date]);
  53. }
  54. protected function regPay(): void
  55. {
  56. $tb = "sdk_order_success";
  57. $sDate = "{$this->date} 00:00:00";
  58. $eDate = date('Y-m-d 23:59:59', strtotime($this->date));
  59. $filed = "game_id, agent_id, site_id, '{$this->date}' as pay_date, DATE_FORMAT(reg_date,'%Y-%m-%d') as reg_date, sum(money) as pay_total, sum(paid_amount) as pay_amount, count(distinct uid) as pay_num";
  60. $where = [
  61. ['pay_date', 'between', [$sDate, $eDate]],
  62. ];
  63. $result = Db::connect('db_game_log')->table($tb)->field($filed)->where($where)->group($this->group)->select()->toArray();
  64. if($result){
  65. $this->pushData($result);
  66. $regDateList = array_unique(array_column($result, 'reg_date'));
  67. foreach ($regDateList as $regDate){
  68. $res = $this->addupTotalDay($regDate);
  69. if($res){
  70. $this->pushData($res);
  71. }
  72. }
  73. }
  74. }
  75. protected function addupTotalDay($regDate)
  76. {
  77. $tb = "sdk_order_success";
  78. $filed = "game_id, agent_id, site_id, '{$this->date}' as pay_date,'{$regDate}' as reg_date, sum(money) as addup_pay_total, sum(paid_amount) as addup_pay_amount, count(distinct uid) as addup_pay_num";
  79. $srData = $regDate." 00:00:00";
  80. $erDate = date('Y-m-d 23:59:59', strtotime($srData));
  81. $epDate = date('Y-m-d 23:59:59', strtotime($this->date));
  82. $where = [
  83. ['reg_date', 'between', [$srData, $erDate]],
  84. ['pay_date', 'between', [$srData, $epDate]],
  85. ];
  86. return Db::connect('db_game_log')->table($tb)->field($filed)->where($where)->group($this->group)->select()->toArray();
  87. }
  88. }