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