BackflowService.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. if (isset($search['username']) && !empty($search['username'])) {
  68. $username = $search['username'];
  69. $query = $query->whereHas('user', function ($query1) use ($username) {
  70. $query1->where('username', $username);
  71. });
  72. }
  73. $query->with(['user'])->orderByDesc('date')->orderBy('status');
  74. $paginator = $query->paginate($limit);
  75. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  76. }
  77. }