GameRegPayDayLogic.php 3.3 KB

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