BackflowService.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace App\Services;
  3. use App\Constants\HttpStatus;
  4. use App\Models\Backflow;
  5. use App\Models\Config;
  6. use Carbon\Carbon;
  7. use Exception;
  8. class BackflowService extends BaseService
  9. {
  10. public static string $MODEL = Backflow::class;
  11. public static function findOne(array $search): ?Backflow
  12. {
  13. return self::$MODEL::where(static::getWhere($search))->first();
  14. }
  15. /**
  16. * 进行回水操作
  17. * @param $id
  18. * @return bool
  19. * @throws Exception
  20. */
  21. public static function grant($id): bool
  22. {
  23. $params = ['id' => $id];
  24. $backflow = static::findOne($params);
  25. if (!$backflow) throw new Exception('数据不存在', HttpStatus::CUSTOM_ERROR);
  26. if ($backflow->status !== 0) throw new Exception("已回水,无需再次回水", HttpStatus::CUSTOM_ERROR);
  27. $date = Carbon::now('Asia/Shanghai')->format('Y-m');
  28. if (strtotime($backflow->date) >= strtotime($date)) {
  29. throw new Exception('未到发放时间', HttpStatus::CUSTOM_ERROR);
  30. }
  31. //回水门槛金额
  32. $restriction = Config::where('field', 'huishui_restriction')->first()->val;
  33. $amount = bcadd($backflow->recharge_amount, $backflow->withdrawal_amount, 2);
  34. $amount = abs($amount);
  35. if ($amount >= $restriction) {
  36. //回水比例
  37. $ratio = Config::where('field', 'huishui_percentage')->first()->val;
  38. $backflow->backflow_ratio = $ratio;
  39. $backflow->amount = bcmul($amount, $backflow->backflow_ratio, 2);
  40. if ($backflow->amount > 0) {
  41. $percentage = bcmul($ratio, 100, 2);//返水比例百分比
  42. $percentage = floatval($percentage);
  43. $res = WalletService::updateBalance($backflow->member_id, $backflow->amount);
  44. BalanceLogService::addLog($backflow->member_id, $backflow->amount, $res['before_balance'], $res['after_balance'], "回水", $backflow->id, "日期:$backflow->date;盈亏:-{$amount};回水比例:$percentage%");
  45. }
  46. } else {
  47. $backflow->amount = 0;
  48. }
  49. $backflow->grant_time = time();
  50. $backflow->status = 1;
  51. if (false !== $backflow->save()) return true;
  52. throw new Exception('发放失败', HttpStatus::CUSTOM_ERROR);
  53. }
  54. /**
  55. * @param $memberId string 会员ID
  56. * @param $changeAmount float 充值或提现金额
  57. * @return Backflow
  58. */
  59. public static function updateOrCreate(string $memberId, float $changeAmount): Backflow
  60. {
  61. $data['date'] = Carbon::now('Asia/Shanghai')->format('Y-m');
  62. $data['backflow_ratio'] = Config::where('field', 'huishui_percentage')->first()->val;
  63. $data['member_id'] = $memberId;
  64. if ($changeAmount > 0) $data['recharge_amount'] = $changeAmount;
  65. else $data['withdrawal_amount'] = $changeAmount;
  66. $backflow = static::$MODEL::where('date', $data['date'])
  67. ->where('member_id', $memberId)->first();
  68. if ($backflow) {
  69. if ($changeAmount > 0) $field = "recharge_amount";
  70. else $field = 'withdrawal_amount';
  71. $backflow->backflow_ratio = $data['backflow_ratio'];
  72. $backflow->increment($field, $changeAmount);
  73. $backflow->save();
  74. } else {
  75. $backflow = static::$MODEL::create($data);
  76. }
  77. $restriction = Config::where('field', 'huishui_restriction')->first()->val;
  78. $difference = bcadd($backflow->recharge_amount, $backflow->withdrawal_amount, 2);
  79. $difference = abs($difference);
  80. if ($difference >= $restriction) {
  81. $backflow->amount = bcmul($difference, $backflow->backflow_ratio, 2);
  82. } else {
  83. $backflow->amount = 0;
  84. }
  85. $backflow->save();
  86. return $backflow;
  87. }
  88. public static function getWhere(array $search = []): array
  89. {
  90. $where = [];
  91. if (isset($search['id']) && !empty($search['id'])) {
  92. $where[] = ['id', '=', $search['id']];
  93. }
  94. if (isset($search['member_id']) && !empty($search['member_id'])) {
  95. $where[] = ['member_id', '=', $search['member_id']];
  96. }
  97. if (isset($search['date']) && !empty($search['date'])) {
  98. $where[] = ['date', '=', $search['date']];
  99. }
  100. if (isset($search['status']) && $search['status'] != '') {
  101. $where[] = ['status', '=', $search['status']];
  102. }
  103. return $where;
  104. }
  105. public static function paginate(array $search = []): array
  106. {
  107. $date = Carbon::now('Asia/Shanghai')->format('Y-m');
  108. $query = static::$MODEL::where(static::getWhere($search));
  109. $query->where('date', '<', $date);
  110. if (isset($search['username']) && !empty($search['username'])) {
  111. $username = $search['username'];
  112. $query = $query->whereHas('user', function ($query1) use ($username) {
  113. $query1->where('username', $username);
  114. });
  115. }
  116. $paginator = $query
  117. ->with(['user'])->orderByDesc('date')->orderBy('status')
  118. ->paginate($search['limit']);
  119. // $count = $query->count();
  120. // $query->with(['user'])->orderByDesc('date')->orderBy('status');
  121. // $list = $query->forPage($search['page'], $search['limit'])->get()->toArray();
  122. // return ['total' => $count, 'data' => $list];
  123. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  124. }
  125. }