Ken il y a 5 jours
Parent
commit
26c0bc6f34
2 fichiers modifiés avec 179 ajouts et 0 suppressions
  1. 84 0
      app/Http/Controllers/admin/Rebate.php
  2. 95 0
      app/Services/RebateService.php

+ 84 - 0
app/Http/Controllers/admin/Rebate.php

@@ -0,0 +1,84 @@
+<?php
+
+namespace App\Http\Controllers\admin;
+
+use App\Constants\HttpStatus;
+use App\Http\Controllers\Controller;
+use App\Models\Config;
+use App\Services\BalanceLogService;
+use App\Services\RebateService;
+use App\Services\WalletService;
+use Carbon\Carbon;
+use Illuminate\Http\Request;
+use Illuminate\Support\Facades\DB;
+use Illuminate\Validation\ValidationException;
+use Exception;
+
+class Rebate extends Controller
+{
+    function index()
+    {
+        try {
+            $search = request()->validate([
+                'page' => ['nullable', 'integer', 'min:1'],
+                'limit' => ['nullable', 'integer', 'min:1'],
+                'member_id' => ['nullable', 'string', 'min:1'],
+                'date' => ['nullable', 'date_format:Y-m-d'],
+            ]);
+            $result = RebateService::paginate($search);
+        } catch (ValidationException $e) {
+            return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
+        } catch (Exception $e) {
+            return $this->error(intval($e->getCode()));
+        }
+        return $this->success($result);
+    }
+
+
+    public function store(Request $request)
+    {
+        DB::beginTransaction();
+        try {
+            $params = request()->validate([
+                'id' => ['required', 'integer', 'min:1'],
+            ]);
+            $params['id'];
+            $params['status'] = 0;
+            $rebate = RebateService::findOne($params);
+            if (!$rebate) throw new Exception('数据不存在', HttpStatus::CUSTOM_ERROR);
+            $date = Carbon::now('America/New_York')->format('Y-m-d');
+            if (strtotime($rebate->date) >= strtotime($date)) {
+                throw new Exception('未到发放时间', HttpStatus::CUSTOM_ERROR);
+            }
+
+            $rebate_ratio = Config::where('field', 'rebate')->first()->val;
+            $rebateAmount = bcmul($rebate->betting_amount, $rebate_ratio, 2); // 返利金额
+
+
+            $rebate->amount = $rebateAmount;
+            $rebate->rebate_ratio = $rebate_ratio;
+            $rebate->status = 1;
+            $rebate->save();
+            if ($rebateAmount > 0) {
+                $res = WalletService::updateBalance($rebate->member_id, $rebateAmount);
+                BalanceLogService::addLog(
+                    $rebate->member_id,
+                    $rebateAmount,
+                    $res['before_balance'],
+                    $res['after_balance'],
+                    '返水',
+                    $rebate->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(intval($e->getCode()), $e->getMessage());
+        }
+        return $this->success();
+    }
+}

+ 95 - 0
app/Services/RebateService.php

@@ -0,0 +1,95 @@
+<?php
+
+
+namespace App\Services;
+
+
+use App\Models\Rebate;
+use App\Models\User;
+use Carbon\Carbon;
+
+class RebateService extends BaseService
+{
+    /**
+     * @description: 模型
+     * @return {string}
+     */
+    public static function model(): string
+    {
+        return Rebate::class;
+    }
+
+    /**
+     * @description: 枚举
+     * @return {*}
+     */
+    public static function enum(): string
+    {
+        return '';
+    }
+
+
+    /**
+     * @description: 获取查询条件
+     * @param {array} $search 查询内容
+     * @return {array}
+     */
+    public static function getWhere(array $search = []): array
+    {
+        $where = [];
+        if (isset($search['id']) && !empty($search['id'])) {
+            $where[] = ['id', '=', $search['id']];
+        }
+
+        if (isset($search['member_id']) && !empty($search['member_id'])) {
+            $where[] = ['member_id', '=', $search['member_id']];
+        }
+
+        if (isset($search['status']) && !empty($search['status'])) {
+            $where[] = ['status', '=', $search['status']];
+        }
+
+        if (isset($search['date']) && !empty($search['date'])) {
+            $where[] = ['date', '=', $search['date']];
+        }
+
+        return $where;
+    }
+
+
+    /**
+     * @description: 查询单条数据
+     * @param array $search
+     * @return \App\Models\Rebate|null
+     */
+    public static function findOne(array $search): ?Rebate
+    {
+        return self::model()::where(self::getWhere($search))->first();
+    }
+
+    /**
+     * @description: 查询所有数据
+     * @param array $search
+     * @return \Illuminate\Database\Eloquent\Collection
+     */
+    public static function findAll(array $search = [])
+    {
+        return self::model()::where(self::getWhere($search))->get();
+    }
+
+    /**
+     * @description: 分页查询
+     * @param array $search
+     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
+     */
+    public static function paginate(array $search = [])
+    {
+        $limit = isset($search['limit']) ? $search['limit'] : 15;
+        $date = Carbon::now('America/New_York')->format('Y-m-d');
+
+        $paginator = self::model()::where(self::getWhere($search))
+            ->where('date', '<', $date)
+            ->with('wallet:user_id,member_id,address,available_balance')->paginate($limit);
+        return ['total' => $paginator->total(), 'data' => $paginator->items()];
+    }
+}