Преглед на файлове

编辑行为、生成日志

durong преди 5 години
родител
ревизия
909451888e

+ 11 - 0
app/Http/Controllers/Behavior/BehaviorController.php

@@ -55,6 +55,17 @@ class BehaviorController extends Controller
     //编辑行为
     public function edit(Request $request)
     {
+        $validator = Validator::make($request->all(), [
+            'id' => 'required|exists:behavior',
+            'virus_behavior_id' => 'required',
+            'name' => 'required|string',
+            'behavior_level' => 'required|integer',
+            'behavior_cycle_type' => ['required',Rule::in(0, 1)],
+        ]);
+        if ($validator->fails()) {
+            return $this->response->error($validator->errors()->first(), 500);
+        }
+        return  $this->behaviorRepository->edit($request->all());
 
     }
 

+ 6 - 0
app/Models/Behavior.php

@@ -13,4 +13,10 @@ class Behavior extends Model
 {
     protected $table = 'behavior';
     protected $guarded = [];
+    /**
+     * 可被批量赋值的字段
+     * @var array
+     */
+    protected $fillable = ['virus_behavior_id','name','behavior_level','behavior_cycle_type','behavior_action_id','behavior_cycle','is_open'];
+
 }

+ 7 - 0
app/Models/BehaviorOperationLog.php

@@ -13,4 +13,11 @@ class BehaviorOperationLog extends Model
 {
     protected $table = 'behavior_operation_log';
     protected $guarded = [];
+
+    /**
+     * 可被批量赋值的字段
+     * @var array
+     */
+    protected $fillable = ['operator_id','behavior_id','username','type','content'];
+
 }

+ 99 - 8
app/Repositories/Behavior/BehaviorRepository.php

@@ -1,12 +1,16 @@
 <?php
 namespace App\Repositories\Behavior;
 use App\Models\Behavior;
+use App\Models\BehaviorOperationLog;
 use GuzzleHttp\Client;
 use GuzzleHttp\Exception\RequestException;
+use Illuminate\Support\Facades\Auth;
 use Illuminate\Support\Facades\DB;
+use Illuminate\Support\Facades\Log;
 use Tymon\JWTAuth\Facades\JWTAuth;
 use Dingo\Api\Http\Response;
 use Symfony\Component\HttpKernel\Exception\HttpException;
+use Illuminate\Database\QueryException;
 
 /**
  * Created by PhpStorm.
@@ -17,9 +21,10 @@ use Symfony\Component\HttpKernel\Exception\HttpException;
 
 class BehaviorRepository
 {
-    public function __construct(Behavior $behavior)
+    public function __construct(Behavior $behavior,BehaviorOperationLog $behaviorOperationLog)
     {
         $this->behavior = $behavior;
+        $this->behaviorOperationLog = $behaviorOperationLog;
         //初始化virus域名
         $this->client = new Client([
             'base_uri' => config('constants.VIRUS_URL'),
@@ -51,6 +56,7 @@ class BehaviorRepository
                 'returnCode' => '-1',
                 'errMsg' => '网络超时'.$exception->getMessage()
             ];
+            Log::debug('获取virus行为列表:'.$exception->getMessage());
         }
         //已登记行为
         $registered_bahavior = $this->behavior->get();
@@ -132,11 +138,11 @@ class BehaviorRepository
 
     public function create($request)
     {
-        $behavior_name = $this->behavior->where(['name'=>$request['name']])->first();
-        if($behavior_name){
+        $behavior_name = $this->behavior->where(['name' => $request['name']])->first();
+        if ($behavior_name) {
             return Response::create([
-                'message'  => '该行为已存在',
-                'status_code'   => 500
+                'message' => '该行为已存在',
+                'status_code' => 500
             ]);
         }
         $data = [
@@ -145,7 +151,7 @@ class BehaviorRepository
             'behavior_level' => $request['behavior_level'],
             'behavior_cycle_type' => $request['behavior_cycle_type'],
             'is_open' => 0,
-            'behavior_action_id' => isset($request['behavior_action_id']) ? $request['behavior_action_id'] : 0,
+            'behavior_action_id' => isset($request['behavior_action_id']) ? $request['behavior_action_id'] : '',
             'behavior_cycle' => isset($request['behavior_cycle']) ? $request['behavior_cycle'] : '',
             'behavior_binding_users' => isset($request['behavior_binding_users']) ? $request['behavior_binding_users'] : 0,
             'physical_strength' => isset($request['physical_strength']) ? $request['physical_strength'] : '',
@@ -160,10 +166,95 @@ class BehaviorRepository
             'relative_series' => isset($request['relative_series']) ? $request['relative_series'] : 0,
             'absolute_progression' => isset($request['absolute_progression']) ? $request['absolute_progression'] : 0,
         ];
+        DB::beginTransaction();
+        try {
+            $res = $this->behavior->create($data);
+            if ($res) {
+                $cerate_bahavior_data = [
+                    'operator_id' => Auth::user()->id,
+                    'behavior_id' => $res->virus_behavior_id,
+                    'username' => Auth::user()->username,
+                    'content' => json_encode($data),
+                    'type' => 0,
+                    'created_at' => date('Y-m-d H:i:s'),
+                    'updated_at' => date('Y-m-d H:i:s')
+                ];
+                $result = $this->behaviorOperationLog->insert($cerate_bahavior_data);
+                if (!$result) {
+                    throw new HttpException(500, '操作日志记录失败');
+                }
+            }
+            DB::commit();
+            return Response::create();
+
+        } catch (QueryException $exception) {
+            DB::rollBack();
+            Log::debug('注册行为:'.$exception->getMessage());
+            return Response::create([
+                'message' => '添加失败,请重试',
+                'error' => $exception->getMessage(),
+                'status_code' => 500
+            ]);
+        }
+    }
 
-        if (!$this->behavior->create($data)) {
-            throw new HttpException(500, '添加失败,请重试');
+    public function edit($request)
+    {
+        $behavior_id = $this->behavior->find($request['id']);
+        if ($behavior_id->is_open == 1){
+            throw new HttpException(500, '无法编辑已经在记录的行为');
         }
+        $update_bahavior = [
+            'virus_behavior_id' => $request['virus_behavior_id'],
+            'name' => $request['name'],
+            'behavior_level' => $request['behavior_level'],
+            'behavior_cycle_type' => $request['behavior_cycle_type'],
+            'is_open' => 0,
+            'behavior_cycle' => isset($request['behavior_cycle']) ? $request['behavior_cycle'] : '',
+            'behavior_binding_users' => isset($request['behavior_binding_users']) ? $request['behavior_binding_users'] : 0,
+            'physical_strength' => isset($request['physical_strength']) ? $request['physical_strength'] : '',
+            'rainbow_beans' => isset($request['rainbow_beans']) ? $request['rainbow_beans'] : '',
+            'remarks' => isset($request['remarks']) ? $request['remarks'] : '',
+            'behavioral_cycle_start_time' => isset($request['behavioral_cycle_start_time']) ? $request['behavioral_cycle_start_time'] : null,
+            'behavioral_cycle_end_time' => isset($request['behavioral_cycle_end_time']) ? $request['behavioral_cycle_end_time'] : null,
+            'allotted_quantity_rule' => isset($request['allotted_quantity_rule']) ? json_encode($request['allotted_quantity_rule']) : '',
+            'behavior_identification' => isset($request['behavior_identification']) ? $request['behavior_identification'] : '',
+            'trigger_times' => isset($request['trigger_times']) ? $request['trigger_times'] : 0,
+            'effective_trigger' => isset($request['effective_trigger']) ? $request['effective_trigger'] : 0,
+            'relative_series' => isset($request['relative_series']) ? $request['relative_series'] : 0,
+            'absolute_progression' => isset($request['absolute_progression']) ? $request['absolute_progression'] : 0,
+            'updated_at' => date('Y-m-d H:i:s')
+            ];
+        DB::beginTransaction();
+        try{
+            $res = $this->behavior->where('id',$request['id'])->update($update_bahavior);
+                if ($res) {
+                    $cerate_bahavior_data = [
+                        'operator_id' => Auth::user()->id,
+                        'behavior_id' => $request['virus_behavior_id'],
+                        'username' => Auth::user()->username,
+                        'content' => json_encode($update_bahavior),
+                        'type' => 1,
+                        'created_at' => date('Y-m-d H:i:s'),
+                        'updated_at' => date('Y-m-d H:i:s')
+                    ];
+                    $result = $this->behaviorOperationLog->insert($cerate_bahavior_data);
+                    if (!$result) {
+                        throw new HttpException(500, '操作日志记录失败');
+                    }
+                }
+                DB::commit();
+                return Response::create();
+
+            } catch (QueryException $exception) {
+                DB::rollBack();
+                Log::debug('编辑行为:'.$exception->getMessage());
+                return Response::create([
+                    'message' => '编辑失败,请重试',
+                    'error' => $exception->getMessage(),
+                    'status_code' => 500
+                ]);
+            }
     }
 }
 

+ 2 - 0
routes/api.php

@@ -91,5 +91,7 @@ $api->version('v1', [
         $api->get('behavior/list', 'BehaviorController@index');
         //登记/注册行为
         $api->post('behavior/create', 'BehaviorController@create');
+        //编辑行为
+        $api->put('behavior/edit', 'BehaviorController@edit');
     });
 });