BackflowService.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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['account'])) {
  53. $where[] = ['users.account', 'like', '%' . trim((string)$search['account']) . '%'];
  54. }
  55. if (!empty($search['first_name'])) {
  56. $where[] = ['users.first_name','like', '%'.$search['first_name'].'%'];
  57. }
  58. return $where;
  59. }
  60. public static function paginate(array $search = []): array
  61. {
  62. // 当月(上海时间)的记录也展示实时统计,但要到下个月才能发放,见 RewardGrantService::grant
  63. $month = Carbon::now('Asia/Shanghai')->format('Y-m');
  64. $query = static::$MODEL::join('users', 'users.member_id', '=', 'backflow.member_id')
  65. ->where(static::getWhere($search))
  66. ->where('backflow.date', '<=', $month);
  67. $paginator = $query->select("backflow.*", "users.first_name", "users.account", "users.admin_note as user_admin_note")->orderByDesc('date')->orderBy('status')
  68. ->paginate($search['limit']);
  69. foreach ($paginator->items() as $item) {
  70. $item['can_grant'] = (int)$item->status === 0 && (string)$item->date < $month;
  71. }
  72. // $count = $query->count();
  73. // $query->with(['user'])->orderByDesc('date')->orderBy('status');
  74. // $list = $query->forPage($search['page'], $search['limit'])->get()->toArray();
  75. // return ['total' => $count, 'data' => $list];
  76. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  77. }
  78. }