BehaviorRepository.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?php
  2. namespace App\Repositories\Behavior;
  3. use App\Models\Behavior;
  4. use App\Models\BehaviorOperationLog;
  5. use GuzzleHttp\Client;
  6. use GuzzleHttp\Exception\RequestException;
  7. use Illuminate\Support\Facades\Auth;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Support\Facades\Log;
  10. use Tymon\JWTAuth\Facades\JWTAuth;
  11. use Dingo\Api\Http\Response;
  12. use Symfony\Component\HttpKernel\Exception\HttpException;
  13. use Illuminate\Database\QueryException;
  14. /**
  15. * Created by PhpStorm.
  16. * User: durong
  17. * Date: 2019/6/10
  18. * Time: 上午11:11
  19. */
  20. class BehaviorRepository
  21. {
  22. public function __construct(Behavior $behavior,BehaviorOperationLog $behaviorOperationLog)
  23. {
  24. $this->behavior = $behavior;
  25. $this->behaviorOperationLog = $behaviorOperationLog;
  26. //初始化virus域名
  27. $this->client = new Client([
  28. 'base_uri' => config('constants.VIRUS_URL'),
  29. 'timeout' => 3
  30. ]);
  31. }
  32. public function index($request)
  33. {
  34. try {
  35. $signKey = [
  36. 'app' => config('constants.VIRUS_APP_ID')
  37. ];
  38. ksort($signKey);
  39. $signKey = urldecode(http_build_query($signKey));
  40. $sign = md5(config('constants.VIRUS_APP_SECRET') . $signKey);
  41. $response = $this->client->request('GET', 'behaviorList', [
  42. 'headers' => [
  43. 'Content-Type' => 'application/json',
  44. 'app' => config('constants.VIRUS_APP_ID'),
  45. 'sign' => $sign
  46. ]
  47. ]);
  48. $res = json_decode($response->getBody()->getContents(), true);
  49. $res = $res ? $res['behaviors'] : [];
  50. }catch (RequestException $exception){
  51. $res = [
  52. 'returnCode' => '-1',
  53. 'errMsg' => '网络超时'.$exception->getMessage()
  54. ];
  55. Log::debug('获取virus行为列表:'.$exception->getMessage());
  56. }
  57. //已登记行为
  58. $registered_bahavior = $this->behavior->get();
  59. $registered_bahaviors = $registered_bahavior->toArray();
  60. foreach ($registered_bahaviors as $key=>$val){
  61. $registered_bahaviors[$key]['behavior_status'] = 1;//已登记
  62. }
  63. $virus_behavior_id = array_column($registered_bahaviors,'virus_behavior_id');
  64. $new_res = [];
  65. //行为列表去重
  66. foreach ($res as $k=>$v) {
  67. if (isset($v['_id']) && !empty($virus_behavior_id)){
  68. $res[$k]['behavior_status'] = 0;//未登记
  69. if (in_array($v['_id'], $virus_behavior_id)) {
  70. unset($res[$k]);
  71. }
  72. $new_res = array_merge($res, $registered_bahaviors);
  73. }elseif (!isset($v['_id']) && $virus_behavior_id) {
  74. $new_res = $registered_bahaviors;
  75. }elseif (isset($v['_id']) && empty($virus_behavior_id)){
  76. $res[$k]['behavior_status'] = 0;//未登记
  77. $new_res = $res;
  78. }
  79. }
  80. $new_re = [];
  81. //根据行为ID筛选
  82. if (isset($request['virus_behavior_id'])){
  83. foreach ($new_res as $k=>$v){
  84. if (isset($v['virus_behavior_id']) && $v['virus_behavior_id'] != $request['virus_behavior_id']){
  85. unset($new_res[$k]);
  86. $new_re = array_merge($new_res);
  87. }elseif (isset($v['_id']) && $v['_id'] != $request['virus_behavior_id']){
  88. unset($new_res[$k]);
  89. $new_re = array_merge($new_res);
  90. }
  91. }
  92. return $new_re;
  93. }
  94. //根据行为状态(类型)筛选
  95. if (isset($request['behavior_status'])){
  96. foreach ($new_res as $kk=>$vv){
  97. if (isset($vv['behavior_status']) && $vv['behavior_status'] != $request['behavior_status']){
  98. unset($new_res[$kk]);
  99. $new_re = array_merge($new_res);
  100. }
  101. }
  102. return $new_re;
  103. }
  104. //根据绑定的动作ID筛选(暂时取消)
  105. // if (isset($request['behavior_action_id'])){
  106. // foreach ($new_res as $key=>$val) {
  107. // if (isset($val['behavior_action_id']) && ($val['behavior_action_id'] != $request['behavior_action_id'])) {
  108. // unset($new_res[$key]);
  109. //// $new_re = array_merge($new_res);
  110. // } elseif (!isset($val['behavior_action_id'])) {
  111. // unset($new_res[$key]);
  112. //// $new_re = array_merge($new_res);
  113. // }
  114. // }
  115. // return $new_re;
  116. // }
  117. //根据行为名称筛选
  118. if (isset($request['name'])){
  119. foreach ($new_res as $key => $val) {
  120. if (isset($val['name']) && strpos($val['name'],$request['name']) !== false) {
  121. $new_re[] = $new_res[$key];
  122. }
  123. if (isset($val['behavior_name']) && strpos($val['behavior_name'],$request['name']) !== false ) {
  124. $new_re[] = $new_res[$key];
  125. }
  126. }
  127. return $new_re;
  128. }
  129. return $new_res;
  130. }
  131. public function create($request)
  132. {
  133. $behavior_name = $this->behavior->where(['name' => $request['name']])->first();
  134. if ($behavior_name) {
  135. return Response::create([
  136. 'message' => '该行为已存在',
  137. 'status_code' => 500
  138. ]);
  139. }
  140. $data = [
  141. 'virus_behavior_id' => $request['virus_behavior_id'],
  142. 'name' => $request['name'],
  143. 'behavior_level' => $request['behavior_level'],
  144. 'behavior_cycle_type' => $request['behavior_cycle_type'],
  145. 'is_open' => 0,
  146. 'behavior_action_id' => isset($request['behavior_action_id']) ? $request['behavior_action_id'] : '',
  147. 'behavior_cycle' => isset($request['behavior_cycle']) ? $request['behavior_cycle'] : '',
  148. 'behavior_binding_users' => isset($request['behavior_binding_users']) ? $request['behavior_binding_users'] : 0,
  149. 'physical_strength' => isset($request['physical_strength']) ? $request['physical_strength'] : '',
  150. 'rainbow_beans' => isset($request['rainbow_beans']) ? $request['rainbow_beans'] : '',
  151. 'remarks' => isset($request['remarks']) ? $request['remarks'] : '',
  152. 'behavioral_cycle_start_time' => isset($request['behavioral_cycle_start_time']) ? $request['behavioral_cycle_start_time'] : null,
  153. 'behavioral_cycle_end_time' => isset($request['behavioral_cycle_end_time']) ? $request['behavioral_cycle_end_time'] : null,
  154. 'allotted_quantity_rule' => isset($request['allotted_quantity_rule']) ? json_encode($request['allotted_quantity_rule']) : '',
  155. 'behavior_identification' => isset($request['behavior_identification']) ? $request['behavior_identification'] : '',
  156. 'trigger_times' => isset($request['trigger_times']) ? $request['trigger_times'] : 0,
  157. 'effective_trigger' => isset($request['effective_trigger']) ? $request['effective_trigger'] : 0,
  158. 'relative_series' => isset($request['relative_series']) ? $request['relative_series'] : 0,
  159. 'absolute_progression' => isset($request['absolute_progression']) ? $request['absolute_progression'] : 0,
  160. ];
  161. DB::beginTransaction();
  162. try {
  163. $res = $this->behavior->create($data);
  164. if ($res) {
  165. $token = JWTAuth::decode(JWTAuth::getToken());
  166. $uid = $token['user']->id;
  167. $username = $token['user']->username;
  168. $cerate_bahavior_data = [
  169. 'operator_id' => $uid,
  170. 'behavior_id' => $res['id'],
  171. 'username' => $username,
  172. 'content' => '创建'.$request['name'].'行为',
  173. 'type' => 0,
  174. 'created_at' => date('Y-m-d H:i:s'),
  175. 'updated_at' => date('Y-m-d H:i:s')
  176. ];
  177. $result = $this->behaviorOperationLog->insert($cerate_bahavior_data);
  178. if (!$result) {
  179. throw new HttpException(500, '操作日志记录失败');
  180. }
  181. }
  182. DB::commit();
  183. return Response::create();
  184. } catch (QueryException $exception) {
  185. DB::rollBack();
  186. Log::debug('注册行为:'.$exception->getMessage());
  187. return Response::create([
  188. 'message' => '添加失败,请重试',
  189. 'error' => $exception->getMessage(),
  190. 'status_code' => 500
  191. ]);
  192. }
  193. }
  194. public function edit($request)
  195. {
  196. $behavior_id = $this->behavior->find($request['id']);
  197. if ($behavior_id->is_open == 1){
  198. throw new HttpException(500, '无法编辑已经在记录的行为');
  199. }
  200. $update_bahavior = [
  201. 'virus_behavior_id' => $request['virus_behavior_id'],
  202. 'name' => $request['name'],
  203. 'behavior_level' => $request['behavior_level'],
  204. 'behavior_cycle_type' => $request['behavior_cycle_type'],
  205. 'is_open' => 0,
  206. 'behavior_cycle' => isset($request['behavior_cycle']) ? $request['behavior_cycle'] : '',
  207. 'behavior_binding_users' => isset($request['behavior_binding_users']) ? $request['behavior_binding_users'] : 0,
  208. 'physical_strength' => isset($request['physical_strength']) ? $request['physical_strength'] : '',
  209. 'rainbow_beans' => isset($request['rainbow_beans']) ? $request['rainbow_beans'] : '',
  210. 'remarks' => isset($request['remarks']) ? $request['remarks'] : '',
  211. 'behavioral_cycle_start_time' => isset($request['behavioral_cycle_start_time']) ? $request['behavioral_cycle_start_time'] : null,
  212. 'behavioral_cycle_end_time' => isset($request['behavioral_cycle_end_time']) ? $request['behavioral_cycle_end_time'] : null,
  213. 'allotted_quantity_rule' => isset($request['allotted_quantity_rule']) ? json_encode($request['allotted_quantity_rule']) : '',
  214. 'behavior_identification' => isset($request['behavior_identification']) ? $request['behavior_identification'] : '',
  215. 'trigger_times' => isset($request['trigger_times']) ? $request['trigger_times'] : 0,
  216. 'effective_trigger' => isset($request['effective_trigger']) ? $request['effective_trigger'] : 0,
  217. 'relative_series' => isset($request['relative_series']) ? $request['relative_series'] : 0,
  218. 'absolute_progression' => isset($request['absolute_progression']) ? $request['absolute_progression'] : 0,
  219. 'updated_at' => date('Y-m-d H:i:s')
  220. ];
  221. DB::beginTransaction();
  222. try{
  223. $res = $this->behavior->where('id',$request['id'])->update($update_bahavior);
  224. if ($res) {
  225. $token = JWTAuth::decode(JWTAuth::getToken());
  226. $uid = $token['user']->id;
  227. $username = $token['user']->username;
  228. $cerate_bahavior_data = [
  229. 'operator_id' => $uid,
  230. 'behavior_id' => $request['id'],
  231. 'username' => $username,
  232. 'content' => json_encode($update_bahavior),
  233. 'type' => 1,
  234. 'created_at' => date('Y-m-d H:i:s'),
  235. 'updated_at' => date('Y-m-d H:i:s')
  236. ];
  237. $result = $this->behaviorOperationLog->insert($cerate_bahavior_data);
  238. if (!$result) {
  239. throw new HttpException(500, '操作日志记录失败');
  240. }
  241. }
  242. DB::commit();
  243. return Response::create();
  244. } catch (QueryException $exception) {
  245. DB::rollBack();
  246. Log::debug('编辑行为:'.$exception->getMessage());
  247. return Response::create([
  248. 'message' => '编辑失败,请重试',
  249. 'error' => $exception->getMessage(),
  250. 'status_code' => 500
  251. ]);
  252. }
  253. }
  254. public function editStatus($request)
  255. {
  256. $behavior = $this->behavior->find($request['id']);
  257. $behavior_array = [
  258. 'is_open' => $request['is_open'],
  259. 'updated_at' => date('Y-m-d H:i:s')
  260. ];
  261. if ($request['is_open'] == 0){
  262. $is_open = '关闭';
  263. }else{
  264. $is_open = '开启';
  265. }
  266. $behavior_name = $behavior->name;
  267. DB::beginTransaction();
  268. try {
  269. $res = $behavior->where('id',$request['id'])->update($behavior_array);
  270. if ($res) {
  271. $token = JWTAuth::decode(JWTAuth::getToken());
  272. $uid = $token['user']->id;
  273. $username = $token['user']->username;
  274. $cerate_bahavior_data = [
  275. 'operator_id' => $uid,
  276. 'behavior_id' => $request['id'],
  277. 'username' => $username,
  278. 'content' => $is_open.'了'.$behavior_name.'行为',
  279. 'type' => 2,
  280. 'created_at' => date('Y-m-d H:i:s'),
  281. 'updated_at' => date('Y-m-d H:i:s')
  282. ];
  283. $result = $this->behaviorOperationLog->insert($cerate_bahavior_data);
  284. if (!$result) {
  285. throw new HttpException(500, '操作日志记录失败');
  286. }
  287. }
  288. DB::commit();
  289. return Response::create();
  290. } catch (QueryException $exception) {
  291. DB::rollBack();
  292. Log::debug('修改状态行为:' . $exception->getMessage());
  293. return Response::create([
  294. 'message' => '修改状态失败,请重试',
  295. 'error' => $exception->getMessage(),
  296. 'status_code' => 500
  297. ]);
  298. }
  299. }
  300. }