BehaviorRepository.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. $cerate_bahavior_data = [
  166. 'operator_id' => Auth::user()->id,
  167. 'behavior_id' => $res->virus_behavior_id,
  168. 'username' => Auth::user()->username,
  169. 'content' => json_encode($data),
  170. 'type' => 0,
  171. 'created_at' => date('Y-m-d H:i:s'),
  172. 'updated_at' => date('Y-m-d H:i:s')
  173. ];
  174. $result = $this->behaviorOperationLog->insert($cerate_bahavior_data);
  175. if (!$result) {
  176. throw new HttpException(500, '操作日志记录失败');
  177. }
  178. }
  179. DB::commit();
  180. return Response::create();
  181. } catch (QueryException $exception) {
  182. DB::rollBack();
  183. Log::debug('注册行为:'.$exception->getMessage());
  184. return Response::create([
  185. 'message' => '添加失败,请重试',
  186. 'error' => $exception->getMessage(),
  187. 'status_code' => 500
  188. ]);
  189. }
  190. }
  191. public function edit($request)
  192. {
  193. $behavior_id = $this->behavior->find($request['id']);
  194. if ($behavior_id->is_open == 1){
  195. throw new HttpException(500, '无法编辑已经在记录的行为');
  196. }
  197. $update_bahavior = [
  198. 'virus_behavior_id' => $request['virus_behavior_id'],
  199. 'name' => $request['name'],
  200. 'behavior_level' => $request['behavior_level'],
  201. 'behavior_cycle_type' => $request['behavior_cycle_type'],
  202. 'is_open' => 0,
  203. 'behavior_cycle' => isset($request['behavior_cycle']) ? $request['behavior_cycle'] : '',
  204. 'behavior_binding_users' => isset($request['behavior_binding_users']) ? $request['behavior_binding_users'] : 0,
  205. 'physical_strength' => isset($request['physical_strength']) ? $request['physical_strength'] : '',
  206. 'rainbow_beans' => isset($request['rainbow_beans']) ? $request['rainbow_beans'] : '',
  207. 'remarks' => isset($request['remarks']) ? $request['remarks'] : '',
  208. 'behavioral_cycle_start_time' => isset($request['behavioral_cycle_start_time']) ? $request['behavioral_cycle_start_time'] : null,
  209. 'behavioral_cycle_end_time' => isset($request['behavioral_cycle_end_time']) ? $request['behavioral_cycle_end_time'] : null,
  210. 'allotted_quantity_rule' => isset($request['allotted_quantity_rule']) ? json_encode($request['allotted_quantity_rule']) : '',
  211. 'behavior_identification' => isset($request['behavior_identification']) ? $request['behavior_identification'] : '',
  212. 'trigger_times' => isset($request['trigger_times']) ? $request['trigger_times'] : 0,
  213. 'effective_trigger' => isset($request['effective_trigger']) ? $request['effective_trigger'] : 0,
  214. 'relative_series' => isset($request['relative_series']) ? $request['relative_series'] : 0,
  215. 'absolute_progression' => isset($request['absolute_progression']) ? $request['absolute_progression'] : 0,
  216. 'updated_at' => date('Y-m-d H:i:s')
  217. ];
  218. DB::beginTransaction();
  219. try{
  220. $res = $this->behavior->where('id',$request['id'])->update($update_bahavior);
  221. if ($res) {
  222. $cerate_bahavior_data = [
  223. 'operator_id' => Auth::user()->id,
  224. 'behavior_id' => $request['virus_behavior_id'],
  225. 'username' => Auth::user()->username,
  226. 'content' => json_encode($update_bahavior),
  227. 'type' => 1,
  228. 'created_at' => date('Y-m-d H:i:s'),
  229. 'updated_at' => date('Y-m-d H:i:s')
  230. ];
  231. $result = $this->behaviorOperationLog->insert($cerate_bahavior_data);
  232. if (!$result) {
  233. throw new HttpException(500, '操作日志记录失败');
  234. }
  235. }
  236. DB::commit();
  237. return Response::create();
  238. } catch (QueryException $exception) {
  239. DB::rollBack();
  240. Log::debug('编辑行为:'.$exception->getMessage());
  241. return Response::create([
  242. 'message' => '编辑失败,请重试',
  243. 'error' => $exception->getMessage(),
  244. 'status_code' => 500
  245. ]);
  246. }
  247. }
  248. }