BackflowService.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace App\Services;
  3. use App\Models\Backflow;
  4. use App\Models\Config;
  5. use Carbon\Carbon;
  6. use Exception;
  7. class BackflowService extends BaseService
  8. {
  9. public static string $MODEL = Backflow::class;
  10. public static function findOne(array $search): ?Backflow
  11. {
  12. return self::$MODEL::where(static::getWhere($search))->first();
  13. }
  14. /**
  15. * 进行回水操作
  16. * @param $id
  17. * @return bool
  18. * @throws Exception
  19. */
  20. public static function grant($id): bool
  21. {
  22. return RewardGrantService::grant('backflow', (int) $id, (string) (request()->user->username ?? 'system'));
  23. }
  24. /**
  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. $data['date'] = Carbon::now('Asia/Shanghai')->format('Y-m');
  32. $data['backflow_ratio'] = Config::where('field', 'huishui_percentage')->first()->val;
  33. $data['member_id'] = $memberId;
  34. if ($changeAmount > 0) $data['recharge_amount'] = $changeAmount;
  35. else $data['withdrawal_amount'] = $changeAmount;
  36. $backflow = static::$MODEL::where('date', $data['date'])
  37. ->where('member_id', $memberId)->first();
  38. if ($backflow) {
  39. if ($changeAmount > 0) $field = "recharge_amount";
  40. else $field = 'withdrawal_amount';
  41. $backflow->backflow_ratio = $data['backflow_ratio'];
  42. $backflow->increment($field, $changeAmount);
  43. $backflow->save();
  44. } else {
  45. $backflow = static::$MODEL::create($data);
  46. }
  47. $restriction = Config::where('field', 'huishui_restriction')->first()->val;
  48. $difference = bcadd($backflow->recharge_amount, $backflow->withdrawal_amount, 2);
  49. $difference = abs($difference);
  50. if ($difference >= $restriction) {
  51. $backflow->amount = bcmul($difference, $backflow->backflow_ratio, 2);
  52. } else {
  53. $backflow->amount = 0;
  54. }
  55. $backflow->save();
  56. return $backflow;
  57. }
  58. public static function getWhere(array $search = []): array
  59. {
  60. $where = [];
  61. if (isset($search['id']) && !empty($search['id'])) {
  62. $where[] = ['backflow.id', '=', $search['id']];
  63. }
  64. if (isset($search['member_id']) && !empty($search['member_id'])) {
  65. $where[] = ['backflow.member_id', '=', $search['member_id']];
  66. }
  67. if (isset($search['date']) && !empty($search['date'])) {
  68. $where[] = ['backflow.date', '=', $search['date']];
  69. }
  70. if (isset($search['status']) && $search['status'] != '') {
  71. $where[] = ['backflow.status', '=', $search['status']];
  72. }
  73. if (!empty($search['first_name'])) {
  74. $where[] = ['users.first_name','like', '%'.$search['first_name'].'%'];
  75. }
  76. return $where;
  77. }
  78. public static function paginate(array $search = []): array
  79. {
  80. $date = Carbon::now('Asia/Shanghai')->format('Y-m');
  81. $query = static::$MODEL::join('users', 'users.member_id', '=', 'backflow.member_id')
  82. ->where(static::getWhere($search))
  83. ->where('backflow.date', '<', $date);
  84. $paginator = $query->select("backflow.*", "users.first_name","users.admin_note as user_admin_note")->orderByDesc('date')->orderBy('status')
  85. ->paginate($search['limit']);
  86. // $count = $query->count();
  87. // $query->with(['user'])->orderByDesc('date')->orderBy('status');
  88. // $list = $query->forPage($search['page'], $search['limit'])->get()->toArray();
  89. // return ['total' => $count, 'data' => $list];
  90. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  91. }
  92. }