GameRegPayDayDataReportLogic.php 3.6 KB

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