BackflowService.php 5.2 KB

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