|
@@ -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();
|
|
@@ -82,7 +88,7 @@ class BehaviorRepository
|
|
|
//根据行为ID筛选
|
|
|
if (isset($request['virus_behavior_id'])){
|
|
|
foreach ($new_res as $k=>$v){
|
|
|
- if (isset($v['virus_behavior_id']) && $v['virus_behavior_id'] != $request['virus_behavior_id']){
|
|
|
+ if (isset($v['id']) && $v['id'] != $request['virus_behavior_id']){
|
|
|
unset($new_res[$k]);
|
|
|
$new_re = array_merge($new_res);
|
|
|
}elseif (isset($v['_id']) && $v['_id'] != $request['virus_behavior_id']){
|
|
@@ -102,19 +108,19 @@ class BehaviorRepository
|
|
|
}
|
|
|
return $new_re;
|
|
|
}
|
|
|
- //根据绑定的动作ID筛选
|
|
|
- if (isset($request['behavior_action_id'])){
|
|
|
- foreach ($new_res as $key=>$val) {
|
|
|
- if (isset($val['behavior_action_id']) && ($val['behavior_action_id'] != $request['behavior_action_id'])) {
|
|
|
- unset($new_res[$key]);
|
|
|
- $new_re = array_merge($new_res);
|
|
|
- } elseif (!isset($val['behavior_action_id'])) {
|
|
|
- unset($new_res[$key]);
|
|
|
- $new_re = array_merge($new_res);
|
|
|
- }
|
|
|
- }
|
|
|
- return $new_re;
|
|
|
- }
|
|
|
+ //根据绑定的动作ID筛选(暂时取消)
|
|
|
+// if (isset($request['behavior_action_id'])){
|
|
|
+// foreach ($new_res as $key=>$val) {
|
|
|
+// if (isset($val['behavior_action_id']) && ($val['behavior_action_id'] != $request['behavior_action_id'])) {
|
|
|
+// unset($new_res[$key]);
|
|
|
+//// $new_re = array_merge($new_res);
|
|
|
+// } elseif (!isset($val['behavior_action_id'])) {
|
|
|
+// unset($new_res[$key]);
|
|
|
+//// $new_re = array_merge($new_res);
|
|
|
+// }
|
|
|
+// }
|
|
|
+// return $new_re;
|
|
|
+// }
|
|
|
//根据行为名称筛选
|
|
|
if (isset($request['name'])){
|
|
|
foreach ($new_res as $key => $val) {
|
|
@@ -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,68 @@ 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'] : '',
|
|
|
+ '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,
|
|
|
+ ];
|
|
|
+ DB::beginTransaction();
|
|
|
+ try {
|
|
|
+ $res = $this->behavior->create($data);
|
|
|
+ if ($res) {
|
|
|
+ $token = JWTAuth::decode(JWTAuth::getToken());
|
|
|
+ $uid = $token['user']->id;
|
|
|
+ $username = $token['user']->username;
|
|
|
+ $cerate_bahavior_data = [
|
|
|
+ 'operator_id' => $uid,
|
|
|
+ 'behavior_id' => $res['id'],
|
|
|
+ 'username' => $username,
|
|
|
+ 'content' => '创建'.$request['name'].'行为',
|
|
|
+ '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
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ 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'] : '',
|
|
@@ -159,11 +226,92 @@ class BehaviorRepository
|
|
|
'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) {
|
|
|
+ $token = JWTAuth::decode(JWTAuth::getToken());
|
|
|
+ $uid = $token['user']->id;
|
|
|
+ $username = $token['user']->username;
|
|
|
+ $cerate_bahavior_data = [
|
|
|
+ 'operator_id' => $uid,
|
|
|
+ 'behavior_id' => $request['id'],
|
|
|
+ 'username' => $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
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public function editStatus($request)
|
|
|
+ {
|
|
|
+ $behavior = $this->behavior->find($request['id']);
|
|
|
+
|
|
|
+ $behavior_array = [
|
|
|
+ 'is_open' => $request['is_open'],
|
|
|
+ 'updated_at' => date('Y-m-d H:i:s')
|
|
|
];
|
|
|
+ if ($request['is_open'] == 0){
|
|
|
+ $is_open = '关闭';
|
|
|
+ }else{
|
|
|
+ $is_open = '开启';
|
|
|
+ }
|
|
|
+ $behavior_name = $behavior->name;
|
|
|
|
|
|
- if (!$this->behavior->create($data)) {
|
|
|
- throw new HttpException(500, '添加失败,请重试');
|
|
|
+ DB::beginTransaction();
|
|
|
+ try {
|
|
|
+ $res = $behavior->where('id',$request['id'])->update($behavior_array);
|
|
|
+ if ($res) {
|
|
|
+ $token = JWTAuth::decode(JWTAuth::getToken());
|
|
|
+ $uid = $token['user']->id;
|
|
|
+ $username = $token['user']->username;
|
|
|
+ $cerate_bahavior_data = [
|
|
|
+ 'operator_id' => $uid,
|
|
|
+ 'behavior_id' => $request['id'],
|
|
|
+ 'username' => $username,
|
|
|
+ 'content' => $is_open.'了'.$behavior_name.'行为',
|
|
|
+ 'type' => 2,
|
|
|
+ '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
|
|
|
+ ]);
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
}
|
|
|
|