| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- <?php
- namespace App\Module\Point\Repositorys;
- use App\Module\Point\Models\PointModel;
- use Dcat\Admin\Repositories\EloquentRepository;
- /**
- * 积分数据仓库
- *
- * 专门为后台管理提供数据访问功能
- */
- class PointRepository extends EloquentRepository
- {
- /**
- * 模型类名
- */
- protected $eloquentClass = PointModel::class;
- /**
- * 获取表格数据
- *
- * @return \Illuminate\Database\Eloquent\Builder
- */
- public function grid()
- {
- return $this->model()
- ->orderBy('update_time', 'desc')
- ->orderBy('id', 'desc');
- }
- /**
- * 获取详情数据
- *
- * @param mixed $key
- * @return \Illuminate\Database\Eloquent\Model
- */
- public function detail($key)
- {
- return $this->model()->findOrFail($key);
- }
- /**
- * 获取编辑数据
- *
- * @param mixed $key
- * @return \Illuminate\Database\Eloquent\Model
- */
- public function edit($key)
- {
- return $this->model()->findOrFail($key);
- }
- /**
- * 新增数据
- *
- * @param array $data
- * @return mixed
- */
- public function store(array $data)
- {
- return $this->model()->create($data);
- }
- /**
- * 更新数据
- *
- * @param mixed $key
- * @param array $data
- * @return bool
- */
- public function update($key, array $data)
- {
- return $this->model()->findOrFail($key)->update($data);
- }
- /**
- * 删除数据
- *
- * @param mixed $key
- * @return bool
- */
- public function destroy($key)
- {
- return $this->model()->findOrFail($key)->delete();
- }
- /**
- * 获取用户积分统计
- *
- * @param int $userId
- * @return array
- */
- public function getUserPointStats(int $userId): array
- {
- $points = $this->model()->where('user_id', $userId)->get();
-
- $stats = [
- 'total_types' => $points->count(),
- 'total_balance' => $points->sum('balance'),
- 'details' => []
- ];
- foreach ($points as $point) {
- $stats['details'][] = [
- 'point_id' => $point->point_id,
- 'balance' => $point->balance,
- 'create_time' => $point->create_time,
- 'update_time' => $point->update_time,
- ];
- }
- return $stats;
- }
- /**
- * 获取积分类型统计
- *
- * @return array
- */
- public function getPointTypeStats(): array
- {
- $stats = $this->model()
- ->selectRaw('point_id, COUNT(*) as user_count, SUM(balance) as total_balance, AVG(balance) as avg_balance')
- ->groupBy('point_id')
- ->get()
- ->toArray();
- return $stats;
- }
- /**
- * 获取积分排行榜
- *
- * @param int $pointId
- * @param int $limit
- * @return array
- */
- public function getPointRanking(int $pointId, int $limit = 100): array
- {
- return $this->model()
- ->where('point_id', $pointId)
- ->where('balance', '>', 0)
- ->orderBy('balance', 'desc')
- ->limit($limit)
- ->get()
- ->toArray();
- }
- /**
- * 搜索用户积分
- *
- * @param array $filters
- * @return \Illuminate\Database\Eloquent\Builder
- */
- public function search(array $filters)
- {
- $query = $this->model();
- if (isset($filters['user_id'])) {
- $query = $query->where('user_id', $filters['user_id']);
- }
- if (isset($filters['point_id'])) {
- $query = $query->where('point_id', $filters['point_id']);
- }
- if (isset($filters['min_balance'])) {
- $query = $query->where('balance', '>=', $filters['min_balance']);
- }
- if (isset($filters['max_balance'])) {
- $query = $query->where('balance', '<=', $filters['max_balance']);
- }
- return $query->orderBy('balance', 'desc');
- }
- }
|