GameRegPayMonthLogic.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace app\v1\logic\tool;
  3. use support\think\Db;
  4. class GameRegPayMonthLogic extends BaseLogic
  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 = 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->replaceData(['reg_month'=>$this->month]);
  48. }
  49. protected function regPay(): void
  50. {
  51. $tb = "sdk_order_success";
  52. $sDate = "{$this->month}-01 00:00:00";
  53. $eDate = date('Y-m-t 23:59:59', strtotime($this->month));
  54. $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";
  55. $where = [
  56. ['pay_date', 'between', [$sDate, $eDate]],
  57. ];
  58. $result = Db::connect('db_game_log')->table($tb)->field($filed)->where($where)->group($this->group)->select()->toArray();
  59. if($result){
  60. $this->pushData($result);
  61. $regMonthList = array_unique(array_column($result, 'reg_month'));
  62. foreach ($regMonthList as $regMonth){
  63. $res = $this->addupTotalMonth($regMonth);
  64. if($res){
  65. $this->pushData($res);
  66. }
  67. }
  68. }
  69. }
  70. protected function addupTotalMonth($regMonth)
  71. {
  72. $tb = "sdk_order_success";
  73. $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";
  74. $srData = $regMonth."-01 00:00:00";
  75. $erDate = date('Y-m-t 23:59:59', strtotime($srData));
  76. $epDate = date('Y-m-t 23:59:59', strtotime($this->month));
  77. $where = [
  78. ['reg_date', 'between', [$srData, $erDate]],
  79. ['pay_date', 'between', [$srData, $epDate]],
  80. ];
  81. return Db::connect('db_game_log')->table($tb)->field($filed)->where($where)->group($this->group)->select()->toArray();
  82. }
  83. }