Ken 4 dní pred
rodič
commit
c53bd804d5

+ 23 - 1
app/Http/Controllers/admin/Backflow.php

@@ -6,11 +6,33 @@ use App\Constants\HttpStatus;
 use App\Http\Controllers\Controller;
 use App\Http\Controllers\Controller;
 use App\Services\BackflowService;
 use App\Services\BackflowService;
 use Exception;
 use Exception;
+use Illuminate\Http\JsonResponse;
+use Illuminate\Support\Facades\DB;
 use Illuminate\Validation\ValidationException;
 use Illuminate\Validation\ValidationException;
+use Carbon\Carbon;
 
 
 class Backflow extends Controller
 class Backflow extends Controller
 {
 {
-    function index()
+    function grant(): JsonResponse
+    {
+        DB::beginTransaction();
+        try {
+            $params = request()->validate([
+                'id' => ['required', 'integer', 'min:1'],
+            ]);
+            BackflowService::grant($params['id']);
+            DB::commit();
+        } catch (ValidationException $e) {
+            DB::rollBack();
+            return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
+        } catch (Exception $e) {
+            DB::rollBack();
+            return $this->error($e->getCode(), $e->getMessage());
+        }
+        return $this->success();
+    }
+
+    function index(): JsonResponse
     {
     {
         try {
         try {
             $params = request()->validate([
             $params = request()->validate([

+ 10 - 1
app/Models/Backflow.php

@@ -2,7 +2,16 @@
 
 
 namespace App\Models;
 namespace App\Models;
 
 
-
+/**
+ * @property $date
+ * @property $recharge_amount
+ * @property $withdrawal_amount
+ * @property $status
+ * @property $backflow_ratio
+ * @property $member_id
+ * @property $amount
+ *
+ */
 class Backflow extends BaseModel
 class Backflow extends BaseModel
 {
 {
     protected $table = 'backflow';
     protected $table = 'backflow';

+ 49 - 2
app/Services/BackflowService.php

@@ -2,14 +2,63 @@
 
 
 namespace App\Services;
 namespace App\Services;
 
 
+use App\Constants\HttpStatus;
 use App\Models\Backflow;
 use App\Models\Backflow;
 use App\Models\Config;
 use App\Models\Config;
 use Carbon\Carbon;
 use Carbon\Carbon;
+use Exception;
 
 
 class BackflowService extends BaseService
 class BackflowService extends BaseService
 {
 {
     public static string $MODEL = Backflow::class;
     public static string $MODEL = Backflow::class;
 
 
+
+    public static function findOne(array $search): ?Backflow
+    {
+        return self::$MODEL::where(static::getWhere($search))->first();
+    }
+
+
+    /**
+     * @param $id
+     * @return bool
+     * @throws Exception
+     */
+    public static function grant($id): bool
+    {
+        $params = ['id' => $id, 'status' => 0];
+        $backflow = static::findOne($params);
+        if (!$backflow) throw new Exception('数据不存在', HttpStatus::CUSTOM_ERROR);
+        $date = Carbon::now('Asia/Shanghai')->format('Y-m');
+        if (strtotime($backflow->date) >= strtotime($date)) {
+            throw new Exception('未到发放时间', HttpStatus::CUSTOM_ERROR);
+        }
+
+        //回水门槛金额
+        $restriction = Config::where('field', 'huishui_restriction')->first()->val;
+        $amount = bcadd($backflow->recharge_amount, $backflow->withdrawal_amount, 2);
+        $amount = abs($amount);
+        if ($amount >= $restriction) {
+            //回水比例
+            $ratio = Config::where('field', 'huishui_percentage')->first()->val;
+            $backflow->backflow_ratio = $ratio;
+            $backflow->amount = bcmul($amount, $backflow->backflow_ratio, 2);
+            if ($backflow->amount > 0) {
+                $percentage = bcmul($ratio, 100, 2);//返水比例百分比
+                $percentage = floatval($percentage);
+                $res = WalletService::updateBalance($backflow->member_id, $backflow->amount);
+                BalanceLogService::addLog($backflow->member_id, $backflow->amount, $res['before_balance'], $res['after_balance'], "回水", $backflow->id, "日期:$backflow->date;盈亏:-{$amount};回水比例:$percentage%");
+            }
+        } else {
+            $backflow->amount = 0;
+        }
+        $backflow->status = 1;
+        if (false !== $backflow->save()) return true;
+        return false;
+
+    }
+
+
     /**
     /**
      * @param $memberId string 会员ID
      * @param $memberId string 会员ID
      * @param  $changeAmount float 充值或提现金额
      * @param  $changeAmount float 充值或提现金额
@@ -85,8 +134,6 @@ class BackflowService extends BaseService
 
 
         $paginator = $query
         $paginator = $query
             ->with(['user'])->orderByDesc('date')->orderBy('status')
             ->with(['user'])->orderByDesc('date')->orderBy('status')
-
-
             ->paginate($search['limit']);
             ->paginate($search['limit']);
 
 
 //        $count = $query->count();
 //        $count = $query->count();