FkDimension.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. namespace app\admin\controller;
  3. use think\Controller;
  4. use think\Db;
  5. use think\Request;
  6. // 异常维度管理
  7. class FkDimension extends Controller
  8. {
  9. /**
  10. * 获取异常对象选项(监控对象类型)
  11. */
  12. public function getAbnormalObjectOptions()
  13. {
  14. // 定义监控对象类型选项
  15. $options = [
  16. 'ip' => 'IP地址',
  17. 'imei' => 'IMEI设备号',
  18. 'username' => '用户名',
  19. 'id_card' => '身份证号',
  20. 'phone' => '手机号'
  21. ];
  22. return json(['code' => 0, 'data' => $options]);
  23. }
  24. /**
  25. * 异常维度列表页面
  26. */
  27. public function index()
  28. {
  29. if ($this->request->isAjax()) {
  30. $page = input('get.page', 1);
  31. $limit = input('get.limit', 20);
  32. $where = [];
  33. // 搜索条件
  34. $title = input('get.title', '');
  35. if ($title) {
  36. $where['title'] = ['like', "%{$title}%"];
  37. }
  38. $monitorObject = input('get.monitor_object', '');
  39. if ($monitorObject) {
  40. $where['monitor_object'] = $monitorObject;
  41. }
  42. $abnormalJudgeType = input('get.abnormal_judge_type', '');
  43. if ($abnormalJudgeType) {
  44. $where['abnormal_judge_type'] = $abnormalJudgeType;
  45. }
  46. $abnormalObject = input('get.abnormal_object', '');
  47. if ($abnormalObject) {
  48. $where['abnormal_object'] = ['like', "%{$abnormalObject}%"];
  49. }
  50. $status = input('get.status', '');
  51. if ($status !== '') {
  52. $where['status'] = $status;
  53. }
  54. $count = Db::name('fk_dimension')->where($where)->count();
  55. $data = Db::name('fk_dimension')
  56. ->where($where)
  57. ->page($page, $limit)
  58. ->order('dimension_id desc')
  59. ->select();
  60. // dump($data);
  61. return json([
  62. 'code' => 0,
  63. 'msg' => '',
  64. 'count' => $count,
  65. 'data' => $data
  66. ]);
  67. }
  68. return $this->fetch();
  69. }
  70. /**
  71. * 添加异常维度
  72. */
  73. public function add()
  74. {
  75. if ($this->request->isPost()) {
  76. // 处理异常对象多选数据
  77. $abnormalObjectArray = input('post.abnormal_object/a', []);
  78. $abnormalObject = is_array($abnormalObjectArray) ? implode(',', $abnormalObjectArray) : input('post.abnormal_object', '');
  79. $data = [
  80. 'title' => input('post.title'),
  81. 'monitor_object' => input('post.monitor_object'),
  82. 'abnormal_object' => $abnormalObject,
  83. 'abnormal_judge_type' => trim(input('post.abnormal_judge_type')),
  84. 'execute_type' => trim(input('post.execute_type')),
  85. 'abnormal_judge_value' => input('post.abnormal_judge_value', 0),
  86. 'abnormal_judge_level' => input('post.abnormal_judge_level', 0),
  87. 'lifespan_time' => input('post.lifespan_time', 0),
  88. 'status' => input('post.status', 1),
  89. 'create_time' => date('Y-m-d H:i:s'),
  90. 'update_time' => date('Y-m-d H:i:s')
  91. ];
  92. // 验证
  93. if (empty($data['title']) || empty($data['monitor_object']) || empty($data['abnormal_object'])) {
  94. return json(['code' => 1, 'msg' => '维度名、监控对象和异常对象不能为空']);
  95. }
  96. if (!in_array($data['abnormal_judge_type'], ['gt', 'lt', 'eq'])) {
  97. return json(['code' => 1, 'msg' => '异常判定类型无效']);
  98. }
  99. if (!in_array($data['execute_type'], ['hour', 'day'])) {
  100. return json(['code' => 1, 'msg' => '执行类型无效']);
  101. }
  102. try {
  103. Db::name('fk_dimension')->insert($data);
  104. return json(['code' => 0, 'msg' => '添加成功']);
  105. } catch (\Exception $e) {
  106. return json(['code' => 1, 'msg' => '添加失败:' . $e->getMessage()]);
  107. }
  108. }
  109. return $this->fetch();
  110. }
  111. /**
  112. * 编辑异常维度
  113. */
  114. public function edit($id)
  115. {
  116. if ($this->request->isPost()) {
  117. // 处理异常对象多选数据
  118. $abnormalObjectArray = input('post.abnormal_object/a', []);
  119. $abnormalObject = is_array($abnormalObjectArray) ? implode(',', $abnormalObjectArray) : input('post.abnormal_object', '');
  120. $data = [
  121. 'title' => input('post.title'),
  122. 'monitor_object' => input('post.monitor_object'),
  123. 'abnormal_object' => $abnormalObject,
  124. 'abnormal_judge_type' => trim(input('post.abnormal_judge_type')),
  125. 'execute_type' => trim(input('post.execute_type')),
  126. 'abnormal_judge_value' => input('post.abnormal_judge_value', 0),
  127. 'abnormal_judge_level' => input('post.abnormal_judge_level', 0),
  128. 'lifespan_time' => input('post.lifespan_time', 0),
  129. 'status' => input('post.status', 1),
  130. 'update_time' => date('Y-m-d H:i:s')
  131. ];
  132. // 验证
  133. if (empty($data['title']) || empty($data['monitor_object']) || empty($data['abnormal_object'])) {
  134. return json(['code' => 1, 'msg' => '维度名、监控对象和异常对象不能为空']);
  135. }
  136. if (!in_array($data['abnormal_judge_type'], ['gt', 'lt', 'eq'])) {
  137. return json(['code' => 1, 'msg' => '异常判定类型无效']);
  138. }
  139. if (!in_array($data['execute_type'], ['hour', 'day'])) {
  140. return json(['code' => 1, 'msg' => '执行类型无效']);
  141. }
  142. try {
  143. Db::name('fk_dimension')->where('dimension_id', $id)->update($data);
  144. return json(['code' => 0, 'msg' => '修改成功']);
  145. } catch (\Exception $e) {
  146. return json(['code' => 1, 'msg' => '修改失败:' . $e->getMessage()]);
  147. }
  148. }
  149. $info = Db::name('fk_dimension')->where('dimension_id', $id)->find();
  150. if($info){
  151. $info['abnormal_object'] = explode(',', $info['abnormal_object']);
  152. }
  153. $this->assign('info', $info);
  154. return $this->fetch();
  155. }
  156. /**
  157. * 删除异常维度
  158. */
  159. public function delete()
  160. {
  161. $id = input('post.id');
  162. if (!$id) {
  163. return json(['code' => 1, 'msg' => '参数错误']);
  164. }
  165. try {
  166. Db::name('fk_dimension')->where('dimension_id', $id)->delete();
  167. return json(['code' => 0, 'msg' => '删除成功']);
  168. } catch (\Exception $e) {
  169. return json(['code' => 1, 'msg' => '删除失败:' . $e->getMessage()]);
  170. }
  171. }
  172. /**
  173. * 批量删除
  174. */
  175. public function batchDelete()
  176. {
  177. $ids = input('post.ids/a', []);
  178. if (empty($ids)) {
  179. return json(['code' => 1, 'msg' => '请选择要删除的数据']);
  180. }
  181. try {
  182. Db::name('fk_dimension')->whereIn('dimension_id', $ids)->delete();
  183. return json(['code' => 0, 'msg' => '删除成功']);
  184. } catch (\Exception $e) {
  185. return json(['code' => 1, 'msg' => '删除失败:' . $e->getMessage()]);
  186. }
  187. }
  188. }