PointRepository.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. namespace App\Module\Point\Repositorys;
  3. use App\Module\Point\Models\PointModel;
  4. use Dcat\Admin\Repositories\EloquentRepository;
  5. /**
  6. * 积分数据仓库
  7. *
  8. * 专门为后台管理提供数据访问功能
  9. */
  10. class PointRepository extends EloquentRepository
  11. {
  12. /**
  13. * 模型类名
  14. */
  15. protected $eloquentClass = PointModel::class;
  16. /**
  17. * 获取表格数据
  18. *
  19. * @return \Illuminate\Database\Eloquent\Builder
  20. */
  21. public function grid()
  22. {
  23. return $this->model()
  24. ->orderBy('update_time', 'desc')
  25. ->orderBy('id', 'desc');
  26. }
  27. /**
  28. * 获取详情数据
  29. *
  30. * @param mixed $key
  31. * @return \Illuminate\Database\Eloquent\Model
  32. */
  33. public function detail($key)
  34. {
  35. return $this->model()->findOrFail($key);
  36. }
  37. /**
  38. * 获取编辑数据
  39. *
  40. * @param mixed $key
  41. * @return \Illuminate\Database\Eloquent\Model
  42. */
  43. public function edit($key)
  44. {
  45. return $this->model()->findOrFail($key);
  46. }
  47. /**
  48. * 新增数据
  49. *
  50. * @param array $data
  51. * @return mixed
  52. */
  53. public function store(array $data)
  54. {
  55. return $this->model()->create($data);
  56. }
  57. /**
  58. * 更新数据
  59. *
  60. * @param mixed $key
  61. * @param array $data
  62. * @return bool
  63. */
  64. public function update($key, array $data)
  65. {
  66. return $this->model()->findOrFail($key)->update($data);
  67. }
  68. /**
  69. * 删除数据
  70. *
  71. * @param mixed $key
  72. * @return bool
  73. */
  74. public function destroy($key)
  75. {
  76. return $this->model()->findOrFail($key)->delete();
  77. }
  78. /**
  79. * 获取用户积分统计
  80. *
  81. * @param int $userId
  82. * @return array
  83. */
  84. public function getUserPointStats(int $userId): array
  85. {
  86. $points = $this->model()->where('user_id', $userId)->get();
  87. $stats = [
  88. 'total_types' => $points->count(),
  89. 'total_balance' => $points->sum('balance'),
  90. 'details' => []
  91. ];
  92. foreach ($points as $point) {
  93. $stats['details'][] = [
  94. 'point_id' => $point->point_id,
  95. 'balance' => $point->balance,
  96. 'create_time' => $point->create_time,
  97. 'update_time' => $point->update_time,
  98. ];
  99. }
  100. return $stats;
  101. }
  102. /**
  103. * 获取积分类型统计
  104. *
  105. * @return array
  106. */
  107. public function getPointTypeStats(): array
  108. {
  109. $stats = $this->model()
  110. ->selectRaw('point_id, COUNT(*) as user_count, SUM(balance) as total_balance, AVG(balance) as avg_balance')
  111. ->groupBy('point_id')
  112. ->get()
  113. ->toArray();
  114. return $stats;
  115. }
  116. /**
  117. * 获取积分排行榜
  118. *
  119. * @param int $pointId
  120. * @param int $limit
  121. * @return array
  122. */
  123. public function getPointRanking(int $pointId, int $limit = 100): array
  124. {
  125. return $this->model()
  126. ->where('point_id', $pointId)
  127. ->where('balance', '>', 0)
  128. ->orderBy('balance', 'desc')
  129. ->limit($limit)
  130. ->get()
  131. ->toArray();
  132. }
  133. /**
  134. * 搜索用户积分
  135. *
  136. * @param array $filters
  137. * @return \Illuminate\Database\Eloquent\Builder
  138. */
  139. public function search(array $filters)
  140. {
  141. $query = $this->model();
  142. if (isset($filters['user_id'])) {
  143. $query = $query->where('user_id', $filters['user_id']);
  144. }
  145. if (isset($filters['point_id'])) {
  146. $query = $query->where('point_id', $filters['point_id']);
  147. }
  148. if (isset($filters['min_balance'])) {
  149. $query = $query->where('balance', '>=', $filters['min_balance']);
  150. }
  151. if (isset($filters['max_balance'])) {
  152. $query = $query->where('balance', '<=', $filters['max_balance']);
  153. }
  154. return $query->orderBy('balance', 'desc');
  155. }
  156. }