BackflowService.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace App\Services;
  3. use App\Models\Backflow;
  4. use Carbon\Carbon;
  5. use Exception;
  6. class BackflowService extends BaseService
  7. {
  8. public static string $MODEL = Backflow::class;
  9. public static function findOne(array $search): ?Backflow
  10. {
  11. return self::$MODEL::where(static::getWhere($search))->first();
  12. }
  13. /**
  14. * 进行回水操作
  15. * @param $id
  16. * @return bool
  17. * @throws Exception
  18. */
  19. public static function grant($id): bool
  20. {
  21. return RewardGrantService::grant('backflow', (int) $id, (string) (request()->user->username ?? 'system'));
  22. }
  23. /**
  24. * 只记录当月充值/提现,回水金额由 rewards:sync-bases 按分类亏损和规则计算。
  25. * @param $memberId string 会员ID
  26. * @param $changeAmount float 充值或提现金额
  27. * @return Backflow
  28. */
  29. public static function updateOrCreate(string $memberId, float $changeAmount): Backflow
  30. {
  31. $date = Carbon::now('Asia/Shanghai')->format('Y-m');
  32. $field = $changeAmount > 0 ? 'recharge_amount' : 'withdrawal_amount';
  33. $backflow = static::$MODEL::firstOrCreate(['date' => $date, 'member_id' => $memberId]);
  34. if ((int) $backflow->status === 0) $backflow->increment($field, $changeAmount);
  35. return $backflow->refresh();
  36. }
  37. public static function getWhere(array $search = []): array
  38. {
  39. $where = [];
  40. if (isset($search['id']) && !empty($search['id'])) {
  41. $where[] = ['backflow.id', '=', $search['id']];
  42. }
  43. if (isset($search['member_id']) && !empty($search['member_id'])) {
  44. $where[] = ['backflow.member_id', '=', $search['member_id']];
  45. }
  46. if (isset($search['date']) && !empty($search['date'])) {
  47. $where[] = ['backflow.date', '=', $search['date']];
  48. }
  49. if (isset($search['status']) && $search['status'] != '') {
  50. $where[] = ['backflow.status', '=', $search['status']];
  51. }
  52. if (!empty($search['first_name'])) {
  53. $where[] = ['users.first_name','like', '%'.$search['first_name'].'%'];
  54. }
  55. return $where;
  56. }
  57. public static function paginate(array $search = []): array
  58. {
  59. $date = Carbon::now('Asia/Shanghai')->format('Y-m');
  60. $query = static::$MODEL::join('users', 'users.member_id', '=', 'backflow.member_id')
  61. ->where(static::getWhere($search))
  62. ->where('backflow.date', '<', $date);
  63. $paginator = $query->select("backflow.*", "users.first_name","users.admin_note as user_admin_note")->orderByDesc('date')->orderBy('status')
  64. ->paginate($search['limit']);
  65. // $count = $query->count();
  66. // $query->with(['user'])->orderByDesc('date')->orderBy('status');
  67. // $list = $query->forPage($search['page'], $search['limit'])->get()->toArray();
  68. // return ['total' => $count, 'data' => $list];
  69. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  70. }
  71. }