ControlRecordLogic.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace App\Http\logic\admin;
  3. use App\Http\helper\NumConfig;
  4. use App\Models\ControlRecord;
  5. use Illuminate\Support\Facades\DB;
  6. class ControlRecordLogic
  7. {
  8. protected $TPKindIDS = [1005];
  9. protected $RMKindIDS = [2030, 2050];
  10. public function monitor($start_time, $end_time, $date, $excel)
  11. {
  12. switch ($date) {
  13. case 1:
  14. $start_time = date('Y-m-d');
  15. $end_time = date('Y-m-d');
  16. break;
  17. case 2:
  18. $start_time = date('Y-m-d', strtotime('-1 day'));
  19. $end_time = date('Y-m-d');
  20. break;
  21. case 3:
  22. $start_time = date('Y-m-d', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600));
  23. $end_time = date('Y-m-d');
  24. break;
  25. case 4:
  26. $start_time = date('Y-m-01');
  27. $end_time = date('Y-m-d');
  28. break;
  29. }
  30. $db = DB::connection('read')->table('QPTreasureDB.dbo.GameControlPool');
  31. if (empty($start_time) && empty($end_time)) {
  32. // 总库存
  33. $total = DB::connection('read')->table('QPTreasureDB.dbo.GameControlPool')
  34. ->selectRaw('IsNull(sum(TotalPool),0) TotalPool,IsNull(sum(TotalIncome),0) TotalIncome,IsNull(sum(TotalExpend),0) TotalExpend')
  35. ->first();
  36. // TP单控池总库存
  37. $tp = $db->whereIn('KindID', $this->TPKindIDS)
  38. ->selectRaw('IsNull(sum(TotalPool),0) TotalPool,IsNull(sum(TotalIncome),0) TotalIncome,IsNull(sum(TotalExpend),0) TotalExpend')
  39. ->first();
  40. // RM单控池总库存
  41. $rm = $db->whereIn('KindID', $this->RMKindIDS)
  42. ->selectRaw('IsNull(sum(TotalPool),0) TotalPool,IsNull(sum(TotalIncome),0) TotalIncome,IsNull(sum(TotalExpend),0) TotalExpend')
  43. ->first();
  44. foreach ($tp as &$val) {
  45. $val = number_float($val / 100);
  46. }
  47. foreach ($rm as &$val) {
  48. $val = number_float($val / 100);
  49. }
  50. } else {
  51. $where = [];
  52. !empty($start_time) && $where[] = ['ControlDate', '>=', $start_time . ' 00:00:00'];
  53. !empty($end_time) && $where[] = ['ControlDate', '<=', $end_time . ' 23:59:59'];
  54. $db = DB::connection('read')->table('QPRecordDB.dbo.RecordUserScoreControl');
  55. // TP 单控池库存
  56. $tp = $db->whereIn('KindID', $this->TPKindIDS)
  57. ->where($where)
  58. ->selectRaw('IsNull(sum(EffectiveScore),0) TotalPool')
  59. ->first();
  60. $tp_totalIncome = $db->whereIn('KindID', $this->TPKindIDS)
  61. ->where($where)
  62. ->where('EffectiveScore', '<', 0)
  63. ->selectRaw('IsNull(sum(EffectiveScore),0) TotalIncome')
  64. ->first()->TotalIncome ?? 0;
  65. $tp_totalExpend = $db->whereIn('KindID', $this->TPKindIDS)
  66. ->where($where)
  67. ->where('EffectiveScore', '>', 0)
  68. ->selectRaw('IsNull(sum(EffectiveScore),0) TotalIncome')
  69. ->first()->TotalIncome ?? 0;
  70. // RM单控池库存
  71. $rm = $db->whereIn('KindID', $this->RMKindIDS)
  72. ->where($where)
  73. ->selectRaw('IsNull(sum(EffectiveScore),0) TotalPool')
  74. ->first();
  75. $rm_totalIncome = $db->whereIn('KindID', $this->RMKindIDS)
  76. ->where($where)
  77. ->where('EffectiveScore', '<', 0)
  78. ->selectRaw('IsNull(sum(EffectiveScore),0) TotalIncome')
  79. ->first()->TotalIncome ?? 0;
  80. $rm_totalExpend = $db->whereIn('KindID', $this->RMKindIDS)
  81. ->where($where)
  82. ->where('EffectiveScore', '>', 0)
  83. ->selectRaw('IsNull(sum(EffectiveScore),0) TotalExpend')
  84. ->first()->TotalExpend ?? 0;
  85. $rm->TotalPool = $this->return_val($rm->TotalPool);
  86. $rm->TotalIncome = $this->return_val($rm_totalIncome);
  87. $rm->TotalExpend = $this->return_val($rm_totalExpend);
  88. $tp->TotalPool = $this->return_val($tp->TotalPool);
  89. $tp->TotalIncome = $this->return_val($tp_totalIncome);
  90. $tp->TotalExpend = $this->return_val($tp_totalExpend);
  91. // 总库存
  92. $total = new \stdClass();
  93. $total->TotalPool = $rm->TotalPool + $tp->TotalPool;
  94. $total->TotalIncome = $rm->TotalIncome + $tp->TotalIncome;
  95. $total->TotalExpend = $rm->TotalExpend + $tp->TotalExpend;
  96. }
  97. if ($excel) {
  98. $list = [];
  99. $list[] = [
  100. 'TotalPool' => $total->TotalPool,
  101. 'TotalIncome' => $total->TotalIncome,
  102. 'TotalExpend' => $total->TotalExpend,
  103. 'tp_TotalPool' => $tp->TotalPool,
  104. 'tp_TotalIncome' => $tp->TotalIncome,
  105. 'tp_TotalExpend' => $tp->TotalExpend,
  106. 'rm_TotalPool' => $rm->TotalPool,
  107. 'rm_TotalIncome' => $rm->TotalIncome,
  108. 'rm_TotalExpend' => $rm->TotalExpend,
  109. ];
  110. $title = ['单控池总库存', '单控池累计成功回收', '单控池累计成功吐分', 'TP单控池总库存', 'TP单控池累计成功回收', 'TP单控池累计成功吐分', 'RM单控池总库存', 'RM单控池累计成功回收', 'RM单控池累计成功吐分'];
  111. downloadExcel($list, $title, '单控池监控' . date('YmdHis'));
  112. }
  113. return compact('total', 'tp', 'rm', 'start_time', 'end_time');
  114. }
  115. public function query_list($user_id, $time)
  116. {
  117. $where = [];
  118. list($start_time, $end_time) = [$time['start_time'], $time['end_time']];
  119. !empty($start_time) && $where[] = ['cr.created_at', '>=', $start_time];
  120. !empty($end_time) && $where[] = ['cr.created_at', '<=', $end_time];
  121. !empty($user_id) && $where[] = ['GameID', '=', $user_id];
  122. $field = ['cr.created_at', 'contents', 'GameID', 'ai.NickName', 'account', 'before_config', 'score'];
  123. $list = DB::connection('read')->table('agent.dbo.control_record as cr')
  124. ->join('QPAccountsDB.dbo.AccountsInfo as ai', 'cr.user_id', '=', 'ai.UserID')
  125. ->leftJoin('agent.dbo.admin_users as au', 'cr.admin_id', '=', 'au.id')
  126. ->where($where)
  127. ->select($field)
  128. ->orderByDesc('cr.created_at')
  129. ->paginate(10);
  130. foreach ($list as &$value) {
  131. $value->score = $value->score > 0 ? number_float($value->score / NumConfig::NUM_VALUE) : 0;
  132. }unset($value);
  133. return compact('list', 'start_time', 'end_time', 'user_id');
  134. }
  135. protected function return_val($val)
  136. {
  137. return $val < 0 ? number_float(-$val / 100) : number_float($val / 100);
  138. }
  139. }