Security.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\util\ExcelUtil;
  4. use think\Db;
  5. class Security extends Auth
  6. {
  7. public function index(){
  8. if(request()->isAjax()){
  9. //分页参数
  10. $length = input('rows',10,'intval'); //每页条数
  11. $page = input('page',1,'intval'); //第几页
  12. $start = ($page - 1) * $length; //分页开始位置
  13. //排序
  14. $sortRow = input('sidx','sort','trim'); //排序列
  15. $sort = input('sord','asc','trim'); //排序方式
  16. $order = $sortRow.' '.$sort.' ,id desc';
  17. $title = input('title','','trim');
  18. if($title){
  19. $map[] = ['title','like','%'.$title.'%'];
  20. }
  21. $enable = input('enable','','trim');
  22. if($enable != ''){
  23. $map[] = ['enable','=',$enable];
  24. }
  25. $map[] = ['del','=',0];
  26. $map[] = ['org_id','=',cur_org_id()];
  27. $map= empty($map) ? true: $map;
  28. //数据查询
  29. $lists = Db::name('security')->where($map)
  30. ->limit($start,$length)->order('sort asc')->select();
  31. //数据返回
  32. $totalCount = Db::name('security')->where($map)->count();
  33. $totalPage = ceil($totalCount/$length);
  34. $result['page'] = $page;
  35. $result['total'] = $totalPage;
  36. $result['records'] = $totalCount;
  37. $result['rows'] = $lists;
  38. return json($result);
  39. }else{
  40. $this->assign('meta_title','报警设备列表');
  41. return $this->fetch();
  42. }
  43. }
  44. /**
  45. * 新增/编辑
  46. */
  47. public function add($id=0){
  48. if(request()->isPost()){
  49. $res = model('Security')->updates();
  50. if($res){
  51. $this->success('操作成功',url('index'));
  52. }else{
  53. $this->error(model('Security')->getError());
  54. }
  55. }else{
  56. $meta_title = '添加报警设备';
  57. if($id){
  58. $info = Db::name('security')->where('id',$id)->find();
  59. $this->assign('info',$info);
  60. $meta_title = '编辑报警设备';
  61. }
  62. $this->assign('meta_title',$meta_title);
  63. return $this->fetch();
  64. }
  65. }
  66. /**
  67. * 删除记录
  68. * @param int $id
  69. */
  70. public function del($id=0){
  71. if(!$id){
  72. $this->error('参数错误');
  73. }
  74. $res = Db::name('security')->where('id',$id)->setField('del',1);
  75. if($res){
  76. // model('ActionLog')->addlog(is_login(),33,'报警设备管理:删除',['id' => $id],cur_org_id());
  77. $this->success('删除成功');
  78. }else{
  79. $this->error('删除失败');
  80. }
  81. }
  82. /**
  83. * 改变字段值
  84. * @param int $fv
  85. * @param string $fn
  86. * @param int $fv
  87. */
  88. public function changeField($id=0,$fn='',$fv=0){
  89. if(!$fn||!$id){
  90. $this->error('参数错误');
  91. }
  92. $res = Db::name('security')->where('id',$id)->update([$fn => $fv]);
  93. if($res){
  94. $name = '报警设备管理:';
  95. $content = '';
  96. if($fn == 'enable'){
  97. $content = $name.'禁用';
  98. if($fv == 1){
  99. $content = $name.'启用';
  100. }
  101. }
  102. // model('ActionLog')->addlog(is_login(),33,$content,['id' => $id],cur_org_id());
  103. $this->success('操作成功');
  104. }else{
  105. $this->error('操作失败');
  106. }
  107. }
  108. public function screen(){
  109. if(request()->isPost()){
  110. // 地点
  111. $lists = Db::name('security')
  112. ->where('org_id',$this->orgId)
  113. ->where('del',0)->where('enable',1)
  114. ->order('sort','asc')
  115. ->select();
  116. foreach ($lists as $k=>$v){
  117. $lists[$k]['fault'] = $v['fault']>0?1:0;
  118. $count = Db::name('security_record')->where('security_id',$v['id'])->count();
  119. $lists[$k]['count'] = $count;
  120. $lists[$k]['class'] = 'status-color5';
  121. if($count >= 10 && $count < 20){
  122. $lists[$k]['class'] = 'status-color4';
  123. }else if($count >= 20 && $count < 30){
  124. $lists[$k]['class'] = 'status-color3';
  125. }else if($count >= 30){
  126. $lists[$k]['class'] = 'status-color6';
  127. }
  128. }
  129. $data['addrs'] = $lists;
  130. // 报警记录
  131. $records = Db::name('security_record')
  132. ->alias('a')
  133. ->join('security b','b.id = a.security_id')
  134. ->join('user c','c.id = a.cf_user_id','left')
  135. ->field('a.*,b.title,c.real_name')
  136. ->where('a.org_id',$this->orgId)
  137. ->limit(20)
  138. ->order('b.sort asc')
  139. ->select();
  140. $records = $records?$records:[];
  141. $rs = [];
  142. foreach ($records as $k=>$v){
  143. $arr = [$v['title'],date('m-d H:i',strtotime($v['alarm_time'])),$v['update_time']?date('m-d H:i',strtotime($v['update_time'])):'',$v['real_name']];
  144. $rs[] = $arr;
  145. }
  146. $data['records'] = $rs;
  147. $data['year'] = date('Y');
  148. $count = Db::name('security_record')->where('org_id',$this->orgId)->where('create_yyyy',$data['year'])->count();
  149. $data['year_count'] = $count;
  150. $months = monthlater();
  151. $yarr = $yarr2 = [];
  152. foreach ($months as $k=>$v){
  153. $m = str_replace('-','',$v);
  154. $y = Db::name('security_record')->where('org_id',$this->orgId)->where('create_yyyymm',$m)->avg('mins');
  155. $yarr[] = round($y/60,1);
  156. $y2 = Db::name('security_record')->where('org_id',$this->orgId)->where('create_yyyymm',$m)->count();
  157. $yarr2[] = $y2;
  158. }
  159. $data['x'] = $months;
  160. $data['y'] = $yarr; // 平均撤防时长
  161. $data['y2'] = $yarr2; // 报警总数
  162. $this->success('成功','',$data);
  163. }else{
  164. return $this->fetch();
  165. }
  166. }
  167. public function bj($id = 0){
  168. // 获取近24小时的警报
  169. $stime = date('Y-m-d H:i:s',time() - 24*60*60);
  170. $info = Db::name('security_record')
  171. ->alias('a')
  172. ->join('security b','b.id = a.security_id')
  173. ->where('a.org_id',$this->orgId)
  174. ->where('a.status',0)
  175. ->where('a.id','>=',$id)
  176. ->where('a.alarm_time','>=',$stime)
  177. ->field('a.*,b.title,b.img,b.fault,b.sn')
  178. ->order('b.sort asc')
  179. ->find();
  180. if(!$info){
  181. $this->error('无报警');
  182. }
  183. $info['fault'] = $info['fault']>0?1:0;
  184. $count = Db::name('security_record')->where('security_id',$info['security_id'])->count();
  185. $info['count'] = $count;
  186. $this->success('成功','',$info);
  187. }
  188. public function cf($id = 0){
  189. $info = Db::name('security_record')
  190. ->where('org_id',$this->orgId)
  191. ->where('id',$id)
  192. ->find();
  193. if(!$info){
  194. $this->error('记录不存在');
  195. }
  196. if($info['status'] == 1){
  197. $this->error('记录已撤防');
  198. }
  199. $res = Db::name('security_record')->where('id',$id)->update([
  200. 'update_time' => getTime(),
  201. 'status' => 1,
  202. 'cf_user_id' => $this->userId,
  203. 'mins' => time() - strtotime($info['alarm_time'])
  204. ]);
  205. if($res){
  206. $this->success('操作成功');
  207. }else{
  208. $this->error('操作失败');
  209. }
  210. }
  211. public function export(){
  212. $title = input('title','','trim');
  213. if($title){
  214. $map[] = ['title','like','%'.$title.'%'];
  215. }
  216. $enable = input('enable','','trim');
  217. if($enable != ''){
  218. $map[] = ['enable','=',$enable];
  219. }
  220. $map[] = ['org_id','=',cur_org_id()];
  221. $map= empty($map) ? true: $map;
  222. //数据查询
  223. $lists = Db::name('security')->where($map)->order('sort asc')->select();
  224. $filename = '报警设备管理_' . date('YmdHis', time()) . '.xls';
  225. foreach ($lists as $k=>$v){
  226. $lists[$k]['st'] = $v['enable']==1?'启用':'禁用';
  227. }
  228. $header = [
  229. ['title' => '编号', 'name' => 'sn','width'=>'30'],
  230. ['title' => '地点', 'name' => 'title','width'=>'20'],
  231. ['title' => '备注', 'name' => 'remark','width'=>'20'],
  232. ['title' => '状态', 'name' => 'st','width'=>'20'],
  233. ];
  234. ExcelUtil::export($filename,$header,$lists);
  235. }
  236. public function changeSort($id=0,$sort=0){
  237. if($id<0||$sort<0){
  238. $this->error('参数错误');
  239. }
  240. $res = db('security')->where('id',$id)->setField('sort',$sort);
  241. if($res){
  242. // model('ActionLog')->addlog(is_login(),20,'修改排序',['id' => $id],cur_org_id());
  243. $this->success('操作成功');
  244. }else{
  245. $this->error('操作失败');
  246. }
  247. }
  248. }