GameRegPayMonthDataReportLogic.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace app\v1\logic\tool\dataReport;
  3. use support\think\Db;
  4. class GameRegPayMonthDataReportLogic extends BaseDataReportLogic
  5. {
  6. protected string $table = "game_reg_pay_month";
  7. protected string $group = 'game_id, agent_id, site_id, reg_month, pay_month';
  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 = !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');
  21. $eDate = date('Y-m');
  22. }
  23. for ($date = $sDate; $date <= $eDate; $date = date('Y-m', strtotime($date . '+1 month'))){
  24. $this->month = $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->month = date('Y-m', strtotime("-1 month"));
  43. $this->initStart();
  44. }
  45. }
  46. protected function initStart(): void
  47. {
  48. // 重置数据
  49. $this->data = [];
  50. $this->regPay();
  51. $this->replaceData(['reg_month'=>$this->month]);
  52. }
  53. protected function regPay(): void
  54. {
  55. $tb = "sdk_order_success";
  56. $sDate = "{$this->month}-01 00:00:00";
  57. $eDate = date('Y-m-t 23:59:59', strtotime($this->month));
  58. $filed = "game_id, agent_id, site_id, '{$this->month}' as pay_month, 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";
  59. $where = [
  60. ['pay_date', 'between', [$sDate, $eDate]],
  61. ];
  62. $result = Db::connect('db_game_log')->table($tb)->field($filed)->where($where)->group($this->group)->select()->toArray();
  63. if($result){
  64. $this->pushData($result);
  65. $regMonthList = array_unique(array_column($result, 'reg_month'));
  66. foreach ($regMonthList as $regMonth){
  67. $res = $this->addupTotalMonth($regMonth);
  68. if($res){
  69. $this->pushData($res);
  70. }
  71. }
  72. }
  73. }
  74. protected function addupTotalMonth($regMonth)
  75. {
  76. $tb = "sdk_order_success";
  77. $filed = "game_id, agent_id, site_id, '{$this->month}' as pay_month,'{$regMonth}' as reg_month, sum(money) as addup_pay_total, sum(paid_amount) as addup_pay_amount, count(distinct uid) as addup_pay_num";
  78. $srData = $regMonth."-01 00:00:00";
  79. $erDate = date('Y-m-t 23:59:59', strtotime($srData));
  80. $epDate = date('Y-m-t 23:59:59', strtotime($this->month));
  81. $where = [
  82. ['reg_date', 'between', [$srData, $erDate]],
  83. ['pay_date', 'between', [$srData, $epDate]],
  84. ];
  85. return Db::connect('db_game_log')->table($tb)->field($filed)->where($where)->group($this->group)->select()->toArray();
  86. }
  87. }