123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- <?php
- namespace app\admin\controller;
- use app\common\util\ExcelUtil;
- use think\Db;
- class Security extends Auth
- {
- public function index(){
- if(request()->isAjax()){
- //分页参数
- $length = input('rows',10,'intval'); //每页条数
- $page = input('page',1,'intval'); //第几页
- $start = ($page - 1) * $length; //分页开始位置
- //排序
- $sortRow = input('sidx','sort','trim'); //排序列
- $sort = input('sord','asc','trim'); //排序方式
- $order = $sortRow.' '.$sort.' ,id desc';
- $title = input('title','','trim');
- if($title){
- $map[] = ['title','like','%'.$title.'%'];
- }
- $enable = input('enable','','trim');
- if($enable != ''){
- $map[] = ['enable','=',$enable];
- }
- $map[] = ['del','=',0];
- $map[] = ['org_id','=',cur_org_id()];
- $map= empty($map) ? true: $map;
- //数据查询
- $lists = Db::name('security')->where($map)
- ->limit($start,$length)->order('sort asc')->select();
- //数据返回
- $totalCount = Db::name('security')->where($map)->count();
- $totalPage = ceil($totalCount/$length);
- $result['page'] = $page;
- $result['total'] = $totalPage;
- $result['records'] = $totalCount;
- $result['rows'] = $lists;
- return json($result);
- }else{
- $this->assign('meta_title','报警设备列表');
- return $this->fetch();
- }
- }
- /**
- * 新增/编辑
- */
- public function add($id=0){
- if(request()->isPost()){
- $res = model('Security')->updates();
- if($res){
- $this->success('操作成功',url('index'));
- }else{
- $this->error(model('Security')->getError());
- }
- }else{
- $meta_title = '添加报警设备';
- if($id){
- $info = Db::name('security')->where('id',$id)->find();
- $this->assign('info',$info);
- $meta_title = '编辑报警设备';
- }
- $this->assign('meta_title',$meta_title);
- return $this->fetch();
- }
- }
- /**
- * 删除记录
- * @param int $id
- */
- public function del($id=0){
- if(!$id){
- $this->error('参数错误');
- }
- $res = Db::name('security')->where('id',$id)->setField('del',1);
- if($res){
- // model('ActionLog')->addlog(is_login(),33,'报警设备管理:删除',['id' => $id],cur_org_id());
- $this->success('删除成功');
- }else{
- $this->error('删除失败');
- }
- }
- /**
- * 改变字段值
- * @param int $fv
- * @param string $fn
- * @param int $fv
- */
- public function changeField($id=0,$fn='',$fv=0){
- if(!$fn||!$id){
- $this->error('参数错误');
- }
- $res = Db::name('security')->where('id',$id)->update([$fn => $fv]);
- if($res){
- $name = '报警设备管理:';
- $content = '';
- if($fn == 'enable'){
- $content = $name.'禁用';
- if($fv == 1){
- $content = $name.'启用';
- }
- }
- // model('ActionLog')->addlog(is_login(),33,$content,['id' => $id],cur_org_id());
- $this->success('操作成功');
- }else{
- $this->error('操作失败');
- }
- }
- public function screen(){
- if(request()->isPost()){
- // 地点
- $lists = Db::name('security')
- ->where('org_id',$this->orgId)
- ->where('del',0)->where('enable',1)
- ->order('sort','asc')
- ->select();
- foreach ($lists as $k=>$v){
- $lists[$k]['fault'] = $v['fault']>0?1:0;
- $count = Db::name('security_record')->where('security_id',$v['id'])->count();
- $lists[$k]['count'] = $count;
- $lists[$k]['class'] = 'status-color5';
- if($count >= 10 && $count < 20){
- $lists[$k]['class'] = 'status-color4';
- }else if($count >= 20 && $count < 30){
- $lists[$k]['class'] = 'status-color3';
- }else if($count >= 30){
- $lists[$k]['class'] = 'status-color6';
- }
- }
- $data['addrs'] = $lists;
- // 报警记录
- $records = Db::name('security_record')
- ->alias('a')
- ->join('security b','b.id = a.security_id')
- ->join('user c','c.id = a.cf_user_id','left')
- ->field('a.*,b.title,c.real_name')
- ->where('a.org_id',$this->orgId)
- ->limit(20)
- ->order('b.sort asc')
- ->select();
- $records = $records?$records:[];
- $rs = [];
- foreach ($records as $k=>$v){
- $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']];
- $rs[] = $arr;
- }
- $data['records'] = $rs;
- $data['year'] = date('Y');
- $count = Db::name('security_record')->where('org_id',$this->orgId)->where('create_yyyy',$data['year'])->count();
- $data['year_count'] = $count;
- $months = monthlater();
- $yarr = $yarr2 = [];
- foreach ($months as $k=>$v){
- $m = str_replace('-','',$v);
- $y = Db::name('security_record')->where('org_id',$this->orgId)->where('create_yyyymm',$m)->avg('mins');
- $yarr[] = round($y/60,1);
- $y2 = Db::name('security_record')->where('org_id',$this->orgId)->where('create_yyyymm',$m)->count();
- $yarr2[] = $y2;
- }
- $data['x'] = $months;
- $data['y'] = $yarr; // 平均撤防时长
- $data['y2'] = $yarr2; // 报警总数
- $this->success('成功','',$data);
- }else{
- return $this->fetch();
- }
- }
- public function bj($id = 0){
- // 获取近24小时的警报
- $stime = date('Y-m-d H:i:s',time() - 24*60*60);
- $info = Db::name('security_record')
- ->alias('a')
- ->join('security b','b.id = a.security_id')
- ->where('a.org_id',$this->orgId)
- ->where('a.status',0)
- ->where('a.id','>=',$id)
- ->where('a.alarm_time','>=',$stime)
- ->field('a.*,b.title,b.img,b.fault,b.sn')
- ->order('b.sort asc')
- ->find();
- if(!$info){
- $this->error('无报警');
- }
- $info['fault'] = $info['fault']>0?1:0;
- $count = Db::name('security_record')->where('security_id',$info['security_id'])->count();
- $info['count'] = $count;
- $this->success('成功','',$info);
- }
- public function cf($id = 0){
- $info = Db::name('security_record')
- ->where('org_id',$this->orgId)
- ->where('id',$id)
- ->find();
- if(!$info){
- $this->error('记录不存在');
- }
- if($info['status'] == 1){
- $this->error('记录已撤防');
- }
- $res = Db::name('security_record')->where('id',$id)->update([
- 'update_time' => getTime(),
- 'status' => 1,
- 'cf_user_id' => $this->userId,
- 'mins' => time() - strtotime($info['alarm_time'])
- ]);
- if($res){
- $this->success('操作成功');
- }else{
- $this->error('操作失败');
- }
- }
- public function export(){
- $title = input('title','','trim');
- if($title){
- $map[] = ['title','like','%'.$title.'%'];
- }
- $enable = input('enable','','trim');
- if($enable != ''){
- $map[] = ['enable','=',$enable];
- }
- $map[] = ['org_id','=',cur_org_id()];
- $map= empty($map) ? true: $map;
- //数据查询
- $lists = Db::name('security')->where($map)->order('sort asc')->select();
- $filename = '报警设备管理_' . date('YmdHis', time()) . '.xls';
- foreach ($lists as $k=>$v){
- $lists[$k]['st'] = $v['enable']==1?'启用':'禁用';
- }
- $header = [
- ['title' => '编号', 'name' => 'sn','width'=>'30'],
- ['title' => '地点', 'name' => 'title','width'=>'20'],
- ['title' => '备注', 'name' => 'remark','width'=>'20'],
- ['title' => '状态', 'name' => 'st','width'=>'20'],
- ];
- ExcelUtil::export($filename,$header,$lists);
- }
- public function changeSort($id=0,$sort=0){
- if($id<0||$sort<0){
- $this->error('参数错误');
- }
- $res = db('security')->where('id',$id)->setField('sort',$sort);
- if($res){
- // model('ActionLog')->addlog(is_login(),20,'修改排序',['id' => $id],cur_org_id());
- $this->success('操作成功');
- }else{
- $this->error('操作失败');
- }
- }
- }
|