BackflowService.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace App\Services;
  3. use App\Models\Backflow;
  4. use App\Models\Config;
  5. use Carbon\Carbon;
  6. class BackflowService extends BaseService
  7. {
  8. public static string $MODEL = Backflow::class;
  9. /**
  10. * @param $memberId string 会员ID
  11. * @param $changeAmount float 充值或提现金额
  12. * @return Backflow
  13. */
  14. public static function updateOrCreate(string $memberId, float $changeAmount): Backflow
  15. {
  16. $data['date'] = Carbon::now('Asia/Shanghai')->format('Y-m');
  17. $data['backflow_ratio'] = Config::where('field', 'huishui_percentage')->first()->val;
  18. $data['member_id'] = $memberId;
  19. if ($changeAmount > 0) $data['recharge_amount'] = $changeAmount;
  20. else $data['withdrawal_amount'] = $changeAmount;
  21. $backflow = static::$MODEL::where('date', $data['date'])
  22. ->where('member_id', $memberId)->first();
  23. if ($backflow) {
  24. if ($changeAmount > 0) $field = "recharge_amount";
  25. else $field = 'withdrawal_amount';
  26. $backflow->backflow_ratio = $data['backflow_ratio'];
  27. $backflow->increment($field, $changeAmount);
  28. $backflow->save();
  29. } else {
  30. $backflow = static::$MODEL::create($data);
  31. }
  32. $restriction = Config::where('field', 'huishui_restriction')->first()->val;
  33. $difference = bcadd($backflow->recharge_amount, $backflow->withdrawal_amount, 2);
  34. $difference = abs($difference);
  35. if ($difference >= $restriction) {
  36. $backflow->amount = bcmul($difference, $backflow->backflow_ratio, 2);
  37. } else {
  38. $backflow->amount = 0;
  39. }
  40. $backflow->save();
  41. return $backflow;
  42. }
  43. public static function getWhere(array $search = []): array
  44. {
  45. $where = [];
  46. if (isset($search['id']) && !empty($search['id'])) {
  47. $where[] = ['id', '=', $search['id']];
  48. }
  49. if (isset($search['member_id']) && !empty($search['member_id'])) {
  50. $where[] = ['member_id', '=', $search['member_id']];
  51. }
  52. if (isset($search['date']) && !empty($search['date'])) {
  53. $where[] = ['date', '=', $search['date']];
  54. }
  55. if (isset($search['status']) && $search['status'] != '') {
  56. $where[] = ['status', '=', $search['status']];
  57. }
  58. return $where;
  59. }
  60. public static function paginate(array $search = []): array
  61. {
  62. $page = request()->input('page', 1);
  63. $limit = request()->input('limit', 10);
  64. $date = Carbon::now('Asia/Shanghai')->format('Y-m');
  65. $query = static::$MODEL::where(static::getWhere($search))
  66. // ->where('date', '<', $date)
  67. ;
  68. if (isset($search['username']) && !empty($search['username'])) {
  69. $username = $search['username'];
  70. $query = $query->whereHas('user', function ($query) use ($username) {
  71. $query->where('bot_users.username', $username);
  72. });
  73. }
  74. $count = $query->count();
  75. $query->with(['user'])->orderByDesc('date')->orderBy('status');
  76. $list = $query->forPage($page, $limit)->get()->toArray();
  77. return ['total' => $count, 'data' => $list];
  78. }
  79. }