Ken пре 1 недеља
родитељ
комит
65c38045ea

+ 3 - 1
app/Http/Controllers/api/ActivityReward.php

@@ -6,6 +6,7 @@ use App\Constants\HttpStatus;
 use App\Models\ActivityReward as ActivityRewardModel;
 use App\Models\ActivityUser;
 use App\Services\ActivityRewardService;
+use App\Services\ActivityUserService;
 use Illuminate\Http\JsonResponse;
 use Illuminate\Validation\ValidationException;
 use Exception;
@@ -39,7 +40,8 @@ class ActivityReward extends BaseController
                 'id' => $activityId
             ]);
             if (!$activity) throw new Exception('活动不存在', HttpStatus::CUSTOM_ERROR);
-            ActivityUser::create([
+
+            ActivityUserService::submit([
                 'activity_id' => $activityId,
                 'title' => $activity->title,
                 'sub_title' => $activity->sub_title,

+ 61 - 0
app/Services/ActivityUserService.php

@@ -0,0 +1,61 @@
+<?php
+
+namespace App\Services;
+
+use App\Constants\HttpStatus;
+use App\Constants\Util;
+use App\Models\ActivityReward;
+use App\Models\ActivityUser;
+use Exception;
+use Illuminate\Database\Eloquent\Builder;
+
+class ActivityUserService extends BaseService
+{
+    public static string $MODEL = ActivityUser::class;
+
+
+
+    public static function getWhere(array $search = []): array
+    {
+        $where = [];
+        if (isset($search['id']) && !empty($search['id'])) {
+            $where[] = ['id', '=', $search['id']];
+        }
+        if (isset($search['title']) && !empty($search['title'])) {
+            $where[] = ['title', 'like', "%{$search['title']}%"];
+        }
+
+        if (isset($search['status']) && !empty($search['status'])) {
+            $where[] = ['status', '=', $search['status']];
+        }
+
+
+        return $where;
+    }
+
+    /**
+     * Update or create
+     * @param array $params
+     * @return bool
+     * @throws Exception
+     */
+    public static function submit(array $params = []): bool
+    {
+        if (isset($params['start_time']))
+            $params['start_time'] = strtotime($params['start_time'] . " 00:00:00");
+        if (isset($params['end_time']))
+            $params['end_time'] = strtotime($params['end_time'] . " 23:59:59");
+        if (isset($params['detail_image']))
+            $params['detail_image'] = Util::replacePartInUrl($params['detail_image']);
+
+
+        if (!empty($params['id'])) {
+            $info = static::findOne(['id' => $params['id']]);
+            if (!$info) throw new Exception("操作失败", HttpStatus::CUSTOM_ERROR);
+            $info->update($params);
+        } else {
+            $info = static::$MODEL::create($params);
+        }
+        return true;
+    }
+}

+ 11 - 0
app/Services/BaseService.php

@@ -19,6 +19,17 @@ abstract class BaseService
 
     public static string $MODEL = "";
 
+
+    /**
+     * @description: 查询单条数据
+     * @param array $search
+     * @return ActivityReward|null
+     */
+    public static function findOne(array $search): ActivityReward|null
+    {
+        return static::$MODEL::where(static::getWhere($search))->first();
+    }
+
     /**
      * @description: 模型
      * @return string